blob: a48f233da79641fcc0dff26fbcce768550d5e414 [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 Stinner8c62be82010-05-06 00:08:46 +0000562 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
563 PyBytes_AsString(name));
564 Py_DECREF(name);
565 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000566}
567
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000568#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000569static PyObject *
570win32_error(char* function, char* filename)
571{
Victor Stinner8c62be82010-05-06 00:08:46 +0000572 /* XXX We should pass the function name along in the future.
573 (winreg.c also wants to pass the function name.)
574 This would however require an additional param to the
575 Windows error object, which is non-trivial.
576 */
577 errno = GetLastError();
578 if (filename)
579 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
580 else
581 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000582}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000583
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000584static PyObject *
585win32_error_unicode(char* function, Py_UNICODE* filename)
586{
Victor Stinner8c62be82010-05-06 00:08:46 +0000587 /* XXX - see win32_error for comments on 'function' */
588 errno = GetLastError();
589 if (filename)
590 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
591 else
592 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000593}
594
Thomas Wouters477c8d52006-05-27 19:21:47 +0000595static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000596convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000597{
Victor Stinner8c62be82010-05-06 00:08:46 +0000598 if (PyUnicode_CheckExact(*param))
599 Py_INCREF(*param);
600 else if (PyUnicode_Check(*param))
601 /* For a Unicode subtype that's not a Unicode object,
602 return a true Unicode object with the same data. */
603 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
604 PyUnicode_GET_SIZE(*param));
605 else
606 *param = PyUnicode_FromEncodedObject(*param,
607 Py_FileSystemDefaultEncoding,
608 "strict");
609 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000610}
611
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000612#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613
Guido van Rossumd48f2521997-12-05 22:19:34 +0000614#if defined(PYOS_OS2)
615/**********************************************************************
616 * Helper Function to Trim and Format OS/2 Messages
617 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000618static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000619os2_formatmsg(char *msgbuf, int msglen, char *reason)
620{
621 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
622
623 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
624 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
625
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000626 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000627 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
628 }
629
630 /* Add Optional Reason Text */
631 if (reason) {
632 strcat(msgbuf, " : ");
633 strcat(msgbuf, reason);
634 }
635}
636
637/**********************************************************************
638 * Decode an OS/2 Operating System Error Code
639 *
640 * A convenience function to lookup an OS/2 error code and return a
641 * text message we can use to raise a Python exception.
642 *
643 * Notes:
644 * The messages for errors returned from the OS/2 kernel reside in
645 * the file OSO001.MSG in the \OS2 directory hierarchy.
646 *
647 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000648static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000649os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
650{
651 APIRET rc;
652 ULONG msglen;
653
654 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
655 Py_BEGIN_ALLOW_THREADS
656 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
657 errorcode, "oso001.msg", &msglen);
658 Py_END_ALLOW_THREADS
659
660 if (rc == NO_ERROR)
661 os2_formatmsg(msgbuf, msglen, reason);
662 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000663 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000664 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000665
666 return msgbuf;
667}
668
669/* Set an OS/2-specific error and return NULL. OS/2 kernel
670 errors are not in a global variable e.g. 'errno' nor are
671 they congruent with posix error numbers. */
672
Victor Stinner8c62be82010-05-06 00:08:46 +0000673static PyObject *
674os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000675{
676 char text[1024];
677 PyObject *v;
678
679 os2_strerror(text, sizeof(text), code, "");
680
681 v = Py_BuildValue("(is)", code, text);
682 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000683 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000684 Py_DECREF(v);
685 }
686 return NULL; /* Signal to Python that an Exception is Pending */
687}
688
689#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000690
691/* POSIX generic methods */
692
Barry Warsaw53699e91996-12-10 23:23:01 +0000693static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000694posix_fildes(PyObject *fdobj, int (*func)(int))
695{
Victor Stinner8c62be82010-05-06 00:08:46 +0000696 int fd;
697 int res;
698 fd = PyObject_AsFileDescriptor(fdobj);
699 if (fd < 0)
700 return NULL;
701 if (!_PyVerify_fd(fd))
702 return posix_error();
703 Py_BEGIN_ALLOW_THREADS
704 res = (*func)(fd);
705 Py_END_ALLOW_THREADS
706 if (res < 0)
707 return posix_error();
708 Py_INCREF(Py_None);
709 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000710}
Guido van Rossum21142a01999-01-08 21:05:37 +0000711
712static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000713posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714{
Victor Stinner8c62be82010-05-06 00:08:46 +0000715 PyObject *opath1 = NULL;
716 char *path1;
717 int res;
718 if (!PyArg_ParseTuple(args, format,
719 PyUnicode_FSConverter, &opath1))
720 return NULL;
721 path1 = PyBytes_AsString(opath1);
722 Py_BEGIN_ALLOW_THREADS
723 res = (*func)(path1);
724 Py_END_ALLOW_THREADS
725 if (res < 0)
726 return posix_error_with_allocated_filename(opath1);
727 Py_DECREF(opath1);
728 Py_INCREF(Py_None);
729 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000730}
731
Barry Warsaw53699e91996-12-10 23:23:01 +0000732static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000733posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000734 char *format,
735 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736{
Victor Stinner8c62be82010-05-06 00:08:46 +0000737 PyObject *opath1 = NULL, *opath2 = NULL;
738 char *path1, *path2;
739 int res;
740 if (!PyArg_ParseTuple(args, format,
741 PyUnicode_FSConverter, &opath1,
742 PyUnicode_FSConverter, &opath2)) {
743 return NULL;
744 }
745 path1 = PyBytes_AsString(opath1);
746 path2 = PyBytes_AsString(opath2);
747 Py_BEGIN_ALLOW_THREADS
748 res = (*func)(path1, path2);
749 Py_END_ALLOW_THREADS
750 Py_DECREF(opath1);
751 Py_DECREF(opath2);
752 if (res != 0)
753 /* XXX how to report both path1 and path2??? */
754 return posix_error();
755 Py_INCREF(Py_None);
756 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000757}
758
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000759#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000760static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000761win32_1str(PyObject* args, char* func,
762 char* format, BOOL (__stdcall *funcA)(LPCSTR),
763 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000764{
Victor Stinner8c62be82010-05-06 00:08:46 +0000765 PyObject *uni;
766 char *ansi;
767 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000768
Victor Stinner8c62be82010-05-06 00:08:46 +0000769 if (!PyArg_ParseTuple(args, wformat, &uni))
770 PyErr_Clear();
771 else {
772 Py_BEGIN_ALLOW_THREADS
773 result = funcW(PyUnicode_AsUnicode(uni));
774 Py_END_ALLOW_THREADS
775 if (!result)
776 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
777 Py_INCREF(Py_None);
778 return Py_None;
779 }
780 if (!PyArg_ParseTuple(args, format, &ansi))
781 return NULL;
782 Py_BEGIN_ALLOW_THREADS
783 result = funcA(ansi);
784 Py_END_ALLOW_THREADS
785 if (!result)
786 return win32_error(func, ansi);
787 Py_INCREF(Py_None);
788 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000789
790}
791
792/* This is a reimplementation of the C library's chdir function,
793 but one that produces Win32 errors instead of DOS error codes.
794 chdir is essentially a wrapper around SetCurrentDirectory; however,
795 it also needs to set "magic" environment variables indicating
796 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000797static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000798win32_chdir(LPCSTR path)
799{
Victor Stinner8c62be82010-05-06 00:08:46 +0000800 char new_path[MAX_PATH+1];
801 int result;
802 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000803
Victor Stinner8c62be82010-05-06 00:08:46 +0000804 if(!SetCurrentDirectoryA(path))
805 return FALSE;
806 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
807 if (!result)
808 return FALSE;
809 /* In the ANSI API, there should not be any paths longer
810 than MAX_PATH. */
811 assert(result <= MAX_PATH+1);
812 if (strncmp(new_path, "\\\\", 2) == 0 ||
813 strncmp(new_path, "//", 2) == 0)
814 /* UNC path, nothing to do. */
815 return TRUE;
816 env[1] = new_path[0];
817 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000818}
819
820/* The Unicode version differs from the ANSI version
821 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000822static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000823win32_wchdir(LPCWSTR path)
824{
Victor Stinner8c62be82010-05-06 00:08:46 +0000825 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
826 int result;
827 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828
Victor Stinner8c62be82010-05-06 00:08:46 +0000829 if(!SetCurrentDirectoryW(path))
830 return FALSE;
831 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
832 if (!result)
833 return FALSE;
834 if (result > MAX_PATH+1) {
835 new_path = malloc(result * sizeof(wchar_t));
836 if (!new_path) {
837 SetLastError(ERROR_OUTOFMEMORY);
838 return FALSE;
839 }
840 result = GetCurrentDirectoryW(result, new_path);
841 if (!result) {
842 free(new_path);
843 return FALSE;
844 }
845 }
846 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
847 wcsncmp(new_path, L"//", 2) == 0)
848 /* UNC path, nothing to do. */
849 return TRUE;
850 env[1] = new_path[0];
851 result = SetEnvironmentVariableW(env, new_path);
852 if (new_path != _new_path)
853 free(new_path);
854 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000855}
856#endif
857
Martin v. Löwis14694662006-02-03 12:54:16 +0000858#ifdef MS_WINDOWS
859/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
860 - time stamps are restricted to second resolution
861 - file modification times suffer from forth-and-back conversions between
862 UTC and local time
863 Therefore, we implement our own stat, based on the Win32 API directly.
864*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000865#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000866
867struct win32_stat{
868 int st_dev;
869 __int64 st_ino;
870 unsigned short st_mode;
871 int st_nlink;
872 int st_uid;
873 int st_gid;
874 int st_rdev;
875 __int64 st_size;
876 int st_atime;
877 int st_atime_nsec;
878 int st_mtime;
879 int st_mtime_nsec;
880 int st_ctime;
881 int st_ctime_nsec;
882};
883
884static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
885
886static void
887FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
888{
Victor Stinner8c62be82010-05-06 00:08:46 +0000889 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
890 /* Cannot simply cast and dereference in_ptr,
891 since it might not be aligned properly */
892 __int64 in;
893 memcpy(&in, in_ptr, sizeof(in));
894 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
895 /* XXX Win32 supports time stamps past 2038; we currently don't */
896 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000897}
898
Thomas Wouters477c8d52006-05-27 19:21:47 +0000899static void
900time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
901{
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 /* XXX endianness */
903 __int64 out;
904 out = time_in + secs_between_epochs;
905 out = out * 10000000 + nsec_in / 100;
906 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907}
908
Martin v. Löwis14694662006-02-03 12:54:16 +0000909/* Below, we *know* that ugo+r is 0444 */
910#if _S_IREAD != 0400
911#error Unsupported C library
912#endif
913static int
914attributes_to_mode(DWORD attr)
915{
Victor Stinner8c62be82010-05-06 00:08:46 +0000916 int m = 0;
917 if (attr & FILE_ATTRIBUTE_DIRECTORY)
918 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
919 else
920 m |= _S_IFREG;
921 if (attr & FILE_ATTRIBUTE_READONLY)
922 m |= 0444;
923 else
924 m |= 0666;
925 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000926}
927
928static int
929attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
930{
Victor Stinner8c62be82010-05-06 00:08:46 +0000931 memset(result, 0, sizeof(*result));
932 result->st_mode = attributes_to_mode(info->dwFileAttributes);
933 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
934 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
935 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
936 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000937
Victor Stinner8c62be82010-05-06 00:08:46 +0000938 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000939}
940
Guido van Rossumd8faa362007-04-27 19:54:29 +0000941static BOOL
942attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
943{
Victor Stinner8c62be82010-05-06 00:08:46 +0000944 HANDLE hFindFile;
945 WIN32_FIND_DATAA FileData;
946 hFindFile = FindFirstFileA(pszFile, &FileData);
947 if (hFindFile == INVALID_HANDLE_VALUE)
948 return FALSE;
949 FindClose(hFindFile);
950 pfad->dwFileAttributes = FileData.dwFileAttributes;
951 pfad->ftCreationTime = FileData.ftCreationTime;
952 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
953 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
954 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
955 pfad->nFileSizeLow = FileData.nFileSizeLow;
956 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000957}
958
959static BOOL
960attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
961{
Victor Stinner8c62be82010-05-06 00:08:46 +0000962 HANDLE hFindFile;
963 WIN32_FIND_DATAW FileData;
964 hFindFile = FindFirstFileW(pszFile, &FileData);
965 if (hFindFile == INVALID_HANDLE_VALUE)
966 return FALSE;
967 FindClose(hFindFile);
968 pfad->dwFileAttributes = FileData.dwFileAttributes;
969 pfad->ftCreationTime = FileData.ftCreationTime;
970 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
971 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
972 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
973 pfad->nFileSizeLow = FileData.nFileSizeLow;
974 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000975}
976
Victor Stinner8c62be82010-05-06 00:08:46 +0000977static int
Martin v. Löwis14694662006-02-03 12:54:16 +0000978win32_stat(const char* path, struct win32_stat *result)
979{
Victor Stinner8c62be82010-05-06 00:08:46 +0000980 WIN32_FILE_ATTRIBUTE_DATA info;
981 int code;
982 char *dot;
983 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
984 if (GetLastError() != ERROR_SHARING_VIOLATION) {
985 /* Protocol violation: we explicitly clear errno, instead of
986 setting it to a POSIX error. Callers should use GetLastError. */
987 errno = 0;
988 return -1;
989 } else {
990 /* Could not get attributes on open file. Fall back to
991 reading the directory. */
992 if (!attributes_from_dir(path, &info)) {
993 /* Very strange. This should not fail now */
994 errno = 0;
995 return -1;
996 }
997 }
998 }
999 code = attribute_data_to_stat(&info, result);
1000 if (code != 0)
1001 return code;
1002 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1003 dot = strrchr(path, '.');
1004 if (dot) {
1005 if (stricmp(dot, ".bat") == 0 ||
1006 stricmp(dot, ".cmd") == 0 ||
1007 stricmp(dot, ".exe") == 0 ||
1008 stricmp(dot, ".com") == 0)
1009 result->st_mode |= 0111;
1010 }
1011 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001012}
1013
Victor Stinner8c62be82010-05-06 00:08:46 +00001014static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001015win32_wstat(const wchar_t* path, struct win32_stat *result)
1016{
Victor Stinner8c62be82010-05-06 00:08:46 +00001017 int code;
1018 const wchar_t *dot;
1019 WIN32_FILE_ATTRIBUTE_DATA info;
1020 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1021 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1022 /* Protocol violation: we explicitly clear errno, instead of
1023 setting it to a POSIX error. Callers should use GetLastError. */
1024 errno = 0;
1025 return -1;
1026 } else {
1027 /* Could not get attributes on open file. Fall back to
1028 reading the directory. */
1029 if (!attributes_from_dir_w(path, &info)) {
1030 /* Very strange. This should not fail now */
1031 errno = 0;
1032 return -1;
1033 }
1034 }
1035 }
1036 code = attribute_data_to_stat(&info, result);
1037 if (code < 0)
1038 return code;
1039 /* Set IFEXEC if it is an .exe, .bat, ... */
1040 dot = wcsrchr(path, '.');
1041 if (dot) {
1042 if (_wcsicmp(dot, L".bat") == 0 ||
1043 _wcsicmp(dot, L".cmd") == 0 ||
1044 _wcsicmp(dot, L".exe") == 0 ||
1045 _wcsicmp(dot, L".com") == 0)
1046 result->st_mode |= 0111;
1047 }
1048 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001049}
1050
1051static int
1052win32_fstat(int file_number, struct win32_stat *result)
1053{
Victor Stinner8c62be82010-05-06 00:08:46 +00001054 BY_HANDLE_FILE_INFORMATION info;
1055 HANDLE h;
1056 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001057
Victor Stinner8c62be82010-05-06 00:08:46 +00001058 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001059
Victor Stinner8c62be82010-05-06 00:08:46 +00001060 /* Protocol violation: we explicitly clear errno, instead of
1061 setting it to a POSIX error. Callers should use GetLastError. */
1062 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001063
Victor Stinner8c62be82010-05-06 00:08:46 +00001064 if (h == INVALID_HANDLE_VALUE) {
1065 /* This is really a C library error (invalid file handle).
1066 We set the Win32 error to the closes one matching. */
1067 SetLastError(ERROR_INVALID_HANDLE);
1068 return -1;
1069 }
1070 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001071
Victor Stinner8c62be82010-05-06 00:08:46 +00001072 type = GetFileType(h);
1073 if (type == FILE_TYPE_UNKNOWN) {
1074 DWORD error = GetLastError();
1075 if (error != 0) {
1076 return -1;
1077 }
1078 /* else: valid but unknown file */
1079 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001080
Victor Stinner8c62be82010-05-06 00:08:46 +00001081 if (type != FILE_TYPE_DISK) {
1082 if (type == FILE_TYPE_CHAR)
1083 result->st_mode = _S_IFCHR;
1084 else if (type == FILE_TYPE_PIPE)
1085 result->st_mode = _S_IFIFO;
1086 return 0;
1087 }
1088
1089 if (!GetFileInformationByHandle(h, &info)) {
1090 return -1;
1091 }
1092
1093 /* similar to stat() */
1094 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1095 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1096 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1097 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1098 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1099 /* specific to fstat() */
1100 result->st_nlink = info.nNumberOfLinks;
1101 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1102 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001103}
1104
1105#endif /* MS_WINDOWS */
1106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001107PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001108"stat_result: Result from stat or lstat.\n\n\
1109This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001110 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001111or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1112\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001113Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1114or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001116See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001117
1118static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001119 {"st_mode", "protection bits"},
1120 {"st_ino", "inode"},
1121 {"st_dev", "device"},
1122 {"st_nlink", "number of hard links"},
1123 {"st_uid", "user ID of owner"},
1124 {"st_gid", "group ID of owner"},
1125 {"st_size", "total size, in bytes"},
1126 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1127 {NULL, "integer time of last access"},
1128 {NULL, "integer time of last modification"},
1129 {NULL, "integer time of last change"},
1130 {"st_atime", "time of last access"},
1131 {"st_mtime", "time of last modification"},
1132 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001133#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001134 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001135#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001136#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001137 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001138#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001139#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001140 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001141#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001142#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001143 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001144#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001145#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001146 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001147#endif
1148#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001149 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001150#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001151 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001152};
1153
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001154#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001155#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001156#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001157#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001158#endif
1159
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001160#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001161#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1162#else
1163#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1164#endif
1165
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001166#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001167#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1168#else
1169#define ST_RDEV_IDX ST_BLOCKS_IDX
1170#endif
1171
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001172#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1173#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1174#else
1175#define ST_FLAGS_IDX ST_RDEV_IDX
1176#endif
1177
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001178#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001179#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001180#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001181#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001182#endif
1183
1184#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1185#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1186#else
1187#define ST_BIRTHTIME_IDX ST_GEN_IDX
1188#endif
1189
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001190static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001191 "stat_result", /* name */
1192 stat_result__doc__, /* doc */
1193 stat_result_fields,
1194 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001195};
1196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001197PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001198"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1199This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001200 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001201or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001202\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001203See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001204
1205static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001206 {"f_bsize", },
1207 {"f_frsize", },
1208 {"f_blocks", },
1209 {"f_bfree", },
1210 {"f_bavail", },
1211 {"f_files", },
1212 {"f_ffree", },
1213 {"f_favail", },
1214 {"f_flag", },
1215 {"f_namemax",},
1216 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001217};
1218
1219static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001220 "statvfs_result", /* name */
1221 statvfs_result__doc__, /* doc */
1222 statvfs_result_fields,
1223 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001224};
1225
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001226static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001227static PyTypeObject StatResultType;
1228static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001229static newfunc structseq_new;
1230
1231static PyObject *
1232statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1233{
Victor Stinner8c62be82010-05-06 00:08:46 +00001234 PyStructSequence *result;
1235 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001236
Victor Stinner8c62be82010-05-06 00:08:46 +00001237 result = (PyStructSequence*)structseq_new(type, args, kwds);
1238 if (!result)
1239 return NULL;
1240 /* If we have been initialized from a tuple,
1241 st_?time might be set to None. Initialize it
1242 from the int slots. */
1243 for (i = 7; i <= 9; i++) {
1244 if (result->ob_item[i+3] == Py_None) {
1245 Py_DECREF(Py_None);
1246 Py_INCREF(result->ob_item[i]);
1247 result->ob_item[i+3] = result->ob_item[i];
1248 }
1249 }
1250 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001251}
1252
1253
1254
1255/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001256static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001257
1258PyDoc_STRVAR(stat_float_times__doc__,
1259"stat_float_times([newval]) -> oldval\n\n\
1260Determine whether os.[lf]stat represents time stamps as float objects.\n\
1261If newval is True, future calls to stat() return floats, if it is False,\n\
1262future calls return ints. \n\
1263If newval is omitted, return the current setting.\n");
1264
1265static PyObject*
1266stat_float_times(PyObject* self, PyObject *args)
1267{
Victor Stinner8c62be82010-05-06 00:08:46 +00001268 int newval = -1;
1269 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1270 return NULL;
1271 if (newval == -1)
1272 /* Return old value */
1273 return PyBool_FromLong(_stat_float_times);
1274 _stat_float_times = newval;
1275 Py_INCREF(Py_None);
1276 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001277}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001278
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001279static void
1280fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1281{
Victor Stinner8c62be82010-05-06 00:08:46 +00001282 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001283#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001284 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001285#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001286 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001287#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001288 if (!ival)
1289 return;
1290 if (_stat_float_times) {
1291 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1292 } else {
1293 fval = ival;
1294 Py_INCREF(fval);
1295 }
1296 PyStructSequence_SET_ITEM(v, index, ival);
1297 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001298}
1299
Tim Peters5aa91602002-01-30 05:46:57 +00001300/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001301 (used by posix_stat() and posix_fstat()) */
1302static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001303_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001304{
Victor Stinner8c62be82010-05-06 00:08:46 +00001305 unsigned long ansec, mnsec, cnsec;
1306 PyObject *v = PyStructSequence_New(&StatResultType);
1307 if (v == NULL)
1308 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001309
Victor Stinner8c62be82010-05-06 00:08:46 +00001310 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001311#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001312 PyStructSequence_SET_ITEM(v, 1,
1313 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001314#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001315 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001316#endif
1317#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001318 PyStructSequence_SET_ITEM(v, 2,
1319 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001320#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001321 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001322#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001323 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1324 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1325 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001326#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001327 PyStructSequence_SET_ITEM(v, 6,
1328 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001329#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001330 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001331#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001332
Martin v. Löwis14694662006-02-03 12:54:16 +00001333#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001334 ansec = st->st_atim.tv_nsec;
1335 mnsec = st->st_mtim.tv_nsec;
1336 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001337#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001338 ansec = st->st_atimespec.tv_nsec;
1339 mnsec = st->st_mtimespec.tv_nsec;
1340 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001341#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001342 ansec = st->st_atime_nsec;
1343 mnsec = st->st_mtime_nsec;
1344 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001345#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001346 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001347#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001348 fill_time(v, 7, st->st_atime, ansec);
1349 fill_time(v, 8, st->st_mtime, mnsec);
1350 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001351
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001352#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001353 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1354 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001355#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001356#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001357 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1358 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001360#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001361 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1362 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001363#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001364#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001365 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1366 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001367#endif
1368#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001369 {
1370 PyObject *val;
1371 unsigned long bsec,bnsec;
1372 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001373#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001374 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001375#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001376 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001377#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001378 if (_stat_float_times) {
1379 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1380 } else {
1381 val = PyLong_FromLong((long)bsec);
1382 }
1383 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1384 val);
1385 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001386#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001387#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001388 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1389 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001390#endif
Fred Drake699f3522000-06-29 21:12:41 +00001391
Victor Stinner8c62be82010-05-06 00:08:46 +00001392 if (PyErr_Occurred()) {
1393 Py_DECREF(v);
1394 return NULL;
1395 }
Fred Drake699f3522000-06-29 21:12:41 +00001396
Victor Stinner8c62be82010-05-06 00:08:46 +00001397 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001398}
1399
Martin v. Löwisd8948722004-06-02 09:57:56 +00001400#ifdef MS_WINDOWS
1401
1402/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1403 where / can be used in place of \ and the trailing slash is optional.
1404 Both SERVER and SHARE must have at least one character.
1405*/
1406
1407#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1408#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001409#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001410#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001411#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001412
Tim Peters4ad82172004-08-30 17:02:04 +00001413static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001414IsUNCRootA(char *path, int pathlen)
1415{
Victor Stinner8c62be82010-05-06 00:08:46 +00001416 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001417
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001419
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1421 /* minimum UNCRoot is \\x\y */
1422 return FALSE;
1423 for (i = 2; i < pathlen ; i++)
1424 if (ISSLASH(path[i])) break;
1425 if (i == 2 || i == pathlen)
1426 /* do not allow \\\SHARE or \\SERVER */
1427 return FALSE;
1428 share = i+1;
1429 for (i = share; i < pathlen; i++)
1430 if (ISSLASH(path[i])) break;
1431 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001432
Victor Stinner8c62be82010-05-06 00:08:46 +00001433 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001434}
1435
Tim Peters4ad82172004-08-30 17:02:04 +00001436static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001437IsUNCRootW(Py_UNICODE *path, int pathlen)
1438{
Victor Stinner8c62be82010-05-06 00:08:46 +00001439 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001440
Victor Stinner8c62be82010-05-06 00:08:46 +00001441 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001442
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1444 /* minimum UNCRoot is \\x\y */
1445 return FALSE;
1446 for (i = 2; i < pathlen ; i++)
1447 if (ISSLASH(path[i])) break;
1448 if (i == 2 || i == pathlen)
1449 /* do not allow \\\SHARE or \\SERVER */
1450 return FALSE;
1451 share = i+1;
1452 for (i = share; i < pathlen; i++)
1453 if (ISSLASH(path[i])) break;
1454 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001455
Victor Stinner8c62be82010-05-06 00:08:46 +00001456 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001457}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001458#endif /* MS_WINDOWS */
1459
Barry Warsaw53699e91996-12-10 23:23:01 +00001460static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001461posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001463#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001464 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001465#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001466 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001467#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 char *wformat,
1469 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001470{
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 STRUCT_STAT st;
1472 PyObject *opath;
1473 char *path;
1474 int res;
1475 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001476
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001477#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001478 PyUnicodeObject *po;
1479 if (PyArg_ParseTuple(args, wformat, &po)) {
1480 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001481
Victor Stinner8c62be82010-05-06 00:08:46 +00001482 Py_BEGIN_ALLOW_THREADS
1483 /* PyUnicode_AS_UNICODE result OK without
1484 thread lock as it is a simple dereference. */
1485 res = wstatfunc(wpath, &st);
1486 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001487
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 if (res != 0)
1489 return win32_error_unicode("stat", wpath);
1490 return _pystat_fromstructstat(&st);
1491 }
1492 /* Drop the argument parsing error as narrow strings
1493 are also valid. */
1494 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001495#endif
1496
Victor Stinner8c62be82010-05-06 00:08:46 +00001497 if (!PyArg_ParseTuple(args, format,
1498 PyUnicode_FSConverter, &opath))
1499 return NULL;
1500 path = PyBytes_AsString(opath);
1501 Py_BEGIN_ALLOW_THREADS
1502 res = (*statfunc)(path, &st);
1503 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001504
Victor Stinner8c62be82010-05-06 00:08:46 +00001505 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001506#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001507 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001508#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001509 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001510#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001511 }
1512 else
1513 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001514
Victor Stinner8c62be82010-05-06 00:08:46 +00001515 Py_DECREF(opath);
1516 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001517}
1518
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519/* POSIX methods */
1520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001521PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001522"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001523Use the real uid/gid to test for access to a path. Note that most\n\
1524operations will use the effective uid/gid, therefore this routine can\n\
1525be used in a suid/sgid environment to test if the invoking user has the\n\
1526specified access to the path. The mode argument can be F_OK to test\n\
1527existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001528
1529static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001530posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001531{
Victor Stinner8c62be82010-05-06 00:08:46 +00001532 PyObject *opath;
1533 char *path;
1534 int mode;
1535
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001536#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001537 DWORD attr;
1538 PyUnicodeObject *po;
1539 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1540 Py_BEGIN_ALLOW_THREADS
1541 /* PyUnicode_AS_UNICODE OK without thread lock as
1542 it is a simple dereference. */
1543 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1544 Py_END_ALLOW_THREADS
1545 goto finish;
1546 }
1547 /* Drop the argument parsing error as narrow strings
1548 are also valid. */
1549 PyErr_Clear();
1550 if (!PyArg_ParseTuple(args, "O&i:access",
1551 PyUnicode_FSConverter, &opath, &mode))
1552 return NULL;
1553 path = PyBytes_AsString(opath);
1554 Py_BEGIN_ALLOW_THREADS
1555 attr = GetFileAttributesA(path);
1556 Py_END_ALLOW_THREADS
1557 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001558finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001559 if (attr == 0xFFFFFFFF)
1560 /* File does not exist, or cannot read attributes */
1561 return PyBool_FromLong(0);
1562 /* Access is possible if either write access wasn't requested, or
1563 the file isn't read-only, or if it's a directory, as there are
1564 no read-only directories on Windows. */
1565 return PyBool_FromLong(!(mode & 2)
1566 || !(attr & FILE_ATTRIBUTE_READONLY)
1567 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001568#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001569 int res;
1570 if (!PyArg_ParseTuple(args, "O&i:access",
1571 PyUnicode_FSConverter, &opath, &mode))
1572 return NULL;
1573 path = PyBytes_AsString(opath);
1574 Py_BEGIN_ALLOW_THREADS
1575 res = access(path, mode);
1576 Py_END_ALLOW_THREADS
1577 Py_DECREF(opath);
1578 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001579#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001580}
1581
Guido van Rossumd371ff11999-01-25 16:12:23 +00001582#ifndef F_OK
1583#define F_OK 0
1584#endif
1585#ifndef R_OK
1586#define R_OK 4
1587#endif
1588#ifndef W_OK
1589#define W_OK 2
1590#endif
1591#ifndef X_OK
1592#define X_OK 1
1593#endif
1594
1595#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001596PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001597"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001599
1600static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001601posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001602{
Victor Stinner8c62be82010-05-06 00:08:46 +00001603 int id;
1604 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001605
Victor Stinner8c62be82010-05-06 00:08:46 +00001606 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1607 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001608
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001609#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001610 /* file descriptor 0 only, the default input device (stdin) */
1611 if (id == 0) {
1612 ret = ttyname();
1613 }
1614 else {
1615 ret = NULL;
1616 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001617#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001619#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001620 if (ret == NULL)
1621 return posix_error();
1622 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001623}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001624#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001625
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001626#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001627PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001628"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001630
1631static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001632posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001633{
Victor Stinner8c62be82010-05-06 00:08:46 +00001634 char *ret;
1635 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001636
Greg Wardb48bc172000-03-01 21:51:56 +00001637#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001638 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001639#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001640 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001641#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001642 if (ret == NULL)
1643 return posix_error();
1644 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001645}
1646#endif
1647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001648PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001649"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001651
Barry Warsaw53699e91996-12-10 23:23:01 +00001652static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001653posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001654{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001655#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001656 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001657#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001658 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001659#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001661#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001663#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001664}
1665
Fred Drake4d1e64b2002-04-15 19:40:07 +00001666#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001667PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001668"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001669Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001670opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001671
1672static PyObject *
1673posix_fchdir(PyObject *self, PyObject *fdobj)
1674{
Victor Stinner8c62be82010-05-06 00:08:46 +00001675 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001676}
1677#endif /* HAVE_FCHDIR */
1678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001680PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001681"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001682Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001683
Barry Warsaw53699e91996-12-10 23:23:01 +00001684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001685posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001686{
Victor Stinner8c62be82010-05-06 00:08:46 +00001687 PyObject *opath = NULL;
1688 char *path = NULL;
1689 int i;
1690 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001691#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001692 DWORD attr;
1693 PyUnicodeObject *po;
1694 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1695 Py_BEGIN_ALLOW_THREADS
1696 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1697 if (attr != 0xFFFFFFFF) {
1698 if (i & _S_IWRITE)
1699 attr &= ~FILE_ATTRIBUTE_READONLY;
1700 else
1701 attr |= FILE_ATTRIBUTE_READONLY;
1702 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1703 }
1704 else
1705 res = 0;
1706 Py_END_ALLOW_THREADS
1707 if (!res)
1708 return win32_error_unicode("chmod",
1709 PyUnicode_AS_UNICODE(po));
1710 Py_INCREF(Py_None);
1711 return Py_None;
1712 }
1713 /* Drop the argument parsing error as narrow strings
1714 are also valid. */
1715 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001716
Victor Stinner8c62be82010-05-06 00:08:46 +00001717 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1718 &opath, &i))
1719 return NULL;
1720 path = PyBytes_AsString(opath);
1721 Py_BEGIN_ALLOW_THREADS
1722 attr = GetFileAttributesA(path);
1723 if (attr != 0xFFFFFFFF) {
1724 if (i & _S_IWRITE)
1725 attr &= ~FILE_ATTRIBUTE_READONLY;
1726 else
1727 attr |= FILE_ATTRIBUTE_READONLY;
1728 res = SetFileAttributesA(path, attr);
1729 }
1730 else
1731 res = 0;
1732 Py_END_ALLOW_THREADS
1733 if (!res) {
1734 win32_error("chmod", path);
1735 Py_DECREF(opath);
1736 return NULL;
1737 }
1738 Py_DECREF(opath);
1739 Py_INCREF(Py_None);
1740 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001741#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001742 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1743 &opath, &i))
1744 return NULL;
1745 path = PyBytes_AsString(opath);
1746 Py_BEGIN_ALLOW_THREADS
1747 res = chmod(path, i);
1748 Py_END_ALLOW_THREADS
1749 if (res < 0)
1750 return posix_error_with_allocated_filename(opath);
1751 Py_DECREF(opath);
1752 Py_INCREF(Py_None);
1753 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001754#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001755}
1756
Christian Heimes4e30a842007-11-30 22:12:06 +00001757#ifdef HAVE_FCHMOD
1758PyDoc_STRVAR(posix_fchmod__doc__,
1759"fchmod(fd, mode)\n\n\
1760Change the access permissions of the file given by file\n\
1761descriptor fd.");
1762
1763static PyObject *
1764posix_fchmod(PyObject *self, PyObject *args)
1765{
Victor Stinner8c62be82010-05-06 00:08:46 +00001766 int fd, mode, res;
1767 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1768 return NULL;
1769 Py_BEGIN_ALLOW_THREADS
1770 res = fchmod(fd, mode);
1771 Py_END_ALLOW_THREADS
1772 if (res < 0)
1773 return posix_error();
1774 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001775}
1776#endif /* HAVE_FCHMOD */
1777
1778#ifdef HAVE_LCHMOD
1779PyDoc_STRVAR(posix_lchmod__doc__,
1780"lchmod(path, mode)\n\n\
1781Change the access permissions of a file. If path is a symlink, this\n\
1782affects the link itself rather than the target.");
1783
1784static PyObject *
1785posix_lchmod(PyObject *self, PyObject *args)
1786{
Victor Stinner8c62be82010-05-06 00:08:46 +00001787 PyObject *opath;
1788 char *path;
1789 int i;
1790 int res;
1791 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1792 &opath, &i))
1793 return NULL;
1794 path = PyBytes_AsString(opath);
1795 Py_BEGIN_ALLOW_THREADS
1796 res = lchmod(path, i);
1797 Py_END_ALLOW_THREADS
1798 if (res < 0)
1799 return posix_error_with_allocated_filename(opath);
1800 Py_DECREF(opath);
1801 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001802}
1803#endif /* HAVE_LCHMOD */
1804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001805
Thomas Wouterscf297e42007-02-23 15:07:44 +00001806#ifdef HAVE_CHFLAGS
1807PyDoc_STRVAR(posix_chflags__doc__,
1808"chflags(path, flags)\n\n\
1809Set file flags.");
1810
1811static PyObject *
1812posix_chflags(PyObject *self, PyObject *args)
1813{
Victor Stinner8c62be82010-05-06 00:08:46 +00001814 PyObject *opath;
1815 char *path;
1816 unsigned long flags;
1817 int res;
1818 if (!PyArg_ParseTuple(args, "O&k:chflags",
1819 PyUnicode_FSConverter, &opath, &flags))
1820 return NULL;
1821 path = PyBytes_AsString(opath);
1822 Py_BEGIN_ALLOW_THREADS
1823 res = chflags(path, flags);
1824 Py_END_ALLOW_THREADS
1825 if (res < 0)
1826 return posix_error_with_allocated_filename(opath);
1827 Py_DECREF(opath);
1828 Py_INCREF(Py_None);
1829 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001830}
1831#endif /* HAVE_CHFLAGS */
1832
1833#ifdef HAVE_LCHFLAGS
1834PyDoc_STRVAR(posix_lchflags__doc__,
1835"lchflags(path, flags)\n\n\
1836Set file flags.\n\
1837This function will not follow symbolic links.");
1838
1839static PyObject *
1840posix_lchflags(PyObject *self, PyObject *args)
1841{
Victor Stinner8c62be82010-05-06 00:08:46 +00001842 PyObject *opath;
1843 char *path;
1844 unsigned long flags;
1845 int res;
1846 if (!PyArg_ParseTuple(args, "O&k:lchflags",
1847 PyUnicode_FSConverter, &opath, &flags))
1848 return NULL;
1849 path = PyBytes_AsString(opath);
1850 Py_BEGIN_ALLOW_THREADS
1851 res = lchflags(path, flags);
1852 Py_END_ALLOW_THREADS
1853 if (res < 0)
1854 return posix_error_with_allocated_filename(opath);
1855 Py_DECREF(opath);
1856 Py_INCREF(Py_None);
1857 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001858}
1859#endif /* HAVE_LCHFLAGS */
1860
Martin v. Löwis244edc82001-10-04 22:44:26 +00001861#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001862PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001863"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001864Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001865
1866static PyObject *
1867posix_chroot(PyObject *self, PyObject *args)
1868{
Victor Stinner8c62be82010-05-06 00:08:46 +00001869 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001870}
1871#endif
1872
Guido van Rossum21142a01999-01-08 21:05:37 +00001873#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001875"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001877
1878static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001879posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001880{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001881 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001882}
1883#endif /* HAVE_FSYNC */
1884
1885#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001886
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001887#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001888extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1889#endif
1890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001891PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001892"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001893force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001895
1896static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001897posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001898{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001899 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001900}
1901#endif /* HAVE_FDATASYNC */
1902
1903
Fredrik Lundh10723342000-07-10 16:38:09 +00001904#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001905PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001906"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001907Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001908
Barry Warsaw53699e91996-12-10 23:23:01 +00001909static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001910posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001911{
Victor Stinner8c62be82010-05-06 00:08:46 +00001912 PyObject *opath;
1913 char *path;
1914 long uid, gid;
1915 int res;
1916 if (!PyArg_ParseTuple(args, "O&ll:chown",
1917 PyUnicode_FSConverter, &opath,
1918 &uid, &gid))
1919 return NULL;
1920 path = PyBytes_AsString(opath);
1921 Py_BEGIN_ALLOW_THREADS
1922 res = chown(path, (uid_t) uid, (gid_t) gid);
1923 Py_END_ALLOW_THREADS
1924 if (res < 0)
1925 return posix_error_with_allocated_filename(opath);
1926 Py_DECREF(opath);
1927 Py_INCREF(Py_None);
1928 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001929}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001930#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001931
Christian Heimes4e30a842007-11-30 22:12:06 +00001932#ifdef HAVE_FCHOWN
1933PyDoc_STRVAR(posix_fchown__doc__,
1934"fchown(fd, uid, gid)\n\n\
1935Change the owner and group id of the file given by file descriptor\n\
1936fd to the numeric uid and gid.");
1937
1938static PyObject *
1939posix_fchown(PyObject *self, PyObject *args)
1940{
Victor Stinner8c62be82010-05-06 00:08:46 +00001941 int fd;
1942 long uid, gid;
1943 int res;
1944 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
1945 return NULL;
1946 Py_BEGIN_ALLOW_THREADS
1947 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1948 Py_END_ALLOW_THREADS
1949 if (res < 0)
1950 return posix_error();
1951 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001952}
1953#endif /* HAVE_FCHOWN */
1954
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001955#ifdef HAVE_LCHOWN
1956PyDoc_STRVAR(posix_lchown__doc__,
1957"lchown(path, uid, gid)\n\n\
1958Change the owner and group id of path to the numeric uid and gid.\n\
1959This function will not follow symbolic links.");
1960
1961static PyObject *
1962posix_lchown(PyObject *self, PyObject *args)
1963{
Victor Stinner8c62be82010-05-06 00:08:46 +00001964 PyObject *opath;
1965 char *path;
1966 long uid, gid;
1967 int res;
1968 if (!PyArg_ParseTuple(args, "O&ll:lchown",
1969 PyUnicode_FSConverter, &opath,
1970 &uid, &gid))
1971 return NULL;
1972 path = PyBytes_AsString(opath);
1973 Py_BEGIN_ALLOW_THREADS
1974 res = lchown(path, (uid_t) uid, (gid_t) gid);
1975 Py_END_ALLOW_THREADS
1976 if (res < 0)
1977 return posix_error_with_allocated_filename(opath);
1978 Py_DECREF(opath);
1979 Py_INCREF(Py_None);
1980 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001981}
1982#endif /* HAVE_LCHOWN */
1983
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001984
Guido van Rossum36bc6801995-06-14 22:54:23 +00001985#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00001986static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001987posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001988{
Victor Stinner8c62be82010-05-06 00:08:46 +00001989 char buf[1026];
1990 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001991
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001992#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001993 if (!use_bytes) {
1994 wchar_t wbuf[1026];
1995 wchar_t *wbuf2 = wbuf;
1996 PyObject *resobj;
1997 DWORD len;
1998 Py_BEGIN_ALLOW_THREADS
1999 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2000 /* If the buffer is large enough, len does not include the
2001 terminating \0. If the buffer is too small, len includes
2002 the space needed for the terminator. */
2003 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2004 wbuf2 = malloc(len * sizeof(wchar_t));
2005 if (wbuf2)
2006 len = GetCurrentDirectoryW(len, wbuf2);
2007 }
2008 Py_END_ALLOW_THREADS
2009 if (!wbuf2) {
2010 PyErr_NoMemory();
2011 return NULL;
2012 }
2013 if (!len) {
2014 if (wbuf2 != wbuf) free(wbuf2);
2015 return win32_error("getcwdu", NULL);
2016 }
2017 resobj = PyUnicode_FromWideChar(wbuf2, len);
2018 if (wbuf2 != wbuf) free(wbuf2);
2019 return resobj;
2020 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002021#endif
2022
Victor Stinner8c62be82010-05-06 00:08:46 +00002023 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002024#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002025 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002026#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002027 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002028#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002029 Py_END_ALLOW_THREADS
2030 if (res == NULL)
2031 return posix_error();
2032 if (use_bytes)
2033 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002034 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002035}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002036
2037PyDoc_STRVAR(posix_getcwd__doc__,
2038"getcwd() -> path\n\n\
2039Return a unicode string representing the current working directory.");
2040
2041static PyObject *
2042posix_getcwd_unicode(PyObject *self)
2043{
2044 return posix_getcwd(0);
2045}
2046
2047PyDoc_STRVAR(posix_getcwdb__doc__,
2048"getcwdb() -> path\n\n\
2049Return a bytes string representing the current working directory.");
2050
2051static PyObject *
2052posix_getcwd_bytes(PyObject *self)
2053{
2054 return posix_getcwd(1);
2055}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002056#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002057
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002058
Guido van Rossumb6775db1994-08-01 11:34:53 +00002059#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002060PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002061"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002063
Barry Warsaw53699e91996-12-10 23:23:01 +00002064static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002065posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002066{
Victor Stinner8c62be82010-05-06 00:08:46 +00002067 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002068}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002069#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002073"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002074Return a list containing the names of the entries in the directory.\n\
2075\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00002076 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002077\n\
2078The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002079entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002080
Barry Warsaw53699e91996-12-10 23:23:01 +00002081static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002082posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002083{
Victor Stinner8c62be82010-05-06 00:08:46 +00002084 /* XXX Should redo this putting the (now four) versions of opendir
2085 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002086#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002087
Victor Stinner8c62be82010-05-06 00:08:46 +00002088 PyObject *d, *v;
2089 HANDLE hFindFile;
2090 BOOL result;
2091 WIN32_FIND_DATA FileData;
2092 PyObject *opath;
2093 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2094 char *bufptr = namebuf;
2095 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002096
Victor Stinner8c62be82010-05-06 00:08:46 +00002097 PyObject *po;
2098 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2099 WIN32_FIND_DATAW wFileData;
2100 Py_UNICODE *wnamebuf;
2101 /* Overallocate for \\*.*\0 */
2102 len = PyUnicode_GET_SIZE(po);
2103 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2104 if (!wnamebuf) {
2105 PyErr_NoMemory();
2106 return NULL;
2107 }
2108 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2109 if (len > 0) {
2110 Py_UNICODE wch = wnamebuf[len-1];
2111 if (wch != L'/' && wch != L'\\' && wch != L':')
2112 wnamebuf[len++] = L'\\';
2113 wcscpy(wnamebuf + len, L"*.*");
2114 }
2115 if ((d = PyList_New(0)) == NULL) {
2116 free(wnamebuf);
2117 return NULL;
2118 }
2119 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2120 if (hFindFile == INVALID_HANDLE_VALUE) {
2121 int error = GetLastError();
2122 if (error == ERROR_FILE_NOT_FOUND) {
2123 free(wnamebuf);
2124 return d;
2125 }
2126 Py_DECREF(d);
2127 win32_error_unicode("FindFirstFileW", wnamebuf);
2128 free(wnamebuf);
2129 return NULL;
2130 }
2131 do {
2132 /* Skip over . and .. */
2133 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2134 wcscmp(wFileData.cFileName, L"..") != 0) {
2135 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2136 if (v == NULL) {
2137 Py_DECREF(d);
2138 d = NULL;
2139 break;
2140 }
2141 if (PyList_Append(d, v) != 0) {
2142 Py_DECREF(v);
2143 Py_DECREF(d);
2144 d = NULL;
2145 break;
2146 }
2147 Py_DECREF(v);
2148 }
2149 Py_BEGIN_ALLOW_THREADS
2150 result = FindNextFileW(hFindFile, &wFileData);
2151 Py_END_ALLOW_THREADS
2152 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2153 it got to the end of the directory. */
2154 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2155 Py_DECREF(d);
2156 win32_error_unicode("FindNextFileW", wnamebuf);
2157 FindClose(hFindFile);
2158 free(wnamebuf);
2159 return NULL;
2160 }
2161 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002162
Victor Stinner8c62be82010-05-06 00:08:46 +00002163 if (FindClose(hFindFile) == FALSE) {
2164 Py_DECREF(d);
2165 win32_error_unicode("FindClose", wnamebuf);
2166 free(wnamebuf);
2167 return NULL;
2168 }
2169 free(wnamebuf);
2170 return d;
2171 }
2172 /* Drop the argument parsing error as narrow strings
2173 are also valid. */
2174 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002175
Victor Stinner8c62be82010-05-06 00:08:46 +00002176 if (!PyArg_ParseTuple(args, "O&:listdir",
2177 PyUnicode_FSConverter, &opath))
2178 return NULL;
2179 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2180 PyErr_SetString(PyExc_ValueError, "path too long");
2181 Py_DECREF(opath);
2182 return NULL;
2183 }
2184 strcpy(namebuf, PyBytes_AsString(opath));
2185 len = PyObject_Size(opath);
2186 if (len > 0) {
2187 char ch = namebuf[len-1];
2188 if (ch != SEP && ch != ALTSEP && ch != ':')
2189 namebuf[len++] = '/';
2190 strcpy(namebuf + len, "*.*");
2191 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002192
Victor Stinner8c62be82010-05-06 00:08:46 +00002193 if ((d = PyList_New(0)) == NULL)
2194 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002195
Victor Stinner8c62be82010-05-06 00:08:46 +00002196 hFindFile = FindFirstFile(namebuf, &FileData);
2197 if (hFindFile == INVALID_HANDLE_VALUE) {
2198 int error = GetLastError();
2199 if (error == ERROR_FILE_NOT_FOUND)
2200 return d;
2201 Py_DECREF(d);
2202 return win32_error("FindFirstFile", namebuf);
2203 }
2204 do {
2205 /* Skip over . and .. */
2206 if (strcmp(FileData.cFileName, ".") != 0 &&
2207 strcmp(FileData.cFileName, "..") != 0) {
2208 v = PyBytes_FromString(FileData.cFileName);
2209 if (v == NULL) {
2210 Py_DECREF(d);
2211 d = NULL;
2212 break;
2213 }
2214 if (PyList_Append(d, v) != 0) {
2215 Py_DECREF(v);
2216 Py_DECREF(d);
2217 d = NULL;
2218 break;
2219 }
2220 Py_DECREF(v);
2221 }
2222 Py_BEGIN_ALLOW_THREADS
2223 result = FindNextFile(hFindFile, &FileData);
2224 Py_END_ALLOW_THREADS
2225 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2226 it got to the end of the directory. */
2227 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2228 Py_DECREF(d);
2229 win32_error("FindNextFile", namebuf);
2230 FindClose(hFindFile);
2231 return NULL;
2232 }
2233 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002234
Victor Stinner8c62be82010-05-06 00:08:46 +00002235 if (FindClose(hFindFile) == FALSE) {
2236 Py_DECREF(d);
2237 return win32_error("FindClose", namebuf);
2238 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002239
Victor Stinner8c62be82010-05-06 00:08:46 +00002240 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002241
Tim Peters0bb44a42000-09-15 07:44:49 +00002242#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002243
2244#ifndef MAX_PATH
2245#define MAX_PATH CCHMAXPATH
2246#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002247 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002248 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002249 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002250 PyObject *d, *v;
2251 char namebuf[MAX_PATH+5];
2252 HDIR hdir = 1;
2253 ULONG srchcnt = 1;
2254 FILEFINDBUF3 ep;
2255 APIRET rc;
2256
Victor Stinner8c62be82010-05-06 00:08:46 +00002257 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002258 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002259 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002260 name = PyBytes_AsString(oname);
2261 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002262 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002263 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002264 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002265 return NULL;
2266 }
2267 strcpy(namebuf, name);
2268 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002269 if (*pt == ALTSEP)
2270 *pt = SEP;
2271 if (namebuf[len-1] != SEP)
2272 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002273 strcpy(namebuf + len, "*.*");
2274
Neal Norwitz6c913782007-10-14 03:23:09 +00002275 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002276 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002277 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002278 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002279
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002280 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2281 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002282 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002283 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2284 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2285 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002286
2287 if (rc != NO_ERROR) {
2288 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002289 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002290 }
2291
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002292 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002293 do {
2294 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002295 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002296 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002297
2298 strcpy(namebuf, ep.achName);
2299
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002300 /* Leave Case of Name Alone -- In Native Form */
2301 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002302
Christian Heimes72b710a2008-05-26 13:28:38 +00002303 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002304 if (v == NULL) {
2305 Py_DECREF(d);
2306 d = NULL;
2307 break;
2308 }
2309 if (PyList_Append(d, v) != 0) {
2310 Py_DECREF(v);
2311 Py_DECREF(d);
2312 d = NULL;
2313 break;
2314 }
2315 Py_DECREF(v);
2316 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2317 }
2318
Victor Stinnerdcb24032010-04-22 12:08:36 +00002319 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002320 return d;
2321#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002322 PyObject *oname;
2323 char *name;
2324 PyObject *d, *v;
2325 DIR *dirp;
2326 struct dirent *ep;
2327 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002328
Victor Stinner8c62be82010-05-06 00:08:46 +00002329 errno = 0;
2330 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2331 arg_is_unicode = 0;
2332 PyErr_Clear();
2333 }
2334 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
2335 return NULL;
2336 name = PyBytes_AsString(oname);
2337 if ((dirp = opendir(name)) == NULL) {
2338 return posix_error_with_allocated_filename(oname);
2339 }
2340 if ((d = PyList_New(0)) == NULL) {
2341 closedir(dirp);
2342 Py_DECREF(oname);
2343 return NULL;
2344 }
2345 for (;;) {
2346 errno = 0;
2347 Py_BEGIN_ALLOW_THREADS
2348 ep = readdir(dirp);
2349 Py_END_ALLOW_THREADS
2350 if (ep == NULL) {
2351 if (errno == 0) {
2352 break;
2353 } else {
2354 closedir(dirp);
2355 Py_DECREF(d);
2356 return posix_error_with_allocated_filename(oname);
2357 }
2358 }
2359 if (ep->d_name[0] == '.' &&
2360 (NAMLEN(ep) == 1 ||
2361 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2362 continue;
2363 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
2364 if (v == NULL) {
2365 Py_DECREF(d);
2366 d = NULL;
2367 break;
2368 }
2369 if (arg_is_unicode) {
2370 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002371
Victor Stinner8c62be82010-05-06 00:08:46 +00002372 w = PyUnicode_FromEncodedObject(v,
2373 Py_FileSystemDefaultEncoding,
2374 "surrogateescape");
2375 Py_DECREF(v);
2376 if (w != NULL)
2377 v = w;
2378 else {
2379 /* Encoding failed to decode ASCII bytes.
2380 Raise exception. */
2381 Py_DECREF(d);
2382 d = NULL;
2383 break;
2384 }
2385 }
2386 if (PyList_Append(d, v) != 0) {
2387 Py_DECREF(v);
2388 Py_DECREF(d);
2389 d = NULL;
2390 break;
2391 }
2392 Py_DECREF(v);
2393 }
2394 closedir(dirp);
2395 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002396
Victor Stinner8c62be82010-05-06 00:08:46 +00002397 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002398
Tim Peters0bb44a42000-09-15 07:44:49 +00002399#endif /* which OS */
2400} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002401
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002402#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002403/* A helper function for abspath on win32 */
2404static PyObject *
2405posix__getfullpathname(PyObject *self, PyObject *args)
2406{
Victor Stinner8c62be82010-05-06 00:08:46 +00002407 PyObject *opath;
2408 char *path;
2409 char outbuf[MAX_PATH*2];
2410 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002411#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002412 PyUnicodeObject *po;
2413 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2414 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2415 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2416 Py_UNICODE *wtemp;
2417 DWORD result;
2418 PyObject *v;
2419 result = GetFullPathNameW(wpath,
2420 sizeof(woutbuf)/sizeof(woutbuf[0]),
2421 woutbuf, &wtemp);
2422 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2423 woutbufp = malloc(result * sizeof(Py_UNICODE));
2424 if (!woutbufp)
2425 return PyErr_NoMemory();
2426 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2427 }
2428 if (result)
2429 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2430 else
2431 v = win32_error_unicode("GetFullPathNameW", wpath);
2432 if (woutbufp != woutbuf)
2433 free(woutbufp);
2434 return v;
2435 }
2436 /* Drop the argument parsing error as narrow strings
2437 are also valid. */
2438 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002439
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002440#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002441 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2442 PyUnicode_FSConverter, &opath))
2443 return NULL;
2444 path = PyBytes_AsString(opath);
2445 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2446 outbuf, &temp)) {
2447 win32_error("GetFullPathName", path);
2448 Py_DECREF(opath);
2449 return NULL;
2450 }
2451 Py_DECREF(opath);
2452 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2453 return PyUnicode_Decode(outbuf, strlen(outbuf),
2454 Py_FileSystemDefaultEncoding, NULL);
2455 }
2456 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002457} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002458#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002460PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002461"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002462Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002463
Barry Warsaw53699e91996-12-10 23:23:01 +00002464static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002465posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002466{
Victor Stinner8c62be82010-05-06 00:08:46 +00002467 int res;
2468 PyObject *opath;
2469 char *path;
2470 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002471
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002472#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002473 PyUnicodeObject *po;
2474 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2475 Py_BEGIN_ALLOW_THREADS
2476 /* PyUnicode_AS_UNICODE OK without thread lock as
2477 it is a simple dereference. */
2478 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2479 Py_END_ALLOW_THREADS
2480 if (!res)
2481 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2482 Py_INCREF(Py_None);
2483 return Py_None;
2484 }
2485 /* Drop the argument parsing error as narrow strings
2486 are also valid. */
2487 PyErr_Clear();
2488 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2489 PyUnicode_FSConverter, &opath, &mode))
2490 return NULL;
2491 path = PyBytes_AsString(opath);
2492 Py_BEGIN_ALLOW_THREADS
2493 /* PyUnicode_AS_UNICODE OK without thread lock as
2494 it is a simple dereference. */
2495 res = CreateDirectoryA(path, NULL);
2496 Py_END_ALLOW_THREADS
2497 if (!res) {
2498 win32_error("mkdir", path);
2499 Py_DECREF(opath);
2500 return NULL;
2501 }
2502 Py_DECREF(opath);
2503 Py_INCREF(Py_None);
2504 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002505#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002506
Victor Stinner8c62be82010-05-06 00:08:46 +00002507 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2508 PyUnicode_FSConverter, &opath, &mode))
2509 return NULL;
2510 path = PyBytes_AsString(opath);
2511 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002512#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002513 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002514#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002515 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002516#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002517 Py_END_ALLOW_THREADS
2518 if (res < 0)
2519 return posix_error_with_allocated_filename(opath);
2520 Py_DECREF(opath);
2521 Py_INCREF(Py_None);
2522 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002523#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002524}
2525
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002526
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002527/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2528#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002529#include <sys/resource.h>
2530#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002531
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002532
2533#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002534PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002535"nice(inc) -> new_priority\n\n\
2536Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002537
Barry Warsaw53699e91996-12-10 23:23:01 +00002538static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002539posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002540{
Victor Stinner8c62be82010-05-06 00:08:46 +00002541 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002542
Victor Stinner8c62be82010-05-06 00:08:46 +00002543 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2544 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002545
Victor Stinner8c62be82010-05-06 00:08:46 +00002546 /* There are two flavours of 'nice': one that returns the new
2547 priority (as required by almost all standards out there) and the
2548 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2549 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002550
Victor Stinner8c62be82010-05-06 00:08:46 +00002551 If we are of the nice family that returns the new priority, we
2552 need to clear errno before the call, and check if errno is filled
2553 before calling posix_error() on a returnvalue of -1, because the
2554 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002555
Victor Stinner8c62be82010-05-06 00:08:46 +00002556 errno = 0;
2557 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002558#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 if (value == 0)
2560 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002561#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002562 if (value == -1 && errno != 0)
2563 /* either nice() or getpriority() returned an error */
2564 return posix_error();
2565 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002566}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002567#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002568
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002569PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002570"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002571Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002572
Barry Warsaw53699e91996-12-10 23:23:01 +00002573static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002574posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002575{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002576#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002577 PyObject *o1, *o2;
2578 char *p1, *p2;
2579 BOOL result;
2580 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2581 goto error;
2582 if (!convert_to_unicode(&o1))
2583 goto error;
2584 if (!convert_to_unicode(&o2)) {
2585 Py_DECREF(o1);
2586 goto error;
2587 }
2588 Py_BEGIN_ALLOW_THREADS
2589 result = MoveFileW(PyUnicode_AsUnicode(o1),
2590 PyUnicode_AsUnicode(o2));
2591 Py_END_ALLOW_THREADS
2592 Py_DECREF(o1);
2593 Py_DECREF(o2);
2594 if (!result)
2595 return win32_error("rename", NULL);
2596 Py_INCREF(Py_None);
2597 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002598error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002599 PyErr_Clear();
2600 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2601 return NULL;
2602 Py_BEGIN_ALLOW_THREADS
2603 result = MoveFileA(p1, p2);
2604 Py_END_ALLOW_THREADS
2605 if (!result)
2606 return win32_error("rename", NULL);
2607 Py_INCREF(Py_None);
2608 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002609#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002610 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002611#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002612}
2613
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002615PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002616"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002617Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002618
Barry Warsaw53699e91996-12-10 23:23:01 +00002619static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002620posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002621{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002622#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002623 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002624#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002625 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002626#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002627}
2628
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002630PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002631"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002632Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002633
Barry Warsaw53699e91996-12-10 23:23:01 +00002634static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002635posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002636{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002637#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002638 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002639#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002640 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002641#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002642}
2643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002644
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002645#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002646PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002647"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002648Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002649
Barry Warsaw53699e91996-12-10 23:23:01 +00002650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002651posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652{
Victor Stinner8c62be82010-05-06 00:08:46 +00002653 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002654#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002655 wchar_t *command;
2656 if (!PyArg_ParseTuple(args, "u:system", &command))
2657 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002658
Victor Stinner8c62be82010-05-06 00:08:46 +00002659 Py_BEGIN_ALLOW_THREADS
2660 sts = _wsystem(command);
2661 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002662#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 PyObject *command_obj;
2664 char *command;
2665 if (!PyArg_ParseTuple(args, "O&:system",
2666 PyUnicode_FSConverter, &command_obj))
2667 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002668
Victor Stinner8c62be82010-05-06 00:08:46 +00002669 command = PyBytes_AsString(command_obj);
2670 Py_BEGIN_ALLOW_THREADS
2671 sts = system(command);
2672 Py_END_ALLOW_THREADS
2673 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002674#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002675 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002676}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002677#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002681"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002683
Barry Warsaw53699e91996-12-10 23:23:01 +00002684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002685posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002686{
Victor Stinner8c62be82010-05-06 00:08:46 +00002687 int i;
2688 if (!PyArg_ParseTuple(args, "i:umask", &i))
2689 return NULL;
2690 i = (int)umask(i);
2691 if (i < 0)
2692 return posix_error();
2693 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002694}
2695
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002697PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002698"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002699Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002700
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002701PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002702"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002703Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002704
Barry Warsaw53699e91996-12-10 23:23:01 +00002705static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002706posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002707{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002708#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002709 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002710#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002711 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002712#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002713}
2714
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002715
Guido van Rossumb6775db1994-08-01 11:34:53 +00002716#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002717PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002718"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002719Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002720
Barry Warsaw53699e91996-12-10 23:23:01 +00002721static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002722posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002723{
Victor Stinner8c62be82010-05-06 00:08:46 +00002724 struct utsname u;
2725 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002726
Victor Stinner8c62be82010-05-06 00:08:46 +00002727 Py_BEGIN_ALLOW_THREADS
2728 res = uname(&u);
2729 Py_END_ALLOW_THREADS
2730 if (res < 0)
2731 return posix_error();
2732 return Py_BuildValue("(sssss)",
2733 u.sysname,
2734 u.nodename,
2735 u.release,
2736 u.version,
2737 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002738}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002739#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002740
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002741static int
2742extract_time(PyObject *t, long* sec, long* usec)
2743{
Victor Stinner8c62be82010-05-06 00:08:46 +00002744 long intval;
2745 if (PyFloat_Check(t)) {
2746 double tval = PyFloat_AsDouble(t);
2747 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2748 if (!intobj)
2749 return -1;
2750 intval = PyLong_AsLong(intobj);
2751 Py_DECREF(intobj);
2752 if (intval == -1 && PyErr_Occurred())
2753 return -1;
2754 *sec = intval;
2755 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2756 if (*usec < 0)
2757 /* If rounding gave us a negative number,
2758 truncate. */
2759 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002760 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00002761 }
2762 intval = PyLong_AsLong(t);
2763 if (intval == -1 && PyErr_Occurred())
2764 return -1;
2765 *sec = intval;
2766 *usec = 0;
2767 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002768}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002769
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002770PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002771"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002772utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002773Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002775
Barry Warsaw53699e91996-12-10 23:23:01 +00002776static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002777posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002778{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002779#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002780 PyObject *arg;
2781 PyUnicodeObject *obwpath;
2782 wchar_t *wpath = NULL;
2783 PyObject *oapath;
2784 char *apath;
2785 HANDLE hFile;
2786 long atimesec, mtimesec, ausec, musec;
2787 FILETIME atime, mtime;
2788 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002789
Victor Stinner8c62be82010-05-06 00:08:46 +00002790 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2791 wpath = PyUnicode_AS_UNICODE(obwpath);
2792 Py_BEGIN_ALLOW_THREADS
2793 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2794 NULL, OPEN_EXISTING,
2795 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2796 Py_END_ALLOW_THREADS
2797 if (hFile == INVALID_HANDLE_VALUE)
2798 return win32_error_unicode("utime", wpath);
2799 } else
2800 /* Drop the argument parsing error as narrow strings
2801 are also valid. */
2802 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002803
Victor Stinner8c62be82010-05-06 00:08:46 +00002804 if (!wpath) {
2805 if (!PyArg_ParseTuple(args, "O&O:utime",
2806 PyUnicode_FSConverter, &oapath, &arg))
2807 return NULL;
2808 apath = PyBytes_AsString(oapath);
2809 Py_BEGIN_ALLOW_THREADS
2810 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2811 NULL, OPEN_EXISTING,
2812 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2813 Py_END_ALLOW_THREADS
2814 if (hFile == INVALID_HANDLE_VALUE) {
2815 win32_error("utime", apath);
2816 Py_DECREF(oapath);
2817 return NULL;
2818 }
2819 Py_DECREF(oapath);
2820 }
2821
2822 if (arg == Py_None) {
2823 SYSTEMTIME now;
2824 GetSystemTime(&now);
2825 if (!SystemTimeToFileTime(&now, &mtime) ||
2826 !SystemTimeToFileTime(&now, &atime)) {
2827 win32_error("utime", NULL);
2828 goto done;
2829 }
2830 }
2831 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2832 PyErr_SetString(PyExc_TypeError,
2833 "utime() arg 2 must be a tuple (atime, mtime)");
2834 goto done;
2835 }
2836 else {
2837 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2838 &atimesec, &ausec) == -1)
2839 goto done;
2840 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2841 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2842 &mtimesec, &musec) == -1)
2843 goto done;
2844 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2845 }
2846 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2847 /* Avoid putting the file name into the error here,
2848 as that may confuse the user into believing that
2849 something is wrong with the file, when it also
2850 could be the time stamp that gives a problem. */
2851 win32_error("utime", NULL);
2852 }
2853 Py_INCREF(Py_None);
2854 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002855done:
Victor Stinner8c62be82010-05-06 00:08:46 +00002856 CloseHandle(hFile);
2857 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002858#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002859
Victor Stinner8c62be82010-05-06 00:08:46 +00002860 PyObject *opath;
2861 char *path;
2862 long atime, mtime, ausec, musec;
2863 int res;
2864 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002865
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002866#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00002867 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002868#define ATIME buf[0].tv_sec
2869#define MTIME buf[1].tv_sec
2870#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002871/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00002872 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002873#define ATIME buf.actime
2874#define MTIME buf.modtime
2875#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002876#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00002877 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002878#define ATIME buf[0]
2879#define MTIME buf[1]
2880#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002881#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002882
Mark Hammond817c9292003-12-03 01:22:38 +00002883
Victor Stinner8c62be82010-05-06 00:08:46 +00002884 if (!PyArg_ParseTuple(args, "O&O:utime",
2885 PyUnicode_FSConverter, &opath, &arg))
2886 return NULL;
2887 path = PyBytes_AsString(opath);
2888 if (arg == Py_None) {
2889 /* optional time values not given */
2890 Py_BEGIN_ALLOW_THREADS
2891 res = utime(path, NULL);
2892 Py_END_ALLOW_THREADS
2893 }
2894 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2895 PyErr_SetString(PyExc_TypeError,
2896 "utime() arg 2 must be a tuple (atime, mtime)");
2897 Py_DECREF(opath);
2898 return NULL;
2899 }
2900 else {
2901 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2902 &atime, &ausec) == -1) {
2903 Py_DECREF(opath);
2904 return NULL;
2905 }
2906 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2907 &mtime, &musec) == -1) {
2908 Py_DECREF(opath);
2909 return NULL;
2910 }
2911 ATIME = atime;
2912 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002913#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00002914 buf[0].tv_usec = ausec;
2915 buf[1].tv_usec = musec;
2916 Py_BEGIN_ALLOW_THREADS
2917 res = utimes(path, buf);
2918 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002919#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002920 Py_BEGIN_ALLOW_THREADS
2921 res = utime(path, UTIME_ARG);
2922 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002923#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00002924 }
2925 if (res < 0) {
2926 return posix_error_with_allocated_filename(opath);
2927 }
2928 Py_DECREF(opath);
2929 Py_INCREF(Py_None);
2930 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002931#undef UTIME_ARG
2932#undef ATIME
2933#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002934#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002935}
2936
Guido van Rossum85e3b011991-06-03 12:42:10 +00002937
Guido van Rossum3b066191991-06-04 19:40:25 +00002938/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002939
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002941"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002942Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002943
Barry Warsaw53699e91996-12-10 23:23:01 +00002944static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002945posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002946{
Victor Stinner8c62be82010-05-06 00:08:46 +00002947 int sts;
2948 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2949 return NULL;
2950 _exit(sts);
2951 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002952}
2953
Martin v. Löwis114619e2002-10-07 06:44:21 +00002954#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2955static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002956free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002957{
Victor Stinner8c62be82010-05-06 00:08:46 +00002958 Py_ssize_t i;
2959 for (i = 0; i < count; i++)
2960 PyMem_Free(array[i]);
2961 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002962}
Martin v. Löwis011e8422009-05-05 04:43:17 +00002963
Antoine Pitrou69f71142009-05-24 21:25:49 +00002964static
Martin v. Löwis011e8422009-05-05 04:43:17 +00002965int fsconvert_strdup(PyObject *o, char**out)
2966{
Victor Stinner8c62be82010-05-06 00:08:46 +00002967 PyObject *bytes;
2968 Py_ssize_t size;
2969 if (!PyUnicode_FSConverter(o, &bytes))
2970 return 0;
2971 size = PyBytes_GET_SIZE(bytes);
2972 *out = PyMem_Malloc(size+1);
2973 if (!*out)
2974 return 0;
2975 memcpy(*out, PyBytes_AsString(bytes), size+1);
2976 Py_DECREF(bytes);
2977 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002978}
Martin v. Löwis114619e2002-10-07 06:44:21 +00002979#endif
2980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002981
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002982#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002983PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002984"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002985Execute an executable path with arguments, replacing current process.\n\
2986\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00002987 path: path of executable file\n\
2988 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002989
Barry Warsaw53699e91996-12-10 23:23:01 +00002990static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002991posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002992{
Victor Stinner8c62be82010-05-06 00:08:46 +00002993 PyObject *opath;
2994 char *path;
2995 PyObject *argv;
2996 char **argvlist;
2997 Py_ssize_t i, argc;
2998 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002999
Victor Stinner8c62be82010-05-06 00:08:46 +00003000 /* execv has two arguments: (path, argv), where
3001 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003002
Victor Stinner8c62be82010-05-06 00:08:46 +00003003 if (!PyArg_ParseTuple(args, "O&O:execv",
3004 PyUnicode_FSConverter,
3005 &opath, &argv))
3006 return NULL;
3007 path = PyBytes_AsString(opath);
3008 if (PyList_Check(argv)) {
3009 argc = PyList_Size(argv);
3010 getitem = PyList_GetItem;
3011 }
3012 else if (PyTuple_Check(argv)) {
3013 argc = PyTuple_Size(argv);
3014 getitem = PyTuple_GetItem;
3015 }
3016 else {
3017 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3018 Py_DECREF(opath);
3019 return NULL;
3020 }
3021 if (argc < 1) {
3022 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3023 Py_DECREF(opath);
3024 return NULL;
3025 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003026
Victor Stinner8c62be82010-05-06 00:08:46 +00003027 argvlist = PyMem_NEW(char *, argc+1);
3028 if (argvlist == NULL) {
3029 Py_DECREF(opath);
3030 return PyErr_NoMemory();
3031 }
3032 for (i = 0; i < argc; i++) {
3033 if (!fsconvert_strdup((*getitem)(argv, i),
3034 &argvlist[i])) {
3035 free_string_array(argvlist, i);
3036 PyErr_SetString(PyExc_TypeError,
3037 "execv() arg 2 must contain only strings");
3038 Py_DECREF(opath);
3039 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003040
Victor Stinner8c62be82010-05-06 00:08:46 +00003041 }
3042 }
3043 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003044
Victor Stinner8c62be82010-05-06 00:08:46 +00003045 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003046
Victor Stinner8c62be82010-05-06 00:08:46 +00003047 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003048
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 free_string_array(argvlist, argc);
3050 Py_DECREF(opath);
3051 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003052}
3053
Victor Stinner13bb71c2010-04-23 21:41:56 +00003054static char**
3055parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3056{
Victor Stinner8c62be82010-05-06 00:08:46 +00003057 char **envlist;
3058 Py_ssize_t i, pos, envc;
3059 PyObject *keys=NULL, *vals=NULL;
3060 PyObject *key, *val, *key2, *val2;
3061 char *p, *k, *v;
3062 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003063
Victor Stinner8c62be82010-05-06 00:08:46 +00003064 i = PyMapping_Size(env);
3065 if (i < 0)
3066 return NULL;
3067 envlist = PyMem_NEW(char *, i + 1);
3068 if (envlist == NULL) {
3069 PyErr_NoMemory();
3070 return NULL;
3071 }
3072 envc = 0;
3073 keys = PyMapping_Keys(env);
3074 vals = PyMapping_Values(env);
3075 if (!keys || !vals)
3076 goto error;
3077 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3078 PyErr_Format(PyExc_TypeError,
3079 "env.keys() or env.values() is not a list");
3080 goto error;
3081 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003082
Victor Stinner8c62be82010-05-06 00:08:46 +00003083 for (pos = 0; pos < i; pos++) {
3084 key = PyList_GetItem(keys, pos);
3085 val = PyList_GetItem(vals, pos);
3086 if (!key || !val)
3087 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003088
Victor Stinner8c62be82010-05-06 00:08:46 +00003089 if (PyUnicode_FSConverter(key, &key2) == 0)
3090 goto error;
3091 if (PyUnicode_FSConverter(val, &val2) == 0) {
3092 Py_DECREF(key2);
3093 goto error;
3094 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003095
3096#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003097 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3098 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003099#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003100 k = PyBytes_AsString(key2);
3101 v = PyBytes_AsString(val2);
3102 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003103
Victor Stinner8c62be82010-05-06 00:08:46 +00003104 p = PyMem_NEW(char, len);
3105 if (p == NULL) {
3106 PyErr_NoMemory();
3107 Py_DECREF(key2);
3108 Py_DECREF(val2);
3109 goto error;
3110 }
3111 PyOS_snprintf(p, len, "%s=%s", k, v);
3112 envlist[envc++] = p;
3113 Py_DECREF(key2);
3114 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003115#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003116 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003117#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003118 }
3119 Py_DECREF(vals);
3120 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003121
Victor Stinner8c62be82010-05-06 00:08:46 +00003122 envlist[envc] = 0;
3123 *envc_ptr = envc;
3124 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003125
3126error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003127 Py_XDECREF(keys);
3128 Py_XDECREF(vals);
3129 while (--envc >= 0)
3130 PyMem_DEL(envlist[envc]);
3131 PyMem_DEL(envlist);
3132 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003133}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003135PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003136"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003137Execute a path with arguments and environment, replacing current process.\n\
3138\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003139 path: path of executable file\n\
3140 args: tuple or list of arguments\n\
3141 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003142
Barry Warsaw53699e91996-12-10 23:23:01 +00003143static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003144posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003145{
Victor Stinner8c62be82010-05-06 00:08:46 +00003146 PyObject *opath;
3147 char *path;
3148 PyObject *argv, *env;
3149 char **argvlist;
3150 char **envlist;
3151 Py_ssize_t i, argc, envc;
3152 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3153 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003154
Victor Stinner8c62be82010-05-06 00:08:46 +00003155 /* execve has three arguments: (path, argv, env), where
3156 argv is a list or tuple of strings and env is a dictionary
3157 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003158
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 if (!PyArg_ParseTuple(args, "O&OO:execve",
3160 PyUnicode_FSConverter,
3161 &opath, &argv, &env))
3162 return NULL;
3163 path = PyBytes_AsString(opath);
3164 if (PyList_Check(argv)) {
3165 argc = PyList_Size(argv);
3166 getitem = PyList_GetItem;
3167 }
3168 else if (PyTuple_Check(argv)) {
3169 argc = PyTuple_Size(argv);
3170 getitem = PyTuple_GetItem;
3171 }
3172 else {
3173 PyErr_SetString(PyExc_TypeError,
3174 "execve() arg 2 must be a tuple or list");
3175 goto fail_0;
3176 }
3177 if (!PyMapping_Check(env)) {
3178 PyErr_SetString(PyExc_TypeError,
3179 "execve() arg 3 must be a mapping object");
3180 goto fail_0;
3181 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003182
Victor Stinner8c62be82010-05-06 00:08:46 +00003183 argvlist = PyMem_NEW(char *, argc+1);
3184 if (argvlist == NULL) {
3185 PyErr_NoMemory();
3186 goto fail_0;
3187 }
3188 for (i = 0; i < argc; i++) {
3189 if (!fsconvert_strdup((*getitem)(argv, i),
3190 &argvlist[i]))
3191 {
3192 lastarg = i;
3193 goto fail_1;
3194 }
3195 }
3196 lastarg = argc;
3197 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003198
Victor Stinner8c62be82010-05-06 00:08:46 +00003199 envlist = parse_envlist(env, &envc);
3200 if (envlist == NULL)
3201 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003202
Victor Stinner8c62be82010-05-06 00:08:46 +00003203 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003204
Victor Stinner8c62be82010-05-06 00:08:46 +00003205 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003206
Victor Stinner8c62be82010-05-06 00:08:46 +00003207 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003208
Victor Stinner8c62be82010-05-06 00:08:46 +00003209 while (--envc >= 0)
3210 PyMem_DEL(envlist[envc]);
3211 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003212 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003213 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003214 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003215 Py_DECREF(opath);
3216 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003217}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003218#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003219
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003220
Guido van Rossuma1065681999-01-25 23:20:23 +00003221#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003222PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003223"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003224Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003225\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003226 mode: mode of process creation\n\
3227 path: path of executable file\n\
3228 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003229
3230static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003231posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003232{
Victor Stinner8c62be82010-05-06 00:08:46 +00003233 PyObject *opath;
3234 char *path;
3235 PyObject *argv;
3236 char **argvlist;
3237 int mode, i;
3238 Py_ssize_t argc;
3239 Py_intptr_t spawnval;
3240 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003241
Victor Stinner8c62be82010-05-06 00:08:46 +00003242 /* spawnv has three arguments: (mode, path, argv), where
3243 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003244
Victor Stinner8c62be82010-05-06 00:08:46 +00003245 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3246 PyUnicode_FSConverter,
3247 &opath, &argv))
3248 return NULL;
3249 path = PyBytes_AsString(opath);
3250 if (PyList_Check(argv)) {
3251 argc = PyList_Size(argv);
3252 getitem = PyList_GetItem;
3253 }
3254 else if (PyTuple_Check(argv)) {
3255 argc = PyTuple_Size(argv);
3256 getitem = PyTuple_GetItem;
3257 }
3258 else {
3259 PyErr_SetString(PyExc_TypeError,
3260 "spawnv() arg 2 must be a tuple or list");
3261 Py_DECREF(opath);
3262 return NULL;
3263 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003264
Victor Stinner8c62be82010-05-06 00:08:46 +00003265 argvlist = PyMem_NEW(char *, argc+1);
3266 if (argvlist == NULL) {
3267 Py_DECREF(opath);
3268 return PyErr_NoMemory();
3269 }
3270 for (i = 0; i < argc; i++) {
3271 if (!fsconvert_strdup((*getitem)(argv, i),
3272 &argvlist[i])) {
3273 free_string_array(argvlist, i);
3274 PyErr_SetString(
3275 PyExc_TypeError,
3276 "spawnv() arg 2 must contain only strings");
3277 Py_DECREF(opath);
3278 return NULL;
3279 }
3280 }
3281 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003282
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003283#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003284 Py_BEGIN_ALLOW_THREADS
3285 spawnval = spawnv(mode, path, argvlist);
3286 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003287#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 if (mode == _OLD_P_OVERLAY)
3289 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003290
Victor Stinner8c62be82010-05-06 00:08:46 +00003291 Py_BEGIN_ALLOW_THREADS
3292 spawnval = _spawnv(mode, path, argvlist);
3293 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003294#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003295
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 free_string_array(argvlist, argc);
3297 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003298
Victor Stinner8c62be82010-05-06 00:08:46 +00003299 if (spawnval == -1)
3300 return posix_error();
3301 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003302#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003304#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003305 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003306#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003307}
3308
3309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003310PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003311"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003312Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003313\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 mode: mode of process creation\n\
3315 path: path of executable file\n\
3316 args: tuple or list of arguments\n\
3317 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003318
3319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003320posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003321{
Victor Stinner8c62be82010-05-06 00:08:46 +00003322 PyObject *opath;
3323 char *path;
3324 PyObject *argv, *env;
3325 char **argvlist;
3326 char **envlist;
3327 PyObject *res = NULL;
3328 int mode, envc;
3329 Py_ssize_t argc, i;
3330 Py_intptr_t spawnval;
3331 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3332 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003333
Victor Stinner8c62be82010-05-06 00:08:46 +00003334 /* spawnve has four arguments: (mode, path, argv, env), where
3335 argv is a list or tuple of strings and env is a dictionary
3336 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003337
Victor Stinner8c62be82010-05-06 00:08:46 +00003338 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3339 PyUnicode_FSConverter,
3340 &opath, &argv, &env))
3341 return NULL;
3342 path = PyBytes_AsString(opath);
3343 if (PyList_Check(argv)) {
3344 argc = PyList_Size(argv);
3345 getitem = PyList_GetItem;
3346 }
3347 else if (PyTuple_Check(argv)) {
3348 argc = PyTuple_Size(argv);
3349 getitem = PyTuple_GetItem;
3350 }
3351 else {
3352 PyErr_SetString(PyExc_TypeError,
3353 "spawnve() arg 2 must be a tuple or list");
3354 goto fail_0;
3355 }
3356 if (!PyMapping_Check(env)) {
3357 PyErr_SetString(PyExc_TypeError,
3358 "spawnve() arg 3 must be a mapping object");
3359 goto fail_0;
3360 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003361
Victor Stinner8c62be82010-05-06 00:08:46 +00003362 argvlist = PyMem_NEW(char *, argc+1);
3363 if (argvlist == NULL) {
3364 PyErr_NoMemory();
3365 goto fail_0;
3366 }
3367 for (i = 0; i < argc; i++) {
3368 if (!fsconvert_strdup((*getitem)(argv, i),
3369 &argvlist[i]))
3370 {
3371 lastarg = i;
3372 goto fail_1;
3373 }
3374 }
3375 lastarg = argc;
3376 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003377
Victor Stinner8c62be82010-05-06 00:08:46 +00003378 envlist = parse_envlist(env, &envc);
3379 if (envlist == NULL)
3380 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003381
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003382#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003383 Py_BEGIN_ALLOW_THREADS
3384 spawnval = spawnve(mode, path, argvlist, envlist);
3385 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003386#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003387 if (mode == _OLD_P_OVERLAY)
3388 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003389
Victor Stinner8c62be82010-05-06 00:08:46 +00003390 Py_BEGIN_ALLOW_THREADS
3391 spawnval = _spawnve(mode, path, argvlist, envlist);
3392 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003393#endif
Tim Peters25059d32001-12-07 20:35:43 +00003394
Victor Stinner8c62be82010-05-06 00:08:46 +00003395 if (spawnval == -1)
3396 (void) posix_error();
3397 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003398#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003399 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003400#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003402#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003403
Victor Stinner8c62be82010-05-06 00:08:46 +00003404 while (--envc >= 0)
3405 PyMem_DEL(envlist[envc]);
3406 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003407 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003408 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003409 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003410 Py_DECREF(opath);
3411 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003412}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003413
3414/* OS/2 supports spawnvp & spawnvpe natively */
3415#if defined(PYOS_OS2)
3416PyDoc_STRVAR(posix_spawnvp__doc__,
3417"spawnvp(mode, file, args)\n\n\
3418Execute the program 'file' in a new process, using the environment\n\
3419search path to find the file.\n\
3420\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003421 mode: mode of process creation\n\
3422 file: executable file name\n\
3423 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003424
3425static PyObject *
3426posix_spawnvp(PyObject *self, PyObject *args)
3427{
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 PyObject *opath;
3429 char *path;
3430 PyObject *argv;
3431 char **argvlist;
3432 int mode, i, argc;
3433 Py_intptr_t spawnval;
3434 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003435
Victor Stinner8c62be82010-05-06 00:08:46 +00003436 /* spawnvp has three arguments: (mode, path, argv), where
3437 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003438
Victor Stinner8c62be82010-05-06 00:08:46 +00003439 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3440 PyUnicode_FSConverter,
3441 &opath, &argv))
3442 return NULL;
3443 path = PyBytes_AsString(opath);
3444 if (PyList_Check(argv)) {
3445 argc = PyList_Size(argv);
3446 getitem = PyList_GetItem;
3447 }
3448 else if (PyTuple_Check(argv)) {
3449 argc = PyTuple_Size(argv);
3450 getitem = PyTuple_GetItem;
3451 }
3452 else {
3453 PyErr_SetString(PyExc_TypeError,
3454 "spawnvp() arg 2 must be a tuple or list");
3455 Py_DECREF(opath);
3456 return NULL;
3457 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003458
Victor Stinner8c62be82010-05-06 00:08:46 +00003459 argvlist = PyMem_NEW(char *, argc+1);
3460 if (argvlist == NULL) {
3461 Py_DECREF(opath);
3462 return PyErr_NoMemory();
3463 }
3464 for (i = 0; i < argc; i++) {
3465 if (!fsconvert_strdup((*getitem)(argv, i),
3466 &argvlist[i])) {
3467 free_string_array(argvlist, i);
3468 PyErr_SetString(
3469 PyExc_TypeError,
3470 "spawnvp() arg 2 must contain only strings");
3471 Py_DECREF(opath);
3472 return NULL;
3473 }
3474 }
3475 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003476
Victor Stinner8c62be82010-05-06 00:08:46 +00003477 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003478#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003479 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003480#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003481 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003482#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003483 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003484
Victor Stinner8c62be82010-05-06 00:08:46 +00003485 free_string_array(argvlist, argc);
3486 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003487
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 if (spawnval == -1)
3489 return posix_error();
3490 else
3491 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003492}
3493
3494
3495PyDoc_STRVAR(posix_spawnvpe__doc__,
3496"spawnvpe(mode, file, args, env)\n\n\
3497Execute the program 'file' in a new process, using the environment\n\
3498search path to find the file.\n\
3499\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 mode: mode of process creation\n\
3501 file: executable file name\n\
3502 args: tuple or list of arguments\n\
3503 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003504
3505static PyObject *
3506posix_spawnvpe(PyObject *self, PyObject *args)
3507{
Victor Stinner8c62be82010-05-06 00:08:46 +00003508 PyObject *opath
3509 char *path;
3510 PyObject *argv, *env;
3511 char **argvlist;
3512 char **envlist;
3513 PyObject *res=NULL;
3514 int mode, i, argc, envc;
3515 Py_intptr_t spawnval;
3516 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3517 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003518
Victor Stinner8c62be82010-05-06 00:08:46 +00003519 /* spawnvpe has four arguments: (mode, path, argv, env), where
3520 argv is a list or tuple of strings and env is a dictionary
3521 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003522
Victor Stinner8c62be82010-05-06 00:08:46 +00003523 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3524 PyUnicode_FSConverter,
3525 &opath, &argv, &env))
3526 return NULL;
3527 path = PyBytes_AsString(opath);
3528 if (PyList_Check(argv)) {
3529 argc = PyList_Size(argv);
3530 getitem = PyList_GetItem;
3531 }
3532 else if (PyTuple_Check(argv)) {
3533 argc = PyTuple_Size(argv);
3534 getitem = PyTuple_GetItem;
3535 }
3536 else {
3537 PyErr_SetString(PyExc_TypeError,
3538 "spawnvpe() arg 2 must be a tuple or list");
3539 goto fail_0;
3540 }
3541 if (!PyMapping_Check(env)) {
3542 PyErr_SetString(PyExc_TypeError,
3543 "spawnvpe() arg 3 must be a mapping object");
3544 goto fail_0;
3545 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003546
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 argvlist = PyMem_NEW(char *, argc+1);
3548 if (argvlist == NULL) {
3549 PyErr_NoMemory();
3550 goto fail_0;
3551 }
3552 for (i = 0; i < argc; i++) {
3553 if (!fsconvert_strdup((*getitem)(argv, i),
3554 &argvlist[i]))
3555 {
3556 lastarg = i;
3557 goto fail_1;
3558 }
3559 }
3560 lastarg = argc;
3561 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003562
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 envlist = parse_envlist(env, &envc);
3564 if (envlist == NULL)
3565 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003566
Victor Stinner8c62be82010-05-06 00:08:46 +00003567 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003568#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003569 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003570#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003571 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003572#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003573 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003574
Victor Stinner8c62be82010-05-06 00:08:46 +00003575 if (spawnval == -1)
3576 (void) posix_error();
3577 else
3578 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003579
Victor Stinner8c62be82010-05-06 00:08:46 +00003580 while (--envc >= 0)
3581 PyMem_DEL(envlist[envc]);
3582 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003583 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003584 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003585 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003586 Py_DECREF(opath);
3587 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003588}
3589#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003590#endif /* HAVE_SPAWNV */
3591
3592
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003593#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003594PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003595"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003596Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3597\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003598Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003599
3600static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003601posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003602{
Victor Stinner8c62be82010-05-06 00:08:46 +00003603 pid_t pid;
3604 int result = 0;
3605 _PyImport_AcquireLock();
3606 pid = fork1();
3607 if (pid == 0) {
3608 /* child: this clobbers and resets the import lock. */
3609 PyOS_AfterFork();
3610 } else {
3611 /* parent: release the import lock. */
3612 result = _PyImport_ReleaseLock();
3613 }
3614 if (pid == -1)
3615 return posix_error();
3616 if (result < 0) {
3617 /* Don't clobber the OSError if the fork failed. */
3618 PyErr_SetString(PyExc_RuntimeError,
3619 "not holding the import lock");
3620 return NULL;
3621 }
3622 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003623}
3624#endif
3625
3626
Guido van Rossumad0ee831995-03-01 10:34:45 +00003627#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003628PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003629"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003630Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003631Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003632
Barry Warsaw53699e91996-12-10 23:23:01 +00003633static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003634posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003635{
Victor Stinner8c62be82010-05-06 00:08:46 +00003636 pid_t pid;
3637 int result = 0;
3638 _PyImport_AcquireLock();
3639 pid = fork();
3640 if (pid == 0) {
3641 /* child: this clobbers and resets the import lock. */
3642 PyOS_AfterFork();
3643 } else {
3644 /* parent: release the import lock. */
3645 result = _PyImport_ReleaseLock();
3646 }
3647 if (pid == -1)
3648 return posix_error();
3649 if (result < 0) {
3650 /* Don't clobber the OSError if the fork failed. */
3651 PyErr_SetString(PyExc_RuntimeError,
3652 "not holding the import lock");
3653 return NULL;
3654 }
3655 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003656}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003657#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003658
Neal Norwitzb59798b2003-03-21 01:43:31 +00003659/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003660/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3661#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003662#define DEV_PTY_FILE "/dev/ptc"
3663#define HAVE_DEV_PTMX
3664#else
3665#define DEV_PTY_FILE "/dev/ptmx"
3666#endif
3667
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003668#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003669#ifdef HAVE_PTY_H
3670#include <pty.h>
3671#else
3672#ifdef HAVE_LIBUTIL_H
3673#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003674#else
3675#ifdef HAVE_UTIL_H
3676#include <util.h>
3677#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003678#endif /* HAVE_LIBUTIL_H */
3679#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003680#ifdef HAVE_STROPTS_H
3681#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003682#endif
3683#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003684
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003685#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003686PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003687"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003688Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003689
3690static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003691posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003692{
Victor Stinner8c62be82010-05-06 00:08:46 +00003693 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003694#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00003695 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003696#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003697#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003698 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003699#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00003700 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003701#endif
3702#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003703
Thomas Wouters70c21a12000-07-14 14:28:33 +00003704#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00003705 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3706 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003707#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003708 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3709 if (slave_name == NULL)
3710 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003711
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 slave_fd = open(slave_name, O_RDWR);
3713 if (slave_fd < 0)
3714 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003715#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003716 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3717 if (master_fd < 0)
3718 return posix_error();
3719 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3720 /* change permission of slave */
3721 if (grantpt(master_fd) < 0) {
3722 PyOS_setsig(SIGCHLD, sig_saved);
3723 return posix_error();
3724 }
3725 /* unlock slave */
3726 if (unlockpt(master_fd) < 0) {
3727 PyOS_setsig(SIGCHLD, sig_saved);
3728 return posix_error();
3729 }
3730 PyOS_setsig(SIGCHLD, sig_saved);
3731 slave_name = ptsname(master_fd); /* get name of slave */
3732 if (slave_name == NULL)
3733 return posix_error();
3734 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3735 if (slave_fd < 0)
3736 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003737#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3739 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003740#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00003741 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003742#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003743#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003744#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003745
Victor Stinner8c62be82010-05-06 00:08:46 +00003746 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003747
Fred Drake8cef4cf2000-06-28 16:40:38 +00003748}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003749#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003750
3751#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003752PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003753"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003754Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3755Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003756To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003757
3758static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003759posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003760{
Victor Stinner8c62be82010-05-06 00:08:46 +00003761 int master_fd = -1, result = 0;
3762 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003763
Victor Stinner8c62be82010-05-06 00:08:46 +00003764 _PyImport_AcquireLock();
3765 pid = forkpty(&master_fd, NULL, NULL, NULL);
3766 if (pid == 0) {
3767 /* child: this clobbers and resets the import lock. */
3768 PyOS_AfterFork();
3769 } else {
3770 /* parent: release the import lock. */
3771 result = _PyImport_ReleaseLock();
3772 }
3773 if (pid == -1)
3774 return posix_error();
3775 if (result < 0) {
3776 /* Don't clobber the OSError if the fork failed. */
3777 PyErr_SetString(PyExc_RuntimeError,
3778 "not holding the import lock");
3779 return NULL;
3780 }
3781 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003782}
3783#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003784
Guido van Rossumad0ee831995-03-01 10:34:45 +00003785#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003786PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003787"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003788Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003789
Barry Warsaw53699e91996-12-10 23:23:01 +00003790static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003791posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003792{
Victor Stinner8c62be82010-05-06 00:08:46 +00003793 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003794}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003795#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003797
Guido van Rossumad0ee831995-03-01 10:34:45 +00003798#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003799PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003800"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003801Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003802
Barry Warsaw53699e91996-12-10 23:23:01 +00003803static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003804posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003805{
Victor Stinner8c62be82010-05-06 00:08:46 +00003806 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003807}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003808#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003809
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003810
Guido van Rossumad0ee831995-03-01 10:34:45 +00003811#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003812PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003813"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003814Return the current process's group 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_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003818{
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003820}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003821#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003822
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003823
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003824PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003825"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003827
Barry Warsaw53699e91996-12-10 23:23:01 +00003828static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003829posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003830{
Victor Stinner8c62be82010-05-06 00:08:46 +00003831 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003832}
3833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003834
Fred Drakec9680921999-12-13 16:37:25 +00003835#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003836PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003837"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003838Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003839
3840static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003841posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003842{
3843 PyObject *result = NULL;
3844
Fred Drakec9680921999-12-13 16:37:25 +00003845#ifdef NGROUPS_MAX
3846#define MAX_GROUPS NGROUPS_MAX
3847#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003848 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00003849#define MAX_GROUPS 64
3850#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003851 gid_t grouplist[MAX_GROUPS];
3852 int n;
Fred Drakec9680921999-12-13 16:37:25 +00003853
Victor Stinner8c62be82010-05-06 00:08:46 +00003854 n = getgroups(MAX_GROUPS, grouplist);
3855 if (n < 0)
3856 posix_error();
3857 else {
3858 result = PyList_New(n);
3859 if (result != NULL) {
3860 int i;
3861 for (i = 0; i < n; ++i) {
3862 PyObject *o = PyLong_FromLong((long)grouplist[i]);
3863 if (o == NULL) {
3864 Py_DECREF(result);
3865 result = NULL;
3866 break;
Fred Drakec9680921999-12-13 16:37:25 +00003867 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003868 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00003869 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003870 }
3871 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003872
Fred Drakec9680921999-12-13 16:37:25 +00003873 return result;
3874}
3875#endif
3876
Antoine Pitroub7572f02009-12-02 20:46:48 +00003877#ifdef HAVE_INITGROUPS
3878PyDoc_STRVAR(posix_initgroups__doc__,
3879"initgroups(username, gid) -> None\n\n\
3880Call the system initgroups() to initialize the group access list with all of\n\
3881the groups of which the specified username is a member, plus the specified\n\
3882group id.");
3883
3884static PyObject *
3885posix_initgroups(PyObject *self, PyObject *args)
3886{
Victor Stinner8c62be82010-05-06 00:08:46 +00003887 char *username;
3888 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003889
Victor Stinner8c62be82010-05-06 00:08:46 +00003890 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
3891 return NULL;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003892
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 if (initgroups(username, (gid_t) gid) == -1)
3894 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00003895
Victor Stinner8c62be82010-05-06 00:08:46 +00003896 Py_INCREF(Py_None);
3897 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003898}
3899#endif
3900
Martin v. Löwis606edc12002-06-13 21:09:11 +00003901#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003902PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003903"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003904Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003905
3906static PyObject *
3907posix_getpgid(PyObject *self, PyObject *args)
3908{
Victor Stinner8c62be82010-05-06 00:08:46 +00003909 pid_t pid, pgid;
3910 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
3911 return NULL;
3912 pgid = getpgid(pid);
3913 if (pgid < 0)
3914 return posix_error();
3915 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003916}
3917#endif /* HAVE_GETPGID */
3918
3919
Guido van Rossumb6775db1994-08-01 11:34:53 +00003920#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003921PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003922"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003923Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003924
Barry Warsaw53699e91996-12-10 23:23:01 +00003925static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003926posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003927{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003928#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00003929 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003930#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003932#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003933}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003934#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003935
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003936
Guido van Rossumb6775db1994-08-01 11:34:53 +00003937#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003938PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003939"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003940Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003941
Barry Warsaw53699e91996-12-10 23:23:01 +00003942static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003943posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003944{
Guido van Rossum64933891994-10-20 21:56:42 +00003945#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00003946 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003947#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003948 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003949#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003950 return posix_error();
3951 Py_INCREF(Py_None);
3952 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003953}
3954
Guido van Rossumb6775db1994-08-01 11:34:53 +00003955#endif /* HAVE_SETPGRP */
3956
Guido van Rossumad0ee831995-03-01 10:34:45 +00003957#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003958PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003959"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003960Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003961
Barry Warsaw53699e91996-12-10 23:23:01 +00003962static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003963posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003964{
Victor Stinner8c62be82010-05-06 00:08:46 +00003965 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003966}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003967#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003968
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003969
Fred Drake12c6e2d1999-12-14 21:25:03 +00003970#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003971PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003972"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003973Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003974
3975static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003976posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003977{
Victor Stinner8c62be82010-05-06 00:08:46 +00003978 PyObject *result = NULL;
3979 char *name;
3980 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003981
Victor Stinner8c62be82010-05-06 00:08:46 +00003982 errno = 0;
3983 name = getlogin();
3984 if (name == NULL) {
3985 if (errno)
3986 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00003987 else
Victor Stinner8c62be82010-05-06 00:08:46 +00003988 PyErr_SetString(PyExc_OSError,
3989 "unable to determine login name");
3990 }
3991 else
3992 result = PyUnicode_FromString(name);
3993 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003994
Fred Drake12c6e2d1999-12-14 21:25:03 +00003995 return result;
3996}
3997#endif
3998
Guido van Rossumad0ee831995-03-01 10:34:45 +00003999#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004000PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004001"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004002Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004003
Barry Warsaw53699e91996-12-10 23:23:01 +00004004static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004005posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004006{
Victor Stinner8c62be82010-05-06 00:08:46 +00004007 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004008}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004009#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004010
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004011
Guido van Rossumad0ee831995-03-01 10:34:45 +00004012#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004013PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004014"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004016
Barry Warsaw53699e91996-12-10 23:23:01 +00004017static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004018posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004019{
Victor Stinner8c62be82010-05-06 00:08:46 +00004020 pid_t pid;
4021 int sig;
4022 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4023 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004024#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004025 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4026 APIRET rc;
4027 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004028 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004029
4030 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4031 APIRET rc;
4032 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004033 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004034
4035 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004036 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004037#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004038 if (kill(pid, sig) == -1)
4039 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004040#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004041 Py_INCREF(Py_None);
4042 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004043}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004044#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004045
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004046#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004047PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004048"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004049Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004050
4051static PyObject *
4052posix_killpg(PyObject *self, PyObject *args)
4053{
Victor Stinner8c62be82010-05-06 00:08:46 +00004054 int sig;
4055 pid_t pgid;
4056 /* XXX some man pages make the `pgid` parameter an int, others
4057 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4058 take the same type. Moreover, pid_t is always at least as wide as
4059 int (else compilation of this module fails), which is safe. */
4060 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4061 return NULL;
4062 if (killpg(pgid, sig) == -1)
4063 return posix_error();
4064 Py_INCREF(Py_None);
4065 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004066}
4067#endif
4068
Brian Curtineb24d742010-04-12 17:16:38 +00004069#ifdef MS_WINDOWS
4070PyDoc_STRVAR(win32_kill__doc__,
4071"kill(pid, sig)\n\n\
4072Kill a process with a signal.");
4073
4074static PyObject *
4075win32_kill(PyObject *self, PyObject *args)
4076{
Victor Stinner8c62be82010-05-06 00:08:46 +00004077 PyObject *result, handle_obj;
4078 DWORD pid, sig, err;
4079 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004080
Victor Stinner8c62be82010-05-06 00:08:46 +00004081 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4082 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004083
Victor Stinner8c62be82010-05-06 00:08:46 +00004084 /* Console processes which share a common console can be sent CTRL+C or
4085 CTRL+BREAK events, provided they handle said events. */
4086 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4087 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4088 err = GetLastError();
4089 PyErr_SetFromWindowsErr(err);
4090 }
4091 else
4092 Py_RETURN_NONE;
4093 }
Brian Curtineb24d742010-04-12 17:16:38 +00004094
Victor Stinner8c62be82010-05-06 00:08:46 +00004095 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4096 attempt to open and terminate the process. */
4097 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4098 if (handle == NULL) {
4099 err = GetLastError();
4100 return PyErr_SetFromWindowsErr(err);
4101 }
Brian Curtineb24d742010-04-12 17:16:38 +00004102
Victor Stinner8c62be82010-05-06 00:08:46 +00004103 if (TerminateProcess(handle, sig) == 0) {
4104 err = GetLastError();
4105 result = PyErr_SetFromWindowsErr(err);
4106 } else {
4107 Py_INCREF(Py_None);
4108 result = Py_None;
4109 }
Brian Curtineb24d742010-04-12 17:16:38 +00004110
Victor Stinner8c62be82010-05-06 00:08:46 +00004111 CloseHandle(handle);
4112 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004113}
4114#endif /* MS_WINDOWS */
4115
Guido van Rossumc0125471996-06-28 18:55:32 +00004116#ifdef HAVE_PLOCK
4117
4118#ifdef HAVE_SYS_LOCK_H
4119#include <sys/lock.h>
4120#endif
4121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004122PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004123"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004124Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004125
Barry Warsaw53699e91996-12-10 23:23:01 +00004126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004127posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004128{
Victor Stinner8c62be82010-05-06 00:08:46 +00004129 int op;
4130 if (!PyArg_ParseTuple(args, "i:plock", &op))
4131 return NULL;
4132 if (plock(op) == -1)
4133 return posix_error();
4134 Py_INCREF(Py_None);
4135 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004136}
4137#endif
4138
Guido van Rossumb6775db1994-08-01 11:34:53 +00004139#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004140PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004141"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004142Set the current process's user id.");
4143
Barry Warsaw53699e91996-12-10 23:23:01 +00004144static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004145posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004146{
Victor Stinner8c62be82010-05-06 00:08:46 +00004147 long uid_arg;
4148 uid_t uid;
4149 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4150 return NULL;
4151 uid = uid_arg;
4152 if (uid != uid_arg) {
4153 PyErr_SetString(PyExc_OverflowError, "user id too big");
4154 return NULL;
4155 }
4156 if (setuid(uid) < 0)
4157 return posix_error();
4158 Py_INCREF(Py_None);
4159 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004160}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004161#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004163
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004164#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004165PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004166"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004167Set the current process's effective user id.");
4168
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004169static PyObject *
4170posix_seteuid (PyObject *self, PyObject *args)
4171{
Victor Stinner8c62be82010-05-06 00:08:46 +00004172 long euid_arg;
4173 uid_t euid;
4174 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4175 return NULL;
4176 euid = euid_arg;
4177 if (euid != euid_arg) {
4178 PyErr_SetString(PyExc_OverflowError, "user id too big");
4179 return NULL;
4180 }
4181 if (seteuid(euid) < 0) {
4182 return posix_error();
4183 } else {
4184 Py_INCREF(Py_None);
4185 return Py_None;
4186 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004187}
4188#endif /* HAVE_SETEUID */
4189
4190#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004191PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004192"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004193Set the current process's effective group id.");
4194
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004195static PyObject *
4196posix_setegid (PyObject *self, PyObject *args)
4197{
Victor Stinner8c62be82010-05-06 00:08:46 +00004198 long egid_arg;
4199 gid_t egid;
4200 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4201 return NULL;
4202 egid = egid_arg;
4203 if (egid != egid_arg) {
4204 PyErr_SetString(PyExc_OverflowError, "group id too big");
4205 return NULL;
4206 }
4207 if (setegid(egid) < 0) {
4208 return posix_error();
4209 } else {
4210 Py_INCREF(Py_None);
4211 return Py_None;
4212 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004213}
4214#endif /* HAVE_SETEGID */
4215
4216#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004217PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004218"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004219Set the current process's real and effective user ids.");
4220
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004221static PyObject *
4222posix_setreuid (PyObject *self, PyObject *args)
4223{
Victor Stinner8c62be82010-05-06 00:08:46 +00004224 long ruid_arg, euid_arg;
4225 uid_t ruid, euid;
4226 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4227 return NULL;
4228 if (ruid_arg == -1)
4229 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4230 else
4231 ruid = ruid_arg; /* otherwise, assign from our long */
4232 if (euid_arg == -1)
4233 euid = (uid_t)-1;
4234 else
4235 euid = euid_arg;
4236 if ((euid_arg != -1 && euid != euid_arg) ||
4237 (ruid_arg != -1 && ruid != ruid_arg)) {
4238 PyErr_SetString(PyExc_OverflowError, "user id too big");
4239 return NULL;
4240 }
4241 if (setreuid(ruid, euid) < 0) {
4242 return posix_error();
4243 } else {
4244 Py_INCREF(Py_None);
4245 return Py_None;
4246 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004247}
4248#endif /* HAVE_SETREUID */
4249
4250#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004251PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004252"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004253Set the current process's real and effective group ids.");
4254
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004255static PyObject *
4256posix_setregid (PyObject *self, PyObject *args)
4257{
Victor Stinner8c62be82010-05-06 00:08:46 +00004258 long rgid_arg, egid_arg;
4259 gid_t rgid, egid;
4260 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4261 return NULL;
4262 if (rgid_arg == -1)
4263 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4264 else
4265 rgid = rgid_arg; /* otherwise, assign from our long */
4266 if (egid_arg == -1)
4267 egid = (gid_t)-1;
4268 else
4269 egid = egid_arg;
4270 if ((egid_arg != -1 && egid != egid_arg) ||
4271 (rgid_arg != -1 && rgid != rgid_arg)) {
4272 PyErr_SetString(PyExc_OverflowError, "group id too big");
4273 return NULL;
4274 }
4275 if (setregid(rgid, egid) < 0) {
4276 return posix_error();
4277 } else {
4278 Py_INCREF(Py_None);
4279 return Py_None;
4280 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004281}
4282#endif /* HAVE_SETREGID */
4283
Guido van Rossumb6775db1994-08-01 11:34:53 +00004284#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004285PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004286"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004287Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004288
Barry Warsaw53699e91996-12-10 23:23:01 +00004289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004290posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004291{
Victor Stinner8c62be82010-05-06 00:08:46 +00004292 long gid_arg;
4293 gid_t gid;
4294 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4295 return NULL;
4296 gid = gid_arg;
4297 if (gid != gid_arg) {
4298 PyErr_SetString(PyExc_OverflowError, "group id too big");
4299 return NULL;
4300 }
4301 if (setgid(gid) < 0)
4302 return posix_error();
4303 Py_INCREF(Py_None);
4304 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004305}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004306#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004307
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004308#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004309PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004310"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004311Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004312
4313static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004314posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004315{
Victor Stinner8c62be82010-05-06 00:08:46 +00004316 int i, len;
4317 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004318
Victor Stinner8c62be82010-05-06 00:08:46 +00004319 if (!PySequence_Check(groups)) {
4320 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4321 return NULL;
4322 }
4323 len = PySequence_Size(groups);
4324 if (len > MAX_GROUPS) {
4325 PyErr_SetString(PyExc_ValueError, "too many groups");
4326 return NULL;
4327 }
4328 for(i = 0; i < len; i++) {
4329 PyObject *elem;
4330 elem = PySequence_GetItem(groups, i);
4331 if (!elem)
4332 return NULL;
4333 if (!PyLong_Check(elem)) {
4334 PyErr_SetString(PyExc_TypeError,
4335 "groups must be integers");
4336 Py_DECREF(elem);
4337 return NULL;
4338 } else {
4339 unsigned long x = PyLong_AsUnsignedLong(elem);
4340 if (PyErr_Occurred()) {
4341 PyErr_SetString(PyExc_TypeError,
4342 "group id too big");
4343 Py_DECREF(elem);
4344 return NULL;
4345 }
4346 grouplist[i] = x;
4347 /* read back the value to see if it fitted in gid_t */
4348 if (grouplist[i] != x) {
4349 PyErr_SetString(PyExc_TypeError,
4350 "group id too big");
4351 Py_DECREF(elem);
4352 return NULL;
4353 }
4354 }
4355 Py_DECREF(elem);
4356 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004357
Victor Stinner8c62be82010-05-06 00:08:46 +00004358 if (setgroups(len, grouplist) < 0)
4359 return posix_error();
4360 Py_INCREF(Py_None);
4361 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004362}
4363#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004364
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004365#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4366static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004367wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004368{
Victor Stinner8c62be82010-05-06 00:08:46 +00004369 PyObject *result;
4370 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004371
Victor Stinner8c62be82010-05-06 00:08:46 +00004372 if (pid == -1)
4373 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004374
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 if (struct_rusage == NULL) {
4376 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4377 if (m == NULL)
4378 return NULL;
4379 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4380 Py_DECREF(m);
4381 if (struct_rusage == NULL)
4382 return NULL;
4383 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004384
Victor Stinner8c62be82010-05-06 00:08:46 +00004385 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4386 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4387 if (!result)
4388 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004389
4390#ifndef doubletime
4391#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4392#endif
4393
Victor Stinner8c62be82010-05-06 00:08:46 +00004394 PyStructSequence_SET_ITEM(result, 0,
4395 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4396 PyStructSequence_SET_ITEM(result, 1,
4397 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004398#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004399 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4400 SET_INT(result, 2, ru->ru_maxrss);
4401 SET_INT(result, 3, ru->ru_ixrss);
4402 SET_INT(result, 4, ru->ru_idrss);
4403 SET_INT(result, 5, ru->ru_isrss);
4404 SET_INT(result, 6, ru->ru_minflt);
4405 SET_INT(result, 7, ru->ru_majflt);
4406 SET_INT(result, 8, ru->ru_nswap);
4407 SET_INT(result, 9, ru->ru_inblock);
4408 SET_INT(result, 10, ru->ru_oublock);
4409 SET_INT(result, 11, ru->ru_msgsnd);
4410 SET_INT(result, 12, ru->ru_msgrcv);
4411 SET_INT(result, 13, ru->ru_nsignals);
4412 SET_INT(result, 14, ru->ru_nvcsw);
4413 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004414#undef SET_INT
4415
Victor Stinner8c62be82010-05-06 00:08:46 +00004416 if (PyErr_Occurred()) {
4417 Py_DECREF(result);
4418 return NULL;
4419 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004420
Victor Stinner8c62be82010-05-06 00:08:46 +00004421 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004422}
4423#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4424
4425#ifdef HAVE_WAIT3
4426PyDoc_STRVAR(posix_wait3__doc__,
4427"wait3(options) -> (pid, status, rusage)\n\n\
4428Wait for completion of a child process.");
4429
4430static PyObject *
4431posix_wait3(PyObject *self, PyObject *args)
4432{
Victor Stinner8c62be82010-05-06 00:08:46 +00004433 pid_t pid;
4434 int options;
4435 struct rusage ru;
4436 WAIT_TYPE status;
4437 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004438
Victor Stinner8c62be82010-05-06 00:08:46 +00004439 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4440 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004441
Victor Stinner8c62be82010-05-06 00:08:46 +00004442 Py_BEGIN_ALLOW_THREADS
4443 pid = wait3(&status, options, &ru);
4444 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004445
Victor Stinner8c62be82010-05-06 00:08:46 +00004446 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004447}
4448#endif /* HAVE_WAIT3 */
4449
4450#ifdef HAVE_WAIT4
4451PyDoc_STRVAR(posix_wait4__doc__,
4452"wait4(pid, options) -> (pid, status, rusage)\n\n\
4453Wait for completion of a given child process.");
4454
4455static PyObject *
4456posix_wait4(PyObject *self, PyObject *args)
4457{
Victor Stinner8c62be82010-05-06 00:08:46 +00004458 pid_t pid;
4459 int options;
4460 struct rusage ru;
4461 WAIT_TYPE status;
4462 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004463
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4465 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004466
Victor Stinner8c62be82010-05-06 00:08:46 +00004467 Py_BEGIN_ALLOW_THREADS
4468 pid = wait4(pid, &status, options, &ru);
4469 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004470
Victor Stinner8c62be82010-05-06 00:08:46 +00004471 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004472}
4473#endif /* HAVE_WAIT4 */
4474
Guido van Rossumb6775db1994-08-01 11:34:53 +00004475#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004476PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004477"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004478Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004479
Barry Warsaw53699e91996-12-10 23:23:01 +00004480static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004481posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004482{
Victor Stinner8c62be82010-05-06 00:08:46 +00004483 pid_t pid;
4484 int options;
4485 WAIT_TYPE status;
4486 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004487
Victor Stinner8c62be82010-05-06 00:08:46 +00004488 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4489 return NULL;
4490 Py_BEGIN_ALLOW_THREADS
4491 pid = waitpid(pid, &status, options);
4492 Py_END_ALLOW_THREADS
4493 if (pid == -1)
4494 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004495
Victor Stinner8c62be82010-05-06 00:08:46 +00004496 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004497}
4498
Tim Petersab034fa2002-02-01 11:27:43 +00004499#elif defined(HAVE_CWAIT)
4500
4501/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004502PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004503"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004504"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004505
4506static PyObject *
4507posix_waitpid(PyObject *self, PyObject *args)
4508{
Victor Stinner8c62be82010-05-06 00:08:46 +00004509 Py_intptr_t pid;
4510 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004511
Victor Stinner8c62be82010-05-06 00:08:46 +00004512 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4513 return NULL;
4514 Py_BEGIN_ALLOW_THREADS
4515 pid = _cwait(&status, pid, options);
4516 Py_END_ALLOW_THREADS
4517 if (pid == -1)
4518 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004519
Victor Stinner8c62be82010-05-06 00:08:46 +00004520 /* shift the status left a byte so this is more like the POSIX waitpid */
4521 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004522}
4523#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004524
Guido van Rossumad0ee831995-03-01 10:34:45 +00004525#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004526PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004527"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004528Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004529
Barry Warsaw53699e91996-12-10 23:23:01 +00004530static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004531posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004532{
Victor Stinner8c62be82010-05-06 00:08:46 +00004533 pid_t pid;
4534 WAIT_TYPE status;
4535 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004536
Victor Stinner8c62be82010-05-06 00:08:46 +00004537 Py_BEGIN_ALLOW_THREADS
4538 pid = wait(&status);
4539 Py_END_ALLOW_THREADS
4540 if (pid == -1)
4541 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004542
Victor Stinner8c62be82010-05-06 00:08:46 +00004543 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004544}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004545#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004546
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004548PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004549"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004550Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004551
Barry Warsaw53699e91996-12-10 23:23:01 +00004552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004553posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004554{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004555#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00004556 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004557#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004558#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004559 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004560#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004561 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004562#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004563#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004564}
4565
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004566
Guido van Rossumb6775db1994-08-01 11:34:53 +00004567#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004568PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004569"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004570Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004571
Barry Warsaw53699e91996-12-10 23:23:01 +00004572static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004573posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004574{
Victor Stinner8c62be82010-05-06 00:08:46 +00004575 PyObject* v;
4576 char buf[MAXPATHLEN];
4577 PyObject *opath;
4578 char *path;
4579 int n;
4580 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004581
Victor Stinner8c62be82010-05-06 00:08:46 +00004582 if (!PyArg_ParseTuple(args, "O&:readlink",
4583 PyUnicode_FSConverter, &opath))
4584 return NULL;
4585 path = PyBytes_AsString(opath);
4586 v = PySequence_GetItem(args, 0);
4587 if (v == NULL) {
4588 Py_DECREF(opath);
4589 return NULL;
4590 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004591
Victor Stinner8c62be82010-05-06 00:08:46 +00004592 if (PyUnicode_Check(v)) {
4593 arg_is_unicode = 1;
4594 }
4595 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004596
Victor Stinner8c62be82010-05-06 00:08:46 +00004597 Py_BEGIN_ALLOW_THREADS
4598 n = readlink(path, buf, (int) sizeof buf);
4599 Py_END_ALLOW_THREADS
4600 if (n < 0)
4601 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004602
Victor Stinner8c62be82010-05-06 00:08:46 +00004603 Py_DECREF(opath);
4604 v = PyBytes_FromStringAndSize(buf, n);
4605 if (arg_is_unicode) {
4606 PyObject *w;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004607
Victor Stinner8c62be82010-05-06 00:08:46 +00004608 w = PyUnicode_FromEncodedObject(v,
4609 Py_FileSystemDefaultEncoding,
4610 "surrogateescape");
4611 if (w != NULL) {
4612 Py_DECREF(v);
4613 v = w;
4614 }
4615 else {
4616 v = NULL;
4617 }
4618 }
4619 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004620}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004621#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004622
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004623
Guido van Rossumb6775db1994-08-01 11:34:53 +00004624#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004625PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004626"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004627Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004628
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004629static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004630posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004631{
Victor Stinner8c62be82010-05-06 00:08:46 +00004632 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004633}
4634#endif /* HAVE_SYMLINK */
4635
4636
4637#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004638#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4639static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004640system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004641{
4642 ULONG value = 0;
4643
4644 Py_BEGIN_ALLOW_THREADS
4645 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4646 Py_END_ALLOW_THREADS
4647
4648 return value;
4649}
4650
4651static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004652posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004653{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004654 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00004655 return Py_BuildValue("ddddd",
4656 (double)0 /* t.tms_utime / HZ */,
4657 (double)0 /* t.tms_stime / HZ */,
4658 (double)0 /* t.tms_cutime / HZ */,
4659 (double)0 /* t.tms_cstime / HZ */,
4660 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004661}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004662#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004663#define NEED_TICKS_PER_SECOND
4664static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004665static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004666posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004667{
Victor Stinner8c62be82010-05-06 00:08:46 +00004668 struct tms t;
4669 clock_t c;
4670 errno = 0;
4671 c = times(&t);
4672 if (c == (clock_t) -1)
4673 return posix_error();
4674 return Py_BuildValue("ddddd",
4675 (double)t.tms_utime / ticks_per_second,
4676 (double)t.tms_stime / ticks_per_second,
4677 (double)t.tms_cutime / ticks_per_second,
4678 (double)t.tms_cstime / ticks_per_second,
4679 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004680}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004681#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004682#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004683
4684
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004685#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004686#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004687static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004688posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004689{
Victor Stinner8c62be82010-05-06 00:08:46 +00004690 FILETIME create, exit, kernel, user;
4691 HANDLE hProc;
4692 hProc = GetCurrentProcess();
4693 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4694 /* The fields of a FILETIME structure are the hi and lo part
4695 of a 64-bit value expressed in 100 nanosecond units.
4696 1e7 is one second in such units; 1e-7 the inverse.
4697 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4698 */
4699 return Py_BuildValue(
4700 "ddddd",
4701 (double)(user.dwHighDateTime*429.4967296 +
4702 user.dwLowDateTime*1e-7),
4703 (double)(kernel.dwHighDateTime*429.4967296 +
4704 kernel.dwLowDateTime*1e-7),
4705 (double)0,
4706 (double)0,
4707 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004708}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004709#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004710
4711#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004712PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004713"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004714Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004715#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004717
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004718#ifdef HAVE_GETSID
4719PyDoc_STRVAR(posix_getsid__doc__,
4720"getsid(pid) -> sid\n\n\
4721Call the system call getsid().");
4722
4723static PyObject *
4724posix_getsid(PyObject *self, PyObject *args)
4725{
Victor Stinner8c62be82010-05-06 00:08:46 +00004726 pid_t pid;
4727 int sid;
4728 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
4729 return NULL;
4730 sid = getsid(pid);
4731 if (sid < 0)
4732 return posix_error();
4733 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004734}
4735#endif /* HAVE_GETSID */
4736
4737
Guido van Rossumb6775db1994-08-01 11:34:53 +00004738#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004739PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004740"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004741Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004742
Barry Warsaw53699e91996-12-10 23:23:01 +00004743static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004744posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004745{
Victor Stinner8c62be82010-05-06 00:08:46 +00004746 if (setsid() < 0)
4747 return posix_error();
4748 Py_INCREF(Py_None);
4749 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004750}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004751#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004752
Guido van Rossumb6775db1994-08-01 11:34:53 +00004753#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004754PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004755"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004756Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004757
Barry Warsaw53699e91996-12-10 23:23:01 +00004758static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004759posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004760{
Victor Stinner8c62be82010-05-06 00:08:46 +00004761 pid_t pid;
4762 int pgrp;
4763 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
4764 return NULL;
4765 if (setpgid(pid, pgrp) < 0)
4766 return posix_error();
4767 Py_INCREF(Py_None);
4768 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004769}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004770#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004771
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004772
Guido van Rossumb6775db1994-08-01 11:34:53 +00004773#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004774PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004775"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004776Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004777
Barry Warsaw53699e91996-12-10 23:23:01 +00004778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004779posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004780{
Victor Stinner8c62be82010-05-06 00:08:46 +00004781 int fd;
4782 pid_t pgid;
4783 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
4784 return NULL;
4785 pgid = tcgetpgrp(fd);
4786 if (pgid < 0)
4787 return posix_error();
4788 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004789}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004790#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004791
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004792
Guido van Rossumb6775db1994-08-01 11:34:53 +00004793#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004794PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004795"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004796Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004797
Barry Warsaw53699e91996-12-10 23:23:01 +00004798static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004799posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004800{
Victor Stinner8c62be82010-05-06 00:08:46 +00004801 int fd;
4802 pid_t pgid;
4803 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
4804 return NULL;
4805 if (tcsetpgrp(fd, pgid) < 0)
4806 return posix_error();
4807 Py_INCREF(Py_None);
4808 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004809}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004810#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004811
Guido van Rossum687dd131993-05-17 08:34:16 +00004812/* Functions acting on file descriptors */
4813
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004814PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004815"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004816Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004817
Barry Warsaw53699e91996-12-10 23:23:01 +00004818static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004819posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004820{
Victor Stinner8c62be82010-05-06 00:08:46 +00004821 PyObject *ofile;
4822 char *file;
4823 int flag;
4824 int mode = 0777;
4825 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004826
4827#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004828 PyUnicodeObject *po;
4829 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4830 Py_BEGIN_ALLOW_THREADS
4831 /* PyUnicode_AS_UNICODE OK without thread
4832 lock as it is a simple dereference. */
4833 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4834 Py_END_ALLOW_THREADS
4835 if (fd < 0)
4836 return posix_error();
4837 return PyLong_FromLong((long)fd);
4838 }
4839 /* Drop the argument parsing error as narrow strings
4840 are also valid. */
4841 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004842#endif
4843
Victor Stinner8c62be82010-05-06 00:08:46 +00004844 if (!PyArg_ParseTuple(args, "O&i|i",
4845 PyUnicode_FSConverter, &ofile,
4846 &flag, &mode))
4847 return NULL;
4848 file = PyBytes_AsString(ofile);
4849 Py_BEGIN_ALLOW_THREADS
4850 fd = open(file, flag, mode);
4851 Py_END_ALLOW_THREADS
4852 if (fd < 0)
4853 return posix_error_with_allocated_filename(ofile);
4854 Py_DECREF(ofile);
4855 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004856}
4857
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004858
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004859PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004860"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004861Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004862
Barry Warsaw53699e91996-12-10 23:23:01 +00004863static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004864posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004865{
Victor Stinner8c62be82010-05-06 00:08:46 +00004866 int fd, res;
4867 if (!PyArg_ParseTuple(args, "i:close", &fd))
4868 return NULL;
4869 if (!_PyVerify_fd(fd))
4870 return posix_error();
4871 Py_BEGIN_ALLOW_THREADS
4872 res = close(fd);
4873 Py_END_ALLOW_THREADS
4874 if (res < 0)
4875 return posix_error();
4876 Py_INCREF(Py_None);
4877 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004878}
4879
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004880
Victor Stinner8c62be82010-05-06 00:08:46 +00004881PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00004882"closerange(fd_low, fd_high)\n\n\
4883Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4884
4885static PyObject *
4886posix_closerange(PyObject *self, PyObject *args)
4887{
Victor Stinner8c62be82010-05-06 00:08:46 +00004888 int fd_from, fd_to, i;
4889 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4890 return NULL;
4891 Py_BEGIN_ALLOW_THREADS
4892 for (i = fd_from; i < fd_to; i++)
4893 if (_PyVerify_fd(i))
4894 close(i);
4895 Py_END_ALLOW_THREADS
4896 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00004897}
4898
4899
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004900PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004901"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004902Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004903
Barry Warsaw53699e91996-12-10 23:23:01 +00004904static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004905posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004906{
Victor Stinner8c62be82010-05-06 00:08:46 +00004907 int fd;
4908 if (!PyArg_ParseTuple(args, "i:dup", &fd))
4909 return NULL;
4910 if (!_PyVerify_fd(fd))
4911 return posix_error();
4912 Py_BEGIN_ALLOW_THREADS
4913 fd = dup(fd);
4914 Py_END_ALLOW_THREADS
4915 if (fd < 0)
4916 return posix_error();
4917 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004918}
4919
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004920
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004921PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004922"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004923Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004924
Barry Warsaw53699e91996-12-10 23:23:01 +00004925static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004926posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004927{
Victor Stinner8c62be82010-05-06 00:08:46 +00004928 int fd, fd2, res;
4929 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
4930 return NULL;
4931 if (!_PyVerify_fd_dup2(fd, fd2))
4932 return posix_error();
4933 Py_BEGIN_ALLOW_THREADS
4934 res = dup2(fd, fd2);
4935 Py_END_ALLOW_THREADS
4936 if (res < 0)
4937 return posix_error();
4938 Py_INCREF(Py_None);
4939 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004940}
4941
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004942
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004943PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004944"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004945Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004946
Barry Warsaw53699e91996-12-10 23:23:01 +00004947static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004948posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004949{
Victor Stinner8c62be82010-05-06 00:08:46 +00004950 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004951#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00004952 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004953#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004954 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004955#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004956 PyObject *posobj;
4957 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
4958 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004959#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00004960 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4961 switch (how) {
4962 case 0: how = SEEK_SET; break;
4963 case 1: how = SEEK_CUR; break;
4964 case 2: how = SEEK_END; break;
4965 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004966#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004967
4968#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00004969 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004970#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004971 pos = PyLong_Check(posobj) ?
4972 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004973#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 if (PyErr_Occurred())
4975 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004976
Victor Stinner8c62be82010-05-06 00:08:46 +00004977 if (!_PyVerify_fd(fd))
4978 return posix_error();
4979 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004980#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00004981 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004982#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004983 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004984#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 Py_END_ALLOW_THREADS
4986 if (res < 0)
4987 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004988
4989#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00004990 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004991#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004992 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004993#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004994}
4995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004996
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004997PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004998"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004999Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005000
Barry Warsaw53699e91996-12-10 23:23:01 +00005001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005002posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005003{
Victor Stinner8c62be82010-05-06 00:08:46 +00005004 int fd, size;
5005 Py_ssize_t n;
5006 PyObject *buffer;
5007 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5008 return NULL;
5009 if (size < 0) {
5010 errno = EINVAL;
5011 return posix_error();
5012 }
5013 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5014 if (buffer == NULL)
5015 return NULL;
5016 if (!_PyVerify_fd(fd))
5017 return posix_error();
5018 Py_BEGIN_ALLOW_THREADS
5019 n = read(fd, PyBytes_AS_STRING(buffer), size);
5020 Py_END_ALLOW_THREADS
5021 if (n < 0) {
5022 Py_DECREF(buffer);
5023 return posix_error();
5024 }
5025 if (n != size)
5026 _PyBytes_Resize(&buffer, n);
5027 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005028}
5029
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005031PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005032"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005033Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005034
Barry Warsaw53699e91996-12-10 23:23:01 +00005035static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005036posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005037{
Victor Stinner8c62be82010-05-06 00:08:46 +00005038 Py_buffer pbuf;
5039 int fd;
5040 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005041
Victor Stinner8c62be82010-05-06 00:08:46 +00005042 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5043 return NULL;
5044 if (!_PyVerify_fd(fd))
5045 return posix_error();
5046 Py_BEGIN_ALLOW_THREADS
5047 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5048 Py_END_ALLOW_THREADS
5049 PyBuffer_Release(&pbuf);
5050 if (size < 0)
5051 return posix_error();
5052 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005053}
5054
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005056PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005057"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005058Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005059
Barry Warsaw53699e91996-12-10 23:23:01 +00005060static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005061posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005062{
Victor Stinner8c62be82010-05-06 00:08:46 +00005063 int fd;
5064 STRUCT_STAT st;
5065 int res;
5066 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5067 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005068#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005069 /* on OpenVMS we must ensure that all bytes are written to the file */
5070 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005071#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005072 if (!_PyVerify_fd(fd))
5073 return posix_error();
5074 Py_BEGIN_ALLOW_THREADS
5075 res = FSTAT(fd, &st);
5076 Py_END_ALLOW_THREADS
5077 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005078#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005079 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005080#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005081 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005082#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005083 }
Tim Peters5aa91602002-01-30 05:46:57 +00005084
Victor Stinner8c62be82010-05-06 00:08:46 +00005085 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005086}
5087
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005088PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005089"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005090Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005091connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005092
5093static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005094posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005095{
Victor Stinner8c62be82010-05-06 00:08:46 +00005096 int fd;
5097 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5098 return NULL;
5099 if (!_PyVerify_fd(fd))
5100 return PyBool_FromLong(0);
5101 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005102}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005103
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005104#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005105PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005106"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005107Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005108
Barry Warsaw53699e91996-12-10 23:23:01 +00005109static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005110posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005111{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005112#if defined(PYOS_OS2)
5113 HFILE read, write;
5114 APIRET rc;
5115
Victor Stinner8c62be82010-05-06 00:08:46 +00005116 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005117 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005118 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005119 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005120 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005121
5122 return Py_BuildValue("(ii)", read, write);
5123#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005124#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005125 int fds[2];
5126 int res;
5127 Py_BEGIN_ALLOW_THREADS
5128 res = pipe(fds);
5129 Py_END_ALLOW_THREADS
5130 if (res != 0)
5131 return posix_error();
5132 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005133#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005134 HANDLE read, write;
5135 int read_fd, write_fd;
5136 BOOL ok;
5137 Py_BEGIN_ALLOW_THREADS
5138 ok = CreatePipe(&read, &write, NULL, 0);
5139 Py_END_ALLOW_THREADS
5140 if (!ok)
5141 return win32_error("CreatePipe", NULL);
5142 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5143 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5144 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005145#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005146#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005147}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005148#endif /* HAVE_PIPE */
5149
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005150
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005151#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005152PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005153"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005154Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005155
Barry Warsaw53699e91996-12-10 23:23:01 +00005156static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005157posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005158{
Victor Stinner8c62be82010-05-06 00:08:46 +00005159 char *filename;
5160 int mode = 0666;
5161 int res;
5162 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
5163 return NULL;
5164 Py_BEGIN_ALLOW_THREADS
5165 res = mkfifo(filename, mode);
5166 Py_END_ALLOW_THREADS
5167 if (res < 0)
5168 return posix_error();
5169 Py_INCREF(Py_None);
5170 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005171}
5172#endif
5173
5174
Neal Norwitz11690112002-07-30 01:08:28 +00005175#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005176PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005177"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005178Create a filesystem node (file, device special file or named pipe)\n\
5179named filename. mode specifies both the permissions to use and the\n\
5180type of node to be created, being combined (bitwise OR) with one of\n\
5181S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005182device defines the newly created device special file (probably using\n\
5183os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005184
5185
5186static PyObject *
5187posix_mknod(PyObject *self, PyObject *args)
5188{
Victor Stinner8c62be82010-05-06 00:08:46 +00005189 char *filename;
5190 int mode = 0600;
5191 int device = 0;
5192 int res;
5193 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
5194 return NULL;
5195 Py_BEGIN_ALLOW_THREADS
5196 res = mknod(filename, mode, device);
5197 Py_END_ALLOW_THREADS
5198 if (res < 0)
5199 return posix_error();
5200 Py_INCREF(Py_None);
5201 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005202}
5203#endif
5204
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005205#ifdef HAVE_DEVICE_MACROS
5206PyDoc_STRVAR(posix_major__doc__,
5207"major(device) -> major number\n\
5208Extracts a device major number from a raw device number.");
5209
5210static PyObject *
5211posix_major(PyObject *self, PyObject *args)
5212{
Victor Stinner8c62be82010-05-06 00:08:46 +00005213 int device;
5214 if (!PyArg_ParseTuple(args, "i:major", &device))
5215 return NULL;
5216 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005217}
5218
5219PyDoc_STRVAR(posix_minor__doc__,
5220"minor(device) -> minor number\n\
5221Extracts a device minor number from a raw device number.");
5222
5223static PyObject *
5224posix_minor(PyObject *self, PyObject *args)
5225{
Victor Stinner8c62be82010-05-06 00:08:46 +00005226 int device;
5227 if (!PyArg_ParseTuple(args, "i:minor", &device))
5228 return NULL;
5229 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005230}
5231
5232PyDoc_STRVAR(posix_makedev__doc__,
5233"makedev(major, minor) -> device number\n\
5234Composes a raw device number from the major and minor device numbers.");
5235
5236static PyObject *
5237posix_makedev(PyObject *self, PyObject *args)
5238{
Victor Stinner8c62be82010-05-06 00:08:46 +00005239 int major, minor;
5240 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5241 return NULL;
5242 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005243}
5244#endif /* device macros */
5245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005246
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005247#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005248PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005249"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005250Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005251
Barry Warsaw53699e91996-12-10 23:23:01 +00005252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005253posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005254{
Victor Stinner8c62be82010-05-06 00:08:46 +00005255 int fd;
5256 off_t length;
5257 int res;
5258 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005259
Victor Stinner8c62be82010-05-06 00:08:46 +00005260 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5261 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005262
5263#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005264 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005265#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005266 length = PyLong_Check(lenobj) ?
5267 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005268#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005269 if (PyErr_Occurred())
5270 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005271
Victor Stinner8c62be82010-05-06 00:08:46 +00005272 Py_BEGIN_ALLOW_THREADS
5273 res = ftruncate(fd, length);
5274 Py_END_ALLOW_THREADS
5275 if (res < 0)
5276 return posix_error();
5277 Py_INCREF(Py_None);
5278 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005279}
5280#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005281
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005282#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005283PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005284"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005285Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005286
Fred Drake762e2061999-08-26 17:23:54 +00005287/* Save putenv() parameters as values here, so we can collect them when they
5288 * get re-set with another call for the same key. */
5289static PyObject *posix_putenv_garbage;
5290
Tim Peters5aa91602002-01-30 05:46:57 +00005291static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005292posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005293{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005294#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005295 wchar_t *s1, *s2;
5296 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005297#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005298 PyObject *os1, *os2;
5299 char *s1, *s2;
5300 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005301#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005302 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005303 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005304
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005305#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005306 if (!PyArg_ParseTuple(args,
5307 "uu:putenv",
5308 &s1, &s2))
5309 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005310#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005311 if (!PyArg_ParseTuple(args,
5312 "O&O&:putenv",
5313 PyUnicode_FSConverter, &os1,
5314 PyUnicode_FSConverter, &os2))
5315 return NULL;
5316 s1 = PyBytes_AsString(os1);
5317 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005318#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005319
5320#if defined(PYOS_OS2)
5321 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5322 APIRET rc;
5323
Guido van Rossumd48f2521997-12-05 22:19:34 +00005324 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005325 if (rc != NO_ERROR) {
5326 os2_error(rc);
5327 goto error;
5328 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005329
5330 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5331 APIRET rc;
5332
Guido van Rossumd48f2521997-12-05 22:19:34 +00005333 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005334 if (rc != NO_ERROR) {
5335 os2_error(rc);
5336 goto error;
5337 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005338 } else {
5339#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005340 /* XXX This can leak memory -- not easy to fix :-( */
5341 /* len includes space for a trailing \0; the size arg to
5342 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005343#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005344 len = wcslen(s1) + wcslen(s2) + 2;
5345 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005346#else
Victor Stinner84ae1182010-05-06 22:05:07 +00005347 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00005348 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005349#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005350 if (newstr == NULL) {
5351 PyErr_NoMemory();
5352 goto error;
5353 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005354#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005355 newenv = PyUnicode_AsUnicode(newstr);
5356 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5357 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005358 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005359 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005360 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005361#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005362 newenv = PyBytes_AS_STRING(newstr);
5363 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5364 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005365 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005366 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005367 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005368#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005369
Victor Stinner8c62be82010-05-06 00:08:46 +00005370 /* Install the first arg and newstr in posix_putenv_garbage;
5371 * this will cause previous value to be collected. This has to
5372 * happen after the real putenv() call because the old value
5373 * was still accessible until then. */
5374 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005375#ifdef MS_WINDOWS
5376 PyTuple_GET_ITEM(args, 0),
5377#else
5378 os1,
5379#endif
5380 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005381 /* really not much we can do; just leak */
5382 PyErr_Clear();
5383 }
5384 else {
5385 Py_DECREF(newstr);
5386 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005387
5388#if defined(PYOS_OS2)
5389 }
5390#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005391
Martin v. Löwis011e8422009-05-05 04:43:17 +00005392#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005393 Py_DECREF(os1);
5394 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005395#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005396 Py_RETURN_NONE;
5397
5398error:
5399#ifndef MS_WINDOWS
5400 Py_DECREF(os1);
5401 Py_DECREF(os2);
5402#endif
5403 Py_XDECREF(newstr);
5404 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005405}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005406#endif /* putenv */
5407
Guido van Rossumc524d952001-10-19 01:31:59 +00005408#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005409PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005410"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005411Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005412
5413static PyObject *
5414posix_unsetenv(PyObject *self, PyObject *args)
5415{
Victor Stinner84ae1182010-05-06 22:05:07 +00005416#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005417 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005418
Victor Stinner8c62be82010-05-06 00:08:46 +00005419 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5420 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00005421#else
5422 PyObject *os1;
5423 char *s1;
5424
5425 if (!PyArg_ParseTuple(args, "O&:unsetenv",
5426 PyUnicode_FSConverter, &os1))
5427 return NULL;
5428 s1 = PyBytes_AsString(os1);
5429#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00005430
Victor Stinner8c62be82010-05-06 00:08:46 +00005431 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00005432
Victor Stinner8c62be82010-05-06 00:08:46 +00005433 /* Remove the key from posix_putenv_garbage;
5434 * this will cause it to be collected. This has to
5435 * happen after the real unsetenv() call because the
5436 * old value was still accessible until then.
5437 */
5438 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005439#ifdef MS_WINDOWS
5440 PyTuple_GET_ITEM(args, 0)
5441#else
5442 os1
5443#endif
5444 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005445 /* really not much we can do; just leak */
5446 PyErr_Clear();
5447 }
Guido van Rossumc524d952001-10-19 01:31:59 +00005448
Victor Stinner84ae1182010-05-06 22:05:07 +00005449#ifndef MS_WINDOWS
5450 Py_DECREF(os1);
5451#endif
5452 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00005453}
5454#endif /* unsetenv */
5455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005456PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005457"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005458Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005459
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005460static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005461posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005462{
Victor Stinner8c62be82010-05-06 00:08:46 +00005463 int code;
5464 char *message;
5465 if (!PyArg_ParseTuple(args, "i:strerror", &code))
5466 return NULL;
5467 message = strerror(code);
5468 if (message == NULL) {
5469 PyErr_SetString(PyExc_ValueError,
5470 "strerror() argument out of range");
5471 return NULL;
5472 }
5473 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005474}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005475
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005476
Guido van Rossumc9641791998-08-04 15:26:23 +00005477#ifdef HAVE_SYS_WAIT_H
5478
Fred Drake106c1a02002-04-23 15:58:02 +00005479#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005480PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005481"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005482Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005483
5484static PyObject *
5485posix_WCOREDUMP(PyObject *self, PyObject *args)
5486{
Victor Stinner8c62be82010-05-06 00:08:46 +00005487 WAIT_TYPE status;
5488 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005489
Victor Stinner8c62be82010-05-06 00:08:46 +00005490 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
5491 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005492
Victor Stinner8c62be82010-05-06 00:08:46 +00005493 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005494}
5495#endif /* WCOREDUMP */
5496
5497#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005498PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005499"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005500Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005501job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005502
5503static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005504posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005505{
Victor Stinner8c62be82010-05-06 00:08:46 +00005506 WAIT_TYPE status;
5507 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005508
Victor Stinner8c62be82010-05-06 00:08:46 +00005509 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
5510 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005511
Victor Stinner8c62be82010-05-06 00:08:46 +00005512 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005513}
5514#endif /* WIFCONTINUED */
5515
Guido van Rossumc9641791998-08-04 15:26:23 +00005516#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005517PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005518"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005519Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005520
5521static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005522posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005523{
Victor Stinner8c62be82010-05-06 00:08:46 +00005524 WAIT_TYPE status;
5525 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005526
Victor Stinner8c62be82010-05-06 00:08:46 +00005527 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
5528 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005529
Victor Stinner8c62be82010-05-06 00:08:46 +00005530 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005531}
5532#endif /* WIFSTOPPED */
5533
5534#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005535PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005536"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005537Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005538
5539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005540posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005541{
Victor Stinner8c62be82010-05-06 00:08:46 +00005542 WAIT_TYPE status;
5543 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005544
Victor Stinner8c62be82010-05-06 00:08:46 +00005545 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
5546 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005547
Victor Stinner8c62be82010-05-06 00:08:46 +00005548 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005549}
5550#endif /* WIFSIGNALED */
5551
5552#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005553PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005554"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005555Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005556system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005557
5558static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005559posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005560{
Victor Stinner8c62be82010-05-06 00:08:46 +00005561 WAIT_TYPE status;
5562 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005563
Victor Stinner8c62be82010-05-06 00:08:46 +00005564 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
5565 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005566
Victor Stinner8c62be82010-05-06 00:08:46 +00005567 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005568}
5569#endif /* WIFEXITED */
5570
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005571#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005572PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005573"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005574Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005575
5576static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005577posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005578{
Victor Stinner8c62be82010-05-06 00:08:46 +00005579 WAIT_TYPE status;
5580 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005581
Victor Stinner8c62be82010-05-06 00:08:46 +00005582 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
5583 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005584
Victor Stinner8c62be82010-05-06 00:08:46 +00005585 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005586}
5587#endif /* WEXITSTATUS */
5588
5589#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005590PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005591"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005592Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005593value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005594
5595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005596posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005597{
Victor Stinner8c62be82010-05-06 00:08:46 +00005598 WAIT_TYPE status;
5599 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005600
Victor Stinner8c62be82010-05-06 00:08:46 +00005601 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
5602 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005603
Victor Stinner8c62be82010-05-06 00:08:46 +00005604 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005605}
5606#endif /* WTERMSIG */
5607
5608#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005609PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005610"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005611Return the signal that stopped the process that provided\n\
5612the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005613
5614static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005615posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005616{
Victor Stinner8c62be82010-05-06 00:08:46 +00005617 WAIT_TYPE status;
5618 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005619
Victor Stinner8c62be82010-05-06 00:08:46 +00005620 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
5621 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005622
Victor Stinner8c62be82010-05-06 00:08:46 +00005623 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005624}
5625#endif /* WSTOPSIG */
5626
5627#endif /* HAVE_SYS_WAIT_H */
5628
5629
Thomas Wouters477c8d52006-05-27 19:21:47 +00005630#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005631#ifdef _SCO_DS
5632/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5633 needed definitions in sys/statvfs.h */
5634#define _SVID3
5635#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005636#include <sys/statvfs.h>
5637
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005638static PyObject*
5639_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005640 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5641 if (v == NULL)
5642 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005643
5644#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005645 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5646 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5647 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5648 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5649 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5650 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5651 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5652 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5653 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5654 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005655#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005656 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5657 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5658 PyStructSequence_SET_ITEM(v, 2,
5659 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
5660 PyStructSequence_SET_ITEM(v, 3,
5661 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
5662 PyStructSequence_SET_ITEM(v, 4,
5663 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
5664 PyStructSequence_SET_ITEM(v, 5,
5665 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
5666 PyStructSequence_SET_ITEM(v, 6,
5667 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
5668 PyStructSequence_SET_ITEM(v, 7,
5669 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
5670 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5671 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005672#endif
5673
Victor Stinner8c62be82010-05-06 00:08:46 +00005674 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005675}
5676
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005677PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005678"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005679Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005680
5681static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005682posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005683{
Victor Stinner8c62be82010-05-06 00:08:46 +00005684 int fd, res;
5685 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005686
Victor Stinner8c62be82010-05-06 00:08:46 +00005687 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
5688 return NULL;
5689 Py_BEGIN_ALLOW_THREADS
5690 res = fstatvfs(fd, &st);
5691 Py_END_ALLOW_THREADS
5692 if (res != 0)
5693 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005694
Victor Stinner8c62be82010-05-06 00:08:46 +00005695 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005696}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005697#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005698
5699
Thomas Wouters477c8d52006-05-27 19:21:47 +00005700#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005701#include <sys/statvfs.h>
5702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005703PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005704"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005705Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005706
5707static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005708posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005709{
Victor Stinner8c62be82010-05-06 00:08:46 +00005710 char *path;
5711 int res;
5712 struct statvfs st;
5713 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
5714 return NULL;
5715 Py_BEGIN_ALLOW_THREADS
5716 res = statvfs(path, &st);
5717 Py_END_ALLOW_THREADS
5718 if (res != 0)
5719 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005720
Victor Stinner8c62be82010-05-06 00:08:46 +00005721 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005722}
5723#endif /* HAVE_STATVFS */
5724
Fred Drakec9680921999-12-13 16:37:25 +00005725/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5726 * It maps strings representing configuration variable names to
5727 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005728 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005729 * rarely-used constants. There are three separate tables that use
5730 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005731 *
5732 * This code is always included, even if none of the interfaces that
5733 * need it are included. The #if hackery needed to avoid it would be
5734 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005735 */
5736struct constdef {
5737 char *name;
5738 long value;
5739};
5740
Fred Drake12c6e2d1999-12-14 21:25:03 +00005741static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005742conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005743 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005744{
Christian Heimes217cfd12007-12-02 14:31:20 +00005745 if (PyLong_Check(arg)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005746 *valuep = PyLong_AS_LONG(arg);
5747 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005748 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005749 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00005750 /* look up the value in the table using a binary search */
5751 size_t lo = 0;
5752 size_t mid;
5753 size_t hi = tablesize;
5754 int cmp;
5755 const char *confname;
5756 if (!PyUnicode_Check(arg)) {
5757 PyErr_SetString(PyExc_TypeError,
5758 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005759 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005760 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005761 confname = _PyUnicode_AsString(arg);
5762 if (confname == NULL)
5763 return 0;
5764 while (lo < hi) {
5765 mid = (lo + hi) / 2;
5766 cmp = strcmp(confname, table[mid].name);
5767 if (cmp < 0)
5768 hi = mid;
5769 else if (cmp > 0)
5770 lo = mid + 1;
5771 else {
5772 *valuep = table[mid].value;
5773 return 1;
5774 }
5775 }
5776 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5777 return 0;
5778 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005779}
5780
5781
5782#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5783static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005784#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00005786#endif
5787#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00005789#endif
Fred Drakec9680921999-12-13 16:37:25 +00005790#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005791 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005792#endif
5793#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00005794 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00005795#endif
5796#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00005797 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00005798#endif
5799#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00005800 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00005801#endif
5802#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005803 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005804#endif
5805#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00005806 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00005807#endif
5808#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00005809 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00005810#endif
5811#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005812 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005813#endif
5814#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00005815 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00005816#endif
5817#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005818 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005819#endif
5820#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00005821 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00005822#endif
5823#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005824 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005825#endif
5826#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00005827 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00005828#endif
5829#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005830 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005831#endif
5832#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00005833 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00005834#endif
5835};
5836
Fred Drakec9680921999-12-13 16:37:25 +00005837static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005838conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005839{
5840 return conv_confname(arg, valuep, posix_constants_pathconf,
5841 sizeof(posix_constants_pathconf)
5842 / sizeof(struct constdef));
5843}
5844#endif
5845
5846#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005847PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005848"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005849Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005850If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005851
5852static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005853posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005854{
5855 PyObject *result = NULL;
5856 int name, fd;
5857
Fred Drake12c6e2d1999-12-14 21:25:03 +00005858 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5859 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005860 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005861
Victor Stinner8c62be82010-05-06 00:08:46 +00005862 errno = 0;
5863 limit = fpathconf(fd, name);
5864 if (limit == -1 && errno != 0)
5865 posix_error();
5866 else
5867 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005868 }
5869 return result;
5870}
5871#endif
5872
5873
5874#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005875PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005876"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005877Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005878If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005879
5880static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005881posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005882{
5883 PyObject *result = NULL;
5884 int name;
5885 char *path;
5886
5887 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5888 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005889 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005890
Victor Stinner8c62be82010-05-06 00:08:46 +00005891 errno = 0;
5892 limit = pathconf(path, name);
5893 if (limit == -1 && errno != 0) {
5894 if (errno == EINVAL)
5895 /* could be a path or name problem */
5896 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00005897 else
Victor Stinner8c62be82010-05-06 00:08:46 +00005898 posix_error_with_filename(path);
5899 }
5900 else
5901 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005902 }
5903 return result;
5904}
5905#endif
5906
5907#ifdef HAVE_CONFSTR
5908static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005909#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00005911#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00005912#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00005914#endif
5915#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005916 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00005917#endif
Fred Draked86ed291999-12-15 15:34:33 +00005918#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005919 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00005920#endif
5921#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00005922 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00005923#endif
5924#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00005925 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00005926#endif
5927#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005928 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00005929#endif
Fred Drakec9680921999-12-13 16:37:25 +00005930#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005931 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005932#endif
5933#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005935#endif
5936#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005937 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005938#endif
5939#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005941#endif
5942#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005944#endif
5945#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005947#endif
5948#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005949 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005950#endif
5951#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005952 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005953#endif
Fred Draked86ed291999-12-15 15:34:33 +00005954#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00005955 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00005956#endif
Fred Drakec9680921999-12-13 16:37:25 +00005957#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00005958 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00005959#endif
Fred Draked86ed291999-12-15 15:34:33 +00005960#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00005961 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00005962#endif
5963#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00005964 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00005965#endif
5966#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005967 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00005968#endif
5969#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005970 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00005971#endif
Fred Drakec9680921999-12-13 16:37:25 +00005972#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005973 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005974#endif
5975#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005976 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005977#endif
5978#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005980#endif
5981#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005982 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005983#endif
5984#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005985 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005986#endif
5987#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005989#endif
5990#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005991 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005992#endif
5993#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005994 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005995#endif
5996#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005997 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005998#endif
5999#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006000 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006001#endif
6002#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006003 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006004#endif
6005#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006006 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006007#endif
6008#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006009 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006010#endif
6011#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006012 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006013#endif
6014#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006016#endif
6017#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006018 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006019#endif
Fred Draked86ed291999-12-15 15:34:33 +00006020#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006022#endif
6023#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006024 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006025#endif
6026#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006027 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006028#endif
6029#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006030 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006031#endif
6032#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006033 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006034#endif
6035#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006036 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006037#endif
6038#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006039 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006040#endif
6041#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006042 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006043#endif
6044#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006045 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006046#endif
6047#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006048 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006049#endif
6050#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006051 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006052#endif
6053#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006054 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006055#endif
6056#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006058#endif
Fred Drakec9680921999-12-13 16:37:25 +00006059};
6060
6061static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006062conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006063{
6064 return conv_confname(arg, valuep, posix_constants_confstr,
6065 sizeof(posix_constants_confstr)
6066 / sizeof(struct constdef));
6067}
6068
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006069PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006070"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006071Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006072
6073static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006074posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006075{
6076 PyObject *result = NULL;
6077 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006078 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006079
6080 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006081 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006082
Fred Drakec9680921999-12-13 16:37:25 +00006083 errno = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006084 len = confstr(name, buffer, sizeof(buffer));
6085 if (len == 0) {
6086 if (errno) {
6087 posix_error();
6088 }
6089 else {
6090 result = Py_None;
6091 Py_INCREF(Py_None);
6092 }
Fred Drakec9680921999-12-13 16:37:25 +00006093 }
6094 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006095 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006096 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006097 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006098 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006099 }
6100 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006101 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006102 }
6103 }
6104 return result;
6105}
6106#endif
6107
6108
6109#ifdef HAVE_SYSCONF
6110static struct constdef posix_constants_sysconf[] = {
6111#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006112 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006113#endif
6114#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006115 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006116#endif
6117#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006119#endif
6120#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006122#endif
6123#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006124 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006125#endif
6126#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006127 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006128#endif
6129#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006131#endif
6132#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006133 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006134#endif
6135#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006136 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006137#endif
6138#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006139 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006140#endif
Fred Draked86ed291999-12-15 15:34:33 +00006141#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006143#endif
6144#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006145 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006146#endif
Fred Drakec9680921999-12-13 16:37:25 +00006147#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006149#endif
Fred Drakec9680921999-12-13 16:37:25 +00006150#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006151 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006152#endif
6153#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006154 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006155#endif
6156#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006158#endif
6159#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006160 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006161#endif
6162#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006164#endif
Fred Draked86ed291999-12-15 15:34:33 +00006165#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006166 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006167#endif
Fred Drakec9680921999-12-13 16:37:25 +00006168#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006169 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006170#endif
6171#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006172 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006173#endif
6174#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006175 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006176#endif
6177#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006178 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006179#endif
6180#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006182#endif
Fred Draked86ed291999-12-15 15:34:33 +00006183#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006185#endif
Fred Drakec9680921999-12-13 16:37:25 +00006186#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006188#endif
6189#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006190 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006191#endif
6192#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006193 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006194#endif
6195#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006196 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006197#endif
6198#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006199 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006200#endif
6201#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006202 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006203#endif
6204#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006205 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006206#endif
6207#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006208 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006209#endif
6210#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006211 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006212#endif
6213#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006214 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006215#endif
6216#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006217 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006218#endif
6219#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006220 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006221#endif
6222#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006223 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006224#endif
6225#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006226 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006227#endif
6228#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006229 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006230#endif
6231#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006232 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006233#endif
6234#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006235 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006236#endif
6237#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006238 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006239#endif
6240#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006241 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006242#endif
6243#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006244 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006245#endif
6246#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006247 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006248#endif
6249#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006250 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006251#endif
6252#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006253 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006254#endif
Fred Draked86ed291999-12-15 15:34:33 +00006255#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006256 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006257#endif
Fred Drakec9680921999-12-13 16:37:25 +00006258#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006259 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006260#endif
6261#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006262 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006263#endif
6264#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006265 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006266#endif
Fred Draked86ed291999-12-15 15:34:33 +00006267#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006268 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006269#endif
Fred Drakec9680921999-12-13 16:37:25 +00006270#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006271 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006272#endif
Fred Draked86ed291999-12-15 15:34:33 +00006273#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006274 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006275#endif
6276#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006278#endif
Fred Drakec9680921999-12-13 16:37:25 +00006279#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006281#endif
6282#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006283 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006284#endif
6285#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006286 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006287#endif
6288#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006289 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006290#endif
Fred Draked86ed291999-12-15 15:34:33 +00006291#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006292 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006293#endif
Fred Drakec9680921999-12-13 16:37:25 +00006294#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006295 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006296#endif
6297#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006298 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006299#endif
6300#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006301 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006302#endif
6303#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006304 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006305#endif
6306#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006307 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006308#endif
6309#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006310 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006311#endif
6312#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006314#endif
Fred Draked86ed291999-12-15 15:34:33 +00006315#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006317#endif
Fred Drakec9680921999-12-13 16:37:25 +00006318#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006319 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006320#endif
6321#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006322 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006323#endif
Fred Draked86ed291999-12-15 15:34:33 +00006324#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006325 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006326#endif
Fred Drakec9680921999-12-13 16:37:25 +00006327#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006328 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006329#endif
6330#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006331 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006332#endif
6333#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006334 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006335#endif
6336#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006337 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006338#endif
6339#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006340 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006341#endif
6342#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006343 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006344#endif
6345#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006346 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006347#endif
6348#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006349 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006350#endif
6351#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006352 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006353#endif
Fred Draked86ed291999-12-15 15:34:33 +00006354#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006355 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006356#endif
6357#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006359#endif
Fred Drakec9680921999-12-13 16:37:25 +00006360#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006362#endif
6363#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006364 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006365#endif
6366#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006367 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006368#endif
6369#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006370 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006371#endif
6372#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006373 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006374#endif
6375#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006376 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006377#endif
6378#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00006379 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006380#endif
6381#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00006382 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006383#endif
6384#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006385 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006386#endif
6387#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006388 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006389#endif
6390#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00006391 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006392#endif
6393#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006394 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006395#endif
6396#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006397 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006398#endif
6399#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00006400 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006401#endif
6402#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00006403 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006404#endif
6405#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00006406 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006407#endif
6408#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00006409 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006410#endif
6411#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006412 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006413#endif
6414#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006415 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006416#endif
6417#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00006418 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006419#endif
6420#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006421 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006422#endif
6423#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006424 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006425#endif
6426#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00006427 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006428#endif
6429#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006430 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006431#endif
6432#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006433 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006434#endif
6435#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006436 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00006437#endif
6438#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00006439 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00006440#endif
6441#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006442 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006443#endif
6444#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006445 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006446#endif
6447#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006448 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00006449#endif
6450#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006451 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006452#endif
6453#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006454 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006455#endif
6456#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006457 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006458#endif
6459#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006460 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006461#endif
6462#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006463 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006464#endif
Fred Draked86ed291999-12-15 15:34:33 +00006465#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00006466 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00006467#endif
Fred Drakec9680921999-12-13 16:37:25 +00006468#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00006469 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00006470#endif
6471#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006473#endif
6474#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00006475 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00006476#endif
6477#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006479#endif
6480#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006482#endif
6483#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006484 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006485#endif
6486#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00006488#endif
6489#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006490 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006491#endif
6492#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006493 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006494#endif
6495#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006496 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006497#endif
6498#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006499 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006500#endif
6501#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006502 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00006503#endif
6504#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006505 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00006506#endif
6507#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00006508 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00006509#endif
6510#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006511 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006512#endif
6513#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006514 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006515#endif
6516#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006517 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006518#endif
6519#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006520 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00006521#endif
6522#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006523 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006524#endif
6525#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006526 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006527#endif
6528#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006529 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006530#endif
6531#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006532 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006533#endif
6534#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006535 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006536#endif
6537#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006539#endif
6540#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00006541 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00006542#endif
6543#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006544 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006545#endif
6546#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006547 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006548#endif
6549#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006550 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006551#endif
6552#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006553 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006554#endif
6555#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00006556 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00006557#endif
6558#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00006559 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006560#endif
6561#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00006562 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00006563#endif
6564#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00006565 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006566#endif
6567#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00006568 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00006569#endif
6570#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00006571 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00006572#endif
6573#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00006575#endif
6576#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00006577 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00006578#endif
6579#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006580 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006581#endif
6582#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00006583 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00006584#endif
6585#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00006586 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00006587#endif
6588#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006589 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006590#endif
6591#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006593#endif
6594#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00006595 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00006596#endif
6597#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00006599#endif
6600#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00006601 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00006602#endif
6603};
6604
6605static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006606conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006607{
6608 return conv_confname(arg, valuep, posix_constants_sysconf,
6609 sizeof(posix_constants_sysconf)
6610 / sizeof(struct constdef));
6611}
6612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006613PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006614"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006615Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006616
6617static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006618posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006619{
6620 PyObject *result = NULL;
6621 int name;
6622
6623 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6624 int value;
6625
6626 errno = 0;
6627 value = sysconf(name);
6628 if (value == -1 && errno != 0)
6629 posix_error();
6630 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006631 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006632 }
6633 return result;
6634}
6635#endif
6636
6637
Fred Drakebec628d1999-12-15 18:31:10 +00006638/* This code is used to ensure that the tables of configuration value names
6639 * are in sorted order as required by conv_confname(), and also to build the
6640 * the exported dictionaries that are used to publish information about the
6641 * names available on the host platform.
6642 *
6643 * Sorting the table at runtime ensures that the table is properly ordered
6644 * when used, even for platforms we're not able to test on. It also makes
6645 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006646 */
Fred Drakebec628d1999-12-15 18:31:10 +00006647
6648static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006649cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006650{
6651 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00006653 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00006654 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00006655
6656 return strcmp(c1->name, c2->name);
6657}
6658
6659static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006660setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006662{
Fred Drakebec628d1999-12-15 18:31:10 +00006663 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006664 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006665
6666 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6667 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006668 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00006669 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006670
Barry Warsaw3155db32000-04-13 15:20:40 +00006671 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006672 PyObject *o = PyLong_FromLong(table[i].value);
6673 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6674 Py_XDECREF(o);
6675 Py_DECREF(d);
6676 return -1;
6677 }
6678 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006679 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006680 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006681}
6682
Fred Drakebec628d1999-12-15 18:31:10 +00006683/* Return -1 on failure, 0 on success. */
6684static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006685setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006686{
6687#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006688 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006689 sizeof(posix_constants_pathconf)
6690 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006691 "pathconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006692 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006693#endif
6694#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006695 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006696 sizeof(posix_constants_confstr)
6697 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006698 "confstr_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006700#endif
6701#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006702 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006703 sizeof(posix_constants_sysconf)
6704 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006705 "sysconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006707#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006708 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006709}
Fred Draked86ed291999-12-15 15:34:33 +00006710
6711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006712PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006713"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006714Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006715in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006716
6717static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006718posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006719{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006720 abort();
6721 /*NOTREACHED*/
6722 Py_FatalError("abort() called from Python code didn't abort!");
6723 return NULL;
6724}
Fred Drakebec628d1999-12-15 18:31:10 +00006725
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006726#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006727PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006728"startfile(filepath [, operation]) - Start a file with its associated\n\
6729application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006730\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006731When \"operation\" is not specified or \"open\", this acts like\n\
6732double-clicking the file in Explorer, or giving the file name as an\n\
6733argument to the DOS \"start\" command: the file is opened with whatever\n\
6734application (if any) its extension is associated.\n\
6735When another \"operation\" is given, it specifies what should be done with\n\
6736the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006737\n\
6738startfile returns as soon as the associated application is launched.\n\
6739There is no option to wait for the application to close, and no way\n\
6740to retrieve the application's exit status.\n\
6741\n\
6742The filepath is relative to the current directory. If you want to use\n\
6743an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006744the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006745
6746static PyObject *
6747win32_startfile(PyObject *self, PyObject *args)
6748{
Victor Stinner8c62be82010-05-06 00:08:46 +00006749 PyObject *ofilepath;
6750 char *filepath;
6751 char *operation = NULL;
6752 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006753
Victor Stinner8c62be82010-05-06 00:08:46 +00006754 PyObject *unipath, *woperation = NULL;
6755 if (!PyArg_ParseTuple(args, "U|s:startfile",
6756 &unipath, &operation)) {
6757 PyErr_Clear();
6758 goto normal;
6759 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006760
Victor Stinner8c62be82010-05-06 00:08:46 +00006761 if (operation) {
6762 woperation = PyUnicode_DecodeASCII(operation,
6763 strlen(operation), NULL);
6764 if (!woperation) {
6765 PyErr_Clear();
6766 operation = NULL;
6767 goto normal;
6768 }
6769 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006770
Victor Stinner8c62be82010-05-06 00:08:46 +00006771 Py_BEGIN_ALLOW_THREADS
6772 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6773 PyUnicode_AS_UNICODE(unipath),
6774 NULL, NULL, SW_SHOWNORMAL);
6775 Py_END_ALLOW_THREADS
6776
6777 Py_XDECREF(woperation);
6778 if (rc <= (HINSTANCE)32) {
6779 PyObject *errval = win32_error_unicode("startfile",
6780 PyUnicode_AS_UNICODE(unipath));
6781 return errval;
6782 }
6783 Py_INCREF(Py_None);
6784 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006785
6786normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6788 PyUnicode_FSConverter, &ofilepath,
6789 &operation))
6790 return NULL;
6791 filepath = PyBytes_AsString(ofilepath);
6792 Py_BEGIN_ALLOW_THREADS
6793 rc = ShellExecute((HWND)0, operation, filepath,
6794 NULL, NULL, SW_SHOWNORMAL);
6795 Py_END_ALLOW_THREADS
6796 if (rc <= (HINSTANCE)32) {
6797 PyObject *errval = win32_error("startfile", filepath);
6798 Py_DECREF(ofilepath);
6799 return errval;
6800 }
6801 Py_DECREF(ofilepath);
6802 Py_INCREF(Py_None);
6803 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006804}
6805#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006806
Martin v. Löwis438b5342002-12-27 10:16:42 +00006807#ifdef HAVE_GETLOADAVG
6808PyDoc_STRVAR(posix_getloadavg__doc__,
6809"getloadavg() -> (float, float, float)\n\n\
6810Return the number of processes in the system run queue averaged over\n\
6811the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6812was unobtainable");
6813
6814static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006815posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006816{
6817 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006818 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6820 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00006821 } else
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00006823}
6824#endif
6825
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006826#ifdef MS_WINDOWS
6827
6828PyDoc_STRVAR(win32_urandom__doc__,
6829"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006830Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006831
6832typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6833 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6834 DWORD dwFlags );
6835typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6836 BYTE *pbBuffer );
6837
6838static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006839/* This handle is never explicitly released. Instead, the operating
6840 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006841static HCRYPTPROV hCryptProv = 0;
6842
Tim Peters4ad82172004-08-30 17:02:04 +00006843static PyObject*
6844win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006845{
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 int howMany;
6847 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006848
Victor Stinner8c62be82010-05-06 00:08:46 +00006849 /* Read arguments */
6850 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6851 return NULL;
6852 if (howMany < 0)
6853 return PyErr_Format(PyExc_ValueError,
6854 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006855
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 if (hCryptProv == 0) {
6857 HINSTANCE hAdvAPI32 = NULL;
6858 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006859
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 /* Obtain handle to the DLL containing CryptoAPI
6861 This should not fail */
6862 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6863 if(hAdvAPI32 == NULL)
6864 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006865
Victor Stinner8c62be82010-05-06 00:08:46 +00006866 /* Obtain pointers to the CryptoAPI functions
6867 This will fail on some early versions of Win95 */
6868 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6869 hAdvAPI32,
6870 "CryptAcquireContextA");
6871 if (pCryptAcquireContext == NULL)
6872 return PyErr_Format(PyExc_NotImplementedError,
6873 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006874
Victor Stinner8c62be82010-05-06 00:08:46 +00006875 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6876 hAdvAPI32, "CryptGenRandom");
6877 if (pCryptGenRandom == NULL)
6878 return PyErr_Format(PyExc_NotImplementedError,
6879 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006880
Victor Stinner8c62be82010-05-06 00:08:46 +00006881 /* Acquire context */
6882 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6883 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6884 return win32_error("CryptAcquireContext", NULL);
6885 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006886
Victor Stinner8c62be82010-05-06 00:08:46 +00006887 /* Allocate bytes */
6888 result = PyBytes_FromStringAndSize(NULL, howMany);
6889 if (result != NULL) {
6890 /* Get random data */
6891 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
6892 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
6893 PyBytes_AS_STRING(result))) {
6894 Py_DECREF(result);
6895 return win32_error("CryptGenRandom", NULL);
6896 }
6897 }
6898 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006899}
6900#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006901
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006902PyDoc_STRVAR(device_encoding__doc__,
6903"device_encoding(fd) -> str\n\n\
6904Return a string describing the encoding of the device\n\
6905if the output is a terminal; else return None.");
6906
6907static PyObject *
6908device_encoding(PyObject *self, PyObject *args)
6909{
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 int fd;
6911 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6912 return NULL;
6913 if (!_PyVerify_fd(fd) || !isatty(fd)) {
6914 Py_INCREF(Py_None);
6915 return Py_None;
6916 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006917#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 if (fd == 0) {
6919 char buf[100];
6920 sprintf(buf, "cp%d", GetConsoleCP());
6921 return PyUnicode_FromString(buf);
6922 }
6923 if (fd == 1 || fd == 2) {
6924 char buf[100];
6925 sprintf(buf, "cp%d", GetConsoleOutputCP());
6926 return PyUnicode_FromString(buf);
6927 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006928#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00006929 {
6930 char *codeset = nl_langinfo(CODESET);
6931 if (codeset != NULL && codeset[0] != 0)
6932 return PyUnicode_FromString(codeset);
6933 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006934#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006935 Py_INCREF(Py_None);
6936 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006937}
6938
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006939#ifdef __VMS
6940/* Use openssl random routine */
6941#include <openssl/rand.h>
6942PyDoc_STRVAR(vms_urandom__doc__,
6943"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006944Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006945
6946static PyObject*
6947vms_urandom(PyObject *self, PyObject *args)
6948{
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 int howMany;
6950 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006951
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 /* Read arguments */
6953 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6954 return NULL;
6955 if (howMany < 0)
6956 return PyErr_Format(PyExc_ValueError,
6957 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006958
Victor Stinner8c62be82010-05-06 00:08:46 +00006959 /* Allocate bytes */
6960 result = PyBytes_FromStringAndSize(NULL, howMany);
6961 if (result != NULL) {
6962 /* Get random data */
6963 if (RAND_pseudo_bytes((unsigned char*)
6964 PyBytes_AS_STRING(result),
6965 howMany) < 0) {
6966 Py_DECREF(result);
6967 return PyErr_Format(PyExc_ValueError,
6968 "RAND_pseudo_bytes");
6969 }
6970 }
6971 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006972}
6973#endif
6974
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006975#ifdef HAVE_SETRESUID
6976PyDoc_STRVAR(posix_setresuid__doc__,
6977"setresuid(ruid, euid, suid)\n\n\
6978Set the current process's real, effective, and saved user ids.");
6979
6980static PyObject*
6981posix_setresuid (PyObject *self, PyObject *args)
6982{
Victor Stinner8c62be82010-05-06 00:08:46 +00006983 /* We assume uid_t is no larger than a long. */
6984 long ruid, euid, suid;
6985 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
6986 return NULL;
6987 if (setresuid(ruid, euid, suid) < 0)
6988 return posix_error();
6989 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006990}
6991#endif
6992
6993#ifdef HAVE_SETRESGID
6994PyDoc_STRVAR(posix_setresgid__doc__,
6995"setresgid(rgid, egid, sgid)\n\n\
6996Set the current process's real, effective, and saved group ids.");
6997
6998static PyObject*
6999posix_setresgid (PyObject *self, PyObject *args)
7000{
Victor Stinner8c62be82010-05-06 00:08:46 +00007001 /* We assume uid_t is no larger than a long. */
7002 long rgid, egid, sgid;
7003 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7004 return NULL;
7005 if (setresgid(rgid, egid, sgid) < 0)
7006 return posix_error();
7007 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007008}
7009#endif
7010
7011#ifdef HAVE_GETRESUID
7012PyDoc_STRVAR(posix_getresuid__doc__,
7013"getresuid() -> (ruid, euid, suid)\n\n\
7014Get tuple of the current process's real, effective, and saved user ids.");
7015
7016static PyObject*
7017posix_getresuid (PyObject *self, PyObject *noargs)
7018{
Victor Stinner8c62be82010-05-06 00:08:46 +00007019 uid_t ruid, euid, suid;
7020 long l_ruid, l_euid, l_suid;
7021 if (getresuid(&ruid, &euid, &suid) < 0)
7022 return posix_error();
7023 /* Force the values into long's as we don't know the size of uid_t. */
7024 l_ruid = ruid;
7025 l_euid = euid;
7026 l_suid = suid;
7027 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007028}
7029#endif
7030
7031#ifdef HAVE_GETRESGID
7032PyDoc_STRVAR(posix_getresgid__doc__,
7033"getresgid() -> (rgid, egid, sgid)\n\n\
7034Get tuple of the current process's real, effective, and saved user ids.");
7035
7036static PyObject*
7037posix_getresgid (PyObject *self, PyObject *noargs)
7038{
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 uid_t rgid, egid, sgid;
7040 long l_rgid, l_egid, l_sgid;
7041 if (getresgid(&rgid, &egid, &sgid) < 0)
7042 return posix_error();
7043 /* Force the values into long's as we don't know the size of uid_t. */
7044 l_rgid = rgid;
7045 l_egid = egid;
7046 l_sgid = sgid;
7047 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007048}
7049#endif
7050
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007051static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007052 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007053#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007055#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007057#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007059#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007061#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007063#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007064#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007065 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007066#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007067#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007069#endif /* HAVE_LCHMOD */
7070#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007071 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007072#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007073#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007075#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007076#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007078#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007079#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007081#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007082#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007084#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007085#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7087 METH_NOARGS, posix_getcwd__doc__},
7088 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7089 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007090#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007091#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007093#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007094 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7095 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7096 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007097#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007099#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007100#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007102#endif /* HAVE_READLINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007103 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7104 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7105 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
7106 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007107#ifdef HAVE_SYMLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007109#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007110#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007112#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007114#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007116#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7118 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7119 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007120#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007121 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007122#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007124#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7126 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007127#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007128#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7130 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007131#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7133 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007134#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007135#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007136#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007137 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007138#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007139#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007140 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007141#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007142#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007143 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007144#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007145#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007147#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007148#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007149 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007150#endif /* HAVE_GETEGID */
7151#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007153#endif /* HAVE_GETEUID */
7154#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007155 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007156#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007157#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007158 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007159#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007160 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007161#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007163#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007164#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007166#endif /* HAVE_GETPPID */
7167#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007169#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007170#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007172#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007173#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007175#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007176#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007178#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007179#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007181#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007182#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7184 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007185#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007186#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007188#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007189#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007191#endif /* HAVE_SETEUID */
7192#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007193 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007194#endif /* HAVE_SETEGID */
7195#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007196 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007197#endif /* HAVE_SETREUID */
7198#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007199 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007200#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007201#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007202 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007203#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007204#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007206#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007207#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007208 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007209#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007210#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007211 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007212#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007213#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007214 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007215#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007216#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007217 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007218#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007219#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007221#endif /* HAVE_WAIT3 */
7222#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007223 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007224#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007225#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007226 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007227#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007228#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007229 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007230#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007231#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007232 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007233#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007234#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007235 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007236#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007237#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007239#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007240#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007242#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7244 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7245 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7246 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7247 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7248 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7249 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7250 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7251 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7252 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7253 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007254#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007256#endif
7257#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007259#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007260#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007262#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007263#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7265 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7266 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007267#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007268#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007269 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007270#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007271#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007272 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007273#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007274#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007276#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007277 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007278#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007280#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007281#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007283#endif
7284#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007286#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007287#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007288#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007289 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007290#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007291#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007292 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007293#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007294#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007295 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007296#endif /* WIFSTOPPED */
7297#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007298 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007299#endif /* WIFSIGNALED */
7300#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007301 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007302#endif /* WIFEXITED */
7303#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007304 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007305#endif /* WEXITSTATUS */
7306#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007307 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007308#endif /* WTERMSIG */
7309#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007311#endif /* WSTOPSIG */
7312#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007313#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007314 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007315#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007316#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007318#endif
Fred Drakec9680921999-12-13 16:37:25 +00007319#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007321#endif
7322#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007324#endif
7325#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007326 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007327#endif
7328#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007329 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007330#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007331 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007332#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007334#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007335#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007337#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007338 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007339 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007340 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007341 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007342 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007343 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007344#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007345 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007346#endif
7347#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007348 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007349#endif
7350#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007352#endif
7353#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007354 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007355#endif
7356
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007358};
7359
7360
Barry Warsaw4a342091996-12-19 23:50:02 +00007361static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007362ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007363{
Victor Stinner8c62be82010-05-06 00:08:46 +00007364 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007365}
7366
Guido van Rossumd48f2521997-12-05 22:19:34 +00007367#if defined(PYOS_OS2)
7368/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007369static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007370{
7371 APIRET rc;
7372 ULONG values[QSV_MAX+1];
7373 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007374 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007375
7376 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007377 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007378 Py_END_ALLOW_THREADS
7379
7380 if (rc != NO_ERROR) {
7381 os2_error(rc);
7382 return -1;
7383 }
7384
Fred Drake4d1e64b2002-04-15 19:40:07 +00007385 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7386 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7387 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7388 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7389 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7390 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7391 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007392
7393 switch (values[QSV_VERSION_MINOR]) {
7394 case 0: ver = "2.00"; break;
7395 case 10: ver = "2.10"; break;
7396 case 11: ver = "2.11"; break;
7397 case 30: ver = "3.00"; break;
7398 case 40: ver = "4.00"; break;
7399 case 50: ver = "5.00"; break;
7400 default:
Tim Peters885d4572001-11-28 20:27:42 +00007401 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00007402 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007403 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007404 ver = &tmp[0];
7405 }
7406
7407 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007408 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007409 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007410
7411 /* Add Indicator of Which Drive was Used to Boot the System */
7412 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7413 tmp[1] = ':';
7414 tmp[2] = '\0';
7415
Fred Drake4d1e64b2002-04-15 19:40:07 +00007416 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007417}
7418#endif
7419
Barry Warsaw4a342091996-12-19 23:50:02 +00007420static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007421all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007422{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007423#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007424 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007425#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007426#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007427 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007428#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007429#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007430 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007431#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007432#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007433 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007434#endif
Fred Drakec9680921999-12-13 16:37:25 +00007435#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007436 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00007437#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007438#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007439 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007440#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007441#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007442 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007443#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007444#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00007445 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007446#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007447#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00007448 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007449#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007450#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007451 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007452#endif
7453#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007454 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007455#endif
7456#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00007457 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007458#endif
7459#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00007460 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007461#endif
7462#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007463 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007464#endif
7465#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00007466 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007467#endif
7468#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007469 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007470#endif
7471#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007473#endif
7474#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007475 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007476#endif
7477#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007478 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007479#endif
7480#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007481 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007482#endif
7483#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00007484 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007485#endif
7486#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007487 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007488#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007489#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007490 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007491#endif
7492#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00007493 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007494#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007495#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00007496 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007497#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007498#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007499 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007500#endif
7501#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007502 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007503#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007504
Tim Peters5aa91602002-01-30 05:46:57 +00007505/* MS Windows */
7506#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007507 /* Don't inherit in child processes. */
7508 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007509#endif
7510#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 /* Optimize for short life (keep in memory). */
7512 /* MS forgot to define this one with a non-underscore form too. */
7513 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007514#endif
7515#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007516 /* Automatically delete when last handle is closed. */
7517 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007518#endif
7519#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00007520 /* Optimize for random access. */
7521 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007522#endif
7523#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007524 /* Optimize for sequential access. */
7525 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007526#endif
7527
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007528/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007529#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007530 /* Send a SIGIO signal whenever input or output
7531 becomes available on file descriptor */
7532 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007533#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007534#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007535 /* Direct disk access. */
7536 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007537#endif
7538#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00007539 /* Must be a directory. */
7540 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007541#endif
7542#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00007543 /* Do not follow links. */
7544 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007545#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007546#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007547 /* Do not update the access time. */
7548 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007549#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007550
Victor Stinner8c62be82010-05-06 00:08:46 +00007551 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007552#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007553 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007554#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007555#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007556 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007557#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007558#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007559 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007560#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007561#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007562 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007563#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007564#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007566#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007567#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00007568 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007569#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007570#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007572#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007573#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00007574 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007575#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007576#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007578#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007579#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00007580 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007581#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007582#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007583 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007584#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007585#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007586 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007587#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007588#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00007589 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007590#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007591#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00007592 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007593#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007594#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007596#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007597#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007599#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007600#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007602#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007603
Guido van Rossum246bc171999-02-01 23:54:31 +00007604#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007605#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00007606 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7607 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7608 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7609 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7610 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7611 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7612 if (ins(d, "P_PM", (long)P_PM)) return -1;
7613 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7614 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7615 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7616 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7617 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7618 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7619 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7620 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7621 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7622 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7623 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7624 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7625 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007626#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7628 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7629 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7630 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7631 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007632#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007633#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007634
Guido van Rossumd48f2521997-12-05 22:19:34 +00007635#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007636 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007637#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007638 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00007639}
7640
7641
Tim Peters5aa91602002-01-30 05:46:57 +00007642#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007643#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007644#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007645
7646#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007647#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007648#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007649
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007650#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007651#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007652#define MODNAME "posix"
7653#endif
7654
Martin v. Löwis1a214512008-06-11 05:26:20 +00007655static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007656 PyModuleDef_HEAD_INIT,
7657 MODNAME,
7658 posix__doc__,
7659 -1,
7660 posix_methods,
7661 NULL,
7662 NULL,
7663 NULL,
7664 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00007665};
7666
7667
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007668PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007669INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007670{
Victor Stinner8c62be82010-05-06 00:08:46 +00007671 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007672
Victor Stinner8c62be82010-05-06 00:08:46 +00007673 m = PyModule_Create(&posixmodule);
7674 if (m == NULL)
7675 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007676
Victor Stinner8c62be82010-05-06 00:08:46 +00007677 /* Initialize environ dictionary */
7678 v = convertenviron();
7679 Py_XINCREF(v);
7680 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
7681 return NULL;
7682 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007683
Victor Stinner8c62be82010-05-06 00:08:46 +00007684 if (all_ins(m))
7685 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007686
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 if (setup_confname_tables(m))
7688 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007689
Victor Stinner8c62be82010-05-06 00:08:46 +00007690 Py_INCREF(PyExc_OSError);
7691 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007692
Guido van Rossumb3d39562000-01-31 18:41:26 +00007693#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007694 if (posix_putenv_garbage == NULL)
7695 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007696#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007697
Victor Stinner8c62be82010-05-06 00:08:46 +00007698 if (!initialized) {
7699 stat_result_desc.name = MODNAME ".stat_result";
7700 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7701 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7702 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7703 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7704 structseq_new = StatResultType.tp_new;
7705 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007706
Victor Stinner8c62be82010-05-06 00:08:46 +00007707 statvfs_result_desc.name = MODNAME ".statvfs_result";
7708 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007709#ifdef NEED_TICKS_PER_SECOND
7710# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007712# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00007713 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007714# else
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007716# endif
7717#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007718 }
7719 Py_INCREF((PyObject*) &StatResultType);
7720 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
7721 Py_INCREF((PyObject*) &StatVFSResultType);
7722 PyModule_AddObject(m, "statvfs_result",
7723 (PyObject*) &StatVFSResultType);
7724 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007725
7726#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 /*
7728 * Step 2 of weak-linking support on Mac OS X.
7729 *
7730 * The code below removes functions that are not available on the
7731 * currently active platform.
7732 *
7733 * This block allow one to use a python binary that was build on
7734 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7735 * OSX 10.4.
7736 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007737#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00007738 if (fstatvfs == NULL) {
7739 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7740 return NULL;
7741 }
7742 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007743#endif /* HAVE_FSTATVFS */
7744
7745#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 if (statvfs == NULL) {
7747 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7748 return NULL;
7749 }
7750 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007751#endif /* HAVE_STATVFS */
7752
7753# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007754 if (lchown == NULL) {
7755 if (PyObject_DelAttrString(m, "lchown") == -1) {
7756 return NULL;
7757 }
7758 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007759#endif /* HAVE_LCHOWN */
7760
7761
7762#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007764
Guido van Rossumb6775db1994-08-01 11:34:53 +00007765}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007766
7767#ifdef __cplusplus
7768}
7769#endif