blob: e9ea6cda6e2e68df8f8850e36e81d50be36ac753 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000072#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000073#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000107#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000114#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000119#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120#define HAVE_WAIT 1
121#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000127#define HAVE_SYSTEM 1
128#define HAVE_CWAIT 1
129#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000134#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000150#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000189/*#ifdef HAVE_FCHMOD
190extern int fchmod(int, mode_t);
191#endif*/
192/*#ifdef HAVE_LCHMOD
193extern int lchmod(const char *, mode_t);
194#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int chown(const char *, uid_t, gid_t);
196extern char *getcwd(char *, int);
197extern char *strerror(int);
198extern int link(const char *, const char *);
199extern int rename(const char *, const char *);
200extern int stat(const char *, struct stat *);
201extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000266#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000268#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
Guido van Rossumd48f2521997-12-05 22:19:34 +0000271#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000276#if defined(PATH_MAX) && PATH_MAX > 1024
277#define MAXPATHLEN PATH_MAX
278#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000279#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000281#endif /* MAXPATHLEN */
282
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000283#ifdef UNION_WAIT
284/* Emulate some macros on systems that have a union instead of macros */
285
286#ifndef WIFEXITED
287#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
288#endif
289
290#ifndef WEXITSTATUS
291#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
292#endif
293
294#ifndef WTERMSIG
295#define WTERMSIG(u_wait) ((u_wait).w_termsig)
296#endif
297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298#define WAIT_TYPE union wait
299#define WAIT_STATUS_INT(s) (s.w_status)
300
301#else /* !UNION_WAIT */
302#define WAIT_TYPE int
303#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000304#endif /* UNION_WAIT */
305
Greg Wardb48bc172000-03-01 21:51:56 +0000306/* Don't use the "_r" form if we don't need it (also, won't have a
307 prototype for it, at least on Solaris -- maybe others as well?). */
308#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
309#define USE_CTERMID_R
310#endif
311
Fred Drake699f3522000-06-29 21:12:41 +0000312/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000313#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000314#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000315# define STAT win32_stat
316# define FSTAT win32_fstat
317# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000318#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000319# define STAT stat
320# define FSTAT fstat
321# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000322#endif
323
Tim Peters11b23062003-04-23 02:39:17 +0000324#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000325#include <sys/mkdev.h>
326#else
327#if defined(MAJOR_IN_SYSMACROS)
328#include <sys/sysmacros.h>
329#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000330#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
331#include <sys/mkdev.h>
332#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000333#endif
Fred Drake699f3522000-06-29 21:12:41 +0000334
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000335#if defined _MSC_VER && _MSC_VER >= 1400
336/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
337 * valid and throw an assertion if it isn't.
338 * Normally, an invalid fd is likely to be a C program error and therefore
339 * an assertion can be useful, but it does contradict the POSIX standard
340 * which for write(2) states:
341 * "Otherwise, -1 shall be returned and errno set to indicate the error."
342 * "[EBADF] The fildes argument is not a valid file descriptor open for
343 * writing."
344 * Furthermore, python allows the user to enter any old integer
345 * as a fd and should merely raise a python exception on error.
346 * The Microsoft CRT doesn't provide an official way to check for the
347 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000348 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000349 * internal structures involved.
350 * The structures below must be updated for each version of visual studio
351 * according to the file internal.h in the CRT source, until MS comes
352 * up with a less hacky way to do this.
353 * (all of this is to avoid globally modifying the CRT behaviour using
354 * _set_invalid_parameter_handler() and _CrtSetReportMode())
355 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000356/* The actual size of the structure is determined at runtime.
357 * Only the first items must be present.
358 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000359typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000360 intptr_t osfhnd;
361 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000362} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000363
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000364extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000365#define IOINFO_L2E 5
366#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
367#define IOINFO_ARRAYS 64
368#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
369#define FOPEN 0x01
370#define _NO_CONSOLE_FILENO (intptr_t)-2
371
372/* This function emulates what the windows CRT does to validate file handles */
373int
374_PyVerify_fd(int fd)
375{
Victor Stinner8c62be82010-05-06 00:08:46 +0000376 const int i1 = fd >> IOINFO_L2E;
377 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000378
Victor Stinner8c62be82010-05-06 00:08:46 +0000379 static int sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380
Victor Stinner8c62be82010-05-06 00:08:46 +0000381 /* Determine the actual size of the ioinfo structure,
382 * as used by the CRT loaded in memory
383 */
384 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
385 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
386 }
387 if (sizeof_ioinfo == 0) {
388 /* This should not happen... */
389 goto fail;
390 }
391
392 /* See that it isn't a special CLEAR fileno */
393 if (fd != _NO_CONSOLE_FILENO) {
394 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
395 * we check pointer validity and other info
396 */
397 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
398 /* finally, check that the file is open */
399 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
400 if (info->osfile & FOPEN) {
401 return 1;
402 }
403 }
404 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000405 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000406 errno = EBADF;
407 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408}
409
410/* the special case of checking dup2. The target fd must be in a sensible range */
411static int
412_PyVerify_fd_dup2(int fd1, int fd2)
413{
Victor Stinner8c62be82010-05-06 00:08:46 +0000414 if (!_PyVerify_fd(fd1))
415 return 0;
416 if (fd2 == _NO_CONSOLE_FILENO)
417 return 0;
418 if ((unsigned)fd2 < _NHANDLE_)
419 return 1;
420 else
421 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422}
423#else
424/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
425#define _PyVerify_fd_dup2(A, B) (1)
426#endif
427
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000429#ifdef WITH_NEXT_FRAMEWORK
430/* On Darwin/MacOSX a shared library or framework has no access to
431** environ directly, we must obtain it with _NSGetEnviron().
432*/
433#include <crt_externs.h>
434static char **environ;
435#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000437#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438
Barry Warsaw53699e91996-12-10 23:23:01 +0000439static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000440convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Victor Stinner8c62be82010-05-06 00:08:46 +0000442 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000443#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000444 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000445#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000446 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000447#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000448#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000449 APIRET rc;
450 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
451#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000452
Victor Stinner8c62be82010-05-06 00:08:46 +0000453 d = PyDict_New();
454 if (d == NULL)
455 return NULL;
456#ifdef WITH_NEXT_FRAMEWORK
457 if (environ == NULL)
458 environ = *_NSGetEnviron();
459#endif
460#ifdef MS_WINDOWS
461 /* _wenviron must be initialized in this way if the program is started
462 through main() instead of wmain(). */
463 _wgetenv(L"");
464 if (_wenviron == NULL)
465 return d;
466 /* This part ignores errors */
467 for (e = _wenviron; *e != NULL; e++) {
468 PyObject *k;
469 PyObject *v;
470 wchar_t *p = wcschr(*e, L'=');
471 if (p == NULL)
472 continue;
473 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
474 if (k == NULL) {
475 PyErr_Clear();
476 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000477 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000478 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
479 if (v == NULL) {
480 PyErr_Clear();
481 Py_DECREF(k);
482 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000483 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000484 if (PyDict_GetItem(d, k) == NULL) {
485 if (PyDict_SetItem(d, k, v) != 0)
486 PyErr_Clear();
487 }
488 Py_DECREF(k);
489 Py_DECREF(v);
490 }
491#else
492 if (environ == NULL)
493 return d;
494 /* This part ignores errors */
495 for (e = environ; *e != NULL; e++) {
496 PyObject *k;
497 PyObject *v;
498 char *p = strchr(*e, '=');
499 if (p == NULL)
500 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000501 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000502 if (k == NULL) {
503 PyErr_Clear();
504 continue;
505 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000506 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000507 if (v == NULL) {
508 PyErr_Clear();
509 Py_DECREF(k);
510 continue;
511 }
512 if (PyDict_GetItem(d, k) == NULL) {
513 if (PyDict_SetItem(d, k, v) != 0)
514 PyErr_Clear();
515 }
516 Py_DECREF(k);
517 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000518 }
519#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000520#if defined(PYOS_OS2)
521 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
522 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
523 PyObject *v = PyBytes_FromString(buffer);
524 PyDict_SetItemString(d, "BEGINLIBPATH", v);
525 Py_DECREF(v);
526 }
527 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
528 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
529 PyObject *v = PyBytes_FromString(buffer);
530 PyDict_SetItemString(d, "ENDLIBPATH", v);
531 Py_DECREF(v);
532 }
533#endif
534 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535}
536
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537/* Set a POSIX-specific error from errno, and return NULL */
538
Barry Warsawd58d7641998-07-23 16:14:40 +0000539static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000540posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000541{
Victor Stinner8c62be82010-05-06 00:08:46 +0000542 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543}
Barry Warsawd58d7641998-07-23 16:14:40 +0000544static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000545posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000546{
Victor Stinner8c62be82010-05-06 00:08:46 +0000547 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000548}
549
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000550#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000551static PyObject *
552posix_error_with_unicode_filename(Py_UNICODE* name)
553{
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000555}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000556#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000557
558
Mark Hammondef8b6542001-05-13 08:04:26 +0000559static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000560posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000561{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000562 PyObject *name_str, *rc;
563 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
564 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000565 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000566 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
567 name_str);
568 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000569 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000570}
571
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000572#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000573static PyObject *
574win32_error(char* function, char* filename)
575{
Victor Stinner8c62be82010-05-06 00:08:46 +0000576 /* XXX We should pass the function name along in the future.
577 (winreg.c also wants to pass the function name.)
578 This would however require an additional param to the
579 Windows error object, which is non-trivial.
580 */
581 errno = GetLastError();
582 if (filename)
583 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
584 else
585 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000586}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000587
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000588static PyObject *
589win32_error_unicode(char* function, Py_UNICODE* filename)
590{
Victor Stinner8c62be82010-05-06 00:08:46 +0000591 /* XXX - see win32_error for comments on 'function' */
592 errno = GetLastError();
593 if (filename)
594 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
595 else
596 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000597}
598
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000600convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000601{
Victor Stinner8c62be82010-05-06 00:08:46 +0000602 if (PyUnicode_CheckExact(*param))
603 Py_INCREF(*param);
604 else if (PyUnicode_Check(*param))
605 /* For a Unicode subtype that's not a Unicode object,
606 return a true Unicode object with the same data. */
607 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
608 PyUnicode_GET_SIZE(*param));
609 else
610 *param = PyUnicode_FromEncodedObject(*param,
611 Py_FileSystemDefaultEncoding,
612 "strict");
613 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000614}
615
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000616#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617
Guido van Rossumd48f2521997-12-05 22:19:34 +0000618#if defined(PYOS_OS2)
619/**********************************************************************
620 * Helper Function to Trim and Format OS/2 Messages
621 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000622static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000623os2_formatmsg(char *msgbuf, int msglen, char *reason)
624{
625 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
626
627 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
628 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
629
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000630 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000631 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
632 }
633
634 /* Add Optional Reason Text */
635 if (reason) {
636 strcat(msgbuf, " : ");
637 strcat(msgbuf, reason);
638 }
639}
640
641/**********************************************************************
642 * Decode an OS/2 Operating System Error Code
643 *
644 * A convenience function to lookup an OS/2 error code and return a
645 * text message we can use to raise a Python exception.
646 *
647 * Notes:
648 * The messages for errors returned from the OS/2 kernel reside in
649 * the file OSO001.MSG in the \OS2 directory hierarchy.
650 *
651 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000652static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000653os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
654{
655 APIRET rc;
656 ULONG msglen;
657
658 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
659 Py_BEGIN_ALLOW_THREADS
660 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
661 errorcode, "oso001.msg", &msglen);
662 Py_END_ALLOW_THREADS
663
664 if (rc == NO_ERROR)
665 os2_formatmsg(msgbuf, msglen, reason);
666 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000667 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000668 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000669
670 return msgbuf;
671}
672
673/* Set an OS/2-specific error and return NULL. OS/2 kernel
674 errors are not in a global variable e.g. 'errno' nor are
675 they congruent with posix error numbers. */
676
Victor Stinner8c62be82010-05-06 00:08:46 +0000677static PyObject *
678os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000679{
680 char text[1024];
681 PyObject *v;
682
683 os2_strerror(text, sizeof(text), code, "");
684
685 v = Py_BuildValue("(is)", code, text);
686 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000687 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000688 Py_DECREF(v);
689 }
690 return NULL; /* Signal to Python that an Exception is Pending */
691}
692
693#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694
695/* POSIX generic methods */
696
Barry Warsaw53699e91996-12-10 23:23:01 +0000697static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000698posix_fildes(PyObject *fdobj, int (*func)(int))
699{
Victor Stinner8c62be82010-05-06 00:08:46 +0000700 int fd;
701 int res;
702 fd = PyObject_AsFileDescriptor(fdobj);
703 if (fd < 0)
704 return NULL;
705 if (!_PyVerify_fd(fd))
706 return posix_error();
707 Py_BEGIN_ALLOW_THREADS
708 res = (*func)(fd);
709 Py_END_ALLOW_THREADS
710 if (res < 0)
711 return posix_error();
712 Py_INCREF(Py_None);
713 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000714}
Guido van Rossum21142a01999-01-08 21:05:37 +0000715
716static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000717posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718{
Victor Stinner8c62be82010-05-06 00:08:46 +0000719 PyObject *opath1 = NULL;
720 char *path1;
721 int res;
722 if (!PyArg_ParseTuple(args, format,
723 PyUnicode_FSConverter, &opath1))
724 return NULL;
725 path1 = PyBytes_AsString(opath1);
726 Py_BEGIN_ALLOW_THREADS
727 res = (*func)(path1);
728 Py_END_ALLOW_THREADS
729 if (res < 0)
730 return posix_error_with_allocated_filename(opath1);
731 Py_DECREF(opath1);
732 Py_INCREF(Py_None);
733 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734}
735
Barry Warsaw53699e91996-12-10 23:23:01 +0000736static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000737posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000738 char *format,
739 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000740{
Victor Stinner8c62be82010-05-06 00:08:46 +0000741 PyObject *opath1 = NULL, *opath2 = NULL;
742 char *path1, *path2;
743 int res;
744 if (!PyArg_ParseTuple(args, format,
745 PyUnicode_FSConverter, &opath1,
746 PyUnicode_FSConverter, &opath2)) {
747 return NULL;
748 }
749 path1 = PyBytes_AsString(opath1);
750 path2 = PyBytes_AsString(opath2);
751 Py_BEGIN_ALLOW_THREADS
752 res = (*func)(path1, path2);
753 Py_END_ALLOW_THREADS
754 Py_DECREF(opath1);
755 Py_DECREF(opath2);
756 if (res != 0)
757 /* XXX how to report both path1 and path2??? */
758 return posix_error();
759 Py_INCREF(Py_None);
760 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761}
762
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000763#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000764static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000765win32_1str(PyObject* args, char* func,
766 char* format, BOOL (__stdcall *funcA)(LPCSTR),
767 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000768{
Victor Stinner8c62be82010-05-06 00:08:46 +0000769 PyObject *uni;
770 char *ansi;
771 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000772
Victor Stinner8c62be82010-05-06 00:08:46 +0000773 if (!PyArg_ParseTuple(args, wformat, &uni))
774 PyErr_Clear();
775 else {
776 Py_BEGIN_ALLOW_THREADS
777 result = funcW(PyUnicode_AsUnicode(uni));
778 Py_END_ALLOW_THREADS
779 if (!result)
780 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
781 Py_INCREF(Py_None);
782 return Py_None;
783 }
784 if (!PyArg_ParseTuple(args, format, &ansi))
785 return NULL;
786 Py_BEGIN_ALLOW_THREADS
787 result = funcA(ansi);
788 Py_END_ALLOW_THREADS
789 if (!result)
790 return win32_error(func, ansi);
791 Py_INCREF(Py_None);
792 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000793
794}
795
796/* This is a reimplementation of the C library's chdir function,
797 but one that produces Win32 errors instead of DOS error codes.
798 chdir is essentially a wrapper around SetCurrentDirectory; however,
799 it also needs to set "magic" environment variables indicating
800 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000801static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802win32_chdir(LPCSTR path)
803{
Victor Stinner8c62be82010-05-06 00:08:46 +0000804 char new_path[MAX_PATH+1];
805 int result;
806 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807
Victor Stinner8c62be82010-05-06 00:08:46 +0000808 if(!SetCurrentDirectoryA(path))
809 return FALSE;
810 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
811 if (!result)
812 return FALSE;
813 /* In the ANSI API, there should not be any paths longer
814 than MAX_PATH. */
815 assert(result <= MAX_PATH+1);
816 if (strncmp(new_path, "\\\\", 2) == 0 ||
817 strncmp(new_path, "//", 2) == 0)
818 /* UNC path, nothing to do. */
819 return TRUE;
820 env[1] = new_path[0];
821 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000822}
823
824/* The Unicode version differs from the ANSI version
825 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000826static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000827win32_wchdir(LPCWSTR path)
828{
Victor Stinner8c62be82010-05-06 00:08:46 +0000829 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
830 int result;
831 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832
Victor Stinner8c62be82010-05-06 00:08:46 +0000833 if(!SetCurrentDirectoryW(path))
834 return FALSE;
835 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
836 if (!result)
837 return FALSE;
838 if (result > MAX_PATH+1) {
839 new_path = malloc(result * sizeof(wchar_t));
840 if (!new_path) {
841 SetLastError(ERROR_OUTOFMEMORY);
842 return FALSE;
843 }
844 result = GetCurrentDirectoryW(result, new_path);
845 if (!result) {
846 free(new_path);
847 return FALSE;
848 }
849 }
850 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
851 wcsncmp(new_path, L"//", 2) == 0)
852 /* UNC path, nothing to do. */
853 return TRUE;
854 env[1] = new_path[0];
855 result = SetEnvironmentVariableW(env, new_path);
856 if (new_path != _new_path)
857 free(new_path);
858 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859}
860#endif
861
Martin v. Löwis14694662006-02-03 12:54:16 +0000862#ifdef MS_WINDOWS
863/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
864 - time stamps are restricted to second resolution
865 - file modification times suffer from forth-and-back conversions between
866 UTC and local time
867 Therefore, we implement our own stat, based on the Win32 API directly.
868*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000869#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000870
871struct win32_stat{
872 int st_dev;
873 __int64 st_ino;
874 unsigned short st_mode;
875 int st_nlink;
876 int st_uid;
877 int st_gid;
878 int st_rdev;
879 __int64 st_size;
880 int st_atime;
881 int st_atime_nsec;
882 int st_mtime;
883 int st_mtime_nsec;
884 int st_ctime;
885 int st_ctime_nsec;
886};
887
888static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
889
890static void
891FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
892{
Victor Stinner8c62be82010-05-06 00:08:46 +0000893 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
894 /* Cannot simply cast and dereference in_ptr,
895 since it might not be aligned properly */
896 __int64 in;
897 memcpy(&in, in_ptr, sizeof(in));
898 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
899 /* XXX Win32 supports time stamps past 2038; we currently don't */
900 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000901}
902
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903static void
904time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
905{
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 /* XXX endianness */
907 __int64 out;
908 out = time_in + secs_between_epochs;
909 out = out * 10000000 + nsec_in / 100;
910 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911}
912
Martin v. Löwis14694662006-02-03 12:54:16 +0000913/* Below, we *know* that ugo+r is 0444 */
914#if _S_IREAD != 0400
915#error Unsupported C library
916#endif
917static int
918attributes_to_mode(DWORD attr)
919{
Victor Stinner8c62be82010-05-06 00:08:46 +0000920 int m = 0;
921 if (attr & FILE_ATTRIBUTE_DIRECTORY)
922 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
923 else
924 m |= _S_IFREG;
925 if (attr & FILE_ATTRIBUTE_READONLY)
926 m |= 0444;
927 else
928 m |= 0666;
929 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000930}
931
932static int
933attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
934{
Victor Stinner8c62be82010-05-06 00:08:46 +0000935 memset(result, 0, sizeof(*result));
936 result->st_mode = attributes_to_mode(info->dwFileAttributes);
937 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
938 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
939 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
940 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000941
Victor Stinner8c62be82010-05-06 00:08:46 +0000942 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000943}
944
Guido van Rossumd8faa362007-04-27 19:54:29 +0000945static BOOL
946attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
947{
Victor Stinner8c62be82010-05-06 00:08:46 +0000948 HANDLE hFindFile;
949 WIN32_FIND_DATAA FileData;
950 hFindFile = FindFirstFileA(pszFile, &FileData);
951 if (hFindFile == INVALID_HANDLE_VALUE)
952 return FALSE;
953 FindClose(hFindFile);
954 pfad->dwFileAttributes = FileData.dwFileAttributes;
955 pfad->ftCreationTime = FileData.ftCreationTime;
956 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
957 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
958 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
959 pfad->nFileSizeLow = FileData.nFileSizeLow;
960 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000961}
962
963static BOOL
964attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
965{
Victor Stinner8c62be82010-05-06 00:08:46 +0000966 HANDLE hFindFile;
967 WIN32_FIND_DATAW FileData;
968 hFindFile = FindFirstFileW(pszFile, &FileData);
969 if (hFindFile == INVALID_HANDLE_VALUE)
970 return FALSE;
971 FindClose(hFindFile);
972 pfad->dwFileAttributes = FileData.dwFileAttributes;
973 pfad->ftCreationTime = FileData.ftCreationTime;
974 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
975 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
976 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
977 pfad->nFileSizeLow = FileData.nFileSizeLow;
978 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000979}
980
Victor Stinner8c62be82010-05-06 00:08:46 +0000981static int
Martin v. Löwis14694662006-02-03 12:54:16 +0000982win32_stat(const char* path, struct win32_stat *result)
983{
Victor Stinner8c62be82010-05-06 00:08:46 +0000984 WIN32_FILE_ATTRIBUTE_DATA info;
985 int code;
986 char *dot;
987 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
988 if (GetLastError() != ERROR_SHARING_VIOLATION) {
989 /* Protocol violation: we explicitly clear errno, instead of
990 setting it to a POSIX error. Callers should use GetLastError. */
991 errno = 0;
992 return -1;
993 } else {
994 /* Could not get attributes on open file. Fall back to
995 reading the directory. */
996 if (!attributes_from_dir(path, &info)) {
997 /* Very strange. This should not fail now */
998 errno = 0;
999 return -1;
1000 }
1001 }
1002 }
1003 code = attribute_data_to_stat(&info, result);
1004 if (code != 0)
1005 return code;
1006 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1007 dot = strrchr(path, '.');
1008 if (dot) {
1009 if (stricmp(dot, ".bat") == 0 ||
1010 stricmp(dot, ".cmd") == 0 ||
1011 stricmp(dot, ".exe") == 0 ||
1012 stricmp(dot, ".com") == 0)
1013 result->st_mode |= 0111;
1014 }
1015 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001016}
1017
Victor Stinner8c62be82010-05-06 00:08:46 +00001018static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001019win32_wstat(const wchar_t* path, struct win32_stat *result)
1020{
Victor Stinner8c62be82010-05-06 00:08:46 +00001021 int code;
1022 const wchar_t *dot;
1023 WIN32_FILE_ATTRIBUTE_DATA info;
1024 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1025 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1026 /* Protocol violation: we explicitly clear errno, instead of
1027 setting it to a POSIX error. Callers should use GetLastError. */
1028 errno = 0;
1029 return -1;
1030 } else {
1031 /* Could not get attributes on open file. Fall back to
1032 reading the directory. */
1033 if (!attributes_from_dir_w(path, &info)) {
1034 /* Very strange. This should not fail now */
1035 errno = 0;
1036 return -1;
1037 }
1038 }
1039 }
1040 code = attribute_data_to_stat(&info, result);
1041 if (code < 0)
1042 return code;
1043 /* Set IFEXEC if it is an .exe, .bat, ... */
1044 dot = wcsrchr(path, '.');
1045 if (dot) {
1046 if (_wcsicmp(dot, L".bat") == 0 ||
1047 _wcsicmp(dot, L".cmd") == 0 ||
1048 _wcsicmp(dot, L".exe") == 0 ||
1049 _wcsicmp(dot, L".com") == 0)
1050 result->st_mode |= 0111;
1051 }
1052 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001053}
1054
1055static int
1056win32_fstat(int file_number, struct win32_stat *result)
1057{
Victor Stinner8c62be82010-05-06 00:08:46 +00001058 BY_HANDLE_FILE_INFORMATION info;
1059 HANDLE h;
1060 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001061
Victor Stinner8c62be82010-05-06 00:08:46 +00001062 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001063
Victor Stinner8c62be82010-05-06 00:08:46 +00001064 /* Protocol violation: we explicitly clear errno, instead of
1065 setting it to a POSIX error. Callers should use GetLastError. */
1066 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001067
Victor Stinner8c62be82010-05-06 00:08:46 +00001068 if (h == INVALID_HANDLE_VALUE) {
1069 /* This is really a C library error (invalid file handle).
1070 We set the Win32 error to the closes one matching. */
1071 SetLastError(ERROR_INVALID_HANDLE);
1072 return -1;
1073 }
1074 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001075
Victor Stinner8c62be82010-05-06 00:08:46 +00001076 type = GetFileType(h);
1077 if (type == FILE_TYPE_UNKNOWN) {
1078 DWORD error = GetLastError();
1079 if (error != 0) {
1080 return -1;
1081 }
1082 /* else: valid but unknown file */
1083 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001084
Victor Stinner8c62be82010-05-06 00:08:46 +00001085 if (type != FILE_TYPE_DISK) {
1086 if (type == FILE_TYPE_CHAR)
1087 result->st_mode = _S_IFCHR;
1088 else if (type == FILE_TYPE_PIPE)
1089 result->st_mode = _S_IFIFO;
1090 return 0;
1091 }
1092
1093 if (!GetFileInformationByHandle(h, &info)) {
1094 return -1;
1095 }
1096
1097 /* similar to stat() */
1098 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1099 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1100 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1101 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1102 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1103 /* specific to fstat() */
1104 result->st_nlink = info.nNumberOfLinks;
1105 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1106 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001107}
1108
1109#endif /* MS_WINDOWS */
1110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001111PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001112"stat_result: Result from stat or lstat.\n\n\
1113This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001114 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1116\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001117Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1118or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001119\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001120See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001121
1122static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001123 {"st_mode", "protection bits"},
1124 {"st_ino", "inode"},
1125 {"st_dev", "device"},
1126 {"st_nlink", "number of hard links"},
1127 {"st_uid", "user ID of owner"},
1128 {"st_gid", "group ID of owner"},
1129 {"st_size", "total size, in bytes"},
1130 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1131 {NULL, "integer time of last access"},
1132 {NULL, "integer time of last modification"},
1133 {NULL, "integer time of last change"},
1134 {"st_atime", "time of last access"},
1135 {"st_mtime", "time of last modification"},
1136 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001137#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001138 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001139#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001140#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001141 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001142#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001143#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001144 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001145#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001146#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001147 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001148#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001149#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001150 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001151#endif
1152#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001153 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001154#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001155 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001156};
1157
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001158#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001159#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001160#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001161#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001162#endif
1163
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001164#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001165#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1166#else
1167#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1168#endif
1169
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001170#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001171#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1172#else
1173#define ST_RDEV_IDX ST_BLOCKS_IDX
1174#endif
1175
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001176#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1177#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1178#else
1179#define ST_FLAGS_IDX ST_RDEV_IDX
1180#endif
1181
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001182#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001183#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001184#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001185#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001186#endif
1187
1188#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1189#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1190#else
1191#define ST_BIRTHTIME_IDX ST_GEN_IDX
1192#endif
1193
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001194static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001195 "stat_result", /* name */
1196 stat_result__doc__, /* doc */
1197 stat_result_fields,
1198 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001199};
1200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001201PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001202"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1203This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001204 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001205or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001206\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001207See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001208
1209static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001210 {"f_bsize", },
1211 {"f_frsize", },
1212 {"f_blocks", },
1213 {"f_bfree", },
1214 {"f_bavail", },
1215 {"f_files", },
1216 {"f_ffree", },
1217 {"f_favail", },
1218 {"f_flag", },
1219 {"f_namemax",},
1220 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001221};
1222
1223static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001224 "statvfs_result", /* name */
1225 statvfs_result__doc__, /* doc */
1226 statvfs_result_fields,
1227 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001228};
1229
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001230static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001231static PyTypeObject StatResultType;
1232static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001233static newfunc structseq_new;
1234
1235static PyObject *
1236statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1237{
Victor Stinner8c62be82010-05-06 00:08:46 +00001238 PyStructSequence *result;
1239 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001240
Victor Stinner8c62be82010-05-06 00:08:46 +00001241 result = (PyStructSequence*)structseq_new(type, args, kwds);
1242 if (!result)
1243 return NULL;
1244 /* If we have been initialized from a tuple,
1245 st_?time might be set to None. Initialize it
1246 from the int slots. */
1247 for (i = 7; i <= 9; i++) {
1248 if (result->ob_item[i+3] == Py_None) {
1249 Py_DECREF(Py_None);
1250 Py_INCREF(result->ob_item[i]);
1251 result->ob_item[i+3] = result->ob_item[i];
1252 }
1253 }
1254 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001255}
1256
1257
1258
1259/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001260static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001261
1262PyDoc_STRVAR(stat_float_times__doc__,
1263"stat_float_times([newval]) -> oldval\n\n\
1264Determine whether os.[lf]stat represents time stamps as float objects.\n\
1265If newval is True, future calls to stat() return floats, if it is False,\n\
1266future calls return ints. \n\
1267If newval is omitted, return the current setting.\n");
1268
1269static PyObject*
1270stat_float_times(PyObject* self, PyObject *args)
1271{
Victor Stinner8c62be82010-05-06 00:08:46 +00001272 int newval = -1;
1273 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1274 return NULL;
1275 if (newval == -1)
1276 /* Return old value */
1277 return PyBool_FromLong(_stat_float_times);
1278 _stat_float_times = newval;
1279 Py_INCREF(Py_None);
1280 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001281}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001282
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001283static void
1284fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1285{
Victor Stinner8c62be82010-05-06 00:08:46 +00001286 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001287#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001288 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001289#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001290 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001291#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001292 if (!ival)
1293 return;
1294 if (_stat_float_times) {
1295 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1296 } else {
1297 fval = ival;
1298 Py_INCREF(fval);
1299 }
1300 PyStructSequence_SET_ITEM(v, index, ival);
1301 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001302}
1303
Tim Peters5aa91602002-01-30 05:46:57 +00001304/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001305 (used by posix_stat() and posix_fstat()) */
1306static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001307_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001308{
Victor Stinner8c62be82010-05-06 00:08:46 +00001309 unsigned long ansec, mnsec, cnsec;
1310 PyObject *v = PyStructSequence_New(&StatResultType);
1311 if (v == NULL)
1312 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001313
Victor Stinner8c62be82010-05-06 00:08:46 +00001314 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001315#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001316 PyStructSequence_SET_ITEM(v, 1,
1317 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001318#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001319 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001320#endif
1321#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001322 PyStructSequence_SET_ITEM(v, 2,
1323 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001324#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001325 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001326#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001327 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1328 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1329 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001330#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001331 PyStructSequence_SET_ITEM(v, 6,
1332 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001333#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001334 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001335#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001336
Martin v. Löwis14694662006-02-03 12:54:16 +00001337#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001338 ansec = st->st_atim.tv_nsec;
1339 mnsec = st->st_mtim.tv_nsec;
1340 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001341#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001342 ansec = st->st_atimespec.tv_nsec;
1343 mnsec = st->st_mtimespec.tv_nsec;
1344 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001345#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001346 ansec = st->st_atime_nsec;
1347 mnsec = st->st_mtime_nsec;
1348 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001349#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001350 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001351#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001352 fill_time(v, 7, st->st_atime, ansec);
1353 fill_time(v, 8, st->st_mtime, mnsec);
1354 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001355
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001356#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001357 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1358 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001360#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001361 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1362 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001363#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001364#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001365 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1366 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001367#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001368#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001369 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1370 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001371#endif
1372#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001373 {
1374 PyObject *val;
1375 unsigned long bsec,bnsec;
1376 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001377#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001378 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001379#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001380 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001381#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001382 if (_stat_float_times) {
1383 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1384 } else {
1385 val = PyLong_FromLong((long)bsec);
1386 }
1387 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1388 val);
1389 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001390#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001391#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001392 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1393 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001394#endif
Fred Drake699f3522000-06-29 21:12:41 +00001395
Victor Stinner8c62be82010-05-06 00:08:46 +00001396 if (PyErr_Occurred()) {
1397 Py_DECREF(v);
1398 return NULL;
1399 }
Fred Drake699f3522000-06-29 21:12:41 +00001400
Victor Stinner8c62be82010-05-06 00:08:46 +00001401 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001402}
1403
Martin v. Löwisd8948722004-06-02 09:57:56 +00001404#ifdef MS_WINDOWS
1405
1406/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1407 where / can be used in place of \ and the trailing slash is optional.
1408 Both SERVER and SHARE must have at least one character.
1409*/
1410
1411#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1412#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001413#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001414#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001415#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001416
Tim Peters4ad82172004-08-30 17:02:04 +00001417static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001418IsUNCRootA(char *path, int pathlen)
1419{
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001423
Victor Stinner8c62be82010-05-06 00:08:46 +00001424 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1425 /* minimum UNCRoot is \\x\y */
1426 return FALSE;
1427 for (i = 2; i < pathlen ; i++)
1428 if (ISSLASH(path[i])) break;
1429 if (i == 2 || i == pathlen)
1430 /* do not allow \\\SHARE or \\SERVER */
1431 return FALSE;
1432 share = i+1;
1433 for (i = share; i < pathlen; i++)
1434 if (ISSLASH(path[i])) break;
1435 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001436
Victor Stinner8c62be82010-05-06 00:08:46 +00001437 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001438}
1439
Tim Peters4ad82172004-08-30 17:02:04 +00001440static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001441IsUNCRootW(Py_UNICODE *path, int pathlen)
1442{
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001444
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001446
Victor Stinner8c62be82010-05-06 00:08:46 +00001447 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1448 /* minimum UNCRoot is \\x\y */
1449 return FALSE;
1450 for (i = 2; i < pathlen ; i++)
1451 if (ISSLASH(path[i])) break;
1452 if (i == 2 || i == pathlen)
1453 /* do not allow \\\SHARE or \\SERVER */
1454 return FALSE;
1455 share = i+1;
1456 for (i = share; i < pathlen; i++)
1457 if (ISSLASH(path[i])) break;
1458 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001459
Victor Stinner8c62be82010-05-06 00:08:46 +00001460 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001461}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001462#endif /* MS_WINDOWS */
1463
Barry Warsaw53699e91996-12-10 23:23:01 +00001464static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001465posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001466 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001467#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001469#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001470 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001471#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001472 char *wformat,
1473 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001474{
Victor Stinner8c62be82010-05-06 00:08:46 +00001475 STRUCT_STAT st;
1476 PyObject *opath;
1477 char *path;
1478 int res;
1479 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001480
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001481#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001482 PyUnicodeObject *po;
1483 if (PyArg_ParseTuple(args, wformat, &po)) {
1484 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001485
Victor Stinner8c62be82010-05-06 00:08:46 +00001486 Py_BEGIN_ALLOW_THREADS
1487 /* PyUnicode_AS_UNICODE result OK without
1488 thread lock as it is a simple dereference. */
1489 res = wstatfunc(wpath, &st);
1490 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001491
Victor Stinner8c62be82010-05-06 00:08:46 +00001492 if (res != 0)
1493 return win32_error_unicode("stat", wpath);
1494 return _pystat_fromstructstat(&st);
1495 }
1496 /* Drop the argument parsing error as narrow strings
1497 are also valid. */
1498 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001499#endif
1500
Victor Stinner8c62be82010-05-06 00:08:46 +00001501 if (!PyArg_ParseTuple(args, format,
1502 PyUnicode_FSConverter, &opath))
1503 return NULL;
1504 path = PyBytes_AsString(opath);
1505 Py_BEGIN_ALLOW_THREADS
1506 res = (*statfunc)(path, &st);
1507 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001508
Victor Stinner8c62be82010-05-06 00:08:46 +00001509 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001510#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001511 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001512#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001513 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001514#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001515 }
1516 else
1517 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001518
Victor Stinner8c62be82010-05-06 00:08:46 +00001519 Py_DECREF(opath);
1520 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001521}
1522
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001523/* POSIX methods */
1524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001526"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001527Use the real uid/gid to test for access to a path. Note that most\n\
1528operations will use the effective uid/gid, therefore this routine can\n\
1529be used in a suid/sgid environment to test if the invoking user has the\n\
1530specified access to the path. The mode argument can be F_OK to test\n\
1531existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001532
1533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001534posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001535{
Victor Stinner8c62be82010-05-06 00:08:46 +00001536 PyObject *opath;
1537 char *path;
1538 int mode;
1539
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001540#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001541 DWORD attr;
1542 PyUnicodeObject *po;
1543 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1544 Py_BEGIN_ALLOW_THREADS
1545 /* PyUnicode_AS_UNICODE OK without thread lock as
1546 it is a simple dereference. */
1547 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1548 Py_END_ALLOW_THREADS
1549 goto finish;
1550 }
1551 /* Drop the argument parsing error as narrow strings
1552 are also valid. */
1553 PyErr_Clear();
1554 if (!PyArg_ParseTuple(args, "O&i:access",
1555 PyUnicode_FSConverter, &opath, &mode))
1556 return NULL;
1557 path = PyBytes_AsString(opath);
1558 Py_BEGIN_ALLOW_THREADS
1559 attr = GetFileAttributesA(path);
1560 Py_END_ALLOW_THREADS
1561 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001562finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 if (attr == 0xFFFFFFFF)
1564 /* File does not exist, or cannot read attributes */
1565 return PyBool_FromLong(0);
1566 /* Access is possible if either write access wasn't requested, or
1567 the file isn't read-only, or if it's a directory, as there are
1568 no read-only directories on Windows. */
1569 return PyBool_FromLong(!(mode & 2)
1570 || !(attr & FILE_ATTRIBUTE_READONLY)
1571 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001572#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001573 int res;
1574 if (!PyArg_ParseTuple(args, "O&i:access",
1575 PyUnicode_FSConverter, &opath, &mode))
1576 return NULL;
1577 path = PyBytes_AsString(opath);
1578 Py_BEGIN_ALLOW_THREADS
1579 res = access(path, mode);
1580 Py_END_ALLOW_THREADS
1581 Py_DECREF(opath);
1582 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001583#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001584}
1585
Guido van Rossumd371ff11999-01-25 16:12:23 +00001586#ifndef F_OK
1587#define F_OK 0
1588#endif
1589#ifndef R_OK
1590#define R_OK 4
1591#endif
1592#ifndef W_OK
1593#define W_OK 2
1594#endif
1595#ifndef X_OK
1596#define X_OK 1
1597#endif
1598
1599#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001600PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001601"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001603
1604static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001605posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001606{
Victor Stinner8c62be82010-05-06 00:08:46 +00001607 int id;
1608 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001609
Victor Stinner8c62be82010-05-06 00:08:46 +00001610 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1611 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001612
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001613#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001614 /* file descriptor 0 only, the default input device (stdin) */
1615 if (id == 0) {
1616 ret = ttyname();
1617 }
1618 else {
1619 ret = NULL;
1620 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001621#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001623#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001624 if (ret == NULL)
1625 return posix_error();
1626 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001627}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001628#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001629
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001630#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001631PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001632"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001634
1635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001636posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001637{
Victor Stinner8c62be82010-05-06 00:08:46 +00001638 char *ret;
1639 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001640
Greg Wardb48bc172000-03-01 21:51:56 +00001641#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001642 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001643#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001644 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001645#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001646 if (ret == NULL)
1647 return posix_error();
1648 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001649}
1650#endif
1651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001653"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001655
Barry Warsaw53699e91996-12-10 23:23:01 +00001656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001657posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001658{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001659#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001661#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001663#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001664 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001665#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001666 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001667#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668}
1669
Fred Drake4d1e64b2002-04-15 19:40:07 +00001670#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001671PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001672"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001673Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001674opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001675
1676static PyObject *
1677posix_fchdir(PyObject *self, PyObject *fdobj)
1678{
Victor Stinner8c62be82010-05-06 00:08:46 +00001679 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001680}
1681#endif /* HAVE_FCHDIR */
1682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001683
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001684PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001685"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001687
Barry Warsaw53699e91996-12-10 23:23:01 +00001688static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001689posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001690{
Victor Stinner8c62be82010-05-06 00:08:46 +00001691 PyObject *opath = NULL;
1692 char *path = NULL;
1693 int i;
1694 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001695#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 DWORD attr;
1697 PyUnicodeObject *po;
1698 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1699 Py_BEGIN_ALLOW_THREADS
1700 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1701 if (attr != 0xFFFFFFFF) {
1702 if (i & _S_IWRITE)
1703 attr &= ~FILE_ATTRIBUTE_READONLY;
1704 else
1705 attr |= FILE_ATTRIBUTE_READONLY;
1706 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1707 }
1708 else
1709 res = 0;
1710 Py_END_ALLOW_THREADS
1711 if (!res)
1712 return win32_error_unicode("chmod",
1713 PyUnicode_AS_UNICODE(po));
1714 Py_INCREF(Py_None);
1715 return Py_None;
1716 }
1717 /* Drop the argument parsing error as narrow strings
1718 are also valid. */
1719 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001720
Victor Stinner8c62be82010-05-06 00:08:46 +00001721 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1722 &opath, &i))
1723 return NULL;
1724 path = PyBytes_AsString(opath);
1725 Py_BEGIN_ALLOW_THREADS
1726 attr = GetFileAttributesA(path);
1727 if (attr != 0xFFFFFFFF) {
1728 if (i & _S_IWRITE)
1729 attr &= ~FILE_ATTRIBUTE_READONLY;
1730 else
1731 attr |= FILE_ATTRIBUTE_READONLY;
1732 res = SetFileAttributesA(path, attr);
1733 }
1734 else
1735 res = 0;
1736 Py_END_ALLOW_THREADS
1737 if (!res) {
1738 win32_error("chmod", path);
1739 Py_DECREF(opath);
1740 return NULL;
1741 }
1742 Py_DECREF(opath);
1743 Py_INCREF(Py_None);
1744 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001745#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001746 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1747 &opath, &i))
1748 return NULL;
1749 path = PyBytes_AsString(opath);
1750 Py_BEGIN_ALLOW_THREADS
1751 res = chmod(path, i);
1752 Py_END_ALLOW_THREADS
1753 if (res < 0)
1754 return posix_error_with_allocated_filename(opath);
1755 Py_DECREF(opath);
1756 Py_INCREF(Py_None);
1757 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001758#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001759}
1760
Christian Heimes4e30a842007-11-30 22:12:06 +00001761#ifdef HAVE_FCHMOD
1762PyDoc_STRVAR(posix_fchmod__doc__,
1763"fchmod(fd, mode)\n\n\
1764Change the access permissions of the file given by file\n\
1765descriptor fd.");
1766
1767static PyObject *
1768posix_fchmod(PyObject *self, PyObject *args)
1769{
Victor Stinner8c62be82010-05-06 00:08:46 +00001770 int fd, mode, res;
1771 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1772 return NULL;
1773 Py_BEGIN_ALLOW_THREADS
1774 res = fchmod(fd, mode);
1775 Py_END_ALLOW_THREADS
1776 if (res < 0)
1777 return posix_error();
1778 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001779}
1780#endif /* HAVE_FCHMOD */
1781
1782#ifdef HAVE_LCHMOD
1783PyDoc_STRVAR(posix_lchmod__doc__,
1784"lchmod(path, mode)\n\n\
1785Change the access permissions of a file. If path is a symlink, this\n\
1786affects the link itself rather than the target.");
1787
1788static PyObject *
1789posix_lchmod(PyObject *self, PyObject *args)
1790{
Victor Stinner8c62be82010-05-06 00:08:46 +00001791 PyObject *opath;
1792 char *path;
1793 int i;
1794 int res;
1795 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1796 &opath, &i))
1797 return NULL;
1798 path = PyBytes_AsString(opath);
1799 Py_BEGIN_ALLOW_THREADS
1800 res = lchmod(path, i);
1801 Py_END_ALLOW_THREADS
1802 if (res < 0)
1803 return posix_error_with_allocated_filename(opath);
1804 Py_DECREF(opath);
1805 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001806}
1807#endif /* HAVE_LCHMOD */
1808
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001809
Thomas Wouterscf297e42007-02-23 15:07:44 +00001810#ifdef HAVE_CHFLAGS
1811PyDoc_STRVAR(posix_chflags__doc__,
1812"chflags(path, flags)\n\n\
1813Set file flags.");
1814
1815static PyObject *
1816posix_chflags(PyObject *self, PyObject *args)
1817{
Victor Stinner8c62be82010-05-06 00:08:46 +00001818 PyObject *opath;
1819 char *path;
1820 unsigned long flags;
1821 int res;
1822 if (!PyArg_ParseTuple(args, "O&k:chflags",
1823 PyUnicode_FSConverter, &opath, &flags))
1824 return NULL;
1825 path = PyBytes_AsString(opath);
1826 Py_BEGIN_ALLOW_THREADS
1827 res = chflags(path, flags);
1828 Py_END_ALLOW_THREADS
1829 if (res < 0)
1830 return posix_error_with_allocated_filename(opath);
1831 Py_DECREF(opath);
1832 Py_INCREF(Py_None);
1833 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001834}
1835#endif /* HAVE_CHFLAGS */
1836
1837#ifdef HAVE_LCHFLAGS
1838PyDoc_STRVAR(posix_lchflags__doc__,
1839"lchflags(path, flags)\n\n\
1840Set file flags.\n\
1841This function will not follow symbolic links.");
1842
1843static PyObject *
1844posix_lchflags(PyObject *self, PyObject *args)
1845{
Victor Stinner8c62be82010-05-06 00:08:46 +00001846 PyObject *opath;
1847 char *path;
1848 unsigned long flags;
1849 int res;
1850 if (!PyArg_ParseTuple(args, "O&k:lchflags",
1851 PyUnicode_FSConverter, &opath, &flags))
1852 return NULL;
1853 path = PyBytes_AsString(opath);
1854 Py_BEGIN_ALLOW_THREADS
1855 res = lchflags(path, flags);
1856 Py_END_ALLOW_THREADS
1857 if (res < 0)
1858 return posix_error_with_allocated_filename(opath);
1859 Py_DECREF(opath);
1860 Py_INCREF(Py_None);
1861 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001862}
1863#endif /* HAVE_LCHFLAGS */
1864
Martin v. Löwis244edc82001-10-04 22:44:26 +00001865#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001866PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001867"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001868Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001869
1870static PyObject *
1871posix_chroot(PyObject *self, PyObject *args)
1872{
Victor Stinner8c62be82010-05-06 00:08:46 +00001873 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001874}
1875#endif
1876
Guido van Rossum21142a01999-01-08 21:05:37 +00001877#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001880force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001881
1882static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001883posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001884{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001885 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001886}
1887#endif /* HAVE_FSYNC */
1888
1889#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001890
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001891#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001892extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1893#endif
1894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001896"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001897force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001899
1900static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001901posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001902{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001903 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001904}
1905#endif /* HAVE_FDATASYNC */
1906
1907
Fredrik Lundh10723342000-07-10 16:38:09 +00001908#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001909PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001910"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001911Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001912
Barry Warsaw53699e91996-12-10 23:23:01 +00001913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001914posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001915{
Victor Stinner8c62be82010-05-06 00:08:46 +00001916 PyObject *opath;
1917 char *path;
1918 long uid, gid;
1919 int res;
1920 if (!PyArg_ParseTuple(args, "O&ll:chown",
1921 PyUnicode_FSConverter, &opath,
1922 &uid, &gid))
1923 return NULL;
1924 path = PyBytes_AsString(opath);
1925 Py_BEGIN_ALLOW_THREADS
1926 res = chown(path, (uid_t) uid, (gid_t) gid);
1927 Py_END_ALLOW_THREADS
1928 if (res < 0)
1929 return posix_error_with_allocated_filename(opath);
1930 Py_DECREF(opath);
1931 Py_INCREF(Py_None);
1932 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001933}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001934#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001935
Christian Heimes4e30a842007-11-30 22:12:06 +00001936#ifdef HAVE_FCHOWN
1937PyDoc_STRVAR(posix_fchown__doc__,
1938"fchown(fd, uid, gid)\n\n\
1939Change the owner and group id of the file given by file descriptor\n\
1940fd to the numeric uid and gid.");
1941
1942static PyObject *
1943posix_fchown(PyObject *self, PyObject *args)
1944{
Victor Stinner8c62be82010-05-06 00:08:46 +00001945 int fd;
1946 long uid, gid;
1947 int res;
1948 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
1949 return NULL;
1950 Py_BEGIN_ALLOW_THREADS
1951 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1952 Py_END_ALLOW_THREADS
1953 if (res < 0)
1954 return posix_error();
1955 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001956}
1957#endif /* HAVE_FCHOWN */
1958
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001959#ifdef HAVE_LCHOWN
1960PyDoc_STRVAR(posix_lchown__doc__,
1961"lchown(path, uid, gid)\n\n\
1962Change the owner and group id of path to the numeric uid and gid.\n\
1963This function will not follow symbolic links.");
1964
1965static PyObject *
1966posix_lchown(PyObject *self, PyObject *args)
1967{
Victor Stinner8c62be82010-05-06 00:08:46 +00001968 PyObject *opath;
1969 char *path;
1970 long uid, gid;
1971 int res;
1972 if (!PyArg_ParseTuple(args, "O&ll:lchown",
1973 PyUnicode_FSConverter, &opath,
1974 &uid, &gid))
1975 return NULL;
1976 path = PyBytes_AsString(opath);
1977 Py_BEGIN_ALLOW_THREADS
1978 res = lchown(path, (uid_t) uid, (gid_t) gid);
1979 Py_END_ALLOW_THREADS
1980 if (res < 0)
1981 return posix_error_with_allocated_filename(opath);
1982 Py_DECREF(opath);
1983 Py_INCREF(Py_None);
1984 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001985}
1986#endif /* HAVE_LCHOWN */
1987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001988
Guido van Rossum36bc6801995-06-14 22:54:23 +00001989#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00001990static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001991posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001992{
Victor Stinner8c62be82010-05-06 00:08:46 +00001993 char buf[1026];
1994 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001996#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001997 if (!use_bytes) {
1998 wchar_t wbuf[1026];
1999 wchar_t *wbuf2 = wbuf;
2000 PyObject *resobj;
2001 DWORD len;
2002 Py_BEGIN_ALLOW_THREADS
2003 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2004 /* If the buffer is large enough, len does not include the
2005 terminating \0. If the buffer is too small, len includes
2006 the space needed for the terminator. */
2007 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2008 wbuf2 = malloc(len * sizeof(wchar_t));
2009 if (wbuf2)
2010 len = GetCurrentDirectoryW(len, wbuf2);
2011 }
2012 Py_END_ALLOW_THREADS
2013 if (!wbuf2) {
2014 PyErr_NoMemory();
2015 return NULL;
2016 }
2017 if (!len) {
2018 if (wbuf2 != wbuf) free(wbuf2);
2019 return win32_error("getcwdu", NULL);
2020 }
2021 resobj = PyUnicode_FromWideChar(wbuf2, len);
2022 if (wbuf2 != wbuf) free(wbuf2);
2023 return resobj;
2024 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002025#endif
2026
Victor Stinner8c62be82010-05-06 00:08:46 +00002027 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002028#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002029 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002030#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002031 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002032#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002033 Py_END_ALLOW_THREADS
2034 if (res == NULL)
2035 return posix_error();
2036 if (use_bytes)
2037 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002038 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002039}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002040
2041PyDoc_STRVAR(posix_getcwd__doc__,
2042"getcwd() -> path\n\n\
2043Return a unicode string representing the current working directory.");
2044
2045static PyObject *
2046posix_getcwd_unicode(PyObject *self)
2047{
2048 return posix_getcwd(0);
2049}
2050
2051PyDoc_STRVAR(posix_getcwdb__doc__,
2052"getcwdb() -> path\n\n\
2053Return a bytes string representing the current working directory.");
2054
2055static PyObject *
2056posix_getcwd_bytes(PyObject *self)
2057{
2058 return posix_getcwd(1);
2059}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002060#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002061
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002062
Guido van Rossumb6775db1994-08-01 11:34:53 +00002063#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002064PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002065"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002066Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002067
Barry Warsaw53699e91996-12-10 23:23:01 +00002068static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002069posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070{
Victor Stinner8c62be82010-05-06 00:08:46 +00002071 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002072}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002073#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002074
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002076PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002077"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002078Return a list containing the names of the entries in the directory.\n\
2079\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00002080 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002081\n\
2082The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002083entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002084
Barry Warsaw53699e91996-12-10 23:23:01 +00002085static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002086posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002087{
Victor Stinner8c62be82010-05-06 00:08:46 +00002088 /* XXX Should redo this putting the (now four) versions of opendir
2089 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002090#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002091
Victor Stinner8c62be82010-05-06 00:08:46 +00002092 PyObject *d, *v;
2093 HANDLE hFindFile;
2094 BOOL result;
2095 WIN32_FIND_DATA FileData;
2096 PyObject *opath;
2097 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2098 char *bufptr = namebuf;
2099 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002100
Victor Stinner8c62be82010-05-06 00:08:46 +00002101 PyObject *po;
2102 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2103 WIN32_FIND_DATAW wFileData;
2104 Py_UNICODE *wnamebuf;
2105 /* Overallocate for \\*.*\0 */
2106 len = PyUnicode_GET_SIZE(po);
2107 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2108 if (!wnamebuf) {
2109 PyErr_NoMemory();
2110 return NULL;
2111 }
2112 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2113 if (len > 0) {
2114 Py_UNICODE wch = wnamebuf[len-1];
2115 if (wch != L'/' && wch != L'\\' && wch != L':')
2116 wnamebuf[len++] = L'\\';
2117 wcscpy(wnamebuf + len, L"*.*");
2118 }
2119 if ((d = PyList_New(0)) == NULL) {
2120 free(wnamebuf);
2121 return NULL;
2122 }
2123 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2124 if (hFindFile == INVALID_HANDLE_VALUE) {
2125 int error = GetLastError();
2126 if (error == ERROR_FILE_NOT_FOUND) {
2127 free(wnamebuf);
2128 return d;
2129 }
2130 Py_DECREF(d);
2131 win32_error_unicode("FindFirstFileW", wnamebuf);
2132 free(wnamebuf);
2133 return NULL;
2134 }
2135 do {
2136 /* Skip over . and .. */
2137 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2138 wcscmp(wFileData.cFileName, L"..") != 0) {
2139 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2140 if (v == NULL) {
2141 Py_DECREF(d);
2142 d = NULL;
2143 break;
2144 }
2145 if (PyList_Append(d, v) != 0) {
2146 Py_DECREF(v);
2147 Py_DECREF(d);
2148 d = NULL;
2149 break;
2150 }
2151 Py_DECREF(v);
2152 }
2153 Py_BEGIN_ALLOW_THREADS
2154 result = FindNextFileW(hFindFile, &wFileData);
2155 Py_END_ALLOW_THREADS
2156 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2157 it got to the end of the directory. */
2158 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2159 Py_DECREF(d);
2160 win32_error_unicode("FindNextFileW", wnamebuf);
2161 FindClose(hFindFile);
2162 free(wnamebuf);
2163 return NULL;
2164 }
2165 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002166
Victor Stinner8c62be82010-05-06 00:08:46 +00002167 if (FindClose(hFindFile) == FALSE) {
2168 Py_DECREF(d);
2169 win32_error_unicode("FindClose", wnamebuf);
2170 free(wnamebuf);
2171 return NULL;
2172 }
2173 free(wnamebuf);
2174 return d;
2175 }
2176 /* Drop the argument parsing error as narrow strings
2177 are also valid. */
2178 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002179
Victor Stinner8c62be82010-05-06 00:08:46 +00002180 if (!PyArg_ParseTuple(args, "O&:listdir",
2181 PyUnicode_FSConverter, &opath))
2182 return NULL;
2183 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2184 PyErr_SetString(PyExc_ValueError, "path too long");
2185 Py_DECREF(opath);
2186 return NULL;
2187 }
2188 strcpy(namebuf, PyBytes_AsString(opath));
2189 len = PyObject_Size(opath);
2190 if (len > 0) {
2191 char ch = namebuf[len-1];
2192 if (ch != SEP && ch != ALTSEP && ch != ':')
2193 namebuf[len++] = '/';
2194 strcpy(namebuf + len, "*.*");
2195 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002196
Victor Stinner8c62be82010-05-06 00:08:46 +00002197 if ((d = PyList_New(0)) == NULL)
2198 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002199
Victor Stinner8c62be82010-05-06 00:08:46 +00002200 hFindFile = FindFirstFile(namebuf, &FileData);
2201 if (hFindFile == INVALID_HANDLE_VALUE) {
2202 int error = GetLastError();
2203 if (error == ERROR_FILE_NOT_FOUND)
2204 return d;
2205 Py_DECREF(d);
2206 return win32_error("FindFirstFile", namebuf);
2207 }
2208 do {
2209 /* Skip over . and .. */
2210 if (strcmp(FileData.cFileName, ".") != 0 &&
2211 strcmp(FileData.cFileName, "..") != 0) {
2212 v = PyBytes_FromString(FileData.cFileName);
2213 if (v == NULL) {
2214 Py_DECREF(d);
2215 d = NULL;
2216 break;
2217 }
2218 if (PyList_Append(d, v) != 0) {
2219 Py_DECREF(v);
2220 Py_DECREF(d);
2221 d = NULL;
2222 break;
2223 }
2224 Py_DECREF(v);
2225 }
2226 Py_BEGIN_ALLOW_THREADS
2227 result = FindNextFile(hFindFile, &FileData);
2228 Py_END_ALLOW_THREADS
2229 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2230 it got to the end of the directory. */
2231 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2232 Py_DECREF(d);
2233 win32_error("FindNextFile", namebuf);
2234 FindClose(hFindFile);
2235 return NULL;
2236 }
2237 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002238
Victor Stinner8c62be82010-05-06 00:08:46 +00002239 if (FindClose(hFindFile) == FALSE) {
2240 Py_DECREF(d);
2241 return win32_error("FindClose", namebuf);
2242 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002243
Victor Stinner8c62be82010-05-06 00:08:46 +00002244 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002245
Tim Peters0bb44a42000-09-15 07:44:49 +00002246#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002247
2248#ifndef MAX_PATH
2249#define MAX_PATH CCHMAXPATH
2250#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002251 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002252 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002253 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002254 PyObject *d, *v;
2255 char namebuf[MAX_PATH+5];
2256 HDIR hdir = 1;
2257 ULONG srchcnt = 1;
2258 FILEFINDBUF3 ep;
2259 APIRET rc;
2260
Victor Stinner8c62be82010-05-06 00:08:46 +00002261 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002262 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002263 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002264 name = PyBytes_AsString(oname);
2265 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002266 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002267 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002268 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002269 return NULL;
2270 }
2271 strcpy(namebuf, name);
2272 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002273 if (*pt == ALTSEP)
2274 *pt = SEP;
2275 if (namebuf[len-1] != SEP)
2276 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002277 strcpy(namebuf + len, "*.*");
2278
Neal Norwitz6c913782007-10-14 03:23:09 +00002279 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002280 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002281 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002282 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002283
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002284 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2285 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002286 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002287 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2288 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2289 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002290
2291 if (rc != NO_ERROR) {
2292 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002293 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002294 }
2295
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002296 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002297 do {
2298 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002299 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002300 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002301
2302 strcpy(namebuf, ep.achName);
2303
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002304 /* Leave Case of Name Alone -- In Native Form */
2305 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002306
Christian Heimes72b710a2008-05-26 13:28:38 +00002307 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002308 if (v == NULL) {
2309 Py_DECREF(d);
2310 d = NULL;
2311 break;
2312 }
2313 if (PyList_Append(d, v) != 0) {
2314 Py_DECREF(v);
2315 Py_DECREF(d);
2316 d = NULL;
2317 break;
2318 }
2319 Py_DECREF(v);
2320 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2321 }
2322
Victor Stinnerdcb24032010-04-22 12:08:36 +00002323 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002324 return d;
2325#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002326 PyObject *oname;
2327 char *name;
2328 PyObject *d, *v;
2329 DIR *dirp;
2330 struct dirent *ep;
2331 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002332
Victor Stinner8c62be82010-05-06 00:08:46 +00002333 errno = 0;
2334 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2335 arg_is_unicode = 0;
2336 PyErr_Clear();
2337 }
2338 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
2339 return NULL;
2340 name = PyBytes_AsString(oname);
2341 if ((dirp = opendir(name)) == NULL) {
2342 return posix_error_with_allocated_filename(oname);
2343 }
2344 if ((d = PyList_New(0)) == NULL) {
2345 closedir(dirp);
2346 Py_DECREF(oname);
2347 return NULL;
2348 }
2349 for (;;) {
2350 errno = 0;
2351 Py_BEGIN_ALLOW_THREADS
2352 ep = readdir(dirp);
2353 Py_END_ALLOW_THREADS
2354 if (ep == NULL) {
2355 if (errno == 0) {
2356 break;
2357 } else {
2358 closedir(dirp);
2359 Py_DECREF(d);
2360 return posix_error_with_allocated_filename(oname);
2361 }
2362 }
2363 if (ep->d_name[0] == '.' &&
2364 (NAMLEN(ep) == 1 ||
2365 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2366 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002367 if (arg_is_unicode)
2368 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2369 else
2370 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002371 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002372 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002373 break;
2374 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002375 if (PyList_Append(d, v) != 0) {
2376 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002377 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002378 break;
2379 }
2380 Py_DECREF(v);
2381 }
2382 closedir(dirp);
2383 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002384
Victor Stinner8c62be82010-05-06 00:08:46 +00002385 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002386
Tim Peters0bb44a42000-09-15 07:44:49 +00002387#endif /* which OS */
2388} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002389
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002390#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002391/* A helper function for abspath on win32 */
2392static PyObject *
2393posix__getfullpathname(PyObject *self, PyObject *args)
2394{
Victor Stinner8c62be82010-05-06 00:08:46 +00002395 PyObject *opath;
2396 char *path;
2397 char outbuf[MAX_PATH*2];
2398 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002399#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002400 PyUnicodeObject *po;
2401 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2402 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2403 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2404 Py_UNICODE *wtemp;
2405 DWORD result;
2406 PyObject *v;
2407 result = GetFullPathNameW(wpath,
2408 sizeof(woutbuf)/sizeof(woutbuf[0]),
2409 woutbuf, &wtemp);
2410 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2411 woutbufp = malloc(result * sizeof(Py_UNICODE));
2412 if (!woutbufp)
2413 return PyErr_NoMemory();
2414 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2415 }
2416 if (result)
2417 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2418 else
2419 v = win32_error_unicode("GetFullPathNameW", wpath);
2420 if (woutbufp != woutbuf)
2421 free(woutbufp);
2422 return v;
2423 }
2424 /* Drop the argument parsing error as narrow strings
2425 are also valid. */
2426 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002427
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002428#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002429 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2430 PyUnicode_FSConverter, &opath))
2431 return NULL;
2432 path = PyBytes_AsString(opath);
2433 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2434 outbuf, &temp)) {
2435 win32_error("GetFullPathName", path);
2436 Py_DECREF(opath);
2437 return NULL;
2438 }
2439 Py_DECREF(opath);
2440 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2441 return PyUnicode_Decode(outbuf, strlen(outbuf),
2442 Py_FileSystemDefaultEncoding, NULL);
2443 }
2444 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002445} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002446#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002448PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002449"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002450Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002451
Barry Warsaw53699e91996-12-10 23:23:01 +00002452static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002453posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002454{
Victor Stinner8c62be82010-05-06 00:08:46 +00002455 int res;
2456 PyObject *opath;
2457 char *path;
2458 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002459
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002460#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002461 PyUnicodeObject *po;
2462 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2463 Py_BEGIN_ALLOW_THREADS
2464 /* PyUnicode_AS_UNICODE OK without thread lock as
2465 it is a simple dereference. */
2466 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2467 Py_END_ALLOW_THREADS
2468 if (!res)
2469 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2470 Py_INCREF(Py_None);
2471 return Py_None;
2472 }
2473 /* Drop the argument parsing error as narrow strings
2474 are also valid. */
2475 PyErr_Clear();
2476 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2477 PyUnicode_FSConverter, &opath, &mode))
2478 return NULL;
2479 path = PyBytes_AsString(opath);
2480 Py_BEGIN_ALLOW_THREADS
2481 /* PyUnicode_AS_UNICODE OK without thread lock as
2482 it is a simple dereference. */
2483 res = CreateDirectoryA(path, NULL);
2484 Py_END_ALLOW_THREADS
2485 if (!res) {
2486 win32_error("mkdir", path);
2487 Py_DECREF(opath);
2488 return NULL;
2489 }
2490 Py_DECREF(opath);
2491 Py_INCREF(Py_None);
2492 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002493#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002494
Victor Stinner8c62be82010-05-06 00:08:46 +00002495 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2496 PyUnicode_FSConverter, &opath, &mode))
2497 return NULL;
2498 path = PyBytes_AsString(opath);
2499 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002500#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002501 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002502#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002503 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002504#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002505 Py_END_ALLOW_THREADS
2506 if (res < 0)
2507 return posix_error_with_allocated_filename(opath);
2508 Py_DECREF(opath);
2509 Py_INCREF(Py_None);
2510 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002511#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002512}
2513
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002514
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002515/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2516#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002517#include <sys/resource.h>
2518#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002519
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002520
2521#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002522PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002523"nice(inc) -> new_priority\n\n\
2524Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002525
Barry Warsaw53699e91996-12-10 23:23:01 +00002526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002527posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002528{
Victor Stinner8c62be82010-05-06 00:08:46 +00002529 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002530
Victor Stinner8c62be82010-05-06 00:08:46 +00002531 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2532 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002533
Victor Stinner8c62be82010-05-06 00:08:46 +00002534 /* There are two flavours of 'nice': one that returns the new
2535 priority (as required by almost all standards out there) and the
2536 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2537 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002538
Victor Stinner8c62be82010-05-06 00:08:46 +00002539 If we are of the nice family that returns the new priority, we
2540 need to clear errno before the call, and check if errno is filled
2541 before calling posix_error() on a returnvalue of -1, because the
2542 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002543
Victor Stinner8c62be82010-05-06 00:08:46 +00002544 errno = 0;
2545 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002546#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002547 if (value == 0)
2548 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002549#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002550 if (value == -1 && errno != 0)
2551 /* either nice() or getpriority() returned an error */
2552 return posix_error();
2553 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002554}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002555#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002557PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002558"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002559Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002560
Barry Warsaw53699e91996-12-10 23:23:01 +00002561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002562posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002563{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002564#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002565 PyObject *o1, *o2;
2566 char *p1, *p2;
2567 BOOL result;
2568 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2569 goto error;
2570 if (!convert_to_unicode(&o1))
2571 goto error;
2572 if (!convert_to_unicode(&o2)) {
2573 Py_DECREF(o1);
2574 goto error;
2575 }
2576 Py_BEGIN_ALLOW_THREADS
2577 result = MoveFileW(PyUnicode_AsUnicode(o1),
2578 PyUnicode_AsUnicode(o2));
2579 Py_END_ALLOW_THREADS
2580 Py_DECREF(o1);
2581 Py_DECREF(o2);
2582 if (!result)
2583 return win32_error("rename", NULL);
2584 Py_INCREF(Py_None);
2585 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002586error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002587 PyErr_Clear();
2588 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2589 return NULL;
2590 Py_BEGIN_ALLOW_THREADS
2591 result = MoveFileA(p1, p2);
2592 Py_END_ALLOW_THREADS
2593 if (!result)
2594 return win32_error("rename", NULL);
2595 Py_INCREF(Py_None);
2596 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002597#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002598 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002599#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002600}
2601
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002603PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002604"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002605Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002606
Barry Warsaw53699e91996-12-10 23:23:01 +00002607static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002608posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002609{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002610#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002611 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002612#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002613 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002614#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002615}
2616
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002618PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002619"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002620Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002621
Barry Warsaw53699e91996-12-10 23:23:01 +00002622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002623posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002624{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002625#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002626 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002627#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002628 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002629#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002630}
2631
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002632
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002633#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002634PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002635"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002636Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002637
Barry Warsaw53699e91996-12-10 23:23:01 +00002638static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002639posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002640{
Victor Stinner8c62be82010-05-06 00:08:46 +00002641 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002642#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002643 wchar_t *command;
2644 if (!PyArg_ParseTuple(args, "u:system", &command))
2645 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002646
Victor Stinner8c62be82010-05-06 00:08:46 +00002647 Py_BEGIN_ALLOW_THREADS
2648 sts = _wsystem(command);
2649 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002650#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002651 PyObject *command_obj;
2652 char *command;
2653 if (!PyArg_ParseTuple(args, "O&:system",
2654 PyUnicode_FSConverter, &command_obj))
2655 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002656
Victor Stinner8c62be82010-05-06 00:08:46 +00002657 command = PyBytes_AsString(command_obj);
2658 Py_BEGIN_ALLOW_THREADS
2659 sts = system(command);
2660 Py_END_ALLOW_THREADS
2661 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002662#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002664}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002665#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002666
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002668PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002669"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002670Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002671
Barry Warsaw53699e91996-12-10 23:23:01 +00002672static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002673posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002674{
Victor Stinner8c62be82010-05-06 00:08:46 +00002675 int i;
2676 if (!PyArg_ParseTuple(args, "i:umask", &i))
2677 return NULL;
2678 i = (int)umask(i);
2679 if (i < 0)
2680 return posix_error();
2681 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002682}
2683
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002684
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002685PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002686"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002687Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002688
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002689PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002690"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002691Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002692
Barry Warsaw53699e91996-12-10 23:23:01 +00002693static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002694posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002695{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002696#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002697 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002698#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002699 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002700#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002701}
2702
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002703
Guido van Rossumb6775db1994-08-01 11:34:53 +00002704#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002705PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002706"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002707Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002708
Barry Warsaw53699e91996-12-10 23:23:01 +00002709static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002710posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002711{
Victor Stinner8c62be82010-05-06 00:08:46 +00002712 struct utsname u;
2713 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002714
Victor Stinner8c62be82010-05-06 00:08:46 +00002715 Py_BEGIN_ALLOW_THREADS
2716 res = uname(&u);
2717 Py_END_ALLOW_THREADS
2718 if (res < 0)
2719 return posix_error();
2720 return Py_BuildValue("(sssss)",
2721 u.sysname,
2722 u.nodename,
2723 u.release,
2724 u.version,
2725 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002726}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002727#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002728
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002729static int
2730extract_time(PyObject *t, long* sec, long* usec)
2731{
Victor Stinner8c62be82010-05-06 00:08:46 +00002732 long intval;
2733 if (PyFloat_Check(t)) {
2734 double tval = PyFloat_AsDouble(t);
2735 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2736 if (!intobj)
2737 return -1;
2738 intval = PyLong_AsLong(intobj);
2739 Py_DECREF(intobj);
2740 if (intval == -1 && PyErr_Occurred())
2741 return -1;
2742 *sec = intval;
2743 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2744 if (*usec < 0)
2745 /* If rounding gave us a negative number,
2746 truncate. */
2747 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002748 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 }
2750 intval = PyLong_AsLong(t);
2751 if (intval == -1 && PyErr_Occurred())
2752 return -1;
2753 *sec = intval;
2754 *usec = 0;
2755 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002756}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002757
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002758PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002759"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002760utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002761Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002762second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002763
Barry Warsaw53699e91996-12-10 23:23:01 +00002764static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002765posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002766{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002767#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002768 PyObject *arg;
2769 PyUnicodeObject *obwpath;
2770 wchar_t *wpath = NULL;
2771 PyObject *oapath;
2772 char *apath;
2773 HANDLE hFile;
2774 long atimesec, mtimesec, ausec, musec;
2775 FILETIME atime, mtime;
2776 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002777
Victor Stinner8c62be82010-05-06 00:08:46 +00002778 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2779 wpath = PyUnicode_AS_UNICODE(obwpath);
2780 Py_BEGIN_ALLOW_THREADS
2781 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2782 NULL, OPEN_EXISTING,
2783 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2784 Py_END_ALLOW_THREADS
2785 if (hFile == INVALID_HANDLE_VALUE)
2786 return win32_error_unicode("utime", wpath);
2787 } else
2788 /* Drop the argument parsing error as narrow strings
2789 are also valid. */
2790 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002791
Victor Stinner8c62be82010-05-06 00:08:46 +00002792 if (!wpath) {
2793 if (!PyArg_ParseTuple(args, "O&O:utime",
2794 PyUnicode_FSConverter, &oapath, &arg))
2795 return NULL;
2796 apath = PyBytes_AsString(oapath);
2797 Py_BEGIN_ALLOW_THREADS
2798 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2799 NULL, OPEN_EXISTING,
2800 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2801 Py_END_ALLOW_THREADS
2802 if (hFile == INVALID_HANDLE_VALUE) {
2803 win32_error("utime", apath);
2804 Py_DECREF(oapath);
2805 return NULL;
2806 }
2807 Py_DECREF(oapath);
2808 }
2809
2810 if (arg == Py_None) {
2811 SYSTEMTIME now;
2812 GetSystemTime(&now);
2813 if (!SystemTimeToFileTime(&now, &mtime) ||
2814 !SystemTimeToFileTime(&now, &atime)) {
2815 win32_error("utime", NULL);
2816 goto done;
2817 }
2818 }
2819 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2820 PyErr_SetString(PyExc_TypeError,
2821 "utime() arg 2 must be a tuple (atime, mtime)");
2822 goto done;
2823 }
2824 else {
2825 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2826 &atimesec, &ausec) == -1)
2827 goto done;
2828 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2829 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2830 &mtimesec, &musec) == -1)
2831 goto done;
2832 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2833 }
2834 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2835 /* Avoid putting the file name into the error here,
2836 as that may confuse the user into believing that
2837 something is wrong with the file, when it also
2838 could be the time stamp that gives a problem. */
2839 win32_error("utime", NULL);
2840 }
2841 Py_INCREF(Py_None);
2842 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002843done:
Victor Stinner8c62be82010-05-06 00:08:46 +00002844 CloseHandle(hFile);
2845 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002846#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002847
Victor Stinner8c62be82010-05-06 00:08:46 +00002848 PyObject *opath;
2849 char *path;
2850 long atime, mtime, ausec, musec;
2851 int res;
2852 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002853
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002854#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00002855 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002856#define ATIME buf[0].tv_sec
2857#define MTIME buf[1].tv_sec
2858#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002859/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00002860 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002861#define ATIME buf.actime
2862#define MTIME buf.modtime
2863#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002864#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00002865 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002866#define ATIME buf[0]
2867#define MTIME buf[1]
2868#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002869#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002870
Mark Hammond817c9292003-12-03 01:22:38 +00002871
Victor Stinner8c62be82010-05-06 00:08:46 +00002872 if (!PyArg_ParseTuple(args, "O&O:utime",
2873 PyUnicode_FSConverter, &opath, &arg))
2874 return NULL;
2875 path = PyBytes_AsString(opath);
2876 if (arg == Py_None) {
2877 /* optional time values not given */
2878 Py_BEGIN_ALLOW_THREADS
2879 res = utime(path, NULL);
2880 Py_END_ALLOW_THREADS
2881 }
2882 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2883 PyErr_SetString(PyExc_TypeError,
2884 "utime() arg 2 must be a tuple (atime, mtime)");
2885 Py_DECREF(opath);
2886 return NULL;
2887 }
2888 else {
2889 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2890 &atime, &ausec) == -1) {
2891 Py_DECREF(opath);
2892 return NULL;
2893 }
2894 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2895 &mtime, &musec) == -1) {
2896 Py_DECREF(opath);
2897 return NULL;
2898 }
2899 ATIME = atime;
2900 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002901#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00002902 buf[0].tv_usec = ausec;
2903 buf[1].tv_usec = musec;
2904 Py_BEGIN_ALLOW_THREADS
2905 res = utimes(path, buf);
2906 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002907#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002908 Py_BEGIN_ALLOW_THREADS
2909 res = utime(path, UTIME_ARG);
2910 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002911#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00002912 }
2913 if (res < 0) {
2914 return posix_error_with_allocated_filename(opath);
2915 }
2916 Py_DECREF(opath);
2917 Py_INCREF(Py_None);
2918 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002919#undef UTIME_ARG
2920#undef ATIME
2921#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002922#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002923}
2924
Guido van Rossum85e3b011991-06-03 12:42:10 +00002925
Guido van Rossum3b066191991-06-04 19:40:25 +00002926/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002928PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002929"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002930Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002931
Barry Warsaw53699e91996-12-10 23:23:01 +00002932static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002933posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002934{
Victor Stinner8c62be82010-05-06 00:08:46 +00002935 int sts;
2936 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2937 return NULL;
2938 _exit(sts);
2939 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002940}
2941
Martin v. Löwis114619e2002-10-07 06:44:21 +00002942#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2943static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002944free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002945{
Victor Stinner8c62be82010-05-06 00:08:46 +00002946 Py_ssize_t i;
2947 for (i = 0; i < count; i++)
2948 PyMem_Free(array[i]);
2949 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002950}
Martin v. Löwis011e8422009-05-05 04:43:17 +00002951
Antoine Pitrou69f71142009-05-24 21:25:49 +00002952static
Martin v. Löwis011e8422009-05-05 04:43:17 +00002953int fsconvert_strdup(PyObject *o, char**out)
2954{
Victor Stinner8c62be82010-05-06 00:08:46 +00002955 PyObject *bytes;
2956 Py_ssize_t size;
2957 if (!PyUnicode_FSConverter(o, &bytes))
2958 return 0;
2959 size = PyBytes_GET_SIZE(bytes);
2960 *out = PyMem_Malloc(size+1);
2961 if (!*out)
2962 return 0;
2963 memcpy(*out, PyBytes_AsString(bytes), size+1);
2964 Py_DECREF(bytes);
2965 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002966}
Martin v. Löwis114619e2002-10-07 06:44:21 +00002967#endif
2968
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002969
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002970#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002971PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002972"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002973Execute an executable path with arguments, replacing current process.\n\
2974\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00002975 path: path of executable file\n\
2976 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002977
Barry Warsaw53699e91996-12-10 23:23:01 +00002978static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002979posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002980{
Victor Stinner8c62be82010-05-06 00:08:46 +00002981 PyObject *opath;
2982 char *path;
2983 PyObject *argv;
2984 char **argvlist;
2985 Py_ssize_t i, argc;
2986 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002987
Victor Stinner8c62be82010-05-06 00:08:46 +00002988 /* execv has two arguments: (path, argv), where
2989 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002990
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 if (!PyArg_ParseTuple(args, "O&O:execv",
2992 PyUnicode_FSConverter,
2993 &opath, &argv))
2994 return NULL;
2995 path = PyBytes_AsString(opath);
2996 if (PyList_Check(argv)) {
2997 argc = PyList_Size(argv);
2998 getitem = PyList_GetItem;
2999 }
3000 else if (PyTuple_Check(argv)) {
3001 argc = PyTuple_Size(argv);
3002 getitem = PyTuple_GetItem;
3003 }
3004 else {
3005 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3006 Py_DECREF(opath);
3007 return NULL;
3008 }
3009 if (argc < 1) {
3010 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3011 Py_DECREF(opath);
3012 return NULL;
3013 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003014
Victor Stinner8c62be82010-05-06 00:08:46 +00003015 argvlist = PyMem_NEW(char *, argc+1);
3016 if (argvlist == NULL) {
3017 Py_DECREF(opath);
3018 return PyErr_NoMemory();
3019 }
3020 for (i = 0; i < argc; i++) {
3021 if (!fsconvert_strdup((*getitem)(argv, i),
3022 &argvlist[i])) {
3023 free_string_array(argvlist, i);
3024 PyErr_SetString(PyExc_TypeError,
3025 "execv() arg 2 must contain only strings");
3026 Py_DECREF(opath);
3027 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003028
Victor Stinner8c62be82010-05-06 00:08:46 +00003029 }
3030 }
3031 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003032
Victor Stinner8c62be82010-05-06 00:08:46 +00003033 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003034
Victor Stinner8c62be82010-05-06 00:08:46 +00003035 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003036
Victor Stinner8c62be82010-05-06 00:08:46 +00003037 free_string_array(argvlist, argc);
3038 Py_DECREF(opath);
3039 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003040}
3041
Victor Stinner13bb71c2010-04-23 21:41:56 +00003042static char**
3043parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3044{
Victor Stinner8c62be82010-05-06 00:08:46 +00003045 char **envlist;
3046 Py_ssize_t i, pos, envc;
3047 PyObject *keys=NULL, *vals=NULL;
3048 PyObject *key, *val, *key2, *val2;
3049 char *p, *k, *v;
3050 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003051
Victor Stinner8c62be82010-05-06 00:08:46 +00003052 i = PyMapping_Size(env);
3053 if (i < 0)
3054 return NULL;
3055 envlist = PyMem_NEW(char *, i + 1);
3056 if (envlist == NULL) {
3057 PyErr_NoMemory();
3058 return NULL;
3059 }
3060 envc = 0;
3061 keys = PyMapping_Keys(env);
3062 vals = PyMapping_Values(env);
3063 if (!keys || !vals)
3064 goto error;
3065 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3066 PyErr_Format(PyExc_TypeError,
3067 "env.keys() or env.values() is not a list");
3068 goto error;
3069 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003070
Victor Stinner8c62be82010-05-06 00:08:46 +00003071 for (pos = 0; pos < i; pos++) {
3072 key = PyList_GetItem(keys, pos);
3073 val = PyList_GetItem(vals, pos);
3074 if (!key || !val)
3075 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003076
Victor Stinner8c62be82010-05-06 00:08:46 +00003077 if (PyUnicode_FSConverter(key, &key2) == 0)
3078 goto error;
3079 if (PyUnicode_FSConverter(val, &val2) == 0) {
3080 Py_DECREF(key2);
3081 goto error;
3082 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003083
3084#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003085 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3086 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003087#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003088 k = PyBytes_AsString(key2);
3089 v = PyBytes_AsString(val2);
3090 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003091
Victor Stinner8c62be82010-05-06 00:08:46 +00003092 p = PyMem_NEW(char, len);
3093 if (p == NULL) {
3094 PyErr_NoMemory();
3095 Py_DECREF(key2);
3096 Py_DECREF(val2);
3097 goto error;
3098 }
3099 PyOS_snprintf(p, len, "%s=%s", k, v);
3100 envlist[envc++] = p;
3101 Py_DECREF(key2);
3102 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003103#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003104 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003105#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003106 }
3107 Py_DECREF(vals);
3108 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003109
Victor Stinner8c62be82010-05-06 00:08:46 +00003110 envlist[envc] = 0;
3111 *envc_ptr = envc;
3112 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003113
3114error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003115 Py_XDECREF(keys);
3116 Py_XDECREF(vals);
3117 while (--envc >= 0)
3118 PyMem_DEL(envlist[envc]);
3119 PyMem_DEL(envlist);
3120 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003121}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003123PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003124"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003125Execute a path with arguments and environment, replacing current process.\n\
3126\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003127 path: path of executable file\n\
3128 args: tuple or list of arguments\n\
3129 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003130
Barry Warsaw53699e91996-12-10 23:23:01 +00003131static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003132posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003133{
Victor Stinner8c62be82010-05-06 00:08:46 +00003134 PyObject *opath;
3135 char *path;
3136 PyObject *argv, *env;
3137 char **argvlist;
3138 char **envlist;
3139 Py_ssize_t i, argc, envc;
3140 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3141 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003142
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 /* execve has three arguments: (path, argv, env), where
3144 argv is a list or tuple of strings and env is a dictionary
3145 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003146
Victor Stinner8c62be82010-05-06 00:08:46 +00003147 if (!PyArg_ParseTuple(args, "O&OO:execve",
3148 PyUnicode_FSConverter,
3149 &opath, &argv, &env))
3150 return NULL;
3151 path = PyBytes_AsString(opath);
3152 if (PyList_Check(argv)) {
3153 argc = PyList_Size(argv);
3154 getitem = PyList_GetItem;
3155 }
3156 else if (PyTuple_Check(argv)) {
3157 argc = PyTuple_Size(argv);
3158 getitem = PyTuple_GetItem;
3159 }
3160 else {
3161 PyErr_SetString(PyExc_TypeError,
3162 "execve() arg 2 must be a tuple or list");
3163 goto fail_0;
3164 }
3165 if (!PyMapping_Check(env)) {
3166 PyErr_SetString(PyExc_TypeError,
3167 "execve() arg 3 must be a mapping object");
3168 goto fail_0;
3169 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003170
Victor Stinner8c62be82010-05-06 00:08:46 +00003171 argvlist = PyMem_NEW(char *, argc+1);
3172 if (argvlist == NULL) {
3173 PyErr_NoMemory();
3174 goto fail_0;
3175 }
3176 for (i = 0; i < argc; i++) {
3177 if (!fsconvert_strdup((*getitem)(argv, i),
3178 &argvlist[i]))
3179 {
3180 lastarg = i;
3181 goto fail_1;
3182 }
3183 }
3184 lastarg = argc;
3185 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003186
Victor Stinner8c62be82010-05-06 00:08:46 +00003187 envlist = parse_envlist(env, &envc);
3188 if (envlist == NULL)
3189 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003190
Victor Stinner8c62be82010-05-06 00:08:46 +00003191 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003192
Victor Stinner8c62be82010-05-06 00:08:46 +00003193 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003194
Victor Stinner8c62be82010-05-06 00:08:46 +00003195 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003196
Victor Stinner8c62be82010-05-06 00:08:46 +00003197 while (--envc >= 0)
3198 PyMem_DEL(envlist[envc]);
3199 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003200 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003201 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003202 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003203 Py_DECREF(opath);
3204 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003205}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003206#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003208
Guido van Rossuma1065681999-01-25 23:20:23 +00003209#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003211"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003212Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003213\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003214 mode: mode of process creation\n\
3215 path: path of executable file\n\
3216 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003217
3218static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003219posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003220{
Victor Stinner8c62be82010-05-06 00:08:46 +00003221 PyObject *opath;
3222 char *path;
3223 PyObject *argv;
3224 char **argvlist;
3225 int mode, i;
3226 Py_ssize_t argc;
3227 Py_intptr_t spawnval;
3228 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003229
Victor Stinner8c62be82010-05-06 00:08:46 +00003230 /* spawnv has three arguments: (mode, path, argv), where
3231 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003232
Victor Stinner8c62be82010-05-06 00:08:46 +00003233 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3234 PyUnicode_FSConverter,
3235 &opath, &argv))
3236 return NULL;
3237 path = PyBytes_AsString(opath);
3238 if (PyList_Check(argv)) {
3239 argc = PyList_Size(argv);
3240 getitem = PyList_GetItem;
3241 }
3242 else if (PyTuple_Check(argv)) {
3243 argc = PyTuple_Size(argv);
3244 getitem = PyTuple_GetItem;
3245 }
3246 else {
3247 PyErr_SetString(PyExc_TypeError,
3248 "spawnv() arg 2 must be a tuple or list");
3249 Py_DECREF(opath);
3250 return NULL;
3251 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003252
Victor Stinner8c62be82010-05-06 00:08:46 +00003253 argvlist = PyMem_NEW(char *, argc+1);
3254 if (argvlist == NULL) {
3255 Py_DECREF(opath);
3256 return PyErr_NoMemory();
3257 }
3258 for (i = 0; i < argc; i++) {
3259 if (!fsconvert_strdup((*getitem)(argv, i),
3260 &argvlist[i])) {
3261 free_string_array(argvlist, i);
3262 PyErr_SetString(
3263 PyExc_TypeError,
3264 "spawnv() arg 2 must contain only strings");
3265 Py_DECREF(opath);
3266 return NULL;
3267 }
3268 }
3269 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003270
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003271#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003272 Py_BEGIN_ALLOW_THREADS
3273 spawnval = spawnv(mode, path, argvlist);
3274 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003275#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003276 if (mode == _OLD_P_OVERLAY)
3277 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003278
Victor Stinner8c62be82010-05-06 00:08:46 +00003279 Py_BEGIN_ALLOW_THREADS
3280 spawnval = _spawnv(mode, path, argvlist);
3281 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003282#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003283
Victor Stinner8c62be82010-05-06 00:08:46 +00003284 free_string_array(argvlist, argc);
3285 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003286
Victor Stinner8c62be82010-05-06 00:08:46 +00003287 if (spawnval == -1)
3288 return posix_error();
3289 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003290#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003291 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003292#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003293 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003294#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003295}
3296
3297
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003298PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003299"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003300Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003301\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003302 mode: mode of process creation\n\
3303 path: path of executable file\n\
3304 args: tuple or list of arguments\n\
3305 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003306
3307static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003308posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003309{
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 PyObject *opath;
3311 char *path;
3312 PyObject *argv, *env;
3313 char **argvlist;
3314 char **envlist;
3315 PyObject *res = NULL;
3316 int mode, envc;
3317 Py_ssize_t argc, i;
3318 Py_intptr_t spawnval;
3319 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3320 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003321
Victor Stinner8c62be82010-05-06 00:08:46 +00003322 /* spawnve has four arguments: (mode, path, argv, env), where
3323 argv is a list or tuple of strings and env is a dictionary
3324 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003325
Victor Stinner8c62be82010-05-06 00:08:46 +00003326 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3327 PyUnicode_FSConverter,
3328 &opath, &argv, &env))
3329 return NULL;
3330 path = PyBytes_AsString(opath);
3331 if (PyList_Check(argv)) {
3332 argc = PyList_Size(argv);
3333 getitem = PyList_GetItem;
3334 }
3335 else if (PyTuple_Check(argv)) {
3336 argc = PyTuple_Size(argv);
3337 getitem = PyTuple_GetItem;
3338 }
3339 else {
3340 PyErr_SetString(PyExc_TypeError,
3341 "spawnve() arg 2 must be a tuple or list");
3342 goto fail_0;
3343 }
3344 if (!PyMapping_Check(env)) {
3345 PyErr_SetString(PyExc_TypeError,
3346 "spawnve() arg 3 must be a mapping object");
3347 goto fail_0;
3348 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003349
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 argvlist = PyMem_NEW(char *, argc+1);
3351 if (argvlist == NULL) {
3352 PyErr_NoMemory();
3353 goto fail_0;
3354 }
3355 for (i = 0; i < argc; i++) {
3356 if (!fsconvert_strdup((*getitem)(argv, i),
3357 &argvlist[i]))
3358 {
3359 lastarg = i;
3360 goto fail_1;
3361 }
3362 }
3363 lastarg = argc;
3364 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003365
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 envlist = parse_envlist(env, &envc);
3367 if (envlist == NULL)
3368 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003369
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003370#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003371 Py_BEGIN_ALLOW_THREADS
3372 spawnval = spawnve(mode, path, argvlist, envlist);
3373 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003374#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003375 if (mode == _OLD_P_OVERLAY)
3376 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003377
Victor Stinner8c62be82010-05-06 00:08:46 +00003378 Py_BEGIN_ALLOW_THREADS
3379 spawnval = _spawnve(mode, path, argvlist, envlist);
3380 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003381#endif
Tim Peters25059d32001-12-07 20:35:43 +00003382
Victor Stinner8c62be82010-05-06 00:08:46 +00003383 if (spawnval == -1)
3384 (void) posix_error();
3385 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003386#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003387 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003388#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003389 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003390#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003391
Victor Stinner8c62be82010-05-06 00:08:46 +00003392 while (--envc >= 0)
3393 PyMem_DEL(envlist[envc]);
3394 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003395 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003396 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003397 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003398 Py_DECREF(opath);
3399 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003400}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003401
3402/* OS/2 supports spawnvp & spawnvpe natively */
3403#if defined(PYOS_OS2)
3404PyDoc_STRVAR(posix_spawnvp__doc__,
3405"spawnvp(mode, file, args)\n\n\
3406Execute the program 'file' in a new process, using the environment\n\
3407search path to find the file.\n\
3408\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003409 mode: mode of process creation\n\
3410 file: executable file name\n\
3411 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003412
3413static PyObject *
3414posix_spawnvp(PyObject *self, PyObject *args)
3415{
Victor Stinner8c62be82010-05-06 00:08:46 +00003416 PyObject *opath;
3417 char *path;
3418 PyObject *argv;
3419 char **argvlist;
3420 int mode, i, argc;
3421 Py_intptr_t spawnval;
3422 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003423
Victor Stinner8c62be82010-05-06 00:08:46 +00003424 /* spawnvp has three arguments: (mode, path, argv), where
3425 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003426
Victor Stinner8c62be82010-05-06 00:08:46 +00003427 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3428 PyUnicode_FSConverter,
3429 &opath, &argv))
3430 return NULL;
3431 path = PyBytes_AsString(opath);
3432 if (PyList_Check(argv)) {
3433 argc = PyList_Size(argv);
3434 getitem = PyList_GetItem;
3435 }
3436 else if (PyTuple_Check(argv)) {
3437 argc = PyTuple_Size(argv);
3438 getitem = PyTuple_GetItem;
3439 }
3440 else {
3441 PyErr_SetString(PyExc_TypeError,
3442 "spawnvp() arg 2 must be a tuple or list");
3443 Py_DECREF(opath);
3444 return NULL;
3445 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003446
Victor Stinner8c62be82010-05-06 00:08:46 +00003447 argvlist = PyMem_NEW(char *, argc+1);
3448 if (argvlist == NULL) {
3449 Py_DECREF(opath);
3450 return PyErr_NoMemory();
3451 }
3452 for (i = 0; i < argc; i++) {
3453 if (!fsconvert_strdup((*getitem)(argv, i),
3454 &argvlist[i])) {
3455 free_string_array(argvlist, i);
3456 PyErr_SetString(
3457 PyExc_TypeError,
3458 "spawnvp() arg 2 must contain only strings");
3459 Py_DECREF(opath);
3460 return NULL;
3461 }
3462 }
3463 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003464
Victor Stinner8c62be82010-05-06 00:08:46 +00003465 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003466#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003467 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003468#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003469 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003470#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003471 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003472
Victor Stinner8c62be82010-05-06 00:08:46 +00003473 free_string_array(argvlist, argc);
3474 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003475
Victor Stinner8c62be82010-05-06 00:08:46 +00003476 if (spawnval == -1)
3477 return posix_error();
3478 else
3479 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003480}
3481
3482
3483PyDoc_STRVAR(posix_spawnvpe__doc__,
3484"spawnvpe(mode, file, args, env)\n\n\
3485Execute the program 'file' in a new process, using the environment\n\
3486search path to find the file.\n\
3487\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 mode: mode of process creation\n\
3489 file: executable file name\n\
3490 args: tuple or list of arguments\n\
3491 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003492
3493static PyObject *
3494posix_spawnvpe(PyObject *self, PyObject *args)
3495{
Victor Stinner8c62be82010-05-06 00:08:46 +00003496 PyObject *opath
3497 char *path;
3498 PyObject *argv, *env;
3499 char **argvlist;
3500 char **envlist;
3501 PyObject *res=NULL;
3502 int mode, i, argc, envc;
3503 Py_intptr_t spawnval;
3504 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3505 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003506
Victor Stinner8c62be82010-05-06 00:08:46 +00003507 /* spawnvpe has four arguments: (mode, path, argv, env), where
3508 argv is a list or tuple of strings and env is a dictionary
3509 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003510
Victor Stinner8c62be82010-05-06 00:08:46 +00003511 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3512 PyUnicode_FSConverter,
3513 &opath, &argv, &env))
3514 return NULL;
3515 path = PyBytes_AsString(opath);
3516 if (PyList_Check(argv)) {
3517 argc = PyList_Size(argv);
3518 getitem = PyList_GetItem;
3519 }
3520 else if (PyTuple_Check(argv)) {
3521 argc = PyTuple_Size(argv);
3522 getitem = PyTuple_GetItem;
3523 }
3524 else {
3525 PyErr_SetString(PyExc_TypeError,
3526 "spawnvpe() arg 2 must be a tuple or list");
3527 goto fail_0;
3528 }
3529 if (!PyMapping_Check(env)) {
3530 PyErr_SetString(PyExc_TypeError,
3531 "spawnvpe() arg 3 must be a mapping object");
3532 goto fail_0;
3533 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003534
Victor Stinner8c62be82010-05-06 00:08:46 +00003535 argvlist = PyMem_NEW(char *, argc+1);
3536 if (argvlist == NULL) {
3537 PyErr_NoMemory();
3538 goto fail_0;
3539 }
3540 for (i = 0; i < argc; i++) {
3541 if (!fsconvert_strdup((*getitem)(argv, i),
3542 &argvlist[i]))
3543 {
3544 lastarg = i;
3545 goto fail_1;
3546 }
3547 }
3548 lastarg = argc;
3549 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003550
Victor Stinner8c62be82010-05-06 00:08:46 +00003551 envlist = parse_envlist(env, &envc);
3552 if (envlist == NULL)
3553 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003554
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003556#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003558#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003559 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003560#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003561 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003562
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 if (spawnval == -1)
3564 (void) posix_error();
3565 else
3566 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003567
Victor Stinner8c62be82010-05-06 00:08:46 +00003568 while (--envc >= 0)
3569 PyMem_DEL(envlist[envc]);
3570 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003571 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003572 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003573 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003574 Py_DECREF(opath);
3575 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003576}
3577#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003578#endif /* HAVE_SPAWNV */
3579
3580
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003581#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003582PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003583"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003584Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3585\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003586Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003587
3588static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003589posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003590{
Victor Stinner8c62be82010-05-06 00:08:46 +00003591 pid_t pid;
3592 int result = 0;
3593 _PyImport_AcquireLock();
3594 pid = fork1();
3595 if (pid == 0) {
3596 /* child: this clobbers and resets the import lock. */
3597 PyOS_AfterFork();
3598 } else {
3599 /* parent: release the import lock. */
3600 result = _PyImport_ReleaseLock();
3601 }
3602 if (pid == -1)
3603 return posix_error();
3604 if (result < 0) {
3605 /* Don't clobber the OSError if the fork failed. */
3606 PyErr_SetString(PyExc_RuntimeError,
3607 "not holding the import lock");
3608 return NULL;
3609 }
3610 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003611}
3612#endif
3613
3614
Guido van Rossumad0ee831995-03-01 10:34:45 +00003615#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003616PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003617"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003618Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003619Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003620
Barry Warsaw53699e91996-12-10 23:23:01 +00003621static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003622posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003623{
Victor Stinner8c62be82010-05-06 00:08:46 +00003624 pid_t pid;
3625 int result = 0;
3626 _PyImport_AcquireLock();
3627 pid = fork();
3628 if (pid == 0) {
3629 /* child: this clobbers and resets the import lock. */
3630 PyOS_AfterFork();
3631 } else {
3632 /* parent: release the import lock. */
3633 result = _PyImport_ReleaseLock();
3634 }
3635 if (pid == -1)
3636 return posix_error();
3637 if (result < 0) {
3638 /* Don't clobber the OSError if the fork failed. */
3639 PyErr_SetString(PyExc_RuntimeError,
3640 "not holding the import lock");
3641 return NULL;
3642 }
3643 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003644}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003645#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003646
Neal Norwitzb59798b2003-03-21 01:43:31 +00003647/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003648/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3649#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003650#define DEV_PTY_FILE "/dev/ptc"
3651#define HAVE_DEV_PTMX
3652#else
3653#define DEV_PTY_FILE "/dev/ptmx"
3654#endif
3655
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003656#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003657#ifdef HAVE_PTY_H
3658#include <pty.h>
3659#else
3660#ifdef HAVE_LIBUTIL_H
3661#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003662#else
3663#ifdef HAVE_UTIL_H
3664#include <util.h>
3665#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003666#endif /* HAVE_LIBUTIL_H */
3667#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003668#ifdef HAVE_STROPTS_H
3669#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003670#endif
3671#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003672
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003673#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003674PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003675"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003676Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003677
3678static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003679posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003680{
Victor Stinner8c62be82010-05-06 00:08:46 +00003681 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003682#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00003683 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003684#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003685#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003686 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003687#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00003688 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003689#endif
3690#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003691
Thomas Wouters70c21a12000-07-14 14:28:33 +00003692#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00003693 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3694 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003695#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003696 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3697 if (slave_name == NULL)
3698 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003699
Victor Stinner8c62be82010-05-06 00:08:46 +00003700 slave_fd = open(slave_name, O_RDWR);
3701 if (slave_fd < 0)
3702 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003703#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3705 if (master_fd < 0)
3706 return posix_error();
3707 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3708 /* change permission of slave */
3709 if (grantpt(master_fd) < 0) {
3710 PyOS_setsig(SIGCHLD, sig_saved);
3711 return posix_error();
3712 }
3713 /* unlock slave */
3714 if (unlockpt(master_fd) < 0) {
3715 PyOS_setsig(SIGCHLD, sig_saved);
3716 return posix_error();
3717 }
3718 PyOS_setsig(SIGCHLD, sig_saved);
3719 slave_name = ptsname(master_fd); /* get name of slave */
3720 if (slave_name == NULL)
3721 return posix_error();
3722 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3723 if (slave_fd < 0)
3724 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003725#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003726 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3727 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003728#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00003729 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003730#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003731#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003732#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003733
Victor Stinner8c62be82010-05-06 00:08:46 +00003734 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003735
Fred Drake8cef4cf2000-06-28 16:40:38 +00003736}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003737#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003738
3739#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003740PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003741"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003742Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3743Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003744To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003745
3746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003747posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003748{
Victor Stinner8c62be82010-05-06 00:08:46 +00003749 int master_fd = -1, result = 0;
3750 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003751
Victor Stinner8c62be82010-05-06 00:08:46 +00003752 _PyImport_AcquireLock();
3753 pid = forkpty(&master_fd, NULL, NULL, NULL);
3754 if (pid == 0) {
3755 /* child: this clobbers and resets the import lock. */
3756 PyOS_AfterFork();
3757 } else {
3758 /* parent: release the import lock. */
3759 result = _PyImport_ReleaseLock();
3760 }
3761 if (pid == -1)
3762 return posix_error();
3763 if (result < 0) {
3764 /* Don't clobber the OSError if the fork failed. */
3765 PyErr_SetString(PyExc_RuntimeError,
3766 "not holding the import lock");
3767 return NULL;
3768 }
3769 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003770}
3771#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003772
Guido van Rossumad0ee831995-03-01 10:34:45 +00003773#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003774PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003775"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003776Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003777
Barry Warsaw53699e91996-12-10 23:23:01 +00003778static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003779posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003780{
Victor Stinner8c62be82010-05-06 00:08:46 +00003781 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003782}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003783#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003785
Guido van Rossumad0ee831995-03-01 10:34:45 +00003786#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003790
Barry Warsaw53699e91996-12-10 23:23:01 +00003791static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003792posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003793{
Victor Stinner8c62be82010-05-06 00:08:46 +00003794 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003795}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003796#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003797
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003798
Guido van Rossumad0ee831995-03-01 10:34:45 +00003799#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003800PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003801"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003802Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003803
Barry Warsaw53699e91996-12-10 23:23:01 +00003804static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003805posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003806{
Victor Stinner8c62be82010-05-06 00:08:46 +00003807 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003808}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003809#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003810
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003812PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003813"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003814Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003815
Barry Warsaw53699e91996-12-10 23:23:01 +00003816static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003817posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003818{
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003820}
3821
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003822
Fred Drakec9680921999-12-13 16:37:25 +00003823#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003824PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003825"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003827
3828static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003829posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003830{
3831 PyObject *result = NULL;
3832
Fred Drakec9680921999-12-13 16:37:25 +00003833#ifdef NGROUPS_MAX
3834#define MAX_GROUPS NGROUPS_MAX
3835#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003836 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00003837#define MAX_GROUPS 64
3838#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 gid_t grouplist[MAX_GROUPS];
3840 int n;
Fred Drakec9680921999-12-13 16:37:25 +00003841
Victor Stinner8c62be82010-05-06 00:08:46 +00003842 n = getgroups(MAX_GROUPS, grouplist);
3843 if (n < 0)
3844 posix_error();
3845 else {
3846 result = PyList_New(n);
3847 if (result != NULL) {
3848 int i;
3849 for (i = 0; i < n; ++i) {
3850 PyObject *o = PyLong_FromLong((long)grouplist[i]);
3851 if (o == NULL) {
3852 Py_DECREF(result);
3853 result = NULL;
3854 break;
Fred Drakec9680921999-12-13 16:37:25 +00003855 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003856 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00003857 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003858 }
3859 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003860
Fred Drakec9680921999-12-13 16:37:25 +00003861 return result;
3862}
3863#endif
3864
Antoine Pitroub7572f02009-12-02 20:46:48 +00003865#ifdef HAVE_INITGROUPS
3866PyDoc_STRVAR(posix_initgroups__doc__,
3867"initgroups(username, gid) -> None\n\n\
3868Call the system initgroups() to initialize the group access list with all of\n\
3869the groups of which the specified username is a member, plus the specified\n\
3870group id.");
3871
3872static PyObject *
3873posix_initgroups(PyObject *self, PyObject *args)
3874{
Victor Stinner8c62be82010-05-06 00:08:46 +00003875 char *username;
3876 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003877
Victor Stinner8c62be82010-05-06 00:08:46 +00003878 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
3879 return NULL;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003880
Victor Stinner8c62be82010-05-06 00:08:46 +00003881 if (initgroups(username, (gid_t) gid) == -1)
3882 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00003883
Victor Stinner8c62be82010-05-06 00:08:46 +00003884 Py_INCREF(Py_None);
3885 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003886}
3887#endif
3888
Martin v. Löwis606edc12002-06-13 21:09:11 +00003889#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003890PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003891"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003892Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003893
3894static PyObject *
3895posix_getpgid(PyObject *self, PyObject *args)
3896{
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 pid_t pid, pgid;
3898 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
3899 return NULL;
3900 pgid = getpgid(pid);
3901 if (pgid < 0)
3902 return posix_error();
3903 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003904}
3905#endif /* HAVE_GETPGID */
3906
3907
Guido van Rossumb6775db1994-08-01 11:34:53 +00003908#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003909PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003910"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003911Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003912
Barry Warsaw53699e91996-12-10 23:23:01 +00003913static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003914posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003915{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003916#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00003917 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003918#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003919 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003920#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003921}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003922#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003923
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003924
Guido van Rossumb6775db1994-08-01 11:34:53 +00003925#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003926PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003927"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00003928Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003929
Barry Warsaw53699e91996-12-10 23:23:01 +00003930static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003931posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003932{
Guido van Rossum64933891994-10-20 21:56:42 +00003933#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00003934 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003935#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003936 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003937#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003938 return posix_error();
3939 Py_INCREF(Py_None);
3940 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003941}
3942
Guido van Rossumb6775db1994-08-01 11:34:53 +00003943#endif /* HAVE_SETPGRP */
3944
Guido van Rossumad0ee831995-03-01 10:34:45 +00003945#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003946PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003947"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003948Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003949
Barry Warsaw53699e91996-12-10 23:23:01 +00003950static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003951posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003952{
Victor Stinner8c62be82010-05-06 00:08:46 +00003953 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003954}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003955#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003956
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003957
Fred Drake12c6e2d1999-12-14 21:25:03 +00003958#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003959PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003960"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003961Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003962
3963static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003964posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003965{
Victor Stinner8c62be82010-05-06 00:08:46 +00003966 PyObject *result = NULL;
3967 char *name;
3968 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003969
Victor Stinner8c62be82010-05-06 00:08:46 +00003970 errno = 0;
3971 name = getlogin();
3972 if (name == NULL) {
3973 if (errno)
3974 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00003975 else
Victor Stinner8c62be82010-05-06 00:08:46 +00003976 PyErr_SetString(PyExc_OSError,
3977 "unable to determine login name");
3978 }
3979 else
3980 result = PyUnicode_FromString(name);
3981 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003982
Fred Drake12c6e2d1999-12-14 21:25:03 +00003983 return result;
3984}
3985#endif
3986
Guido van Rossumad0ee831995-03-01 10:34:45 +00003987#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003988PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003989"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003990Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003991
Barry Warsaw53699e91996-12-10 23:23:01 +00003992static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003993posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003994{
Victor Stinner8c62be82010-05-06 00:08:46 +00003995 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003996}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003997#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003998
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003999
Guido van Rossumad0ee831995-03-01 10:34:45 +00004000#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004001PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004002"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004003Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004004
Barry Warsaw53699e91996-12-10 23:23:01 +00004005static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004006posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004007{
Victor Stinner8c62be82010-05-06 00:08:46 +00004008 pid_t pid;
4009 int sig;
4010 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4011 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004012#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004013 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4014 APIRET rc;
4015 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004016 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004017
4018 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4019 APIRET rc;
4020 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004021 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004022
4023 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004024 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004025#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004026 if (kill(pid, sig) == -1)
4027 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004028#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004029 Py_INCREF(Py_None);
4030 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004031}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004032#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004033
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004034#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004035PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004036"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004037Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004038
4039static PyObject *
4040posix_killpg(PyObject *self, PyObject *args)
4041{
Victor Stinner8c62be82010-05-06 00:08:46 +00004042 int sig;
4043 pid_t pgid;
4044 /* XXX some man pages make the `pgid` parameter an int, others
4045 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4046 take the same type. Moreover, pid_t is always at least as wide as
4047 int (else compilation of this module fails), which is safe. */
4048 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4049 return NULL;
4050 if (killpg(pgid, sig) == -1)
4051 return posix_error();
4052 Py_INCREF(Py_None);
4053 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004054}
4055#endif
4056
Brian Curtineb24d742010-04-12 17:16:38 +00004057#ifdef MS_WINDOWS
4058PyDoc_STRVAR(win32_kill__doc__,
4059"kill(pid, sig)\n\n\
4060Kill a process with a signal.");
4061
4062static PyObject *
4063win32_kill(PyObject *self, PyObject *args)
4064{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004065 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004066 DWORD pid, sig, err;
4067 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004068
Victor Stinner8c62be82010-05-06 00:08:46 +00004069 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4070 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004071
Victor Stinner8c62be82010-05-06 00:08:46 +00004072 /* Console processes which share a common console can be sent CTRL+C or
4073 CTRL+BREAK events, provided they handle said events. */
4074 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4075 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4076 err = GetLastError();
4077 PyErr_SetFromWindowsErr(err);
4078 }
4079 else
4080 Py_RETURN_NONE;
4081 }
Brian Curtineb24d742010-04-12 17:16:38 +00004082
Victor Stinner8c62be82010-05-06 00:08:46 +00004083 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4084 attempt to open and terminate the process. */
4085 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4086 if (handle == NULL) {
4087 err = GetLastError();
4088 return PyErr_SetFromWindowsErr(err);
4089 }
Brian Curtineb24d742010-04-12 17:16:38 +00004090
Victor Stinner8c62be82010-05-06 00:08:46 +00004091 if (TerminateProcess(handle, sig) == 0) {
4092 err = GetLastError();
4093 result = PyErr_SetFromWindowsErr(err);
4094 } else {
4095 Py_INCREF(Py_None);
4096 result = Py_None;
4097 }
Brian Curtineb24d742010-04-12 17:16:38 +00004098
Victor Stinner8c62be82010-05-06 00:08:46 +00004099 CloseHandle(handle);
4100 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004101}
4102#endif /* MS_WINDOWS */
4103
Guido van Rossumc0125471996-06-28 18:55:32 +00004104#ifdef HAVE_PLOCK
4105
4106#ifdef HAVE_SYS_LOCK_H
4107#include <sys/lock.h>
4108#endif
4109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004110PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004111"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004112Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004113
Barry Warsaw53699e91996-12-10 23:23:01 +00004114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004115posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004116{
Victor Stinner8c62be82010-05-06 00:08:46 +00004117 int op;
4118 if (!PyArg_ParseTuple(args, "i:plock", &op))
4119 return NULL;
4120 if (plock(op) == -1)
4121 return posix_error();
4122 Py_INCREF(Py_None);
4123 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004124}
4125#endif
4126
Guido van Rossumb6775db1994-08-01 11:34:53 +00004127#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004128PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004129"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004130Set the current process's user id.");
4131
Barry Warsaw53699e91996-12-10 23:23:01 +00004132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004133posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004134{
Victor Stinner8c62be82010-05-06 00:08:46 +00004135 long uid_arg;
4136 uid_t uid;
4137 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4138 return NULL;
4139 uid = uid_arg;
4140 if (uid != uid_arg) {
4141 PyErr_SetString(PyExc_OverflowError, "user id too big");
4142 return NULL;
4143 }
4144 if (setuid(uid) < 0)
4145 return posix_error();
4146 Py_INCREF(Py_None);
4147 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004148}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004149#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004150
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004151
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004152#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004153PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004154"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004155Set the current process's effective user id.");
4156
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004157static PyObject *
4158posix_seteuid (PyObject *self, PyObject *args)
4159{
Victor Stinner8c62be82010-05-06 00:08:46 +00004160 long euid_arg;
4161 uid_t euid;
4162 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4163 return NULL;
4164 euid = euid_arg;
4165 if (euid != euid_arg) {
4166 PyErr_SetString(PyExc_OverflowError, "user id too big");
4167 return NULL;
4168 }
4169 if (seteuid(euid) < 0) {
4170 return posix_error();
4171 } else {
4172 Py_INCREF(Py_None);
4173 return Py_None;
4174 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004175}
4176#endif /* HAVE_SETEUID */
4177
4178#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004179PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004180"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004181Set the current process's effective group id.");
4182
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004183static PyObject *
4184posix_setegid (PyObject *self, PyObject *args)
4185{
Victor Stinner8c62be82010-05-06 00:08:46 +00004186 long egid_arg;
4187 gid_t egid;
4188 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4189 return NULL;
4190 egid = egid_arg;
4191 if (egid != egid_arg) {
4192 PyErr_SetString(PyExc_OverflowError, "group id too big");
4193 return NULL;
4194 }
4195 if (setegid(egid) < 0) {
4196 return posix_error();
4197 } else {
4198 Py_INCREF(Py_None);
4199 return Py_None;
4200 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004201}
4202#endif /* HAVE_SETEGID */
4203
4204#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004205PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004206"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004207Set the current process's real and effective user ids.");
4208
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004209static PyObject *
4210posix_setreuid (PyObject *self, PyObject *args)
4211{
Victor Stinner8c62be82010-05-06 00:08:46 +00004212 long ruid_arg, euid_arg;
4213 uid_t ruid, euid;
4214 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4215 return NULL;
4216 if (ruid_arg == -1)
4217 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4218 else
4219 ruid = ruid_arg; /* otherwise, assign from our long */
4220 if (euid_arg == -1)
4221 euid = (uid_t)-1;
4222 else
4223 euid = euid_arg;
4224 if ((euid_arg != -1 && euid != euid_arg) ||
4225 (ruid_arg != -1 && ruid != ruid_arg)) {
4226 PyErr_SetString(PyExc_OverflowError, "user id too big");
4227 return NULL;
4228 }
4229 if (setreuid(ruid, euid) < 0) {
4230 return posix_error();
4231 } else {
4232 Py_INCREF(Py_None);
4233 return Py_None;
4234 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004235}
4236#endif /* HAVE_SETREUID */
4237
4238#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004239PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004240"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004241Set the current process's real and effective group ids.");
4242
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004243static PyObject *
4244posix_setregid (PyObject *self, PyObject *args)
4245{
Victor Stinner8c62be82010-05-06 00:08:46 +00004246 long rgid_arg, egid_arg;
4247 gid_t rgid, egid;
4248 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4249 return NULL;
4250 if (rgid_arg == -1)
4251 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4252 else
4253 rgid = rgid_arg; /* otherwise, assign from our long */
4254 if (egid_arg == -1)
4255 egid = (gid_t)-1;
4256 else
4257 egid = egid_arg;
4258 if ((egid_arg != -1 && egid != egid_arg) ||
4259 (rgid_arg != -1 && rgid != rgid_arg)) {
4260 PyErr_SetString(PyExc_OverflowError, "group id too big");
4261 return NULL;
4262 }
4263 if (setregid(rgid, egid) < 0) {
4264 return posix_error();
4265 } else {
4266 Py_INCREF(Py_None);
4267 return Py_None;
4268 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004269}
4270#endif /* HAVE_SETREGID */
4271
Guido van Rossumb6775db1994-08-01 11:34:53 +00004272#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004273PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004274"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004275Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004276
Barry Warsaw53699e91996-12-10 23:23:01 +00004277static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004278posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004279{
Victor Stinner8c62be82010-05-06 00:08:46 +00004280 long gid_arg;
4281 gid_t gid;
4282 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4283 return NULL;
4284 gid = gid_arg;
4285 if (gid != gid_arg) {
4286 PyErr_SetString(PyExc_OverflowError, "group id too big");
4287 return NULL;
4288 }
4289 if (setgid(gid) < 0)
4290 return posix_error();
4291 Py_INCREF(Py_None);
4292 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004293}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004294#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004295
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004296#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004297PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004298"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004299Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004300
4301static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004302posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004303{
Victor Stinner8c62be82010-05-06 00:08:46 +00004304 int i, len;
4305 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004306
Victor Stinner8c62be82010-05-06 00:08:46 +00004307 if (!PySequence_Check(groups)) {
4308 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4309 return NULL;
4310 }
4311 len = PySequence_Size(groups);
4312 if (len > MAX_GROUPS) {
4313 PyErr_SetString(PyExc_ValueError, "too many groups");
4314 return NULL;
4315 }
4316 for(i = 0; i < len; i++) {
4317 PyObject *elem;
4318 elem = PySequence_GetItem(groups, i);
4319 if (!elem)
4320 return NULL;
4321 if (!PyLong_Check(elem)) {
4322 PyErr_SetString(PyExc_TypeError,
4323 "groups must be integers");
4324 Py_DECREF(elem);
4325 return NULL;
4326 } else {
4327 unsigned long x = PyLong_AsUnsignedLong(elem);
4328 if (PyErr_Occurred()) {
4329 PyErr_SetString(PyExc_TypeError,
4330 "group id too big");
4331 Py_DECREF(elem);
4332 return NULL;
4333 }
4334 grouplist[i] = x;
4335 /* read back the value to see if it fitted in gid_t */
4336 if (grouplist[i] != x) {
4337 PyErr_SetString(PyExc_TypeError,
4338 "group id too big");
4339 Py_DECREF(elem);
4340 return NULL;
4341 }
4342 }
4343 Py_DECREF(elem);
4344 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004345
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 if (setgroups(len, grouplist) < 0)
4347 return posix_error();
4348 Py_INCREF(Py_None);
4349 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004350}
4351#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004352
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004353#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4354static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004355wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004356{
Victor Stinner8c62be82010-05-06 00:08:46 +00004357 PyObject *result;
4358 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004359
Victor Stinner8c62be82010-05-06 00:08:46 +00004360 if (pid == -1)
4361 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004362
Victor Stinner8c62be82010-05-06 00:08:46 +00004363 if (struct_rusage == NULL) {
4364 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4365 if (m == NULL)
4366 return NULL;
4367 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4368 Py_DECREF(m);
4369 if (struct_rusage == NULL)
4370 return NULL;
4371 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004372
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4374 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4375 if (!result)
4376 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004377
4378#ifndef doubletime
4379#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4380#endif
4381
Victor Stinner8c62be82010-05-06 00:08:46 +00004382 PyStructSequence_SET_ITEM(result, 0,
4383 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4384 PyStructSequence_SET_ITEM(result, 1,
4385 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004386#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4388 SET_INT(result, 2, ru->ru_maxrss);
4389 SET_INT(result, 3, ru->ru_ixrss);
4390 SET_INT(result, 4, ru->ru_idrss);
4391 SET_INT(result, 5, ru->ru_isrss);
4392 SET_INT(result, 6, ru->ru_minflt);
4393 SET_INT(result, 7, ru->ru_majflt);
4394 SET_INT(result, 8, ru->ru_nswap);
4395 SET_INT(result, 9, ru->ru_inblock);
4396 SET_INT(result, 10, ru->ru_oublock);
4397 SET_INT(result, 11, ru->ru_msgsnd);
4398 SET_INT(result, 12, ru->ru_msgrcv);
4399 SET_INT(result, 13, ru->ru_nsignals);
4400 SET_INT(result, 14, ru->ru_nvcsw);
4401 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004402#undef SET_INT
4403
Victor Stinner8c62be82010-05-06 00:08:46 +00004404 if (PyErr_Occurred()) {
4405 Py_DECREF(result);
4406 return NULL;
4407 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004408
Victor Stinner8c62be82010-05-06 00:08:46 +00004409 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004410}
4411#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4412
4413#ifdef HAVE_WAIT3
4414PyDoc_STRVAR(posix_wait3__doc__,
4415"wait3(options) -> (pid, status, rusage)\n\n\
4416Wait for completion of a child process.");
4417
4418static PyObject *
4419posix_wait3(PyObject *self, PyObject *args)
4420{
Victor Stinner8c62be82010-05-06 00:08:46 +00004421 pid_t pid;
4422 int options;
4423 struct rusage ru;
4424 WAIT_TYPE status;
4425 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004426
Victor Stinner8c62be82010-05-06 00:08:46 +00004427 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4428 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004429
Victor Stinner8c62be82010-05-06 00:08:46 +00004430 Py_BEGIN_ALLOW_THREADS
4431 pid = wait3(&status, options, &ru);
4432 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004433
Victor Stinner8c62be82010-05-06 00:08:46 +00004434 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004435}
4436#endif /* HAVE_WAIT3 */
4437
4438#ifdef HAVE_WAIT4
4439PyDoc_STRVAR(posix_wait4__doc__,
4440"wait4(pid, options) -> (pid, status, rusage)\n\n\
4441Wait for completion of a given child process.");
4442
4443static PyObject *
4444posix_wait4(PyObject *self, PyObject *args)
4445{
Victor Stinner8c62be82010-05-06 00:08:46 +00004446 pid_t pid;
4447 int options;
4448 struct rusage ru;
4449 WAIT_TYPE status;
4450 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004451
Victor Stinner8c62be82010-05-06 00:08:46 +00004452 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4453 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004454
Victor Stinner8c62be82010-05-06 00:08:46 +00004455 Py_BEGIN_ALLOW_THREADS
4456 pid = wait4(pid, &status, options, &ru);
4457 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004458
Victor Stinner8c62be82010-05-06 00:08:46 +00004459 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004460}
4461#endif /* HAVE_WAIT4 */
4462
Guido van Rossumb6775db1994-08-01 11:34:53 +00004463#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004464PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004465"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004466Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004467
Barry Warsaw53699e91996-12-10 23:23:01 +00004468static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004469posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004470{
Victor Stinner8c62be82010-05-06 00:08:46 +00004471 pid_t pid;
4472 int options;
4473 WAIT_TYPE status;
4474 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004475
Victor Stinner8c62be82010-05-06 00:08:46 +00004476 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4477 return NULL;
4478 Py_BEGIN_ALLOW_THREADS
4479 pid = waitpid(pid, &status, options);
4480 Py_END_ALLOW_THREADS
4481 if (pid == -1)
4482 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004483
Victor Stinner8c62be82010-05-06 00:08:46 +00004484 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004485}
4486
Tim Petersab034fa2002-02-01 11:27:43 +00004487#elif defined(HAVE_CWAIT)
4488
4489/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004490PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004491"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004492"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004493
4494static PyObject *
4495posix_waitpid(PyObject *self, PyObject *args)
4496{
Victor Stinner8c62be82010-05-06 00:08:46 +00004497 Py_intptr_t pid;
4498 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004499
Victor Stinner8c62be82010-05-06 00:08:46 +00004500 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4501 return NULL;
4502 Py_BEGIN_ALLOW_THREADS
4503 pid = _cwait(&status, pid, options);
4504 Py_END_ALLOW_THREADS
4505 if (pid == -1)
4506 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004507
Victor Stinner8c62be82010-05-06 00:08:46 +00004508 /* shift the status left a byte so this is more like the POSIX waitpid */
4509 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004510}
4511#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004512
Guido van Rossumad0ee831995-03-01 10:34:45 +00004513#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004514PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004515"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004516Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004517
Barry Warsaw53699e91996-12-10 23:23:01 +00004518static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004519posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004520{
Victor Stinner8c62be82010-05-06 00:08:46 +00004521 pid_t pid;
4522 WAIT_TYPE status;
4523 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004524
Victor Stinner8c62be82010-05-06 00:08:46 +00004525 Py_BEGIN_ALLOW_THREADS
4526 pid = wait(&status);
4527 Py_END_ALLOW_THREADS
4528 if (pid == -1)
4529 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004530
Victor Stinner8c62be82010-05-06 00:08:46 +00004531 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004532}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004533#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004534
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004536PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004537"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004538Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004539
Barry Warsaw53699e91996-12-10 23:23:01 +00004540static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004541posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004542{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004543#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00004544 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004545#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004546#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004547 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004548#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004549 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004550#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004551#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004552}
4553
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004554
Guido van Rossumb6775db1994-08-01 11:34:53 +00004555#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004556PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004557"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004558Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004559
Barry Warsaw53699e91996-12-10 23:23:01 +00004560static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004561posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004562{
Victor Stinner8c62be82010-05-06 00:08:46 +00004563 PyObject* v;
4564 char buf[MAXPATHLEN];
4565 PyObject *opath;
4566 char *path;
4567 int n;
4568 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004569
Victor Stinner8c62be82010-05-06 00:08:46 +00004570 if (!PyArg_ParseTuple(args, "O&:readlink",
4571 PyUnicode_FSConverter, &opath))
4572 return NULL;
4573 path = PyBytes_AsString(opath);
4574 v = PySequence_GetItem(args, 0);
4575 if (v == NULL) {
4576 Py_DECREF(opath);
4577 return NULL;
4578 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004579
Victor Stinner8c62be82010-05-06 00:08:46 +00004580 if (PyUnicode_Check(v)) {
4581 arg_is_unicode = 1;
4582 }
4583 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004584
Victor Stinner8c62be82010-05-06 00:08:46 +00004585 Py_BEGIN_ALLOW_THREADS
4586 n = readlink(path, buf, (int) sizeof buf);
4587 Py_END_ALLOW_THREADS
4588 if (n < 0)
4589 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004590
Victor Stinner8c62be82010-05-06 00:08:46 +00004591 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00004592 if (arg_is_unicode)
4593 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
4594 else
4595 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004596}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004597#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004598
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004599
Guido van Rossumb6775db1994-08-01 11:34:53 +00004600#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004601PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004602"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004603Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004604
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004605static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004606posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004607{
Victor Stinner8c62be82010-05-06 00:08:46 +00004608 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004609}
4610#endif /* HAVE_SYMLINK */
4611
4612
4613#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004614#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4615static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004616system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004617{
4618 ULONG value = 0;
4619
4620 Py_BEGIN_ALLOW_THREADS
4621 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4622 Py_END_ALLOW_THREADS
4623
4624 return value;
4625}
4626
4627static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004628posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004629{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004630 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00004631 return Py_BuildValue("ddddd",
4632 (double)0 /* t.tms_utime / HZ */,
4633 (double)0 /* t.tms_stime / HZ */,
4634 (double)0 /* t.tms_cutime / HZ */,
4635 (double)0 /* t.tms_cstime / HZ */,
4636 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004637}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004638#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004639#define NEED_TICKS_PER_SECOND
4640static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004641static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004642posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004643{
Victor Stinner8c62be82010-05-06 00:08:46 +00004644 struct tms t;
4645 clock_t c;
4646 errno = 0;
4647 c = times(&t);
4648 if (c == (clock_t) -1)
4649 return posix_error();
4650 return Py_BuildValue("ddddd",
4651 (double)t.tms_utime / ticks_per_second,
4652 (double)t.tms_stime / ticks_per_second,
4653 (double)t.tms_cutime / ticks_per_second,
4654 (double)t.tms_cstime / ticks_per_second,
4655 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004656}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004657#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004658#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004659
4660
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004661#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004662#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004663static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004664posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004665{
Victor Stinner8c62be82010-05-06 00:08:46 +00004666 FILETIME create, exit, kernel, user;
4667 HANDLE hProc;
4668 hProc = GetCurrentProcess();
4669 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4670 /* The fields of a FILETIME structure are the hi and lo part
4671 of a 64-bit value expressed in 100 nanosecond units.
4672 1e7 is one second in such units; 1e-7 the inverse.
4673 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4674 */
4675 return Py_BuildValue(
4676 "ddddd",
4677 (double)(user.dwHighDateTime*429.4967296 +
4678 user.dwLowDateTime*1e-7),
4679 (double)(kernel.dwHighDateTime*429.4967296 +
4680 kernel.dwLowDateTime*1e-7),
4681 (double)0,
4682 (double)0,
4683 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004684}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004685#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004686
4687#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004688PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004689"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004690Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004691#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004692
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004693
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004694#ifdef HAVE_GETSID
4695PyDoc_STRVAR(posix_getsid__doc__,
4696"getsid(pid) -> sid\n\n\
4697Call the system call getsid().");
4698
4699static PyObject *
4700posix_getsid(PyObject *self, PyObject *args)
4701{
Victor Stinner8c62be82010-05-06 00:08:46 +00004702 pid_t pid;
4703 int sid;
4704 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
4705 return NULL;
4706 sid = getsid(pid);
4707 if (sid < 0)
4708 return posix_error();
4709 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004710}
4711#endif /* HAVE_GETSID */
4712
4713
Guido van Rossumb6775db1994-08-01 11:34:53 +00004714#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004715PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004716"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004717Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004718
Barry Warsaw53699e91996-12-10 23:23:01 +00004719static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004720posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004721{
Victor Stinner8c62be82010-05-06 00:08:46 +00004722 if (setsid() < 0)
4723 return posix_error();
4724 Py_INCREF(Py_None);
4725 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004726}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004727#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004728
Guido van Rossumb6775db1994-08-01 11:34:53 +00004729#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004730PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004731"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004732Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004733
Barry Warsaw53699e91996-12-10 23:23:01 +00004734static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004735posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004736{
Victor Stinner8c62be82010-05-06 00:08:46 +00004737 pid_t pid;
4738 int pgrp;
4739 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
4740 return NULL;
4741 if (setpgid(pid, pgrp) < 0)
4742 return posix_error();
4743 Py_INCREF(Py_None);
4744 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004745}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004746#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004747
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004748
Guido van Rossumb6775db1994-08-01 11:34:53 +00004749#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004750PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004751"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004752Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004753
Barry Warsaw53699e91996-12-10 23:23:01 +00004754static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004755posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004756{
Victor Stinner8c62be82010-05-06 00:08:46 +00004757 int fd;
4758 pid_t pgid;
4759 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
4760 return NULL;
4761 pgid = tcgetpgrp(fd);
4762 if (pgid < 0)
4763 return posix_error();
4764 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004765}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004766#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004767
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004768
Guido van Rossumb6775db1994-08-01 11:34:53 +00004769#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004770PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004771"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004772Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004773
Barry Warsaw53699e91996-12-10 23:23:01 +00004774static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004775posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004776{
Victor Stinner8c62be82010-05-06 00:08:46 +00004777 int fd;
4778 pid_t pgid;
4779 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
4780 return NULL;
4781 if (tcsetpgrp(fd, pgid) < 0)
4782 return posix_error();
4783 Py_INCREF(Py_None);
4784 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004785}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004786#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004787
Guido van Rossum687dd131993-05-17 08:34:16 +00004788/* Functions acting on file descriptors */
4789
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004790PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004791"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004792Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004793
Barry Warsaw53699e91996-12-10 23:23:01 +00004794static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004795posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004796{
Victor Stinner8c62be82010-05-06 00:08:46 +00004797 PyObject *ofile;
4798 char *file;
4799 int flag;
4800 int mode = 0777;
4801 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004802
4803#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004804 PyUnicodeObject *po;
4805 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4806 Py_BEGIN_ALLOW_THREADS
4807 /* PyUnicode_AS_UNICODE OK without thread
4808 lock as it is a simple dereference. */
4809 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4810 Py_END_ALLOW_THREADS
4811 if (fd < 0)
4812 return posix_error();
4813 return PyLong_FromLong((long)fd);
4814 }
4815 /* Drop the argument parsing error as narrow strings
4816 are also valid. */
4817 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004818#endif
4819
Victor Stinner8c62be82010-05-06 00:08:46 +00004820 if (!PyArg_ParseTuple(args, "O&i|i",
4821 PyUnicode_FSConverter, &ofile,
4822 &flag, &mode))
4823 return NULL;
4824 file = PyBytes_AsString(ofile);
4825 Py_BEGIN_ALLOW_THREADS
4826 fd = open(file, flag, mode);
4827 Py_END_ALLOW_THREADS
4828 if (fd < 0)
4829 return posix_error_with_allocated_filename(ofile);
4830 Py_DECREF(ofile);
4831 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004832}
4833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004835PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004836"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004837Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004838
Barry Warsaw53699e91996-12-10 23:23:01 +00004839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004840posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004841{
Victor Stinner8c62be82010-05-06 00:08:46 +00004842 int fd, res;
4843 if (!PyArg_ParseTuple(args, "i:close", &fd))
4844 return NULL;
4845 if (!_PyVerify_fd(fd))
4846 return posix_error();
4847 Py_BEGIN_ALLOW_THREADS
4848 res = close(fd);
4849 Py_END_ALLOW_THREADS
4850 if (res < 0)
4851 return posix_error();
4852 Py_INCREF(Py_None);
4853 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004854}
4855
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004856
Victor Stinner8c62be82010-05-06 00:08:46 +00004857PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00004858"closerange(fd_low, fd_high)\n\n\
4859Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4860
4861static PyObject *
4862posix_closerange(PyObject *self, PyObject *args)
4863{
Victor Stinner8c62be82010-05-06 00:08:46 +00004864 int fd_from, fd_to, i;
4865 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4866 return NULL;
4867 Py_BEGIN_ALLOW_THREADS
4868 for (i = fd_from; i < fd_to; i++)
4869 if (_PyVerify_fd(i))
4870 close(i);
4871 Py_END_ALLOW_THREADS
4872 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00004873}
4874
4875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004876PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004877"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004878Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004879
Barry Warsaw53699e91996-12-10 23:23:01 +00004880static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004881posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004882{
Victor Stinner8c62be82010-05-06 00:08:46 +00004883 int fd;
4884 if (!PyArg_ParseTuple(args, "i:dup", &fd))
4885 return NULL;
4886 if (!_PyVerify_fd(fd))
4887 return posix_error();
4888 Py_BEGIN_ALLOW_THREADS
4889 fd = dup(fd);
4890 Py_END_ALLOW_THREADS
4891 if (fd < 0)
4892 return posix_error();
4893 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004894}
4895
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004897PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004898"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004899Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004900
Barry Warsaw53699e91996-12-10 23:23:01 +00004901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004902posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004903{
Victor Stinner8c62be82010-05-06 00:08:46 +00004904 int fd, fd2, res;
4905 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
4906 return NULL;
4907 if (!_PyVerify_fd_dup2(fd, fd2))
4908 return posix_error();
4909 Py_BEGIN_ALLOW_THREADS
4910 res = dup2(fd, fd2);
4911 Py_END_ALLOW_THREADS
4912 if (res < 0)
4913 return posix_error();
4914 Py_INCREF(Py_None);
4915 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004916}
4917
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004919PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004920"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004921Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004922
Barry Warsaw53699e91996-12-10 23:23:01 +00004923static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004924posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004925{
Victor Stinner8c62be82010-05-06 00:08:46 +00004926 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004927#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00004928 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004929#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004930 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004931#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004932 PyObject *posobj;
4933 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
4934 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004935#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00004936 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4937 switch (how) {
4938 case 0: how = SEEK_SET; break;
4939 case 1: how = SEEK_CUR; break;
4940 case 2: how = SEEK_END; break;
4941 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004942#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004943
4944#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00004945 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004946#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004947 pos = PyLong_Check(posobj) ?
4948 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004949#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004950 if (PyErr_Occurred())
4951 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004952
Victor Stinner8c62be82010-05-06 00:08:46 +00004953 if (!_PyVerify_fd(fd))
4954 return posix_error();
4955 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004956#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00004957 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004958#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004959 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004960#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004961 Py_END_ALLOW_THREADS
4962 if (res < 0)
4963 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004964
4965#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00004966 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004967#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004968 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004969#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004970}
4971
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004973PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004974"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004975Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004976
Barry Warsaw53699e91996-12-10 23:23:01 +00004977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004978posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004979{
Victor Stinner8c62be82010-05-06 00:08:46 +00004980 int fd, size;
4981 Py_ssize_t n;
4982 PyObject *buffer;
4983 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
4984 return NULL;
4985 if (size < 0) {
4986 errno = EINVAL;
4987 return posix_error();
4988 }
4989 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
4990 if (buffer == NULL)
4991 return NULL;
4992 if (!_PyVerify_fd(fd))
4993 return posix_error();
4994 Py_BEGIN_ALLOW_THREADS
4995 n = read(fd, PyBytes_AS_STRING(buffer), size);
4996 Py_END_ALLOW_THREADS
4997 if (n < 0) {
4998 Py_DECREF(buffer);
4999 return posix_error();
5000 }
5001 if (n != size)
5002 _PyBytes_Resize(&buffer, n);
5003 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005004}
5005
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005006
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005007PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005008"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005009Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005010
Barry Warsaw53699e91996-12-10 23:23:01 +00005011static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005012posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005013{
Victor Stinner8c62be82010-05-06 00:08:46 +00005014 Py_buffer pbuf;
5015 int fd;
5016 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005017
Victor Stinner8c62be82010-05-06 00:08:46 +00005018 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5019 return NULL;
5020 if (!_PyVerify_fd(fd))
5021 return posix_error();
5022 Py_BEGIN_ALLOW_THREADS
5023 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5024 Py_END_ALLOW_THREADS
5025 PyBuffer_Release(&pbuf);
5026 if (size < 0)
5027 return posix_error();
5028 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005029}
5030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005032PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005033"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005034Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005035
Barry Warsaw53699e91996-12-10 23:23:01 +00005036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005037posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005038{
Victor Stinner8c62be82010-05-06 00:08:46 +00005039 int fd;
5040 STRUCT_STAT st;
5041 int res;
5042 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5043 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005044#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005045 /* on OpenVMS we must ensure that all bytes are written to the file */
5046 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005047#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005048 if (!_PyVerify_fd(fd))
5049 return posix_error();
5050 Py_BEGIN_ALLOW_THREADS
5051 res = FSTAT(fd, &st);
5052 Py_END_ALLOW_THREADS
5053 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005054#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005055 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005056#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005057 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005058#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005059 }
Tim Peters5aa91602002-01-30 05:46:57 +00005060
Victor Stinner8c62be82010-05-06 00:08:46 +00005061 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005062}
5063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005064PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005065"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005066Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005067connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005068
5069static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005070posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005071{
Victor Stinner8c62be82010-05-06 00:08:46 +00005072 int fd;
5073 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5074 return NULL;
5075 if (!_PyVerify_fd(fd))
5076 return PyBool_FromLong(0);
5077 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005078}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005079
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005080#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005081PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005082"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005083Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005084
Barry Warsaw53699e91996-12-10 23:23:01 +00005085static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005086posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005087{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005088#if defined(PYOS_OS2)
5089 HFILE read, write;
5090 APIRET rc;
5091
Victor Stinner8c62be82010-05-06 00:08:46 +00005092 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005093 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005095 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005096 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005097
5098 return Py_BuildValue("(ii)", read, write);
5099#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005100#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005101 int fds[2];
5102 int res;
5103 Py_BEGIN_ALLOW_THREADS
5104 res = pipe(fds);
5105 Py_END_ALLOW_THREADS
5106 if (res != 0)
5107 return posix_error();
5108 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005109#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005110 HANDLE read, write;
5111 int read_fd, write_fd;
5112 BOOL ok;
5113 Py_BEGIN_ALLOW_THREADS
5114 ok = CreatePipe(&read, &write, NULL, 0);
5115 Py_END_ALLOW_THREADS
5116 if (!ok)
5117 return win32_error("CreatePipe", NULL);
5118 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5119 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5120 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005121#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005122#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005123}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005124#endif /* HAVE_PIPE */
5125
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005126
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005127#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005128PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005129"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005130Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005131
Barry Warsaw53699e91996-12-10 23:23:01 +00005132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005133posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005134{
Victor Stinner8c62be82010-05-06 00:08:46 +00005135 char *filename;
5136 int mode = 0666;
5137 int res;
5138 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
5139 return NULL;
5140 Py_BEGIN_ALLOW_THREADS
5141 res = mkfifo(filename, mode);
5142 Py_END_ALLOW_THREADS
5143 if (res < 0)
5144 return posix_error();
5145 Py_INCREF(Py_None);
5146 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005147}
5148#endif
5149
5150
Neal Norwitz11690112002-07-30 01:08:28 +00005151#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005152PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005153"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005154Create a filesystem node (file, device special file or named pipe)\n\
5155named filename. mode specifies both the permissions to use and the\n\
5156type of node to be created, being combined (bitwise OR) with one of\n\
5157S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005158device defines the newly created device special file (probably using\n\
5159os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005160
5161
5162static PyObject *
5163posix_mknod(PyObject *self, PyObject *args)
5164{
Victor Stinner8c62be82010-05-06 00:08:46 +00005165 char *filename;
5166 int mode = 0600;
5167 int device = 0;
5168 int res;
5169 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
5170 return NULL;
5171 Py_BEGIN_ALLOW_THREADS
5172 res = mknod(filename, mode, device);
5173 Py_END_ALLOW_THREADS
5174 if (res < 0)
5175 return posix_error();
5176 Py_INCREF(Py_None);
5177 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005178}
5179#endif
5180
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005181#ifdef HAVE_DEVICE_MACROS
5182PyDoc_STRVAR(posix_major__doc__,
5183"major(device) -> major number\n\
5184Extracts a device major number from a raw device number.");
5185
5186static PyObject *
5187posix_major(PyObject *self, PyObject *args)
5188{
Victor Stinner8c62be82010-05-06 00:08:46 +00005189 int device;
5190 if (!PyArg_ParseTuple(args, "i:major", &device))
5191 return NULL;
5192 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005193}
5194
5195PyDoc_STRVAR(posix_minor__doc__,
5196"minor(device) -> minor number\n\
5197Extracts a device minor number from a raw device number.");
5198
5199static PyObject *
5200posix_minor(PyObject *self, PyObject *args)
5201{
Victor Stinner8c62be82010-05-06 00:08:46 +00005202 int device;
5203 if (!PyArg_ParseTuple(args, "i:minor", &device))
5204 return NULL;
5205 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005206}
5207
5208PyDoc_STRVAR(posix_makedev__doc__,
5209"makedev(major, minor) -> device number\n\
5210Composes a raw device number from the major and minor device numbers.");
5211
5212static PyObject *
5213posix_makedev(PyObject *self, PyObject *args)
5214{
Victor Stinner8c62be82010-05-06 00:08:46 +00005215 int major, minor;
5216 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5217 return NULL;
5218 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005219}
5220#endif /* device macros */
5221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005222
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005223#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005224PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005225"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005226Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005227
Barry Warsaw53699e91996-12-10 23:23:01 +00005228static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005229posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005230{
Victor Stinner8c62be82010-05-06 00:08:46 +00005231 int fd;
5232 off_t length;
5233 int res;
5234 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005235
Victor Stinner8c62be82010-05-06 00:08:46 +00005236 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5237 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005238
5239#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005240 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005241#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005242 length = PyLong_Check(lenobj) ?
5243 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005244#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005245 if (PyErr_Occurred())
5246 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005247
Victor Stinner8c62be82010-05-06 00:08:46 +00005248 Py_BEGIN_ALLOW_THREADS
5249 res = ftruncate(fd, length);
5250 Py_END_ALLOW_THREADS
5251 if (res < 0)
5252 return posix_error();
5253 Py_INCREF(Py_None);
5254 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005255}
5256#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005257
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005258#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005259PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005260"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005261Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005262
Fred Drake762e2061999-08-26 17:23:54 +00005263/* Save putenv() parameters as values here, so we can collect them when they
5264 * get re-set with another call for the same key. */
5265static PyObject *posix_putenv_garbage;
5266
Tim Peters5aa91602002-01-30 05:46:57 +00005267static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005268posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005269{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005270#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005271 wchar_t *s1, *s2;
5272 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005273#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005274 PyObject *os1, *os2;
5275 char *s1, *s2;
5276 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005277#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005278 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005279 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005280
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005281#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005282 if (!PyArg_ParseTuple(args,
5283 "uu:putenv",
5284 &s1, &s2))
5285 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005286#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005287 if (!PyArg_ParseTuple(args,
5288 "O&O&:putenv",
5289 PyUnicode_FSConverter, &os1,
5290 PyUnicode_FSConverter, &os2))
5291 return NULL;
5292 s1 = PyBytes_AsString(os1);
5293 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005294#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005295
5296#if defined(PYOS_OS2)
5297 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5298 APIRET rc;
5299
Guido van Rossumd48f2521997-12-05 22:19:34 +00005300 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005301 if (rc != NO_ERROR) {
5302 os2_error(rc);
5303 goto error;
5304 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005305
5306 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5307 APIRET rc;
5308
Guido van Rossumd48f2521997-12-05 22:19:34 +00005309 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005310 if (rc != NO_ERROR) {
5311 os2_error(rc);
5312 goto error;
5313 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005314 } else {
5315#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005316 /* XXX This can leak memory -- not easy to fix :-( */
5317 /* len includes space for a trailing \0; the size arg to
5318 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005319#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005320 len = wcslen(s1) + wcslen(s2) + 2;
5321 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005322#else
Victor Stinner84ae1182010-05-06 22:05:07 +00005323 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00005324 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005325#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005326 if (newstr == NULL) {
5327 PyErr_NoMemory();
5328 goto error;
5329 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005330#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005331 newenv = PyUnicode_AsUnicode(newstr);
5332 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5333 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005334 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005335 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005336 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005337#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005338 newenv = PyBytes_AS_STRING(newstr);
5339 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5340 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005341 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005342 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005343 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005344#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005345
Victor Stinner8c62be82010-05-06 00:08:46 +00005346 /* Install the first arg and newstr in posix_putenv_garbage;
5347 * this will cause previous value to be collected. This has to
5348 * happen after the real putenv() call because the old value
5349 * was still accessible until then. */
5350 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005351#ifdef MS_WINDOWS
5352 PyTuple_GET_ITEM(args, 0),
5353#else
5354 os1,
5355#endif
5356 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005357 /* really not much we can do; just leak */
5358 PyErr_Clear();
5359 }
5360 else {
5361 Py_DECREF(newstr);
5362 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005363
5364#if defined(PYOS_OS2)
5365 }
5366#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005367
Martin v. Löwis011e8422009-05-05 04:43:17 +00005368#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005369 Py_DECREF(os1);
5370 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005371#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005372 Py_RETURN_NONE;
5373
5374error:
5375#ifndef MS_WINDOWS
5376 Py_DECREF(os1);
5377 Py_DECREF(os2);
5378#endif
5379 Py_XDECREF(newstr);
5380 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005381}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005382#endif /* putenv */
5383
Guido van Rossumc524d952001-10-19 01:31:59 +00005384#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005385PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005386"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005387Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005388
5389static PyObject *
5390posix_unsetenv(PyObject *self, PyObject *args)
5391{
Victor Stinner84ae1182010-05-06 22:05:07 +00005392#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005393 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005394
Victor Stinner8c62be82010-05-06 00:08:46 +00005395 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5396 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00005397#else
5398 PyObject *os1;
5399 char *s1;
5400
5401 if (!PyArg_ParseTuple(args, "O&:unsetenv",
5402 PyUnicode_FSConverter, &os1))
5403 return NULL;
5404 s1 = PyBytes_AsString(os1);
5405#endif
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,
Victor Stinner84ae1182010-05-06 22:05:07 +00005415#ifdef MS_WINDOWS
5416 PyTuple_GET_ITEM(args, 0)
5417#else
5418 os1
5419#endif
5420 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005421 /* really not much we can do; just leak */
5422 PyErr_Clear();
5423 }
Guido van Rossumc524d952001-10-19 01:31:59 +00005424
Victor Stinner84ae1182010-05-06 22:05:07 +00005425#ifndef MS_WINDOWS
5426 Py_DECREF(os1);
5427#endif
5428 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00005429}
5430#endif /* unsetenv */
5431
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005432PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005433"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005434Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005435
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005436static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005437posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005438{
Victor Stinner8c62be82010-05-06 00:08:46 +00005439 int code;
5440 char *message;
5441 if (!PyArg_ParseTuple(args, "i:strerror", &code))
5442 return NULL;
5443 message = strerror(code);
5444 if (message == NULL) {
5445 PyErr_SetString(PyExc_ValueError,
5446 "strerror() argument out of range");
5447 return NULL;
5448 }
5449 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005450}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005451
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005452
Guido van Rossumc9641791998-08-04 15:26:23 +00005453#ifdef HAVE_SYS_WAIT_H
5454
Fred Drake106c1a02002-04-23 15:58:02 +00005455#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005456PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005457"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005458Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005459
5460static PyObject *
5461posix_WCOREDUMP(PyObject *self, PyObject *args)
5462{
Victor Stinner8c62be82010-05-06 00:08:46 +00005463 WAIT_TYPE status;
5464 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005465
Victor Stinner8c62be82010-05-06 00:08:46 +00005466 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
5467 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005468
Victor Stinner8c62be82010-05-06 00:08:46 +00005469 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005470}
5471#endif /* WCOREDUMP */
5472
5473#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005474PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005475"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005476Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005477job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005478
5479static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005480posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005481{
Victor Stinner8c62be82010-05-06 00:08:46 +00005482 WAIT_TYPE status;
5483 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005484
Victor Stinner8c62be82010-05-06 00:08:46 +00005485 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
5486 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005487
Victor Stinner8c62be82010-05-06 00:08:46 +00005488 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005489}
5490#endif /* WIFCONTINUED */
5491
Guido van Rossumc9641791998-08-04 15:26:23 +00005492#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005493PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005494"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005495Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005496
5497static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005498posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005499{
Victor Stinner8c62be82010-05-06 00:08:46 +00005500 WAIT_TYPE status;
5501 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005502
Victor Stinner8c62be82010-05-06 00:08:46 +00005503 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
5504 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005505
Victor Stinner8c62be82010-05-06 00:08:46 +00005506 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005507}
5508#endif /* WIFSTOPPED */
5509
5510#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005511PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005512"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005513Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005514
5515static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005516posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005517{
Victor Stinner8c62be82010-05-06 00:08:46 +00005518 WAIT_TYPE status;
5519 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005520
Victor Stinner8c62be82010-05-06 00:08:46 +00005521 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
5522 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005523
Victor Stinner8c62be82010-05-06 00:08:46 +00005524 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005525}
5526#endif /* WIFSIGNALED */
5527
5528#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005529PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005530"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005531Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005532system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005533
5534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005535posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005536{
Victor Stinner8c62be82010-05-06 00:08:46 +00005537 WAIT_TYPE status;
5538 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005539
Victor Stinner8c62be82010-05-06 00:08:46 +00005540 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
5541 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005542
Victor Stinner8c62be82010-05-06 00:08:46 +00005543 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005544}
5545#endif /* WIFEXITED */
5546
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005547#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005548PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005549"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005550Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005551
5552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005553posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005554{
Victor Stinner8c62be82010-05-06 00:08:46 +00005555 WAIT_TYPE status;
5556 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005557
Victor Stinner8c62be82010-05-06 00:08:46 +00005558 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
5559 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005560
Victor Stinner8c62be82010-05-06 00:08:46 +00005561 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005562}
5563#endif /* WEXITSTATUS */
5564
5565#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005567"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005568Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005569value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005570
5571static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005572posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005573{
Victor Stinner8c62be82010-05-06 00:08:46 +00005574 WAIT_TYPE status;
5575 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005576
Victor Stinner8c62be82010-05-06 00:08:46 +00005577 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
5578 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005579
Victor Stinner8c62be82010-05-06 00:08:46 +00005580 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005581}
5582#endif /* WTERMSIG */
5583
5584#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005585PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005586"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005587Return the signal that stopped the process that provided\n\
5588the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005589
5590static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005591posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005592{
Victor Stinner8c62be82010-05-06 00:08:46 +00005593 WAIT_TYPE status;
5594 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005595
Victor Stinner8c62be82010-05-06 00:08:46 +00005596 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
5597 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005598
Victor Stinner8c62be82010-05-06 00:08:46 +00005599 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005600}
5601#endif /* WSTOPSIG */
5602
5603#endif /* HAVE_SYS_WAIT_H */
5604
5605
Thomas Wouters477c8d52006-05-27 19:21:47 +00005606#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005607#ifdef _SCO_DS
5608/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5609 needed definitions in sys/statvfs.h */
5610#define _SVID3
5611#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005612#include <sys/statvfs.h>
5613
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005614static PyObject*
5615_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005616 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5617 if (v == NULL)
5618 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005619
5620#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005621 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5622 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5623 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5624 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5625 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5626 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5627 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5628 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5629 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5630 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005631#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5633 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5634 PyStructSequence_SET_ITEM(v, 2,
5635 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
5636 PyStructSequence_SET_ITEM(v, 3,
5637 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
5638 PyStructSequence_SET_ITEM(v, 4,
5639 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
5640 PyStructSequence_SET_ITEM(v, 5,
5641 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
5642 PyStructSequence_SET_ITEM(v, 6,
5643 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
5644 PyStructSequence_SET_ITEM(v, 7,
5645 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
5646 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5647 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005648#endif
5649
Victor Stinner8c62be82010-05-06 00:08:46 +00005650 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005651}
5652
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005653PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005654"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005655Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005656
5657static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005658posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005659{
Victor Stinner8c62be82010-05-06 00:08:46 +00005660 int fd, res;
5661 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005662
Victor Stinner8c62be82010-05-06 00:08:46 +00005663 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
5664 return NULL;
5665 Py_BEGIN_ALLOW_THREADS
5666 res = fstatvfs(fd, &st);
5667 Py_END_ALLOW_THREADS
5668 if (res != 0)
5669 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005670
Victor Stinner8c62be82010-05-06 00:08:46 +00005671 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005672}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005673#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005674
5675
Thomas Wouters477c8d52006-05-27 19:21:47 +00005676#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005677#include <sys/statvfs.h>
5678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005679PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005680"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005681Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005682
5683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005684posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005685{
Victor Stinner8c62be82010-05-06 00:08:46 +00005686 char *path;
5687 int res;
5688 struct statvfs st;
5689 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
5690 return NULL;
5691 Py_BEGIN_ALLOW_THREADS
5692 res = statvfs(path, &st);
5693 Py_END_ALLOW_THREADS
5694 if (res != 0)
5695 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005696
Victor Stinner8c62be82010-05-06 00:08:46 +00005697 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005698}
5699#endif /* HAVE_STATVFS */
5700
Fred Drakec9680921999-12-13 16:37:25 +00005701/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5702 * It maps strings representing configuration variable names to
5703 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005704 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005705 * rarely-used constants. There are three separate tables that use
5706 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005707 *
5708 * This code is always included, even if none of the interfaces that
5709 * need it are included. The #if hackery needed to avoid it would be
5710 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005711 */
5712struct constdef {
5713 char *name;
5714 long value;
5715};
5716
Fred Drake12c6e2d1999-12-14 21:25:03 +00005717static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005718conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005719 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005720{
Christian Heimes217cfd12007-12-02 14:31:20 +00005721 if (PyLong_Check(arg)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005722 *valuep = PyLong_AS_LONG(arg);
5723 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005724 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005725 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00005726 /* look up the value in the table using a binary search */
5727 size_t lo = 0;
5728 size_t mid;
5729 size_t hi = tablesize;
5730 int cmp;
5731 const char *confname;
5732 if (!PyUnicode_Check(arg)) {
5733 PyErr_SetString(PyExc_TypeError,
5734 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005735 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005736 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005737 confname = _PyUnicode_AsString(arg);
5738 if (confname == NULL)
5739 return 0;
5740 while (lo < hi) {
5741 mid = (lo + hi) / 2;
5742 cmp = strcmp(confname, table[mid].name);
5743 if (cmp < 0)
5744 hi = mid;
5745 else if (cmp > 0)
5746 lo = mid + 1;
5747 else {
5748 *valuep = table[mid].value;
5749 return 1;
5750 }
5751 }
5752 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5753 return 0;
5754 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005755}
5756
5757
5758#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5759static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005760#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005761 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00005762#endif
5763#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005764 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00005765#endif
Fred Drakec9680921999-12-13 16:37:25 +00005766#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005767 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005768#endif
5769#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00005770 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00005771#endif
5772#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00005773 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00005774#endif
5775#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00005776 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00005777#endif
5778#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005779 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005780#endif
5781#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00005782 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00005783#endif
5784#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00005786#endif
5787#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005789#endif
5790#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00005791 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00005792#endif
5793#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005794 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005795#endif
5796#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00005797 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00005798#endif
5799#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005800 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005801#endif
5802#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00005803 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00005804#endif
5805#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005806 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005807#endif
5808#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00005809 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00005810#endif
5811};
5812
Fred Drakec9680921999-12-13 16:37:25 +00005813static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005814conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005815{
5816 return conv_confname(arg, valuep, posix_constants_pathconf,
5817 sizeof(posix_constants_pathconf)
5818 / sizeof(struct constdef));
5819}
5820#endif
5821
5822#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005823PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005824"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005825Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005826If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005827
5828static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005829posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005830{
5831 PyObject *result = NULL;
5832 int name, fd;
5833
Fred Drake12c6e2d1999-12-14 21:25:03 +00005834 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5835 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005836 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005837
Victor Stinner8c62be82010-05-06 00:08:46 +00005838 errno = 0;
5839 limit = fpathconf(fd, name);
5840 if (limit == -1 && errno != 0)
5841 posix_error();
5842 else
5843 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005844 }
5845 return result;
5846}
5847#endif
5848
5849
5850#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005851PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005852"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005853Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005854If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005855
5856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005857posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005858{
5859 PyObject *result = NULL;
5860 int name;
5861 char *path;
5862
5863 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5864 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005866
Victor Stinner8c62be82010-05-06 00:08:46 +00005867 errno = 0;
5868 limit = pathconf(path, name);
5869 if (limit == -1 && errno != 0) {
5870 if (errno == EINVAL)
5871 /* could be a path or name problem */
5872 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00005873 else
Victor Stinner8c62be82010-05-06 00:08:46 +00005874 posix_error_with_filename(path);
5875 }
5876 else
5877 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005878 }
5879 return result;
5880}
5881#endif
5882
5883#ifdef HAVE_CONFSTR
5884static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005885#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00005886 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00005887#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00005888#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005889 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00005890#endif
5891#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005892 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00005893#endif
Fred Draked86ed291999-12-15 15:34:33 +00005894#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005895 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00005896#endif
5897#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00005898 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00005899#endif
5900#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00005901 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00005902#endif
5903#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005904 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00005905#endif
Fred Drakec9680921999-12-13 16:37:25 +00005906#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005907 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005908#endif
5909#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005911#endif
5912#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005914#endif
5915#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005916 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005917#endif
5918#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005919 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005920#endif
5921#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005922 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005923#endif
5924#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005925 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005926#endif
5927#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005928 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005929#endif
Fred Draked86ed291999-12-15 15:34:33 +00005930#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00005931 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00005932#endif
Fred Drakec9680921999-12-13 16:37:25 +00005933#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00005935#endif
Fred Draked86ed291999-12-15 15:34:33 +00005936#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00005937 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00005938#endif
5939#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00005941#endif
5942#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00005944#endif
5945#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00005947#endif
Fred Drakec9680921999-12-13 16:37:25 +00005948#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005949 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005950#endif
5951#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005952 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005953#endif
5954#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005955 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005956#endif
5957#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005958 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005959#endif
5960#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005961 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005962#endif
5963#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005964 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005965#endif
5966#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005967 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005968#endif
5969#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005970 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005971#endif
5972#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005973 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005974#endif
5975#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005976 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005977#endif
5978#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005980#endif
5981#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005982 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005983#endif
5984#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005985 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005986#endif
5987#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005989#endif
5990#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005991 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005992#endif
5993#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005994 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005995#endif
Fred Draked86ed291999-12-15 15:34:33 +00005996#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00005997 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00005998#endif
5999#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006000 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006001#endif
6002#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006003 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006004#endif
6005#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006006 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006007#endif
6008#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006009 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006010#endif
6011#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006012 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006013#endif
6014#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006016#endif
6017#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006018 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006019#endif
6020#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006022#endif
6023#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006024 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006025#endif
6026#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006027 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006028#endif
6029#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006030 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006031#endif
6032#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006033 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006034#endif
Fred Drakec9680921999-12-13 16:37:25 +00006035};
6036
6037static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006038conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006039{
6040 return conv_confname(arg, valuep, posix_constants_confstr,
6041 sizeof(posix_constants_confstr)
6042 / sizeof(struct constdef));
6043}
6044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006045PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006046"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006047Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006048
6049static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006050posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006051{
6052 PyObject *result = NULL;
6053 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006054 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006055
6056 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006058
Fred Drakec9680921999-12-13 16:37:25 +00006059 errno = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006060 len = confstr(name, buffer, sizeof(buffer));
6061 if (len == 0) {
6062 if (errno) {
6063 posix_error();
6064 }
6065 else {
6066 result = Py_None;
6067 Py_INCREF(Py_None);
6068 }
Fred Drakec9680921999-12-13 16:37:25 +00006069 }
6070 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006071 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006072 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006073 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006074 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006075 }
6076 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006077 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006078 }
6079 }
6080 return result;
6081}
6082#endif
6083
6084
6085#ifdef HAVE_SYSCONF
6086static struct constdef posix_constants_sysconf[] = {
6087#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006088 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006089#endif
6090#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006091 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006092#endif
6093#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006094 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006095#endif
6096#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006097 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006098#endif
6099#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006100 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006101#endif
6102#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006103 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006104#endif
6105#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006106 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006107#endif
6108#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006109 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006110#endif
6111#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006112 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006113#endif
6114#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006115 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006116#endif
Fred Draked86ed291999-12-15 15:34:33 +00006117#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006119#endif
6120#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006122#endif
Fred Drakec9680921999-12-13 16:37:25 +00006123#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006124 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006125#endif
Fred Drakec9680921999-12-13 16:37:25 +00006126#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006127 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006128#endif
6129#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006131#endif
6132#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006133 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006134#endif
6135#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006136 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006137#endif
6138#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006139 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006140#endif
Fred Draked86ed291999-12-15 15:34:33 +00006141#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006143#endif
Fred Drakec9680921999-12-13 16:37:25 +00006144#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006145 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006146#endif
6147#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006149#endif
6150#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006151 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006152#endif
6153#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006154 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006155#endif
6156#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006158#endif
Fred Draked86ed291999-12-15 15:34:33 +00006159#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006160 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006161#endif
Fred Drakec9680921999-12-13 16:37:25 +00006162#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006164#endif
6165#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006166 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006167#endif
6168#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006169 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006170#endif
6171#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006172 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006173#endif
6174#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006175 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006176#endif
6177#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006178 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006179#endif
6180#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006182#endif
6183#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006185#endif
6186#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006188#endif
6189#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006190 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006191#endif
6192#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006193 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006194#endif
6195#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006196 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006197#endif
6198#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006199 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006200#endif
6201#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006202 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006203#endif
6204#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006205 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006206#endif
6207#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006208 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006209#endif
6210#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006211 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006212#endif
6213#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006214 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006215#endif
6216#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006217 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006218#endif
6219#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006220 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006221#endif
6222#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006223 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006224#endif
6225#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006226 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006227#endif
6228#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006229 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006230#endif
Fred Draked86ed291999-12-15 15:34:33 +00006231#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006232 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006233#endif
Fred Drakec9680921999-12-13 16:37:25 +00006234#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006235 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006236#endif
6237#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006238 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006239#endif
6240#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006241 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006242#endif
Fred Draked86ed291999-12-15 15:34:33 +00006243#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006244 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006245#endif
Fred Drakec9680921999-12-13 16:37:25 +00006246#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006247 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006248#endif
Fred Draked86ed291999-12-15 15:34:33 +00006249#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006250 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006251#endif
6252#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006253 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006254#endif
Fred Drakec9680921999-12-13 16:37:25 +00006255#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006256 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006257#endif
6258#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006259 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006260#endif
6261#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006262 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006263#endif
6264#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006265 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006266#endif
Fred Draked86ed291999-12-15 15:34:33 +00006267#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006268 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006269#endif
Fred Drakec9680921999-12-13 16:37:25 +00006270#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006271 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006272#endif
6273#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006274 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006275#endif
6276#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006278#endif
6279#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006281#endif
6282#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006283 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006284#endif
6285#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006286 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006287#endif
6288#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006289 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006290#endif
Fred Draked86ed291999-12-15 15:34:33 +00006291#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006292 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006293#endif
Fred Drakec9680921999-12-13 16:37:25 +00006294#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006295 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006296#endif
6297#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006298 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006299#endif
Fred Draked86ed291999-12-15 15:34:33 +00006300#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006301 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006302#endif
Fred Drakec9680921999-12-13 16:37:25 +00006303#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006304 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006305#endif
6306#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006307 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006308#endif
6309#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006310 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006311#endif
6312#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006314#endif
6315#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006317#endif
6318#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006319 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006320#endif
6321#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006322 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006323#endif
6324#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006325 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006326#endif
6327#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006328 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006329#endif
Fred Draked86ed291999-12-15 15:34:33 +00006330#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006331 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006332#endif
6333#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006334 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006335#endif
Fred Drakec9680921999-12-13 16:37:25 +00006336#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00006337 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006338#endif
6339#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006340 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006341#endif
6342#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006343 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006344#endif
6345#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006346 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006347#endif
6348#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006349 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006350#endif
6351#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006352 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006353#endif
6354#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00006355 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006356#endif
6357#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006359#endif
6360#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006362#endif
6363#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006364 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006365#endif
6366#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00006367 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006368#endif
6369#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006370 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006371#endif
6372#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006373 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006374#endif
6375#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00006376 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006377#endif
6378#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00006379 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006380#endif
6381#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00006382 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006383#endif
6384#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00006385 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006386#endif
6387#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006388 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006389#endif
6390#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006391 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006392#endif
6393#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00006394 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006395#endif
6396#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006397 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006398#endif
6399#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006400 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006401#endif
6402#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00006403 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006404#endif
6405#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006406 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006407#endif
6408#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006409 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006410#endif
6411#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006412 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00006413#endif
6414#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00006415 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00006416#endif
6417#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006418 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006419#endif
6420#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006421 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006422#endif
6423#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006424 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00006425#endif
6426#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006427 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006428#endif
6429#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006430 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006431#endif
6432#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006433 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006434#endif
6435#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006436 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006437#endif
6438#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006439 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006440#endif
Fred Draked86ed291999-12-15 15:34:33 +00006441#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00006442 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00006443#endif
Fred Drakec9680921999-12-13 16:37:25 +00006444#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00006445 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00006446#endif
6447#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006448 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006449#endif
6450#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00006451 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00006452#endif
6453#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006454 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006455#endif
6456#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006457 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006458#endif
6459#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006460 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006461#endif
6462#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00006463 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00006464#endif
6465#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006466 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006467#endif
6468#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006469 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006470#endif
6471#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006473#endif
6474#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006475 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006476#endif
6477#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00006479#endif
6480#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00006482#endif
6483#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00006484 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00006485#endif
6486#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006488#endif
6489#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006490 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006491#endif
6492#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006493 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006494#endif
6495#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006496 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00006497#endif
6498#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006499 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006500#endif
6501#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006502 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006503#endif
6504#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006505 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006506#endif
6507#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006508 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006509#endif
6510#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006511 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006512#endif
6513#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006514 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006515#endif
6516#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00006517 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00006518#endif
6519#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006520 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006521#endif
6522#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006523 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006524#endif
6525#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006526 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006527#endif
6528#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006529 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006530#endif
6531#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00006532 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00006533#endif
6534#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00006535 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006536#endif
6537#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00006539#endif
6540#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00006541 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006542#endif
6543#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00006544 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00006545#endif
6546#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00006547 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00006548#endif
6549#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00006550 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00006551#endif
6552#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00006553 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00006554#endif
6555#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006556 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006557#endif
6558#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00006559 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00006560#endif
6561#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00006562 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00006563#endif
6564#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006565 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006566#endif
6567#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006568 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006569#endif
6570#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00006571 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00006572#endif
6573#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00006575#endif
6576#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00006577 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00006578#endif
6579};
6580
6581static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006582conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006583{
6584 return conv_confname(arg, valuep, posix_constants_sysconf,
6585 sizeof(posix_constants_sysconf)
6586 / sizeof(struct constdef));
6587}
6588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006589PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006590"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006591Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006592
6593static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006594posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006595{
6596 PyObject *result = NULL;
6597 int name;
6598
6599 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6600 int value;
6601
6602 errno = 0;
6603 value = sysconf(name);
6604 if (value == -1 && errno != 0)
6605 posix_error();
6606 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006607 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006608 }
6609 return result;
6610}
6611#endif
6612
6613
Fred Drakebec628d1999-12-15 18:31:10 +00006614/* This code is used to ensure that the tables of configuration value names
6615 * are in sorted order as required by conv_confname(), and also to build the
6616 * the exported dictionaries that are used to publish information about the
6617 * names available on the host platform.
6618 *
6619 * Sorting the table at runtime ensures that the table is properly ordered
6620 * when used, even for platforms we're not able to test on. It also makes
6621 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006622 */
Fred Drakebec628d1999-12-15 18:31:10 +00006623
6624static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006625cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006626{
6627 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00006628 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00006629 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00006630 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00006631
6632 return strcmp(c1->name, c2->name);
6633}
6634
6635static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006636setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00006637 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006638{
Fred Drakebec628d1999-12-15 18:31:10 +00006639 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006640 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006641
6642 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6643 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006644 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006646
Barry Warsaw3155db32000-04-13 15:20:40 +00006647 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006648 PyObject *o = PyLong_FromLong(table[i].value);
6649 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6650 Py_XDECREF(o);
6651 Py_DECREF(d);
6652 return -1;
6653 }
6654 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006655 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006656 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006657}
6658
Fred Drakebec628d1999-12-15 18:31:10 +00006659/* Return -1 on failure, 0 on success. */
6660static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006661setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006662{
6663#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006664 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006665 sizeof(posix_constants_pathconf)
6666 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006667 "pathconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006668 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006669#endif
6670#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006671 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006672 sizeof(posix_constants_confstr)
6673 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006674 "confstr_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006675 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006676#endif
6677#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006678 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006679 sizeof(posix_constants_sysconf)
6680 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006681 "sysconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006683#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006684 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006685}
Fred Draked86ed291999-12-15 15:34:33 +00006686
6687
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006688PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006689"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006690Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006691in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006692
6693static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006694posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006695{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006696 abort();
6697 /*NOTREACHED*/
6698 Py_FatalError("abort() called from Python code didn't abort!");
6699 return NULL;
6700}
Fred Drakebec628d1999-12-15 18:31:10 +00006701
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006702#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006703PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006704"startfile(filepath [, operation]) - Start a file with its associated\n\
6705application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006706\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006707When \"operation\" is not specified or \"open\", this acts like\n\
6708double-clicking the file in Explorer, or giving the file name as an\n\
6709argument to the DOS \"start\" command: the file is opened with whatever\n\
6710application (if any) its extension is associated.\n\
6711When another \"operation\" is given, it specifies what should be done with\n\
6712the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006713\n\
6714startfile returns as soon as the associated application is launched.\n\
6715There is no option to wait for the application to close, and no way\n\
6716to retrieve the application's exit status.\n\
6717\n\
6718The filepath is relative to the current directory. If you want to use\n\
6719an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006720the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006721
6722static PyObject *
6723win32_startfile(PyObject *self, PyObject *args)
6724{
Victor Stinner8c62be82010-05-06 00:08:46 +00006725 PyObject *ofilepath;
6726 char *filepath;
6727 char *operation = NULL;
6728 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006729
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 PyObject *unipath, *woperation = NULL;
6731 if (!PyArg_ParseTuple(args, "U|s:startfile",
6732 &unipath, &operation)) {
6733 PyErr_Clear();
6734 goto normal;
6735 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006736
Victor Stinner8c62be82010-05-06 00:08:46 +00006737 if (operation) {
6738 woperation = PyUnicode_DecodeASCII(operation,
6739 strlen(operation), NULL);
6740 if (!woperation) {
6741 PyErr_Clear();
6742 operation = NULL;
6743 goto normal;
6744 }
6745 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006746
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 Py_BEGIN_ALLOW_THREADS
6748 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6749 PyUnicode_AS_UNICODE(unipath),
6750 NULL, NULL, SW_SHOWNORMAL);
6751 Py_END_ALLOW_THREADS
6752
6753 Py_XDECREF(woperation);
6754 if (rc <= (HINSTANCE)32) {
6755 PyObject *errval = win32_error_unicode("startfile",
6756 PyUnicode_AS_UNICODE(unipath));
6757 return errval;
6758 }
6759 Py_INCREF(Py_None);
6760 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006761
6762normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6764 PyUnicode_FSConverter, &ofilepath,
6765 &operation))
6766 return NULL;
6767 filepath = PyBytes_AsString(ofilepath);
6768 Py_BEGIN_ALLOW_THREADS
6769 rc = ShellExecute((HWND)0, operation, filepath,
6770 NULL, NULL, SW_SHOWNORMAL);
6771 Py_END_ALLOW_THREADS
6772 if (rc <= (HINSTANCE)32) {
6773 PyObject *errval = win32_error("startfile", filepath);
6774 Py_DECREF(ofilepath);
6775 return errval;
6776 }
6777 Py_DECREF(ofilepath);
6778 Py_INCREF(Py_None);
6779 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006780}
6781#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006782
Martin v. Löwis438b5342002-12-27 10:16:42 +00006783#ifdef HAVE_GETLOADAVG
6784PyDoc_STRVAR(posix_getloadavg__doc__,
6785"getloadavg() -> (float, float, float)\n\n\
6786Return the number of processes in the system run queue averaged over\n\
6787the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6788was unobtainable");
6789
6790static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006791posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006792{
6793 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006794 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006795 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6796 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00006797 } else
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00006799}
6800#endif
6801
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006802#ifdef MS_WINDOWS
6803
6804PyDoc_STRVAR(win32_urandom__doc__,
6805"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006806Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006807
6808typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6809 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6810 DWORD dwFlags );
6811typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6812 BYTE *pbBuffer );
6813
6814static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006815/* This handle is never explicitly released. Instead, the operating
6816 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006817static HCRYPTPROV hCryptProv = 0;
6818
Tim Peters4ad82172004-08-30 17:02:04 +00006819static PyObject*
6820win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006821{
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 int howMany;
6823 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006824
Victor Stinner8c62be82010-05-06 00:08:46 +00006825 /* Read arguments */
6826 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6827 return NULL;
6828 if (howMany < 0)
6829 return PyErr_Format(PyExc_ValueError,
6830 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006831
Victor Stinner8c62be82010-05-06 00:08:46 +00006832 if (hCryptProv == 0) {
6833 HINSTANCE hAdvAPI32 = NULL;
6834 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006835
Victor Stinner8c62be82010-05-06 00:08:46 +00006836 /* Obtain handle to the DLL containing CryptoAPI
6837 This should not fail */
6838 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6839 if(hAdvAPI32 == NULL)
6840 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006841
Victor Stinner8c62be82010-05-06 00:08:46 +00006842 /* Obtain pointers to the CryptoAPI functions
6843 This will fail on some early versions of Win95 */
6844 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6845 hAdvAPI32,
6846 "CryptAcquireContextA");
6847 if (pCryptAcquireContext == NULL)
6848 return PyErr_Format(PyExc_NotImplementedError,
6849 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006850
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6852 hAdvAPI32, "CryptGenRandom");
6853 if (pCryptGenRandom == NULL)
6854 return PyErr_Format(PyExc_NotImplementedError,
6855 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006856
Victor Stinner8c62be82010-05-06 00:08:46 +00006857 /* Acquire context */
6858 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6859 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6860 return win32_error("CryptAcquireContext", NULL);
6861 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006862
Victor Stinner8c62be82010-05-06 00:08:46 +00006863 /* Allocate bytes */
6864 result = PyBytes_FromStringAndSize(NULL, howMany);
6865 if (result != NULL) {
6866 /* Get random data */
6867 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
6868 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
6869 PyBytes_AS_STRING(result))) {
6870 Py_DECREF(result);
6871 return win32_error("CryptGenRandom", NULL);
6872 }
6873 }
6874 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006875}
6876#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006877
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006878PyDoc_STRVAR(device_encoding__doc__,
6879"device_encoding(fd) -> str\n\n\
6880Return a string describing the encoding of the device\n\
6881if the output is a terminal; else return None.");
6882
6883static PyObject *
6884device_encoding(PyObject *self, PyObject *args)
6885{
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 int fd;
6887 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6888 return NULL;
6889 if (!_PyVerify_fd(fd) || !isatty(fd)) {
6890 Py_INCREF(Py_None);
6891 return Py_None;
6892 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006893#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00006894 if (fd == 0) {
6895 char buf[100];
6896 sprintf(buf, "cp%d", GetConsoleCP());
6897 return PyUnicode_FromString(buf);
6898 }
6899 if (fd == 1 || fd == 2) {
6900 char buf[100];
6901 sprintf(buf, "cp%d", GetConsoleOutputCP());
6902 return PyUnicode_FromString(buf);
6903 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006904#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00006905 {
6906 char *codeset = nl_langinfo(CODESET);
6907 if (codeset != NULL && codeset[0] != 0)
6908 return PyUnicode_FromString(codeset);
6909 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006910#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 Py_INCREF(Py_None);
6912 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006913}
6914
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006915#ifdef __VMS
6916/* Use openssl random routine */
6917#include <openssl/rand.h>
6918PyDoc_STRVAR(vms_urandom__doc__,
6919"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006920Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006921
6922static PyObject*
6923vms_urandom(PyObject *self, PyObject *args)
6924{
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 int howMany;
6926 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006927
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 /* Read arguments */
6929 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6930 return NULL;
6931 if (howMany < 0)
6932 return PyErr_Format(PyExc_ValueError,
6933 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006934
Victor Stinner8c62be82010-05-06 00:08:46 +00006935 /* Allocate bytes */
6936 result = PyBytes_FromStringAndSize(NULL, howMany);
6937 if (result != NULL) {
6938 /* Get random data */
6939 if (RAND_pseudo_bytes((unsigned char*)
6940 PyBytes_AS_STRING(result),
6941 howMany) < 0) {
6942 Py_DECREF(result);
6943 return PyErr_Format(PyExc_ValueError,
6944 "RAND_pseudo_bytes");
6945 }
6946 }
6947 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006948}
6949#endif
6950
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006951#ifdef HAVE_SETRESUID
6952PyDoc_STRVAR(posix_setresuid__doc__,
6953"setresuid(ruid, euid, suid)\n\n\
6954Set the current process's real, effective, and saved user ids.");
6955
6956static PyObject*
6957posix_setresuid (PyObject *self, PyObject *args)
6958{
Victor Stinner8c62be82010-05-06 00:08:46 +00006959 /* We assume uid_t is no larger than a long. */
6960 long ruid, euid, suid;
6961 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
6962 return NULL;
6963 if (setresuid(ruid, euid, suid) < 0)
6964 return posix_error();
6965 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006966}
6967#endif
6968
6969#ifdef HAVE_SETRESGID
6970PyDoc_STRVAR(posix_setresgid__doc__,
6971"setresgid(rgid, egid, sgid)\n\n\
6972Set the current process's real, effective, and saved group ids.");
6973
6974static PyObject*
6975posix_setresgid (PyObject *self, PyObject *args)
6976{
Victor Stinner8c62be82010-05-06 00:08:46 +00006977 /* We assume uid_t is no larger than a long. */
6978 long rgid, egid, sgid;
6979 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
6980 return NULL;
6981 if (setresgid(rgid, egid, sgid) < 0)
6982 return posix_error();
6983 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006984}
6985#endif
6986
6987#ifdef HAVE_GETRESUID
6988PyDoc_STRVAR(posix_getresuid__doc__,
6989"getresuid() -> (ruid, euid, suid)\n\n\
6990Get tuple of the current process's real, effective, and saved user ids.");
6991
6992static PyObject*
6993posix_getresuid (PyObject *self, PyObject *noargs)
6994{
Victor Stinner8c62be82010-05-06 00:08:46 +00006995 uid_t ruid, euid, suid;
6996 long l_ruid, l_euid, l_suid;
6997 if (getresuid(&ruid, &euid, &suid) < 0)
6998 return posix_error();
6999 /* Force the values into long's as we don't know the size of uid_t. */
7000 l_ruid = ruid;
7001 l_euid = euid;
7002 l_suid = suid;
7003 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007004}
7005#endif
7006
7007#ifdef HAVE_GETRESGID
7008PyDoc_STRVAR(posix_getresgid__doc__,
7009"getresgid() -> (rgid, egid, sgid)\n\n\
7010Get tuple of the current process's real, effective, and saved user ids.");
7011
7012static PyObject*
7013posix_getresgid (PyObject *self, PyObject *noargs)
7014{
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 uid_t rgid, egid, sgid;
7016 long l_rgid, l_egid, l_sgid;
7017 if (getresgid(&rgid, &egid, &sgid) < 0)
7018 return posix_error();
7019 /* Force the values into long's as we don't know the size of uid_t. */
7020 l_rgid = rgid;
7021 l_egid = egid;
7022 l_sgid = sgid;
7023 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007024}
7025#endif
7026
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007027static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007028 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007029#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007031#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007033#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007034 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007035#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007037#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007038 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007039#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007040#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007042#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007043#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007044 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007045#endif /* HAVE_LCHMOD */
7046#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007047 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007048#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007049#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007050 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007051#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007052#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007053 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007054#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007055#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007057#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007058#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007060#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007061#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7063 METH_NOARGS, posix_getcwd__doc__},
7064 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7065 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007066#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007067#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007069#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007070 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7071 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7072 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007073#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007075#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007076#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007078#endif /* HAVE_READLINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007079 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7080 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7081 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
7082 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007083#ifdef HAVE_SYMLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007085#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007086#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007088#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007090#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007091 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007092#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7094 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7095 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007096#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007097 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007098#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007100#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7102 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007103#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007104#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7106 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007107#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7109 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007110#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007111#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007112#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007114#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007115#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007116 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007117#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007118#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007119 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007120#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007121#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007123#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007124#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007126#endif /* HAVE_GETEGID */
7127#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007128 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007129#endif /* HAVE_GETEUID */
7130#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007131 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007132#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007133#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007134 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007135#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007136 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007137#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007139#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007140#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007142#endif /* HAVE_GETPPID */
7143#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007145#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007146#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007148#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007149#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007151#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007152#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007154#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007155#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007157#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007158#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7160 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007161#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007162#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007163 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007164#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007165#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007166 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007167#endif /* HAVE_SETEUID */
7168#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007169 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007170#endif /* HAVE_SETEGID */
7171#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007172 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007173#endif /* HAVE_SETREUID */
7174#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007175 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007176#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007177#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007178 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007179#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007180#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007181 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007182#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007183#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007184 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007185#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007186#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007188#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007189#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007191#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007192#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007193 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007194#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007195#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007196 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007197#endif /* HAVE_WAIT3 */
7198#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007199 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007200#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007201#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007202 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007203#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007204#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007206#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007207#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007208 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007209#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007210#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007211 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007212#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007213#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007214 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007215#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007216#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007217 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007218#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7220 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7221 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7222 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7223 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7224 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7225 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7226 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7227 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7228 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7229 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007230#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007232#endif
7233#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007235#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007236#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007238#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007239#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7241 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7242 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007243#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007244#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007246#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007247#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007249#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007250#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007252#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007253 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007254#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007256#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007257#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007259#endif
7260#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007262#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007263#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007264#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007265 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007266#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007267#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007268 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007269#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007270#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007272#endif /* WIFSTOPPED */
7273#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007274 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007275#endif /* WIFSIGNALED */
7276#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007277 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007278#endif /* WIFEXITED */
7279#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007280 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007281#endif /* WEXITSTATUS */
7282#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007283 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007284#endif /* WTERMSIG */
7285#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007286 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007287#endif /* WSTOPSIG */
7288#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007289#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007290 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007291#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007292#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007293 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007294#endif
Fred Drakec9680921999-12-13 16:37:25 +00007295#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00007296 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007297#endif
7298#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007299 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007300#endif
7301#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007302 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007303#endif
7304#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007305 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007306#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007307 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007308#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007310#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007311#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007313#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007314 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007316 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007317 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007319 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007320#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007321 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007322#endif
7323#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007325#endif
7326#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007328#endif
7329#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007331#endif
7332
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007334};
7335
7336
Barry Warsaw4a342091996-12-19 23:50:02 +00007337static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007338ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007339{
Victor Stinner8c62be82010-05-06 00:08:46 +00007340 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007341}
7342
Guido van Rossumd48f2521997-12-05 22:19:34 +00007343#if defined(PYOS_OS2)
7344/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007345static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007346{
7347 APIRET rc;
7348 ULONG values[QSV_MAX+1];
7349 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007350 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007351
7352 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007353 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007354 Py_END_ALLOW_THREADS
7355
7356 if (rc != NO_ERROR) {
7357 os2_error(rc);
7358 return -1;
7359 }
7360
Fred Drake4d1e64b2002-04-15 19:40:07 +00007361 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7362 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7363 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7364 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7365 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7366 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7367 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007368
7369 switch (values[QSV_VERSION_MINOR]) {
7370 case 0: ver = "2.00"; break;
7371 case 10: ver = "2.10"; break;
7372 case 11: ver = "2.11"; break;
7373 case 30: ver = "3.00"; break;
7374 case 40: ver = "4.00"; break;
7375 case 50: ver = "5.00"; break;
7376 default:
Tim Peters885d4572001-11-28 20:27:42 +00007377 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00007378 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007379 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007380 ver = &tmp[0];
7381 }
7382
7383 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007384 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007385 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007386
7387 /* Add Indicator of Which Drive was Used to Boot the System */
7388 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7389 tmp[1] = ':';
7390 tmp[2] = '\0';
7391
Fred Drake4d1e64b2002-04-15 19:40:07 +00007392 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007393}
7394#endif
7395
Barry Warsaw4a342091996-12-19 23:50:02 +00007396static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007397all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007398{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007399#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007400 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007401#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007402#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007403 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007404#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007405#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007406 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007407#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007408#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007409 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007410#endif
Fred Drakec9680921999-12-13 16:37:25 +00007411#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007412 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00007413#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007414#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007415 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007416#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007417#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007418 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007419#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007420#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00007421 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007422#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007423#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00007424 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007425#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007426#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007427 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007428#endif
7429#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007430 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007431#endif
7432#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00007433 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007434#endif
7435#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00007436 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007437#endif
7438#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007439 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007440#endif
7441#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00007442 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007443#endif
7444#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007445 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007446#endif
7447#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007448 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007449#endif
7450#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007451 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007452#endif
7453#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007454 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007455#endif
7456#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007457 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007458#endif
7459#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00007460 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007461#endif
7462#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007463 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007464#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007465#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007466 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007467#endif
7468#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00007469 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007470#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007471#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007473#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007474#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007475 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007476#endif
7477#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007478 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007479#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007480
Tim Peters5aa91602002-01-30 05:46:57 +00007481/* MS Windows */
7482#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007483 /* Don't inherit in child processes. */
7484 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007485#endif
7486#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00007487 /* Optimize for short life (keep in memory). */
7488 /* MS forgot to define this one with a non-underscore form too. */
7489 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007490#endif
7491#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007492 /* Automatically delete when last handle is closed. */
7493 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007494#endif
7495#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00007496 /* Optimize for random access. */
7497 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007498#endif
7499#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007500 /* Optimize for sequential access. */
7501 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007502#endif
7503
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007504/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007505#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007506 /* Send a SIGIO signal whenever input or output
7507 becomes available on file descriptor */
7508 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007509#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007510#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 /* Direct disk access. */
7512 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007513#endif
7514#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00007515 /* Must be a directory. */
7516 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007517#endif
7518#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00007519 /* Do not follow links. */
7520 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007521#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007522#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007523 /* Do not update the access time. */
7524 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007525#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007526
Victor Stinner8c62be82010-05-06 00:08:46 +00007527 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007528#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007529 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007530#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007531#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007532 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007533#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007534#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007535 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007536#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007537#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007539#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007540#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00007541 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007542#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007543#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007545#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007546#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007547 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007548#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007549#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007551#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007552#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007553 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007554#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007555#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00007556 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007557#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007558#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007559 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007560#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007561#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007562 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007563#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007564#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007566#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007567#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00007568 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007569#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007570#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007572#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007573#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007574 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007575#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007576#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007578#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007579
Guido van Rossum246bc171999-02-01 23:54:31 +00007580#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007581#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00007582 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7583 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7584 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7585 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7586 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7587 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7588 if (ins(d, "P_PM", (long)P_PM)) return -1;
7589 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7590 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7591 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7592 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7593 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7594 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7595 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7596 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7597 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7598 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7599 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7600 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7601 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007602#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007603 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7604 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7605 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7606 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7607 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007608#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007609#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007610
Guido van Rossumd48f2521997-12-05 22:19:34 +00007611#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007612 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007613#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007614 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00007615}
7616
7617
Tim Peters5aa91602002-01-30 05:46:57 +00007618#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007619#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007620#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007621
7622#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007623#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007624#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007625
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007626#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007627#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007628#define MODNAME "posix"
7629#endif
7630
Martin v. Löwis1a214512008-06-11 05:26:20 +00007631static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007632 PyModuleDef_HEAD_INIT,
7633 MODNAME,
7634 posix__doc__,
7635 -1,
7636 posix_methods,
7637 NULL,
7638 NULL,
7639 NULL,
7640 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00007641};
7642
7643
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007644PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007645INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007646{
Victor Stinner8c62be82010-05-06 00:08:46 +00007647 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007648
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 m = PyModule_Create(&posixmodule);
7650 if (m == NULL)
7651 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007652
Victor Stinner8c62be82010-05-06 00:08:46 +00007653 /* Initialize environ dictionary */
7654 v = convertenviron();
7655 Py_XINCREF(v);
7656 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
7657 return NULL;
7658 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007659
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 if (all_ins(m))
7661 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007662
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 if (setup_confname_tables(m))
7664 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007665
Victor Stinner8c62be82010-05-06 00:08:46 +00007666 Py_INCREF(PyExc_OSError);
7667 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007668
Guido van Rossumb3d39562000-01-31 18:41:26 +00007669#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007670 if (posix_putenv_garbage == NULL)
7671 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007672#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007673
Victor Stinner8c62be82010-05-06 00:08:46 +00007674 if (!initialized) {
7675 stat_result_desc.name = MODNAME ".stat_result";
7676 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7677 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7678 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7679 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7680 structseq_new = StatResultType.tp_new;
7681 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007682
Victor Stinner8c62be82010-05-06 00:08:46 +00007683 statvfs_result_desc.name = MODNAME ".statvfs_result";
7684 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007685#ifdef NEED_TICKS_PER_SECOND
7686# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007688# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00007689 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007690# else
Victor Stinner8c62be82010-05-06 00:08:46 +00007691 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007692# endif
7693#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007694 }
7695 Py_INCREF((PyObject*) &StatResultType);
7696 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
7697 Py_INCREF((PyObject*) &StatVFSResultType);
7698 PyModule_AddObject(m, "statvfs_result",
7699 (PyObject*) &StatVFSResultType);
7700 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007701
7702#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00007703 /*
7704 * Step 2 of weak-linking support on Mac OS X.
7705 *
7706 * The code below removes functions that are not available on the
7707 * currently active platform.
7708 *
7709 * This block allow one to use a python binary that was build on
7710 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7711 * OSX 10.4.
7712 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007713#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00007714 if (fstatvfs == NULL) {
7715 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7716 return NULL;
7717 }
7718 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007719#endif /* HAVE_FSTATVFS */
7720
7721#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00007722 if (statvfs == NULL) {
7723 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7724 return NULL;
7725 }
7726 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007727#endif /* HAVE_STATVFS */
7728
7729# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 if (lchown == NULL) {
7731 if (PyObject_DelAttrString(m, "lchown") == -1) {
7732 return NULL;
7733 }
7734 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007735#endif /* HAVE_LCHOWN */
7736
7737
7738#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00007739 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007740
Guido van Rossumb6775db1994-08-01 11:34:53 +00007741}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007742
7743#ifdef __cplusplus
7744}
7745#endif