blob: 5237f1466184c606c226aba3a63564a9693a58a4 [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
Ronald Oussorend06b6f22006-04-23 11:59:25 +000016#ifdef __APPLE__
17 /*
Victor Stinnerd6f85422010-05-05 23:33:33 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Ronald Oussorend06b6f22006-04-23 11:59:25 +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
Anthony Baxterac6bd462006-04-13 02:06:09 +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#ifndef Py_USING_UNICODE
48/* This is used in signatures of functions. */
49#define Py_UNICODE void
50#endif
51
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000052#if defined(PYOS_OS2)
53#define INCL_DOS
54#define INCL_DOSERRORS
55#define INCL_DOSPROCESS
56#define INCL_NOPMAPI
57#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#if defined(PYCC_GCC)
59#include <ctype.h>
60#include <io.h>
61#include <stdio.h>
62#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000063#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000064#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#endif
66
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000067#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000069#endif /* HAVE_SYS_TYPES_H */
70
71#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000072#include <sys/stat.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000073#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000074
Guido van Rossum36bc6801995-06-14 22:54:23 +000075#ifdef HAVE_SYS_WAIT_H
Victor Stinnerd6f85422010-05-05 23:33:33 +000076#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000077#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000078
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000079#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000080#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000081#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000082
Guido van Rossumb6775db1994-08-01 11:34:53 +000083#ifdef HAVE_FCNTL_H
84#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000085#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000086
Guido van Rossuma6535fd2001-10-18 19:44:10 +000087#ifdef HAVE_GRP_H
88#include <grp.h>
89#endif
90
Barry Warsaw5676bd12003-01-07 20:57:09 +000091#ifdef HAVE_SYSEXITS_H
92#include <sysexits.h>
93#endif /* HAVE_SYSEXITS_H */
94
Anthony Baxter8a560de2004-10-13 15:30:56 +000095#ifdef HAVE_SYS_LOADAVG_H
96#include <sys/loadavg.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 Stinnerd6f85422010-05-05 23:33:33 +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 Stinnerd6f85422010-05-05 23:33:33 +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 Stinnerd6f85422010-05-05 23:33:33 +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
119#define HAVE_POPEN 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000120#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000121#define HAVE_WAIT 1
122#else
Victor Stinnerd6f85422010-05-05 23:33:33 +0000123#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000124#define HAVE_GETCWD 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000129#define HAVE_SYSTEM 1
130#define HAVE_CWAIT 1
131#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000132#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000133#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000134#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinnerd6f85422010-05-05 23:33:33 +0000136#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137/* Unix functions that the configure script doesn't check for */
138#define HAVE_EXECV 1
139#define HAVE_FORK 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000140#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000141#define HAVE_FORK1 1
142#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_GETEGID 1
145#define HAVE_GETEUID 1
146#define HAVE_GETGID 1
147#define HAVE_GETPPID 1
148#define HAVE_GETUID 1
149#define HAVE_KILL 1
150#define HAVE_OPENDIR 1
151#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000152#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000154#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +0000155#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_WAIT 1
Victor Stinnerd6f85422010-05-05 23:33:33 +0000157#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000158#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#endif /* _MSC_VER */
160#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000161#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000162#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000163
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000165
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000166#if defined(__sgi)&&_COMPILER_VERSION>=700
167/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
168 (default) */
169extern char *ctermid_r(char *);
170#endif
171
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000172#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000173#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000176#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#endif
182#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000183extern int chdir(char *);
184extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int chdir(const char *);
187extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#ifdef __BORLANDC__
190extern int chmod(const char *, int);
191#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000192extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000193#endif
Christian Heimes36281872007-11-30 21:11:28 +0000194/*#ifdef HAVE_FCHMOD
195extern int fchmod(int, mode_t);
196#endif*/
197/*#ifdef HAVE_LCHMOD
198extern int lchmod(const char *, mode_t);
199#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int chown(const char *, uid_t, gid_t);
201extern char *getcwd(char *, int);
202extern char *strerror(int);
203extern int link(const char *, const char *);
204extern int rename(const char *, const char *);
205extern int stat(const char *, struct stat *);
206extern int unlink(const char *);
207extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000210#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000213#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000215
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218#ifdef HAVE_UTIME_H
219#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000220#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000222#ifdef HAVE_SYS_UTIME_H
223#include <sys/utime.h>
224#define HAVE_UTIME_H /* pretend we do for the rest of this file */
225#endif /* HAVE_SYS_UTIME_H */
226
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#ifdef HAVE_SYS_TIMES_H
228#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
231#ifdef HAVE_SYS_PARAM_H
232#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000233#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
235#ifdef HAVE_SYS_UTSNAME_H
236#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000237#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000241#define NAMLEN(dirent) strlen((dirent)->d_name)
242#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000243#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#include <direct.h>
245#define NAMLEN(dirent) strlen((dirent)->d_name)
246#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000255#endif
256#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000258#endif
259#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000262#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000264#endif
265#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000267#endif
268#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000270#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000271#include "osdefs.h"
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000272#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273#include <windows.h>
Victor Stinnerd6f85422010-05-05 23:33:33 +0000274#include <shellapi.h> /* for ShellExecute() */
275#define popen _popen
276#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000277#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278
Guido van Rossumd48f2521997-12-05 22:19:34 +0000279#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000281#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282
Tim Petersbc2e10e2002-03-03 23:17:02 +0000283#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000284#if defined(PATH_MAX) && PATH_MAX > 1024
285#define MAXPATHLEN PATH_MAX
286#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000287#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000288#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000289#endif /* MAXPATHLEN */
290
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000291#ifdef UNION_WAIT
292/* Emulate some macros on systems that have a union instead of macros */
293
294#ifndef WIFEXITED
295#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
296#endif
297
298#ifndef WEXITSTATUS
299#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
300#endif
301
302#ifndef WTERMSIG
303#define WTERMSIG(u_wait) ((u_wait).w_termsig)
304#endif
305
Neal Norwitzd5a37542006-03-20 06:48:34 +0000306#define WAIT_TYPE union wait
307#define WAIT_STATUS_INT(s) (s.w_status)
308
309#else /* !UNION_WAIT */
310#define WAIT_TYPE int
311#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000312#endif /* UNION_WAIT */
313
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000314/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou4fe38582009-05-24 12:15:04 +0000315#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000316#define PARSE_PID "i"
317#define PyLong_FromPid PyInt_FromLong
318#define PyLong_AsPid PyInt_AsLong
319#elif SIZEOF_PID_T == SIZEOF_LONG
320#define PARSE_PID "l"
321#define PyLong_FromPid PyInt_FromLong
322#define PyLong_AsPid PyInt_AsLong
323#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
324#define PARSE_PID "L"
325#define PyLong_FromPid PyLong_FromLongLong
326#define PyLong_AsPid PyInt_AsLongLong
327#else
328#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrou5e858fe2009-05-23 15:37:45 +0000329#endif /* SIZEOF_PID_T */
330
Greg Wardb48bc172000-03-01 21:51:56 +0000331/* Don't use the "_r" form if we don't need it (also, won't have a
332 prototype for it, at least on Solaris -- maybe others as well?). */
333#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
334#define USE_CTERMID_R
335#endif
336
337#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
338#define USE_TMPNAM_R
339#endif
340
Fred Drake699f3522000-06-29 21:12:41 +0000341/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000342#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000343#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +0000344# define STAT win32_stat
345# define FSTAT win32_fstat
346# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000347#else
Victor Stinnerd6f85422010-05-05 23:33:33 +0000348# define STAT stat
349# define FSTAT fstat
350# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000351#endif
352
Tim Peters11b23062003-04-23 02:39:17 +0000353#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000354#include <sys/mkdev.h>
355#else
356#if defined(MAJOR_IN_SYSMACROS)
357#include <sys/sysmacros.h>
358#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000359#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
360#include <sys/mkdev.h>
361#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000362#endif
Fred Drake699f3522000-06-29 21:12:41 +0000363
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000364#if defined _MSC_VER && _MSC_VER >= 1400
365/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
366 * valid and throw an assertion if it isn't.
367 * Normally, an invalid fd is likely to be a C program error and therefore
368 * an assertion can be useful, but it does contradict the POSIX standard
369 * which for write(2) states:
370 * "Otherwise, -1 shall be returned and errno set to indicate the error."
371 * "[EBADF] The fildes argument is not a valid file descriptor open for
372 * writing."
373 * Furthermore, python allows the user to enter any old integer
374 * as a fd and should merely raise a python exception on error.
375 * The Microsoft CRT doesn't provide an official way to check for the
376 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinnerd6f85422010-05-05 23:33:33 +0000377 * by using the exported __pinfo data member and knowledge of the
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000378 * internal structures involved.
379 * The structures below must be updated for each version of visual studio
380 * according to the file internal.h in the CRT source, until MS comes
381 * up with a less hacky way to do this.
382 * (all of this is to avoid globally modifying the CRT behaviour using
383 * _set_invalid_parameter_handler() and _CrtSetReportMode())
384 */
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000385/* The actual size of the structure is determined at runtime.
386 * Only the first items must be present.
387 */
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000388typedef struct {
Victor Stinnerd6f85422010-05-05 23:33:33 +0000389 intptr_t osfhnd;
390 char osfile;
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000391} my_ioinfo;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000392
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000393extern __declspec(dllimport) char * __pioinfo[];
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000394#define IOINFO_L2E 5
395#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
396#define IOINFO_ARRAYS 64
397#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
398#define FOPEN 0x01
399#define _NO_CONSOLE_FILENO (intptr_t)-2
400
401/* This function emulates what the windows CRT does to validate file handles */
402int
403_PyVerify_fd(int fd)
404{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000405 const int i1 = fd >> IOINFO_L2E;
406 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000407
Victor Stinnerd6f85422010-05-05 23:33:33 +0000408 static int sizeof_ioinfo = 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000409
Victor Stinnerd6f85422010-05-05 23:33:33 +0000410 /* Determine the actual size of the ioinfo structure,
411 * as used by the CRT loaded in memory
412 */
413 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
414 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
415 }
416 if (sizeof_ioinfo == 0) {
417 /* This should not happen... */
418 goto fail;
419 }
420
421 /* See that it isn't a special CLEAR fileno */
422 if (fd != _NO_CONSOLE_FILENO) {
423 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
424 * we check pointer validity and other info
425 */
426 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
427 /* finally, check that the file is open */
428 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
429 if (info->osfile & FOPEN) {
430 return 1;
431 }
432 }
433 }
Kristján Valur Jónssonfeab3342009-04-01 16:08:34 +0000434 fail:
Victor Stinnerd6f85422010-05-05 23:33:33 +0000435 errno = EBADF;
436 return 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000437}
438
439/* the special case of checking dup2. The target fd must be in a sensible range */
440static int
441_PyVerify_fd_dup2(int fd1, int fd2)
442{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000443 if (!_PyVerify_fd(fd1))
444 return 0;
445 if (fd2 == _NO_CONSOLE_FILENO)
446 return 0;
447 if ((unsigned)fd2 < _NHANDLE_)
448 return 1;
449 else
450 return 0;
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +0000451}
452#else
453/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
454#define _PyVerify_fd_dup2(A, B) (1)
455#endif
456
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000458#ifdef WITH_NEXT_FRAMEWORK
459/* On Darwin/MacOSX a shared library or framework has no access to
460** environ directly, we must obtain it with _NSGetEnviron().
461*/
462#include <crt_externs.h>
463static char **environ;
464#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000466#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467
Barry Warsaw53699e91996-12-10 23:23:01 +0000468static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000469convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000470{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000471 PyObject *d;
472 char **e;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000473#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +0000474 APIRET rc;
475 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
476#endif
477 d = PyDict_New();
478 if (d == NULL)
479 return NULL;
480#ifdef WITH_NEXT_FRAMEWORK
481 if (environ == NULL)
482 environ = *_NSGetEnviron();
483#endif
484 if (environ == NULL)
485 return d;
486 /* This part ignores errors */
487 for (e = environ; *e != NULL; e++) {
488 PyObject *k;
489 PyObject *v;
490 char *p = strchr(*e, '=');
491 if (p == NULL)
492 continue;
493 k = PyString_FromStringAndSize(*e, (int)(p-*e));
494 if (k == NULL) {
495 PyErr_Clear();
496 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000497 }
Victor Stinnerd6f85422010-05-05 23:33:33 +0000498 v = PyString_FromString(p+1);
499 if (v == NULL) {
500 PyErr_Clear();
501 Py_DECREF(k);
502 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000503 }
Victor Stinnerd6f85422010-05-05 23:33:33 +0000504 if (PyDict_GetItem(d, k) == NULL) {
505 if (PyDict_SetItem(d, k, v) != 0)
506 PyErr_Clear();
507 }
508 Py_DECREF(k);
509 Py_DECREF(v);
510 }
511#if defined(PYOS_OS2)
512 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
513 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
514 PyObject *v = PyString_FromString(buffer);
515 PyDict_SetItemString(d, "BEGINLIBPATH", v);
516 Py_DECREF(v);
517 }
518 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
519 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
520 PyObject *v = PyString_FromString(buffer);
521 PyDict_SetItemString(d, "ENDLIBPATH", v);
522 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000523 }
524#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +0000525 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526}
527
528
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000529/* Set a POSIX-specific error from errno, and return NULL */
530
Barry Warsawd58d7641998-07-23 16:14:40 +0000531static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000532posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000533{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000534 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535}
Barry Warsawd58d7641998-07-23 16:14:40 +0000536static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000537posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000538{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000539 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000540}
541
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000542#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000543static PyObject *
544posix_error_with_unicode_filename(Py_UNICODE* name)
545{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000546 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000547}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000548#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000549
550
Mark Hammondef8b6542001-05-13 08:04:26 +0000551static PyObject *
552posix_error_with_allocated_filename(char* name)
553{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000554 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
555 PyMem_Free(name);
556 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000557}
558
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000559#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000560static PyObject *
561win32_error(char* function, char* filename)
562{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000563 /* XXX We should pass the function name along in the future.
564 (_winreg.c also wants to pass the function name.)
565 This would however require an additional param to the
566 Windows error object, which is non-trivial.
567 */
568 errno = GetLastError();
569 if (filename)
570 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
571 else
572 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000573}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000574
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000575static PyObject *
576win32_error_unicode(char* function, Py_UNICODE* filename)
577{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000578 /* XXX - see win32_error for comments on 'function' */
579 errno = GetLastError();
580 if (filename)
581 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
582 else
583 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000584}
585
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000586static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000587convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000588{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000589 if (PyUnicode_CheckExact(*param))
590 Py_INCREF(*param);
591 else if (PyUnicode_Check(*param))
592 /* For a Unicode subtype that's not a Unicode object,
593 return a true Unicode object with the same data. */
594 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
595 PyUnicode_GET_SIZE(*param));
596 else
597 *param = PyUnicode_FromEncodedObject(*param,
598 Py_FileSystemDefaultEncoding,
599 "strict");
600 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000601}
602
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000603#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604
Guido van Rossumd48f2521997-12-05 22:19:34 +0000605#if defined(PYOS_OS2)
606/**********************************************************************
607 * Helper Function to Trim and Format OS/2 Messages
608 **********************************************************************/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000609static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000610os2_formatmsg(char *msgbuf, int msglen, char *reason)
611{
612 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
613
614 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
Victor Stinner862490a2010-05-06 00:03:44 +0000615 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
Guido van Rossumd48f2521997-12-05 22:19:34 +0000616
Victor Stinner862490a2010-05-06 00:03:44 +0000617 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
618 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000619 }
620
621 /* Add Optional Reason Text */
622 if (reason) {
623 strcat(msgbuf, " : ");
624 strcat(msgbuf, reason);
625 }
626}
627
628/**********************************************************************
629 * Decode an OS/2 Operating System Error Code
630 *
631 * A convenience function to lookup an OS/2 error code and return a
632 * text message we can use to raise a Python exception.
633 *
634 * Notes:
635 * The messages for errors returned from the OS/2 kernel reside in
636 * the file OSO001.MSG in the \OS2 directory hierarchy.
637 *
638 **********************************************************************/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000639static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000640os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
641{
642 APIRET rc;
643 ULONG msglen;
644
645 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
646 Py_BEGIN_ALLOW_THREADS
647 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
648 errorcode, "oso001.msg", &msglen);
649 Py_END_ALLOW_THREADS
650
651 if (rc == NO_ERROR)
652 os2_formatmsg(msgbuf, msglen, reason);
653 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000654 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinnerd6f85422010-05-05 23:33:33 +0000655 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000656
657 return msgbuf;
658}
659
660/* Set an OS/2-specific error and return NULL. OS/2 kernel
661 errors are not in a global variable e.g. 'errno' nor are
662 they congruent with posix error numbers. */
663
Victor Stinnerd6f85422010-05-05 23:33:33 +0000664static PyObject *
665os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000666{
667 char text[1024];
668 PyObject *v;
669
670 os2_strerror(text, sizeof(text), code, "");
671
672 v = Py_BuildValue("(is)", code, text);
673 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000674 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000675 Py_DECREF(v);
676 }
677 return NULL; /* Signal to Python that an Exception is Pending */
678}
679
680#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681
682/* POSIX generic methods */
683
Barry Warsaw53699e91996-12-10 23:23:01 +0000684static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000685posix_fildes(PyObject *fdobj, int (*func)(int))
686{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000687 int fd;
688 int res;
689 fd = PyObject_AsFileDescriptor(fdobj);
690 if (fd < 0)
691 return NULL;
692 if (!_PyVerify_fd(fd))
693 return posix_error();
694 Py_BEGIN_ALLOW_THREADS
695 res = (*func)(fd);
696 Py_END_ALLOW_THREADS
697 if (res < 0)
698 return posix_error();
699 Py_INCREF(Py_None);
700 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000701}
Guido van Rossum21142a01999-01-08 21:05:37 +0000702
703static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000704posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000706 char *path1 = NULL;
707 int res;
708 if (!PyArg_ParseTuple(args, format,
709 Py_FileSystemDefaultEncoding, &path1))
710 return NULL;
711 Py_BEGIN_ALLOW_THREADS
712 res = (*func)(path1);
713 Py_END_ALLOW_THREADS
714 if (res < 0)
715 return posix_error_with_allocated_filename(path1);
716 PyMem_Free(path1);
717 Py_INCREF(Py_None);
718 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719}
720
Barry Warsaw53699e91996-12-10 23:23:01 +0000721static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000722posix_2str(PyObject *args,
Victor Stinnerd6f85422010-05-05 23:33:33 +0000723 char *format,
724 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000725{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000726 char *path1 = NULL, *path2 = NULL;
727 int res;
728 if (!PyArg_ParseTuple(args, format,
729 Py_FileSystemDefaultEncoding, &path1,
730 Py_FileSystemDefaultEncoding, &path2))
731 return NULL;
732 Py_BEGIN_ALLOW_THREADS
733 res = (*func)(path1, path2);
734 Py_END_ALLOW_THREADS
735 PyMem_Free(path1);
736 PyMem_Free(path2);
737 if (res != 0)
738 /* XXX how to report both path1 and path2??? */
739 return posix_error();
740 Py_INCREF(Py_None);
741 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742}
743
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000744#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000745static PyObject*
Victor Stinnerd6f85422010-05-05 23:33:33 +0000746win32_1str(PyObject* args, char* func,
747 char* format, BOOL (__stdcall *funcA)(LPCSTR),
748 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000749{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000750 PyObject *uni;
751 char *ansi;
752 BOOL result;
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +0000753
Victor Stinnerd6f85422010-05-05 23:33:33 +0000754 if (!PyArg_ParseTuple(args, wformat, &uni))
755 PyErr_Clear();
756 else {
757 Py_BEGIN_ALLOW_THREADS
758 result = funcW(PyUnicode_AsUnicode(uni));
759 Py_END_ALLOW_THREADS
760 if (!result)
761 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
762 Py_INCREF(Py_None);
763 return Py_None;
764 }
765 if (!PyArg_ParseTuple(args, format, &ansi))
766 return NULL;
767 Py_BEGIN_ALLOW_THREADS
768 result = funcA(ansi);
769 Py_END_ALLOW_THREADS
770 if (!result)
771 return win32_error(func, ansi);
772 Py_INCREF(Py_None);
773 return Py_None;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000774
775}
776
777/* This is a reimplementation of the C library's chdir function,
778 but one that produces Win32 errors instead of DOS error codes.
779 chdir is essentially a wrapper around SetCurrentDirectory; however,
780 it also needs to set "magic" environment variables indicating
781 the per-drive current directory, which are of the form =<drive>: */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000782static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000783win32_chdir(LPCSTR path)
784{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000785 char new_path[MAX_PATH+1];
786 int result;
787 char env[4] = "=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000788
Victor Stinnerd6f85422010-05-05 23:33:33 +0000789 if(!SetCurrentDirectoryA(path))
790 return FALSE;
791 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
792 if (!result)
793 return FALSE;
794 /* In the ANSI API, there should not be any paths longer
795 than MAX_PATH. */
796 assert(result <= MAX_PATH+1);
797 if (strncmp(new_path, "\\\\", 2) == 0 ||
798 strncmp(new_path, "//", 2) == 0)
799 /* UNC path, nothing to do. */
800 return TRUE;
801 env[1] = new_path[0];
802 return SetEnvironmentVariableA(env, new_path);
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000803}
804
805/* The Unicode version differs from the ANSI version
806 since the current directory might exceed MAX_PATH characters */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000807static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000808win32_wchdir(LPCWSTR path)
809{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000810 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
811 int result;
812 wchar_t env[4] = L"=x:";
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000813
Victor Stinnerd6f85422010-05-05 23:33:33 +0000814 if(!SetCurrentDirectoryW(path))
815 return FALSE;
816 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
817 if (!result)
818 return FALSE;
819 if (result > MAX_PATH+1) {
820 new_path = malloc(result * sizeof(wchar_t));
821 if (!new_path) {
822 SetLastError(ERROR_OUTOFMEMORY);
823 return FALSE;
824 }
825 result = GetCurrentDirectoryW(result, new_path);
826 if (!result) {
827 free(new_path);
828 return FALSE;
829 }
830 }
831 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
832 wcsncmp(new_path, L"//", 2) == 0)
833 /* UNC path, nothing to do. */
834 return TRUE;
835 env[1] = new_path[0];
836 result = SetEnvironmentVariableW(env, new_path);
837 if (new_path != _new_path)
838 free(new_path);
839 return result;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000840}
841#endif
842
Martin v. Löwis14694662006-02-03 12:54:16 +0000843#ifdef MS_WINDOWS
844/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
845 - time stamps are restricted to second resolution
846 - file modification times suffer from forth-and-back conversions between
847 UTC and local time
848 Therefore, we implement our own stat, based on the Win32 API directly.
849*/
Victor Stinnerd6f85422010-05-05 23:33:33 +0000850#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000851
852struct win32_stat{
853 int st_dev;
854 __int64 st_ino;
855 unsigned short st_mode;
856 int st_nlink;
857 int st_uid;
858 int st_gid;
859 int st_rdev;
860 __int64 st_size;
861 int st_atime;
862 int st_atime_nsec;
863 int st_mtime;
864 int st_mtime_nsec;
865 int st_ctime;
866 int st_ctime_nsec;
867};
868
869static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
870
871static void
872FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
873{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000874 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
875 /* Cannot simply cast and dereference in_ptr,
876 since it might not be aligned properly */
877 __int64 in;
878 memcpy(&in, in_ptr, sizeof(in));
879 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
880 /* XXX Win32 supports time stamps past 2038; we currently don't */
881 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000882}
883
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000884static void
885time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
886{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000887 /* XXX endianness */
888 __int64 out;
889 out = time_in + secs_between_epochs;
890 out = out * 10000000 + nsec_in / 100;
891 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000892}
893
Martin v. Löwis14694662006-02-03 12:54:16 +0000894/* Below, we *know* that ugo+r is 0444 */
895#if _S_IREAD != 0400
896#error Unsupported C library
897#endif
898static int
899attributes_to_mode(DWORD attr)
900{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000901 int m = 0;
902 if (attr & FILE_ATTRIBUTE_DIRECTORY)
903 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
904 else
905 m |= _S_IFREG;
906 if (attr & FILE_ATTRIBUTE_READONLY)
907 m |= 0444;
908 else
909 m |= 0666;
910 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000911}
912
913static int
914attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
915{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000916 memset(result, 0, sizeof(*result));
917 result->st_mode = attributes_to_mode(info->dwFileAttributes);
918 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
919 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
920 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
921 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000922
Victor Stinnerd6f85422010-05-05 23:33:33 +0000923 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000924}
925
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000926static BOOL
927attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
928{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000929 HANDLE hFindFile;
930 WIN32_FIND_DATAA FileData;
931 hFindFile = FindFirstFileA(pszFile, &FileData);
932 if (hFindFile == INVALID_HANDLE_VALUE)
933 return FALSE;
934 FindClose(hFindFile);
935 pfad->dwFileAttributes = FileData.dwFileAttributes;
936 pfad->ftCreationTime = FileData.ftCreationTime;
937 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
938 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
939 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
940 pfad->nFileSizeLow = FileData.nFileSizeLow;
941 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000942}
943
944static BOOL
945attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
946{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000947 HANDLE hFindFile;
948 WIN32_FIND_DATAW FileData;
949 hFindFile = FindFirstFileW(pszFile, &FileData);
950 if (hFindFile == INVALID_HANDLE_VALUE)
951 return FALSE;
952 FindClose(hFindFile);
953 pfad->dwFileAttributes = FileData.dwFileAttributes;
954 pfad->ftCreationTime = FileData.ftCreationTime;
955 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
956 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
957 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
958 pfad->nFileSizeLow = FileData.nFileSizeLow;
959 return TRUE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000960}
961
Victor Stinnerd6f85422010-05-05 23:33:33 +0000962static int
Martin v. Löwis14694662006-02-03 12:54:16 +0000963win32_stat(const char* path, struct win32_stat *result)
964{
Victor Stinnerd6f85422010-05-05 23:33:33 +0000965 WIN32_FILE_ATTRIBUTE_DATA info;
966 int code;
967 char *dot;
968 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
969 if (GetLastError() != ERROR_SHARING_VIOLATION) {
970 /* Protocol violation: we explicitly clear errno, instead of
971 setting it to a POSIX error. Callers should use GetLastError. */
972 errno = 0;
973 return -1;
974 } else {
975 /* Could not get attributes on open file. Fall back to
976 reading the directory. */
977 if (!attributes_from_dir(path, &info)) {
978 /* Very strange. This should not fail now */
979 errno = 0;
980 return -1;
981 }
982 }
983 }
984 code = attribute_data_to_stat(&info, result);
985 if (code != 0)
986 return code;
987 /* Set S_IFEXEC if it is an .exe, .bat, ... */
988 dot = strrchr(path, '.');
989 if (dot) {
990 if (stricmp(dot, ".bat") == 0 ||
991 stricmp(dot, ".cmd") == 0 ||
992 stricmp(dot, ".exe") == 0 ||
993 stricmp(dot, ".com") == 0)
994 result->st_mode |= 0111;
995 }
996 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +0000997}
998
Victor Stinnerd6f85422010-05-05 23:33:33 +0000999static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001000win32_wstat(const wchar_t* path, struct win32_stat *result)
1001{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001002 int code;
1003 const wchar_t *dot;
1004 WIN32_FILE_ATTRIBUTE_DATA info;
1005 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1006 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1007 /* Protocol violation: we explicitly clear errno, instead of
1008 setting it to a POSIX error. Callers should use GetLastError. */
1009 errno = 0;
1010 return -1;
1011 } else {
1012 /* Could not get attributes on open file. Fall back to
1013 reading the directory. */
1014 if (!attributes_from_dir_w(path, &info)) {
1015 /* Very strange. This should not fail now */
1016 errno = 0;
1017 return -1;
1018 }
1019 }
1020 }
1021 code = attribute_data_to_stat(&info, result);
1022 if (code < 0)
1023 return code;
1024 /* Set IFEXEC if it is an .exe, .bat, ... */
1025 dot = wcsrchr(path, '.');
1026 if (dot) {
1027 if (_wcsicmp(dot, L".bat") == 0 ||
1028 _wcsicmp(dot, L".cmd") == 0 ||
1029 _wcsicmp(dot, L".exe") == 0 ||
1030 _wcsicmp(dot, L".com") == 0)
1031 result->st_mode |= 0111;
1032 }
1033 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001034}
1035
1036static int
1037win32_fstat(int file_number, struct win32_stat *result)
1038{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001039 BY_HANDLE_FILE_INFORMATION info;
1040 HANDLE h;
1041 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001042
Victor Stinnerd6f85422010-05-05 23:33:33 +00001043 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001044
Victor Stinnerd6f85422010-05-05 23:33:33 +00001045 /* Protocol violation: we explicitly clear errno, instead of
1046 setting it to a POSIX error. Callers should use GetLastError. */
1047 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001048
Victor Stinnerd6f85422010-05-05 23:33:33 +00001049 if (h == INVALID_HANDLE_VALUE) {
1050 /* This is really a C library error (invalid file handle).
1051 We set the Win32 error to the closes one matching. */
1052 SetLastError(ERROR_INVALID_HANDLE);
1053 return -1;
1054 }
1055 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001056
Victor Stinnerd6f85422010-05-05 23:33:33 +00001057 type = GetFileType(h);
1058 if (type == FILE_TYPE_UNKNOWN) {
1059 DWORD error = GetLastError();
1060 if (error != 0) {
1061 return -1;
1062 }
1063 /* else: valid but unknown file */
1064 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001065
Victor Stinnerd6f85422010-05-05 23:33:33 +00001066 if (type != FILE_TYPE_DISK) {
1067 if (type == FILE_TYPE_CHAR)
1068 result->st_mode = _S_IFCHR;
1069 else if (type == FILE_TYPE_PIPE)
1070 result->st_mode = _S_IFIFO;
1071 return 0;
1072 }
1073
1074 if (!GetFileInformationByHandle(h, &info)) {
1075 return -1;
1076 }
1077
1078 /* similar to stat() */
1079 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1080 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1081 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1082 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1083 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1084 /* specific to fstat() */
1085 result->st_nlink = info.nNumberOfLinks;
1086 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1087 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001088}
1089
1090#endif /* MS_WINDOWS */
1091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001092PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001093"stat_result: Result from stat or lstat.\n\n\
1094This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001095 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001096or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1097\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001098Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1099or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001100\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001102
1103static PyStructSequence_Field stat_result_fields[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001104 {"st_mode", "protection bits"},
1105 {"st_ino", "inode"},
1106 {"st_dev", "device"},
1107 {"st_nlink", "number of hard links"},
1108 {"st_uid", "user ID of owner"},
1109 {"st_gid", "group ID of owner"},
1110 {"st_size", "total size, in bytes"},
1111 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1112 {NULL, "integer time of last access"},
1113 {NULL, "integer time of last modification"},
1114 {NULL, "integer time of last change"},
1115 {"st_atime", "time of last access"},
1116 {"st_mtime", "time of last modification"},
1117 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001118#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00001119 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001120#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001121#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001122 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001123#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001124#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00001125 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001127#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001128 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001129#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001130#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00001131 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001132#endif
1133#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00001134 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001135#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001136 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001137};
1138
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001139#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001140#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001141#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001142#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001143#endif
1144
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001145#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001146#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1147#else
1148#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1149#endif
1150
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001151#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001152#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1153#else
1154#define ST_RDEV_IDX ST_BLOCKS_IDX
1155#endif
1156
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001157#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1158#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1159#else
1160#define ST_FLAGS_IDX ST_RDEV_IDX
1161#endif
1162
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001163#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001164#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001165#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001166#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001167#endif
1168
1169#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1170#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1171#else
1172#define ST_BIRTHTIME_IDX ST_GEN_IDX
1173#endif
1174
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001175static PyStructSequence_Desc stat_result_desc = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001176 "stat_result", /* name */
1177 stat_result__doc__, /* doc */
1178 stat_result_fields,
1179 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001180};
1181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001182PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001183"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1184This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001185 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001186or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001187\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001189
1190static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001191 {"f_bsize", },
1192 {"f_frsize", },
1193 {"f_blocks", },
1194 {"f_bfree", },
1195 {"f_bavail", },
1196 {"f_files", },
1197 {"f_ffree", },
1198 {"f_favail", },
1199 {"f_flag", },
1200 {"f_namemax",},
1201 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001202};
1203
1204static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00001205 "statvfs_result", /* name */
1206 statvfs_result__doc__, /* doc */
1207 statvfs_result_fields,
1208 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001209};
1210
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001211static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001212static PyTypeObject StatResultType;
1213static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001214static newfunc structseq_new;
1215
1216static PyObject *
1217statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1218{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001219 PyStructSequence *result;
1220 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001221
Victor Stinnerd6f85422010-05-05 23:33:33 +00001222 result = (PyStructSequence*)structseq_new(type, args, kwds);
1223 if (!result)
1224 return NULL;
1225 /* If we have been initialized from a tuple,
1226 st_?time might be set to None. Initialize it
1227 from the int slots. */
1228 for (i = 7; i <= 9; i++) {
1229 if (result->ob_item[i+3] == Py_None) {
1230 Py_DECREF(Py_None);
1231 Py_INCREF(result->ob_item[i]);
1232 result->ob_item[i+3] = result->ob_item[i];
1233 }
1234 }
1235 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001236}
1237
1238
1239
1240/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001241static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001242
1243PyDoc_STRVAR(stat_float_times__doc__,
1244"stat_float_times([newval]) -> oldval\n\n\
1245Determine whether os.[lf]stat represents time stamps as float objects.\n\
1246If newval is True, future calls to stat() return floats, if it is False,\n\
1247future calls return ints. \n\
1248If newval is omitted, return the current setting.\n");
1249
1250static PyObject*
1251stat_float_times(PyObject* self, PyObject *args)
1252{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001253 int newval = -1;
1254 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1255 return NULL;
1256 if (newval == -1)
1257 /* Return old value */
1258 return PyBool_FromLong(_stat_float_times);
1259 _stat_float_times = newval;
1260 Py_INCREF(Py_None);
1261 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001262}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001263
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001264static void
1265fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1266{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001267 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001268#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinnerd6f85422010-05-05 23:33:33 +00001269 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001270#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001271 ival = PyInt_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001272#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001273 if (!ival)
1274 return;
1275 if (_stat_float_times) {
1276 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1277 } else {
1278 fval = ival;
1279 Py_INCREF(fval);
1280 }
1281 PyStructSequence_SET_ITEM(v, index, ival);
1282 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001283}
1284
Tim Peters5aa91602002-01-30 05:46:57 +00001285/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001286 (used by posix_stat() and posix_fstat()) */
1287static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001288_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001289{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001290 unsigned long ansec, mnsec, cnsec;
1291 PyObject *v = PyStructSequence_New(&StatResultType);
1292 if (v == NULL)
1293 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001294
Victor Stinnerd6f85422010-05-05 23:33:33 +00001295 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001296#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinnerd6f85422010-05-05 23:33:33 +00001297 PyStructSequence_SET_ITEM(v, 1,
1298 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001299#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001300 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001301#endif
1302#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001303 PyStructSequence_SET_ITEM(v, 2,
1304 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001305#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001306 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001307#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001308 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1309 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1310 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001311#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinnerd6f85422010-05-05 23:33:33 +00001312 PyStructSequence_SET_ITEM(v, 6,
1313 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001314#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001315 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001316#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001317
Martin v. Löwis14694662006-02-03 12:54:16 +00001318#if defined(HAVE_STAT_TV_NSEC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001319 ansec = st->st_atim.tv_nsec;
1320 mnsec = st->st_mtim.tv_nsec;
1321 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001322#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001323 ansec = st->st_atimespec.tv_nsec;
1324 mnsec = st->st_mtimespec.tv_nsec;
1325 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001326#elif defined(HAVE_STAT_NSEC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001327 ansec = st->st_atime_nsec;
1328 mnsec = st->st_mtime_nsec;
1329 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001330#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001331 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001332#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001333 fill_time(v, 7, st->st_atime, ansec);
1334 fill_time(v, 8, st->st_mtime, mnsec);
1335 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001336
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001337#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00001338 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1339 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001340#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001341#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001342 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1343 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001344#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001345#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00001346 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1347 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001348#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001349#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00001350 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1351 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001352#endif
1353#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00001354 {
1355 PyObject *val;
1356 unsigned long bsec,bnsec;
1357 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001358#ifdef HAVE_STAT_TV_NSEC2
Victor Stinnerd6f85422010-05-05 23:33:33 +00001359 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001360#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001361 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001362#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001363 if (_stat_float_times) {
1364 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1365 } else {
1366 val = PyInt_FromLong((long)bsec);
1367 }
1368 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1369 val);
1370 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001371#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001372#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001373 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1374 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001375#endif
Fred Drake699f3522000-06-29 21:12:41 +00001376
Victor Stinnerd6f85422010-05-05 23:33:33 +00001377 if (PyErr_Occurred()) {
1378 Py_DECREF(v);
1379 return NULL;
1380 }
Fred Drake699f3522000-06-29 21:12:41 +00001381
Victor Stinnerd6f85422010-05-05 23:33:33 +00001382 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001383}
1384
Martin v. Löwisd8948722004-06-02 09:57:56 +00001385#ifdef MS_WINDOWS
1386
1387/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1388 where / can be used in place of \ and the trailing slash is optional.
1389 Both SERVER and SHARE must have at least one character.
1390*/
1391
1392#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1393#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001394#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001395#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001396#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001397
Tim Peters4ad82172004-08-30 17:02:04 +00001398static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001399IsUNCRootA(char *path, int pathlen)
1400{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001401 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001402
Victor Stinnerd6f85422010-05-05 23:33:33 +00001403 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001404
Victor Stinnerd6f85422010-05-05 23:33:33 +00001405 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1406 /* minimum UNCRoot is \\x\y */
1407 return FALSE;
1408 for (i = 2; i < pathlen ; i++)
1409 if (ISSLASH(path[i])) break;
1410 if (i == 2 || i == pathlen)
1411 /* do not allow \\\SHARE or \\SERVER */
1412 return FALSE;
1413 share = i+1;
1414 for (i = share; i < pathlen; i++)
1415 if (ISSLASH(path[i])) break;
1416 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001417
Victor Stinnerd6f85422010-05-05 23:33:33 +00001418 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001419}
1420
Tim Peters4ad82172004-08-30 17:02:04 +00001421static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001422IsUNCRootW(Py_UNICODE *path, int pathlen)
1423{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001424 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001425
Victor Stinnerd6f85422010-05-05 23:33:33 +00001426 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001427
Victor Stinnerd6f85422010-05-05 23:33:33 +00001428 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1429 /* minimum UNCRoot is \\x\y */
1430 return FALSE;
1431 for (i = 2; i < pathlen ; i++)
1432 if (ISSLASH(path[i])) break;
1433 if (i == 2 || i == pathlen)
1434 /* do not allow \\\SHARE or \\SERVER */
1435 return FALSE;
1436 share = i+1;
1437 for (i = share; i < pathlen; i++)
1438 if (ISSLASH(path[i])) break;
1439 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001440
Victor Stinnerd6f85422010-05-05 23:33:33 +00001441 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001442}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001443#endif /* MS_WINDOWS */
1444
Barry Warsaw53699e91996-12-10 23:23:01 +00001445static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001446posix_do_stat(PyObject *self, PyObject *args,
Victor Stinnerd6f85422010-05-05 23:33:33 +00001447 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001448#ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001449 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001450#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001451 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001452#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001453 char *wformat,
1454 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001455{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001456 STRUCT_STAT st;
1457 char *path = NULL; /* pass this to stat; do not free() it */
1458 char *pathfree = NULL; /* this memory must be free'd */
1459 int res;
1460 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001461
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001462#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001463 PyUnicodeObject *po;
1464 if (PyArg_ParseTuple(args, wformat, &po)) {
1465 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001466
Victor Stinnerd6f85422010-05-05 23:33:33 +00001467 Py_BEGIN_ALLOW_THREADS
1468 /* PyUnicode_AS_UNICODE result OK without
1469 thread lock as it is a simple dereference. */
1470 res = wstatfunc(wpath, &st);
1471 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001472
Victor Stinnerd6f85422010-05-05 23:33:33 +00001473 if (res != 0)
1474 return win32_error_unicode("stat", wpath);
1475 return _pystat_fromstructstat(&st);
1476 }
1477 /* Drop the argument parsing error as narrow strings
1478 are also valid. */
1479 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001480#endif
1481
Victor Stinnerd6f85422010-05-05 23:33:33 +00001482 if (!PyArg_ParseTuple(args, format,
1483 Py_FileSystemDefaultEncoding, &path))
1484 return NULL;
1485 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001486
Victor Stinnerd6f85422010-05-05 23:33:33 +00001487 Py_BEGIN_ALLOW_THREADS
1488 res = (*statfunc)(path, &st);
1489 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001490
Victor Stinnerd6f85422010-05-05 23:33:33 +00001491 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001492#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001493 result = win32_error("stat", pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001494#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001495 result = posix_error_with_filename(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001496#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001497 }
1498 else
1499 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001500
Victor Stinnerd6f85422010-05-05 23:33:33 +00001501 PyMem_Free(pathfree);
1502 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001503}
1504
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001505/* POSIX methods */
1506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001507PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001508"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001509Use the real uid/gid to test for access to a path. Note that most\n\
1510operations will use the effective uid/gid, therefore this routine can\n\
1511be used in a suid/sgid environment to test if the invoking user has the\n\
1512specified access to the path. The mode argument can be F_OK to test\n\
1513existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001514
1515static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001516posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001517{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001518 char *path;
1519 int mode;
1520
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001521#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001522 DWORD attr;
1523 PyUnicodeObject *po;
1524 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1525 Py_BEGIN_ALLOW_THREADS
1526 /* PyUnicode_AS_UNICODE OK without thread lock as
1527 it is a simple dereference. */
1528 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1529 Py_END_ALLOW_THREADS
1530 goto finish;
1531 }
1532 /* Drop the argument parsing error as narrow strings
1533 are also valid. */
1534 PyErr_Clear();
1535 if (!PyArg_ParseTuple(args, "eti:access",
1536 Py_FileSystemDefaultEncoding, &path, &mode))
1537 return NULL;
1538 Py_BEGIN_ALLOW_THREADS
1539 attr = GetFileAttributesA(path);
1540 Py_END_ALLOW_THREADS
1541 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001542finish:
Victor Stinnerd6f85422010-05-05 23:33:33 +00001543 if (attr == 0xFFFFFFFF)
1544 /* File does not exist, or cannot read attributes */
1545 return PyBool_FromLong(0);
1546 /* Access is possible if either write access wasn't requested, or
1547 the file isn't read-only, or if it's a directory, as there are
1548 no read-only directories on Windows. */
1549 return PyBool_FromLong(!(mode & 2)
1550 || !(attr & FILE_ATTRIBUTE_READONLY)
1551 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001552#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00001553 int res;
1554 if (!PyArg_ParseTuple(args, "eti:access",
1555 Py_FileSystemDefaultEncoding, &path, &mode))
1556 return NULL;
1557 Py_BEGIN_ALLOW_THREADS
1558 res = access(path, mode);
1559 Py_END_ALLOW_THREADS
1560 PyMem_Free(path);
1561 return PyBool_FromLong(res == 0);
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001562#endif /* MS_WINDOWS */
Guido van Rossum94f6f721999-01-06 18:42:14 +00001563}
1564
Guido van Rossumd371ff11999-01-25 16:12:23 +00001565#ifndef F_OK
1566#define F_OK 0
1567#endif
1568#ifndef R_OK
1569#define R_OK 4
1570#endif
1571#ifndef W_OK
1572#define W_OK 2
1573#endif
1574#ifndef X_OK
1575#define X_OK 1
1576#endif
1577
1578#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001579PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001580"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001581Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001582
1583static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001584posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001585{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001586 int id;
1587 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001588
Victor Stinnerd6f85422010-05-05 23:33:33 +00001589 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1590 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001591
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001592#if defined(__VMS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001593 /* file descriptor 0 only, the default input device (stdin) */
1594 if (id == 0) {
1595 ret = ttyname();
1596 }
1597 else {
1598 ret = NULL;
1599 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001600#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001601 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001602#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001603 if (ret == NULL)
1604 return posix_error();
1605 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001606}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001607#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001608
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001609#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001610PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001611"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001612Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001613
1614static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001615posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001616{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001617 char *ret;
1618 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001619
Greg Wardb48bc172000-03-01 21:51:56 +00001620#ifdef USE_CTERMID_R
Victor Stinnerd6f85422010-05-05 23:33:33 +00001621 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001622#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001623 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001624#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00001625 if (ret == NULL)
1626 return posix_error();
1627 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001628}
1629#endif
1630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001631PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001632"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001634
Barry Warsaw53699e91996-12-10 23:23:01 +00001635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001636posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001637{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001638#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001639 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001640#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001641 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001642#elif defined(__VMS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001643 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001644#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001645 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001646#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001647}
1648
Fred Drake4d1e64b2002-04-15 19:40:07 +00001649#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001651"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001652Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001653opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001654
1655static PyObject *
1656posix_fchdir(PyObject *self, PyObject *fdobj)
1657{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001658 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001659}
1660#endif /* HAVE_FCHDIR */
1661
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001663PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001664"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001665Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001666
Barry Warsaw53699e91996-12-10 23:23:01 +00001667static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001668posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001669{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001670 char *path = NULL;
1671 int i;
1672 int res;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001673#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00001674 DWORD attr;
1675 PyUnicodeObject *po;
1676 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1677 Py_BEGIN_ALLOW_THREADS
1678 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1679 if (attr != 0xFFFFFFFF) {
1680 if (i & _S_IWRITE)
1681 attr &= ~FILE_ATTRIBUTE_READONLY;
1682 else
1683 attr |= FILE_ATTRIBUTE_READONLY;
1684 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1685 }
1686 else
1687 res = 0;
1688 Py_END_ALLOW_THREADS
1689 if (!res)
1690 return win32_error_unicode("chmod",
1691 PyUnicode_AS_UNICODE(po));
1692 Py_INCREF(Py_None);
1693 return Py_None;
1694 }
1695 /* Drop the argument parsing error as narrow strings
1696 are also valid. */
1697 PyErr_Clear();
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00001698
Victor Stinnerd6f85422010-05-05 23:33:33 +00001699 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1700 &path, &i))
1701 return NULL;
1702 Py_BEGIN_ALLOW_THREADS
1703 attr = GetFileAttributesA(path);
1704 if (attr != 0xFFFFFFFF) {
1705 if (i & _S_IWRITE)
1706 attr &= ~FILE_ATTRIBUTE_READONLY;
1707 else
1708 attr |= FILE_ATTRIBUTE_READONLY;
1709 res = SetFileAttributesA(path, attr);
1710 }
1711 else
1712 res = 0;
1713 Py_END_ALLOW_THREADS
1714 if (!res) {
1715 win32_error("chmod", path);
1716 PyMem_Free(path);
1717 return NULL;
1718 }
1719 PyMem_Free(path);
1720 Py_INCREF(Py_None);
1721 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00001722#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00001723 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1724 &path, &i))
1725 return NULL;
1726 Py_BEGIN_ALLOW_THREADS
1727 res = chmod(path, i);
1728 Py_END_ALLOW_THREADS
1729 if (res < 0)
1730 return posix_error_with_allocated_filename(path);
1731 PyMem_Free(path);
1732 Py_INCREF(Py_None);
1733 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001734#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001735}
1736
Christian Heimes36281872007-11-30 21:11:28 +00001737#ifdef HAVE_FCHMOD
1738PyDoc_STRVAR(posix_fchmod__doc__,
1739"fchmod(fd, mode)\n\n\
1740Change the access permissions of the file given by file\n\
1741descriptor fd.");
1742
1743static PyObject *
1744posix_fchmod(PyObject *self, PyObject *args)
1745{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001746 int fd, mode, res;
1747 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1748 return NULL;
1749 Py_BEGIN_ALLOW_THREADS
1750 res = fchmod(fd, mode);
1751 Py_END_ALLOW_THREADS
1752 if (res < 0)
1753 return posix_error();
1754 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001755}
1756#endif /* HAVE_FCHMOD */
1757
1758#ifdef HAVE_LCHMOD
1759PyDoc_STRVAR(posix_lchmod__doc__,
1760"lchmod(path, mode)\n\n\
1761Change the access permissions of a file. If path is a symlink, this\n\
1762affects the link itself rather than the target.");
1763
1764static PyObject *
1765posix_lchmod(PyObject *self, PyObject *args)
1766{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001767 char *path = NULL;
1768 int i;
1769 int res;
1770 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1771 &path, &i))
1772 return NULL;
1773 Py_BEGIN_ALLOW_THREADS
1774 res = lchmod(path, i);
1775 Py_END_ALLOW_THREADS
1776 if (res < 0)
1777 return posix_error_with_allocated_filename(path);
1778 PyMem_Free(path);
1779 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001780}
1781#endif /* HAVE_LCHMOD */
1782
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001783
Martin v. Löwis382abef2007-02-19 10:55:19 +00001784#ifdef HAVE_CHFLAGS
1785PyDoc_STRVAR(posix_chflags__doc__,
1786"chflags(path, flags)\n\n\
1787Set file flags.");
1788
1789static PyObject *
1790posix_chflags(PyObject *self, PyObject *args)
1791{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001792 char *path;
1793 unsigned long flags;
1794 int res;
1795 if (!PyArg_ParseTuple(args, "etk:chflags",
1796 Py_FileSystemDefaultEncoding, &path, &flags))
1797 return NULL;
1798 Py_BEGIN_ALLOW_THREADS
1799 res = chflags(path, flags);
1800 Py_END_ALLOW_THREADS
1801 if (res < 0)
1802 return posix_error_with_allocated_filename(path);
1803 PyMem_Free(path);
1804 Py_INCREF(Py_None);
1805 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001806}
1807#endif /* HAVE_CHFLAGS */
1808
1809#ifdef HAVE_LCHFLAGS
1810PyDoc_STRVAR(posix_lchflags__doc__,
1811"lchflags(path, flags)\n\n\
1812Set file flags.\n\
1813This function will not follow symbolic links.");
1814
1815static PyObject *
1816posix_lchflags(PyObject *self, PyObject *args)
1817{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001818 char *path;
1819 unsigned long flags;
1820 int res;
1821 if (!PyArg_ParseTuple(args, "etk:lchflags",
1822 Py_FileSystemDefaultEncoding, &path, &flags))
1823 return NULL;
1824 Py_BEGIN_ALLOW_THREADS
1825 res = lchflags(path, flags);
1826 Py_END_ALLOW_THREADS
1827 if (res < 0)
1828 return posix_error_with_allocated_filename(path);
1829 PyMem_Free(path);
1830 Py_INCREF(Py_None);
1831 return Py_None;
Martin v. Löwis382abef2007-02-19 10:55:19 +00001832}
1833#endif /* HAVE_LCHFLAGS */
1834
Martin v. Löwis244edc82001-10-04 22:44:26 +00001835#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001836PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001837"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001838Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001839
1840static PyObject *
1841posix_chroot(PyObject *self, PyObject *args)
1842{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001843 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001844}
1845#endif
1846
Guido van Rossum21142a01999-01-08 21:05:37 +00001847#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001848PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001849"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001850force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001851
1852static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001853posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001854{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001855 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001856}
1857#endif /* HAVE_FSYNC */
1858
1859#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001860
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001861#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001862extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1863#endif
1864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001866"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001867force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001868 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001869
1870static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001871posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001872{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001873 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001874}
1875#endif /* HAVE_FDATASYNC */
1876
1877
Fredrik Lundh10723342000-07-10 16:38:09 +00001878#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001879PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001880"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001881Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001882
Barry Warsaw53699e91996-12-10 23:23:01 +00001883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001884posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001885{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001886 char *path = NULL;
1887 long uid, gid;
1888 int res;
1889 if (!PyArg_ParseTuple(args, "etll:chown",
1890 Py_FileSystemDefaultEncoding, &path,
1891 &uid, &gid))
1892 return NULL;
1893 Py_BEGIN_ALLOW_THREADS
1894 res = chown(path, (uid_t) uid, (gid_t) gid);
1895 Py_END_ALLOW_THREADS
1896 if (res < 0)
1897 return posix_error_with_allocated_filename(path);
1898 PyMem_Free(path);
1899 Py_INCREF(Py_None);
1900 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001901}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001902#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001903
Christian Heimes36281872007-11-30 21:11:28 +00001904#ifdef HAVE_FCHOWN
1905PyDoc_STRVAR(posix_fchown__doc__,
1906"fchown(fd, uid, gid)\n\n\
1907Change the owner and group id of the file given by file descriptor\n\
1908fd to the numeric uid and gid.");
1909
1910static PyObject *
1911posix_fchown(PyObject *self, PyObject *args)
1912{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001913 int fd;
1914 long uid, gid;
1915 int res;
1916 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
1917 return NULL;
1918 Py_BEGIN_ALLOW_THREADS
1919 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1920 Py_END_ALLOW_THREADS
1921 if (res < 0)
1922 return posix_error();
1923 Py_RETURN_NONE;
Christian Heimes36281872007-11-30 21:11:28 +00001924}
1925#endif /* HAVE_FCHOWN */
1926
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001927#ifdef HAVE_LCHOWN
1928PyDoc_STRVAR(posix_lchown__doc__,
1929"lchown(path, uid, gid)\n\n\
1930Change the owner and group id of path to the numeric uid and gid.\n\
1931This function will not follow symbolic links.");
1932
1933static PyObject *
1934posix_lchown(PyObject *self, PyObject *args)
1935{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001936 char *path = NULL;
1937 long uid, gid;
1938 int res;
1939 if (!PyArg_ParseTuple(args, "etll:lchown",
1940 Py_FileSystemDefaultEncoding, &path,
1941 &uid, &gid))
1942 return NULL;
1943 Py_BEGIN_ALLOW_THREADS
1944 res = lchown(path, (uid_t) uid, (gid_t) gid);
1945 Py_END_ALLOW_THREADS
1946 if (res < 0)
1947 return posix_error_with_allocated_filename(path);
1948 PyMem_Free(path);
1949 Py_INCREF(Py_None);
1950 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001951}
1952#endif /* HAVE_LCHOWN */
1953
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001954
Guido van Rossum36bc6801995-06-14 22:54:23 +00001955#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001956PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001957"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001958Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001959
Stefan Krah182ae642010-07-13 19:17:08 +00001960#if (defined(__sun) && defined(__SVR4)) || defined(__OpenBSD__)
1961/* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */
1962static PyObject *
1963posix_getcwd(PyObject *self, PyObject *noargs)
1964{
1965 char buf[PATH_MAX+2];
1966 char *res;
1967
1968 Py_BEGIN_ALLOW_THREADS
Stefan Krah8cb9f032010-07-13 19:40:00 +00001969 res = getcwd(buf, sizeof buf);
Stefan Krah182ae642010-07-13 19:17:08 +00001970 Py_END_ALLOW_THREADS
1971
1972 if (res == NULL)
1973 return posix_error();
1974
1975 return PyString_FromString(buf);
1976}
1977#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001978static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001979posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001980{
Victor Stinnerd6f85422010-05-05 23:33:33 +00001981 int bufsize_incr = 1024;
1982 int bufsize = 0;
1983 char *tmpbuf = NULL;
1984 char *res = NULL;
1985 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00001986
Victor Stinnerd6f85422010-05-05 23:33:33 +00001987 Py_BEGIN_ALLOW_THREADS
1988 do {
1989 bufsize = bufsize + bufsize_incr;
1990 tmpbuf = malloc(bufsize);
1991 if (tmpbuf == NULL) {
1992 break;
1993 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001994#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00001995 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001996#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00001997 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001998#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00001999
Victor Stinnerd6f85422010-05-05 23:33:33 +00002000 if (res == NULL) {
2001 free(tmpbuf);
2002 }
2003 } while ((res == NULL) && (errno == ERANGE));
2004 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00002005
Victor Stinnerd6f85422010-05-05 23:33:33 +00002006 if (res == NULL)
2007 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00002008
Victor Stinnerd6f85422010-05-05 23:33:33 +00002009 dynamic_return = PyString_FromString(tmpbuf);
2010 free(tmpbuf);
Facundo Batista5596b0c2008-06-22 13:36:20 +00002011
Victor Stinnerd6f85422010-05-05 23:33:33 +00002012 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002013}
Stefan Krah182ae642010-07-13 19:17:08 +00002014#endif /* getcwd() NULL/ERANGE workaround. */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002015
Walter Dörwald3b918c32002-11-21 20:18:46 +00002016#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002017PyDoc_STRVAR(posix_getcwdu__doc__,
2018"getcwdu() -> path\n\n\
2019Return a unicode string representing the current working directory.");
2020
2021static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002022posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002023{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002024 char buf[1026];
2025 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002026
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002027#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002028 DWORD len;
2029 wchar_t wbuf[1026];
2030 wchar_t *wbuf2 = wbuf;
2031 PyObject *resobj;
2032 Py_BEGIN_ALLOW_THREADS
2033 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2034 /* If the buffer is large enough, len does not include the
2035 terminating \0. If the buffer is too small, len includes
2036 the space needed for the terminator. */
2037 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2038 wbuf2 = malloc(len * sizeof(wchar_t));
2039 if (wbuf2)
2040 len = GetCurrentDirectoryW(len, wbuf2);
2041 }
2042 Py_END_ALLOW_THREADS
2043 if (!wbuf2) {
2044 PyErr_NoMemory();
2045 return NULL;
2046 }
2047 if (!len) {
2048 if (wbuf2 != wbuf) free(wbuf2);
2049 return win32_error("getcwdu", NULL);
2050 }
2051 resobj = PyUnicode_FromWideChar(wbuf2, len);
2052 if (wbuf2 != wbuf) free(wbuf2);
2053 return resobj;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002054#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002055
Victor Stinnerd6f85422010-05-05 23:33:33 +00002056 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002057#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002058 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002059#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002060 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002061#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002062 Py_END_ALLOW_THREADS
2063 if (res == NULL)
2064 return posix_error();
2065 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002066}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002067#endif /* Py_USING_UNICODE */
2068#endif /* HAVE_GETCWD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002069
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002070
Guido van Rossumb6775db1994-08-01 11:34:53 +00002071#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002073"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075
Barry Warsaw53699e91996-12-10 23:23:01 +00002076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002077posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002078{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002079 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002080}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002081#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002082
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002085"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002086Return a list containing the names of the entries in the directory.\n\
2087\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00002088 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002089\n\
2090The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002091entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002092
Barry Warsaw53699e91996-12-10 23:23:01 +00002093static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002094posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002095{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002096 /* XXX Should redo this putting the (now four) versions of opendir
2097 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002098#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002099
Victor Stinnerd6f85422010-05-05 23:33:33 +00002100 PyObject *d, *v;
2101 HANDLE hFindFile;
2102 BOOL result;
2103 WIN32_FIND_DATA FileData;
2104 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2105 char *bufptr = namebuf;
2106 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002107
Victor Stinnerd6f85422010-05-05 23:33:33 +00002108 PyObject *po;
2109 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2110 WIN32_FIND_DATAW wFileData;
2111 Py_UNICODE *wnamebuf;
2112 /* Overallocate for \\*.*\0 */
2113 len = PyUnicode_GET_SIZE(po);
2114 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2115 if (!wnamebuf) {
2116 PyErr_NoMemory();
2117 return NULL;
2118 }
2119 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2120 if (len > 0) {
2121 Py_UNICODE wch = wnamebuf[len-1];
2122 if (wch != L'/' && wch != L'\\' && wch != L':')
2123 wnamebuf[len++] = L'\\';
2124 wcscpy(wnamebuf + len, L"*.*");
2125 }
2126 if ((d = PyList_New(0)) == NULL) {
2127 free(wnamebuf);
2128 return NULL;
2129 }
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002130 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002131 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002132 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002133 if (hFindFile == INVALID_HANDLE_VALUE) {
2134 int error = GetLastError();
2135 if (error == ERROR_FILE_NOT_FOUND) {
2136 free(wnamebuf);
2137 return d;
2138 }
2139 Py_DECREF(d);
2140 win32_error_unicode("FindFirstFileW", wnamebuf);
2141 free(wnamebuf);
2142 return NULL;
2143 }
2144 do {
2145 /* Skip over . and .. */
2146 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2147 wcscmp(wFileData.cFileName, L"..") != 0) {
2148 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2149 if (v == NULL) {
2150 Py_DECREF(d);
2151 d = NULL;
2152 break;
2153 }
2154 if (PyList_Append(d, v) != 0) {
2155 Py_DECREF(v);
2156 Py_DECREF(d);
2157 d = NULL;
2158 break;
2159 }
2160 Py_DECREF(v);
2161 }
2162 Py_BEGIN_ALLOW_THREADS
2163 result = FindNextFileW(hFindFile, &wFileData);
2164 Py_END_ALLOW_THREADS
2165 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2166 it got to the end of the directory. */
2167 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2168 Py_DECREF(d);
2169 win32_error_unicode("FindNextFileW", wnamebuf);
2170 FindClose(hFindFile);
2171 free(wnamebuf);
2172 return NULL;
2173 }
2174 } while (result == TRUE);
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002175
Victor Stinnerd6f85422010-05-05 23:33:33 +00002176 if (FindClose(hFindFile) == FALSE) {
2177 Py_DECREF(d);
2178 win32_error_unicode("FindClose", wnamebuf);
2179 free(wnamebuf);
2180 return NULL;
2181 }
2182 free(wnamebuf);
2183 return d;
2184 }
2185 /* Drop the argument parsing error as narrow strings
2186 are also valid. */
2187 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002188
Victor Stinnerd6f85422010-05-05 23:33:33 +00002189 if (!PyArg_ParseTuple(args, "et#:listdir",
2190 Py_FileSystemDefaultEncoding, &bufptr, &len))
2191 return NULL;
2192 if (len > 0) {
2193 char ch = namebuf[len-1];
2194 if (ch != SEP && ch != ALTSEP && ch != ':')
2195 namebuf[len++] = '/';
2196 strcpy(namebuf + len, "*.*");
2197 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002198
Victor Stinnerd6f85422010-05-05 23:33:33 +00002199 if ((d = PyList_New(0)) == NULL)
2200 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002201
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002202 Py_BEGIN_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002203 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitrou8cdede42010-08-10 00:04:13 +00002204 Py_END_ALLOW_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002205 if (hFindFile == INVALID_HANDLE_VALUE) {
2206 int error = GetLastError();
2207 if (error == ERROR_FILE_NOT_FOUND)
2208 return d;
2209 Py_DECREF(d);
2210 return win32_error("FindFirstFile", namebuf);
2211 }
2212 do {
2213 /* Skip over . and .. */
2214 if (strcmp(FileData.cFileName, ".") != 0 &&
2215 strcmp(FileData.cFileName, "..") != 0) {
2216 v = PyString_FromString(FileData.cFileName);
2217 if (v == NULL) {
2218 Py_DECREF(d);
2219 d = NULL;
2220 break;
2221 }
2222 if (PyList_Append(d, v) != 0) {
2223 Py_DECREF(v);
2224 Py_DECREF(d);
2225 d = NULL;
2226 break;
2227 }
2228 Py_DECREF(v);
2229 }
2230 Py_BEGIN_ALLOW_THREADS
2231 result = FindNextFile(hFindFile, &FileData);
2232 Py_END_ALLOW_THREADS
2233 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2234 it got to the end of the directory. */
2235 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2236 Py_DECREF(d);
2237 win32_error("FindNextFile", namebuf);
2238 FindClose(hFindFile);
2239 return NULL;
2240 }
2241 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002242
Victor Stinnerd6f85422010-05-05 23:33:33 +00002243 if (FindClose(hFindFile) == FALSE) {
2244 Py_DECREF(d);
2245 return win32_error("FindClose", namebuf);
2246 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002247
Victor Stinnerd6f85422010-05-05 23:33:33 +00002248 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002249
Tim Peters0bb44a42000-09-15 07:44:49 +00002250#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002251
2252#ifndef MAX_PATH
2253#define MAX_PATH CCHMAXPATH
2254#endif
2255 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002256 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002257 PyObject *d, *v;
2258 char namebuf[MAX_PATH+5];
2259 HDIR hdir = 1;
2260 ULONG srchcnt = 1;
2261 FILEFINDBUF3 ep;
2262 APIRET rc;
2263
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002264 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002265 return NULL;
2266 if (len >= MAX_PATH) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00002267 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002268 return NULL;
2269 }
2270 strcpy(namebuf, name);
2271 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002272 if (*pt == ALTSEP)
2273 *pt = SEP;
2274 if (namebuf[len-1] != SEP)
2275 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002276 strcpy(namebuf + len, "*.*");
2277
Victor Stinnerd6f85422010-05-05 23:33:33 +00002278 if ((d = PyList_New(0)) == NULL)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002279 return NULL;
2280
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002281 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2282 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002283 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002284 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2285 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2286 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002287
2288 if (rc != NO_ERROR) {
2289 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002290 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002291 }
2292
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002293 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002294 do {
2295 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002296 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002297 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002298
2299 strcpy(namebuf, ep.achName);
2300
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002301 /* Leave Case of Name Alone -- In Native Form */
2302 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002303
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002304 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002305 if (v == NULL) {
2306 Py_DECREF(d);
2307 d = NULL;
2308 break;
2309 }
2310 if (PyList_Append(d, v) != 0) {
2311 Py_DECREF(v);
2312 Py_DECREF(d);
2313 d = NULL;
2314 break;
2315 }
2316 Py_DECREF(v);
2317 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2318 }
2319
2320 return d;
2321#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002322
Victor Stinnerd6f85422010-05-05 23:33:33 +00002323 char *name = NULL;
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 Stinnerd6f85422010-05-05 23:33:33 +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, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2335 return NULL;
2336 if ((dirp = opendir(name)) == NULL) {
2337 return posix_error_with_allocated_filename(name);
2338 }
2339 if ((d = PyList_New(0)) == NULL) {
2340 closedir(dirp);
2341 PyMem_Free(name);
2342 return NULL;
2343 }
2344 for (;;) {
2345 errno = 0;
2346 Py_BEGIN_ALLOW_THREADS
2347 ep = readdir(dirp);
2348 Py_END_ALLOW_THREADS
2349 if (ep == NULL) {
2350 if (errno == 0) {
2351 break;
2352 } else {
2353 closedir(dirp);
2354 Py_DECREF(d);
2355 return posix_error_with_allocated_filename(name);
2356 }
2357 }
2358 if (ep->d_name[0] == '.' &&
2359 (NAMLEN(ep) == 1 ||
2360 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2361 continue;
2362 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2363 if (v == NULL) {
2364 Py_DECREF(d);
2365 d = NULL;
2366 break;
2367 }
Just van Rossum46c97842003-02-25 21:42:15 +00002368#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00002369 if (arg_is_unicode) {
2370 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002371
Victor Stinnerd6f85422010-05-05 23:33:33 +00002372 w = PyUnicode_FromEncodedObject(v,
2373 Py_FileSystemDefaultEncoding,
2374 "strict");
2375 if (w != NULL) {
2376 Py_DECREF(v);
2377 v = w;
2378 }
2379 else {
2380 /* fall back to the original byte string, as
2381 discussed in patch #683592 */
2382 PyErr_Clear();
2383 }
2384 }
Just van Rossum46c97842003-02-25 21:42:15 +00002385#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002386 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 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002396
Victor Stinnerd6f85422010-05-05 23:33:33 +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 Stinnerd6f85422010-05-05 23:33:33 +00002407 /* assume encoded strings won't more than double no of chars */
2408 char inbuf[MAX_PATH*2];
2409 char *inbufp = inbuf;
2410 Py_ssize_t insize = sizeof(inbuf);
2411 char outbuf[MAX_PATH*2];
2412 char *temp;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002413
Victor Stinnerd6f85422010-05-05 23:33:33 +00002414 PyUnicodeObject *po;
2415 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2416 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2417 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2418 Py_UNICODE *wtemp;
2419 DWORD result;
2420 PyObject *v;
2421 result = GetFullPathNameW(wpath,
2422 sizeof(woutbuf)/sizeof(woutbuf[0]),
2423 woutbuf, &wtemp);
2424 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2425 woutbufp = malloc(result * sizeof(Py_UNICODE));
2426 if (!woutbufp)
2427 return PyErr_NoMemory();
2428 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2429 }
2430 if (result)
2431 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2432 else
2433 v = win32_error_unicode("GetFullPathNameW", wpath);
2434 if (woutbufp != woutbuf)
2435 free(woutbufp);
2436 return v;
2437 }
2438 /* Drop the argument parsing error as narrow strings
2439 are also valid. */
2440 PyErr_Clear();
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002441
Victor Stinnerd6f85422010-05-05 23:33:33 +00002442 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2443 Py_FileSystemDefaultEncoding, &inbufp,
2444 &insize))
2445 return NULL;
2446 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2447 outbuf, &temp))
2448 return win32_error("GetFullPathName", inbuf);
2449 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2450 return PyUnicode_Decode(outbuf, strlen(outbuf),
2451 Py_FileSystemDefaultEncoding, NULL);
2452 }
2453 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002454} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002455#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002457PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002458"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002459Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002460
Barry Warsaw53699e91996-12-10 23:23:01 +00002461static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002462posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002463{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002464 int res;
2465 char *path = NULL;
2466 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002467
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002468#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002469 PyUnicodeObject *po;
2470 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2471 Py_BEGIN_ALLOW_THREADS
2472 /* PyUnicode_AS_UNICODE OK without thread lock as
2473 it is a simple dereference. */
2474 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2475 Py_END_ALLOW_THREADS
2476 if (!res)
2477 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2478 Py_INCREF(Py_None);
2479 return Py_None;
2480 }
2481 /* Drop the argument parsing error as narrow strings
2482 are also valid. */
2483 PyErr_Clear();
2484 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2485 Py_FileSystemDefaultEncoding, &path, &mode))
2486 return NULL;
2487 Py_BEGIN_ALLOW_THREADS
2488 /* PyUnicode_AS_UNICODE OK without thread lock as
2489 it is a simple dereference. */
2490 res = CreateDirectoryA(path, NULL);
2491 Py_END_ALLOW_THREADS
2492 if (!res) {
2493 win32_error("mkdir", path);
2494 PyMem_Free(path);
2495 return NULL;
2496 }
2497 PyMem_Free(path);
2498 Py_INCREF(Py_None);
2499 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002500#else /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002501
Victor Stinnerd6f85422010-05-05 23:33:33 +00002502 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2503 Py_FileSystemDefaultEncoding, &path, &mode))
2504 return NULL;
2505 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002506#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002507 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002508#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002509 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002510#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002511 Py_END_ALLOW_THREADS
2512 if (res < 0)
2513 return posix_error_with_allocated_filename(path);
2514 PyMem_Free(path);
2515 Py_INCREF(Py_None);
2516 return Py_None;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002517#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002518}
2519
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002520
Neal Norwitz1818ed72006-03-26 00:29:48 +00002521/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2522#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002523#include <sys/resource.h>
2524#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002525
Neal Norwitz1818ed72006-03-26 00:29:48 +00002526
2527#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002528PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002529"nice(inc) -> new_priority\n\n\
2530Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002531
Barry Warsaw53699e91996-12-10 23:23:01 +00002532static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002533posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002534{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002535 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002536
Victor Stinnerd6f85422010-05-05 23:33:33 +00002537 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2538 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002539
Victor Stinnerd6f85422010-05-05 23:33:33 +00002540 /* There are two flavours of 'nice': one that returns the new
2541 priority (as required by almost all standards out there) and the
2542 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2543 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002544
Victor Stinnerd6f85422010-05-05 23:33:33 +00002545 If we are of the nice family that returns the new priority, we
2546 need to clear errno before the call, and check if errno is filled
2547 before calling posix_error() on a returnvalue of -1, because the
2548 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002549
Victor Stinnerd6f85422010-05-05 23:33:33 +00002550 errno = 0;
2551 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002552#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002553 if (value == 0)
2554 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002555#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00002556 if (value == -1 && errno != 0)
2557 /* either nice() or getpriority() returned an error */
2558 return posix_error();
2559 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002560}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002561#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002562
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002563PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002564"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002565Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002566
Barry Warsaw53699e91996-12-10 23:23:01 +00002567static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002568posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002569{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002570#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002571 PyObject *o1, *o2;
2572 char *p1, *p2;
2573 BOOL result;
2574 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2575 goto error;
2576 if (!convert_to_unicode(&o1))
2577 goto error;
2578 if (!convert_to_unicode(&o2)) {
2579 Py_DECREF(o1);
2580 goto error;
2581 }
2582 Py_BEGIN_ALLOW_THREADS
2583 result = MoveFileW(PyUnicode_AsUnicode(o1),
2584 PyUnicode_AsUnicode(o2));
2585 Py_END_ALLOW_THREADS
2586 Py_DECREF(o1);
2587 Py_DECREF(o2);
2588 if (!result)
2589 return win32_error("rename", NULL);
2590 Py_INCREF(Py_None);
2591 return Py_None;
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002592error:
Victor Stinnerd6f85422010-05-05 23:33:33 +00002593 PyErr_Clear();
2594 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2595 return NULL;
2596 Py_BEGIN_ALLOW_THREADS
2597 result = MoveFileA(p1, p2);
2598 Py_END_ALLOW_THREADS
2599 if (!result)
2600 return win32_error("rename", NULL);
2601 Py_INCREF(Py_None);
2602 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002603#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002604 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002605#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002606}
2607
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002609PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002610"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002612
Barry Warsaw53699e91996-12-10 23:23:01 +00002613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002614posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002615{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002616#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002617 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002618#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002619 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002620#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002621}
2622
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002624PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002625"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002626Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002627
Barry Warsaw53699e91996-12-10 23:23:01 +00002628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002629posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002630{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002631#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002632 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002633#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002634 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002635#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002636}
2637
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002638
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002639#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002640PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002641"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002642Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002643
Barry Warsaw53699e91996-12-10 23:23:01 +00002644static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002645posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002646{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002647 char *command;
2648 long sts;
2649 if (!PyArg_ParseTuple(args, "s:system", &command))
2650 return NULL;
2651 Py_BEGIN_ALLOW_THREADS
2652 sts = system(command);
2653 Py_END_ALLOW_THREADS
2654 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002655}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002656#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002657
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002658
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002659PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002660"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002661Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002662
Barry Warsaw53699e91996-12-10 23:23:01 +00002663static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002664posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002665{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002666 int i;
2667 if (!PyArg_ParseTuple(args, "i:umask", &i))
2668 return NULL;
2669 i = (int)umask(i);
2670 if (i < 0)
2671 return posix_error();
2672 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002673}
2674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002676PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002677"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002681"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682Remove a file (same as unlink(path)).");
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_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002686{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002687#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002688 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002689#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002690 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002691#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002692}
2693
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002694
Guido van Rossumb6775db1994-08-01 11:34:53 +00002695#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002696PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002697"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002699
Barry Warsaw53699e91996-12-10 23:23:01 +00002700static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002701posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002702{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002703 struct utsname u;
2704 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002705
Victor Stinnerd6f85422010-05-05 23:33:33 +00002706 Py_BEGIN_ALLOW_THREADS
2707 res = uname(&u);
2708 Py_END_ALLOW_THREADS
2709 if (res < 0)
2710 return posix_error();
2711 return Py_BuildValue("(sssss)",
2712 u.sysname,
2713 u.nodename,
2714 u.release,
2715 u.version,
2716 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002717}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002718#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002719
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002720static int
2721extract_time(PyObject *t, long* sec, long* usec)
2722{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002723 long intval;
2724 if (PyFloat_Check(t)) {
2725 double tval = PyFloat_AsDouble(t);
2726 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2727 if (!intobj)
2728 return -1;
2729 intval = PyInt_AsLong(intobj);
2730 Py_DECREF(intobj);
2731 if (intval == -1 && PyErr_Occurred())
2732 return -1;
2733 *sec = intval;
2734 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2735 if (*usec < 0)
2736 /* If rounding gave us a negative number,
2737 truncate. */
2738 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002739 return 0;
Victor Stinnerd6f85422010-05-05 23:33:33 +00002740 }
2741 intval = PyInt_AsLong(t);
2742 if (intval == -1 && PyErr_Occurred())
2743 return -1;
2744 *sec = intval;
2745 *usec = 0;
2746 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002747}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002748
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002749PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002750"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002751utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002752Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002753second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002754
Barry Warsaw53699e91996-12-10 23:23:01 +00002755static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002756posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002757{
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002758#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00002759 PyObject *arg;
2760 PyUnicodeObject *obwpath;
2761 wchar_t *wpath = NULL;
2762 char *apath = NULL;
2763 HANDLE hFile;
2764 long atimesec, mtimesec, ausec, musec;
2765 FILETIME atime, mtime;
2766 PyObject *result = NULL;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002767
Victor Stinnerd6f85422010-05-05 23:33:33 +00002768 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2769 wpath = PyUnicode_AS_UNICODE(obwpath);
2770 Py_BEGIN_ALLOW_THREADS
2771 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2772 NULL, OPEN_EXISTING,
2773 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2774 Py_END_ALLOW_THREADS
2775 if (hFile == INVALID_HANDLE_VALUE)
2776 return win32_error_unicode("utime", wpath);
2777 } else
2778 /* Drop the argument parsing error as narrow strings
2779 are also valid. */
2780 PyErr_Clear();
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00002781
Victor Stinnerd6f85422010-05-05 23:33:33 +00002782 if (!wpath) {
2783 if (!PyArg_ParseTuple(args, "etO:utime",
2784 Py_FileSystemDefaultEncoding, &apath, &arg))
2785 return NULL;
2786 Py_BEGIN_ALLOW_THREADS
2787 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2788 NULL, OPEN_EXISTING,
2789 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2790 Py_END_ALLOW_THREADS
2791 if (hFile == INVALID_HANDLE_VALUE) {
2792 win32_error("utime", apath);
2793 PyMem_Free(apath);
2794 return NULL;
2795 }
2796 PyMem_Free(apath);
2797 }
2798
2799 if (arg == Py_None) {
2800 SYSTEMTIME now;
2801 GetSystemTime(&now);
2802 if (!SystemTimeToFileTime(&now, &mtime) ||
2803 !SystemTimeToFileTime(&now, &atime)) {
2804 win32_error("utime", NULL);
2805 goto done;
2806 }
2807 }
2808 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2809 PyErr_SetString(PyExc_TypeError,
2810 "utime() arg 2 must be a tuple (atime, mtime)");
2811 goto done;
2812 }
2813 else {
2814 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2815 &atimesec, &ausec) == -1)
2816 goto done;
2817 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2818 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2819 &mtimesec, &musec) == -1)
2820 goto done;
2821 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2822 }
2823 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2824 /* Avoid putting the file name into the error here,
2825 as that may confuse the user into believing that
2826 something is wrong with the file, when it also
2827 could be the time stamp that gives a problem. */
2828 win32_error("utime", NULL);
2829 }
2830 Py_INCREF(Py_None);
2831 result = Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002832done:
Victor Stinnerd6f85422010-05-05 23:33:33 +00002833 CloseHandle(hFile);
2834 return result;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002835#else /* MS_WINDOWS */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002836
Victor Stinnerd6f85422010-05-05 23:33:33 +00002837 char *path = NULL;
2838 long atime, mtime, ausec, musec;
2839 int res;
2840 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002841
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002842#if defined(HAVE_UTIMES)
Victor Stinnerd6f85422010-05-05 23:33:33 +00002843 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002844#define ATIME buf[0].tv_sec
2845#define MTIME buf[1].tv_sec
2846#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002847/* XXX should define struct utimbuf instead, above */
Victor Stinnerd6f85422010-05-05 23:33:33 +00002848 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002849#define ATIME buf.actime
2850#define MTIME buf.modtime
2851#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002852#else /* HAVE_UTIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00002853 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002854#define ATIME buf[0]
2855#define MTIME buf[1]
2856#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002857#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002858
Mark Hammond817c9292003-12-03 01:22:38 +00002859
Victor Stinnerd6f85422010-05-05 23:33:33 +00002860 if (!PyArg_ParseTuple(args, "etO:utime",
2861 Py_FileSystemDefaultEncoding, &path, &arg))
2862 return NULL;
2863 if (arg == Py_None) {
2864 /* optional time values not given */
2865 Py_BEGIN_ALLOW_THREADS
2866 res = utime(path, NULL);
2867 Py_END_ALLOW_THREADS
2868 }
2869 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2870 PyErr_SetString(PyExc_TypeError,
2871 "utime() arg 2 must be a tuple (atime, mtime)");
2872 PyMem_Free(path);
2873 return NULL;
2874 }
2875 else {
2876 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2877 &atime, &ausec) == -1) {
2878 PyMem_Free(path);
2879 return NULL;
2880 }
2881 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2882 &mtime, &musec) == -1) {
2883 PyMem_Free(path);
2884 return NULL;
2885 }
2886 ATIME = atime;
2887 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002888#ifdef HAVE_UTIMES
Victor Stinnerd6f85422010-05-05 23:33:33 +00002889 buf[0].tv_usec = ausec;
2890 buf[1].tv_usec = musec;
2891 Py_BEGIN_ALLOW_THREADS
2892 res = utimes(path, buf);
2893 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002894#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00002895 Py_BEGIN_ALLOW_THREADS
2896 res = utime(path, UTIME_ARG);
2897 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002898#endif /* HAVE_UTIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00002899 }
2900 if (res < 0) {
2901 return posix_error_with_allocated_filename(path);
2902 }
2903 PyMem_Free(path);
2904 Py_INCREF(Py_None);
2905 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002906#undef UTIME_ARG
2907#undef ATIME
2908#undef MTIME
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00002909#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002910}
2911
Guido van Rossum85e3b011991-06-03 12:42:10 +00002912
Guido van Rossum3b066191991-06-04 19:40:25 +00002913/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002916"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002917Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002918
Barry Warsaw53699e91996-12-10 23:23:01 +00002919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002920posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002921{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002922 int sts;
2923 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2924 return NULL;
2925 _exit(sts);
2926 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002927}
2928
Martin v. Löwis114619e2002-10-07 06:44:21 +00002929#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2930static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002931free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002932{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002933 Py_ssize_t i;
2934 for (i = 0; i < count; i++)
2935 PyMem_Free(array[i]);
2936 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002937}
2938#endif
2939
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002940
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002941#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002942PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002943"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002944Execute an executable path with arguments, replacing current process.\n\
2945\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00002946 path: path of executable file\n\
2947 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002948
Barry Warsaw53699e91996-12-10 23:23:01 +00002949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002950posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002951{
Victor Stinnerd6f85422010-05-05 23:33:33 +00002952 char *path;
2953 PyObject *argv;
2954 char **argvlist;
2955 Py_ssize_t i, argc;
2956 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002957
Victor Stinnerd6f85422010-05-05 23:33:33 +00002958 /* execv has two arguments: (path, argv), where
2959 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002960
Victor Stinnerd6f85422010-05-05 23:33:33 +00002961 if (!PyArg_ParseTuple(args, "etO:execv",
2962 Py_FileSystemDefaultEncoding,
2963 &path, &argv))
2964 return NULL;
2965 if (PyList_Check(argv)) {
2966 argc = PyList_Size(argv);
2967 getitem = PyList_GetItem;
2968 }
2969 else if (PyTuple_Check(argv)) {
2970 argc = PyTuple_Size(argv);
2971 getitem = PyTuple_GetItem;
2972 }
2973 else {
2974 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
2975 PyMem_Free(path);
2976 return NULL;
2977 }
2978 if (argc < 1) {
2979 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2980 PyMem_Free(path);
2981 return NULL;
2982 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002983
Victor Stinnerd6f85422010-05-05 23:33:33 +00002984 argvlist = PyMem_NEW(char *, argc+1);
2985 if (argvlist == NULL) {
2986 PyMem_Free(path);
2987 return PyErr_NoMemory();
2988 }
2989 for (i = 0; i < argc; i++) {
2990 if (!PyArg_Parse((*getitem)(argv, i), "et",
2991 Py_FileSystemDefaultEncoding,
2992 &argvlist[i])) {
2993 free_string_array(argvlist, i);
2994 PyErr_SetString(PyExc_TypeError,
2995 "execv() arg 2 must contain only strings");
2996 PyMem_Free(path);
2997 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002998
Victor Stinnerd6f85422010-05-05 23:33:33 +00002999 }
3000 }
3001 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003002
Victor Stinnerd6f85422010-05-05 23:33:33 +00003003 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003004
Victor Stinnerd6f85422010-05-05 23:33:33 +00003005 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003006
Victor Stinnerd6f85422010-05-05 23:33:33 +00003007 free_string_array(argvlist, argc);
3008 PyMem_Free(path);
3009 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003010}
3011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003013PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003014"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003015Execute a path with arguments and environment, replacing current process.\n\
3016\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003017 path: path of executable file\n\
3018 args: tuple or list of arguments\n\
3019 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003020
Barry Warsaw53699e91996-12-10 23:23:01 +00003021static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003022posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003023{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003024 char *path;
3025 PyObject *argv, *env;
3026 char **argvlist;
3027 char **envlist;
3028 PyObject *key, *val, *keys=NULL, *vals=NULL;
3029 Py_ssize_t i, pos, argc, envc;
3030 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3031 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003032
Victor Stinnerd6f85422010-05-05 23:33:33 +00003033 /* execve has three arguments: (path, argv, env), where
3034 argv is a list or tuple of strings and env is a dictionary
3035 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003036
Victor Stinnerd6f85422010-05-05 23:33:33 +00003037 if (!PyArg_ParseTuple(args, "etOO:execve",
3038 Py_FileSystemDefaultEncoding,
3039 &path, &argv, &env))
3040 return NULL;
3041 if (PyList_Check(argv)) {
3042 argc = PyList_Size(argv);
3043 getitem = PyList_GetItem;
3044 }
3045 else if (PyTuple_Check(argv)) {
3046 argc = PyTuple_Size(argv);
3047 getitem = PyTuple_GetItem;
3048 }
3049 else {
3050 PyErr_SetString(PyExc_TypeError,
3051 "execve() arg 2 must be a tuple or list");
3052 goto fail_0;
3053 }
3054 if (!PyMapping_Check(env)) {
3055 PyErr_SetString(PyExc_TypeError,
3056 "execve() arg 3 must be a mapping object");
3057 goto fail_0;
3058 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003059
Victor Stinnerd6f85422010-05-05 23:33:33 +00003060 argvlist = PyMem_NEW(char *, argc+1);
3061 if (argvlist == NULL) {
3062 PyErr_NoMemory();
3063 goto fail_0;
3064 }
3065 for (i = 0; i < argc; i++) {
3066 if (!PyArg_Parse((*getitem)(argv, i),
3067 "et;execve() arg 2 must contain only strings",
3068 Py_FileSystemDefaultEncoding,
3069 &argvlist[i]))
3070 {
3071 lastarg = i;
3072 goto fail_1;
3073 }
3074 }
3075 lastarg = argc;
3076 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003077
Victor Stinnerd6f85422010-05-05 23:33:33 +00003078 i = PyMapping_Size(env);
3079 if (i < 0)
3080 goto fail_1;
3081 envlist = PyMem_NEW(char *, i + 1);
3082 if (envlist == NULL) {
3083 PyErr_NoMemory();
3084 goto fail_1;
3085 }
3086 envc = 0;
3087 keys = PyMapping_Keys(env);
3088 vals = PyMapping_Values(env);
3089 if (!keys || !vals)
3090 goto fail_2;
3091 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3092 PyErr_SetString(PyExc_TypeError,
3093 "execve(): env.keys() or env.values() is not a list");
3094 goto fail_2;
3095 }
Tim Peters5aa91602002-01-30 05:46:57 +00003096
Victor Stinnerd6f85422010-05-05 23:33:33 +00003097 for (pos = 0; pos < i; pos++) {
3098 char *p, *k, *v;
3099 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003100
Victor Stinnerd6f85422010-05-05 23:33:33 +00003101 key = PyList_GetItem(keys, pos);
3102 val = PyList_GetItem(vals, pos);
3103 if (!key || !val)
3104 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003105
Victor Stinnerd6f85422010-05-05 23:33:33 +00003106 if (!PyArg_Parse(
3107 key,
3108 "s;execve() arg 3 contains a non-string key",
3109 &k) ||
3110 !PyArg_Parse(
3111 val,
3112 "s;execve() arg 3 contains a non-string value",
3113 &v))
3114 {
3115 goto fail_2;
3116 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003117
3118#if defined(PYOS_OS2)
3119 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3120 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3121#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003122 len = PyString_Size(key) + PyString_Size(val) + 2;
3123 p = PyMem_NEW(char, len);
3124 if (p == NULL) {
3125 PyErr_NoMemory();
3126 goto fail_2;
3127 }
3128 PyOS_snprintf(p, len, "%s=%s", k, v);
3129 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003130#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003131 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003132#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003133 }
3134 envlist[envc] = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003135
Victor Stinnerd6f85422010-05-05 23:33:33 +00003136 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003137
Victor Stinnerd6f85422010-05-05 23:33:33 +00003138 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003139
Victor Stinnerd6f85422010-05-05 23:33:33 +00003140 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003141
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003142 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003143 while (--envc >= 0)
3144 PyMem_DEL(envlist[envc]);
3145 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003146 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003147 free_string_array(argvlist, lastarg);
3148 Py_XDECREF(vals);
3149 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003150 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003151 PyMem_Free(path);
3152 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003153}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003154#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003155
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003156
Guido van Rossuma1065681999-01-25 23:20:23 +00003157#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003158PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003159"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003160Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003161\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003162 mode: mode of process creation\n\
3163 path: path of executable file\n\
3164 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003165
3166static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003167posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003168{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003169 char *path;
3170 PyObject *argv;
3171 char **argvlist;
3172 int mode, i;
3173 Py_ssize_t argc;
3174 Py_intptr_t spawnval;
3175 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003176
Victor Stinnerd6f85422010-05-05 23:33:33 +00003177 /* spawnv has three arguments: (mode, path, argv), where
3178 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003179
Victor Stinnerd6f85422010-05-05 23:33:33 +00003180 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3181 Py_FileSystemDefaultEncoding,
3182 &path, &argv))
3183 return NULL;
3184 if (PyList_Check(argv)) {
3185 argc = PyList_Size(argv);
3186 getitem = PyList_GetItem;
3187 }
3188 else if (PyTuple_Check(argv)) {
3189 argc = PyTuple_Size(argv);
3190 getitem = PyTuple_GetItem;
3191 }
3192 else {
3193 PyErr_SetString(PyExc_TypeError,
3194 "spawnv() arg 2 must be a tuple or list");
3195 PyMem_Free(path);
3196 return NULL;
3197 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003198
Victor Stinnerd6f85422010-05-05 23:33:33 +00003199 argvlist = PyMem_NEW(char *, argc+1);
3200 if (argvlist == NULL) {
3201 PyMem_Free(path);
3202 return PyErr_NoMemory();
3203 }
3204 for (i = 0; i < argc; i++) {
3205 if (!PyArg_Parse((*getitem)(argv, i), "et",
3206 Py_FileSystemDefaultEncoding,
3207 &argvlist[i])) {
3208 free_string_array(argvlist, i);
3209 PyErr_SetString(
3210 PyExc_TypeError,
3211 "spawnv() arg 2 must contain only strings");
3212 PyMem_Free(path);
3213 return NULL;
3214 }
3215 }
3216 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003217
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003218#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003219 Py_BEGIN_ALLOW_THREADS
3220 spawnval = spawnv(mode, path, argvlist);
3221 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003222#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003223 if (mode == _OLD_P_OVERLAY)
3224 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003225
Victor Stinnerd6f85422010-05-05 23:33:33 +00003226 Py_BEGIN_ALLOW_THREADS
3227 spawnval = _spawnv(mode, path, argvlist);
3228 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003229#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003230
Victor Stinnerd6f85422010-05-05 23:33:33 +00003231 free_string_array(argvlist, argc);
3232 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003233
Victor Stinnerd6f85422010-05-05 23:33:33 +00003234 if (spawnval == -1)
3235 return posix_error();
3236 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003237#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinnerd6f85422010-05-05 23:33:33 +00003238 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003239#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003240 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003241#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003242}
3243
3244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003245PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003246"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003247Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003248\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003249 mode: mode of process creation\n\
3250 path: path of executable file\n\
3251 args: tuple or list of arguments\n\
3252 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003253
3254static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003255posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003256{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003257 char *path;
3258 PyObject *argv, *env;
3259 char **argvlist;
3260 char **envlist;
3261 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3262 int mode, pos, envc;
3263 Py_ssize_t argc, i;
3264 Py_intptr_t spawnval;
3265 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3266 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003267
Victor Stinnerd6f85422010-05-05 23:33:33 +00003268 /* spawnve has four arguments: (mode, path, argv, env), where
3269 argv is a list or tuple of strings and env is a dictionary
3270 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003271
Victor Stinnerd6f85422010-05-05 23:33:33 +00003272 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3273 Py_FileSystemDefaultEncoding,
3274 &path, &argv, &env))
3275 return NULL;
3276 if (PyList_Check(argv)) {
3277 argc = PyList_Size(argv);
3278 getitem = PyList_GetItem;
3279 }
3280 else if (PyTuple_Check(argv)) {
3281 argc = PyTuple_Size(argv);
3282 getitem = PyTuple_GetItem;
3283 }
3284 else {
3285 PyErr_SetString(PyExc_TypeError,
3286 "spawnve() arg 2 must be a tuple or list");
3287 goto fail_0;
3288 }
3289 if (!PyMapping_Check(env)) {
3290 PyErr_SetString(PyExc_TypeError,
3291 "spawnve() arg 3 must be a mapping object");
3292 goto fail_0;
3293 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003294
Victor Stinnerd6f85422010-05-05 23:33:33 +00003295 argvlist = PyMem_NEW(char *, argc+1);
3296 if (argvlist == NULL) {
3297 PyErr_NoMemory();
3298 goto fail_0;
3299 }
3300 for (i = 0; i < argc; i++) {
3301 if (!PyArg_Parse((*getitem)(argv, i),
3302 "et;spawnve() arg 2 must contain only strings",
3303 Py_FileSystemDefaultEncoding,
3304 &argvlist[i]))
3305 {
3306 lastarg = i;
3307 goto fail_1;
3308 }
3309 }
3310 lastarg = argc;
3311 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003312
Victor Stinnerd6f85422010-05-05 23:33:33 +00003313 i = PyMapping_Size(env);
3314 if (i < 0)
3315 goto fail_1;
3316 envlist = PyMem_NEW(char *, i + 1);
3317 if (envlist == NULL) {
3318 PyErr_NoMemory();
3319 goto fail_1;
3320 }
3321 envc = 0;
3322 keys = PyMapping_Keys(env);
3323 vals = PyMapping_Values(env);
3324 if (!keys || !vals)
3325 goto fail_2;
3326 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3327 PyErr_SetString(PyExc_TypeError,
3328 "spawnve(): env.keys() or env.values() is not a list");
3329 goto fail_2;
3330 }
Tim Peters5aa91602002-01-30 05:46:57 +00003331
Victor Stinnerd6f85422010-05-05 23:33:33 +00003332 for (pos = 0; pos < i; pos++) {
3333 char *p, *k, *v;
3334 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003335
Victor Stinnerd6f85422010-05-05 23:33:33 +00003336 key = PyList_GetItem(keys, pos);
3337 val = PyList_GetItem(vals, pos);
3338 if (!key || !val)
3339 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003340
Victor Stinnerd6f85422010-05-05 23:33:33 +00003341 if (!PyArg_Parse(
3342 key,
3343 "s;spawnve() arg 3 contains a non-string key",
3344 &k) ||
3345 !PyArg_Parse(
3346 val,
3347 "s;spawnve() arg 3 contains a non-string value",
3348 &v))
3349 {
3350 goto fail_2;
3351 }
3352 len = PyString_Size(key) + PyString_Size(val) + 2;
3353 p = PyMem_NEW(char, len);
3354 if (p == NULL) {
3355 PyErr_NoMemory();
3356 goto fail_2;
3357 }
3358 PyOS_snprintf(p, len, "%s=%s", k, v);
3359 envlist[envc++] = p;
3360 }
3361 envlist[envc] = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003362
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003363#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003364 Py_BEGIN_ALLOW_THREADS
3365 spawnval = spawnve(mode, path, argvlist, envlist);
3366 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003367#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003368 if (mode == _OLD_P_OVERLAY)
3369 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003370
Victor Stinnerd6f85422010-05-05 23:33:33 +00003371 Py_BEGIN_ALLOW_THREADS
3372 spawnval = _spawnve(mode, path, argvlist, envlist);
3373 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003374#endif
Tim Peters25059d32001-12-07 20:35:43 +00003375
Victor Stinnerd6f85422010-05-05 23:33:33 +00003376 if (spawnval == -1)
3377 (void) posix_error();
3378 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003379#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinnerd6f85422010-05-05 23:33:33 +00003380 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003381#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003382 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003383#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003384
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003385 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003386 while (--envc >= 0)
3387 PyMem_DEL(envlist[envc]);
3388 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003389 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003390 free_string_array(argvlist, lastarg);
3391 Py_XDECREF(vals);
3392 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003393 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003394 PyMem_Free(path);
3395 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003396}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003397
3398/* OS/2 supports spawnvp & spawnvpe natively */
3399#if defined(PYOS_OS2)
3400PyDoc_STRVAR(posix_spawnvp__doc__,
3401"spawnvp(mode, file, args)\n\n\
3402Execute the program 'file' in a new process, using the environment\n\
3403search path to find the file.\n\
3404\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003405 mode: mode of process creation\n\
3406 file: executable file name\n\
3407 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003408
3409static PyObject *
3410posix_spawnvp(PyObject *self, PyObject *args)
3411{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003412 char *path;
3413 PyObject *argv;
3414 char **argvlist;
3415 int mode, i, argc;
3416 Py_intptr_t spawnval;
3417 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003418
Victor Stinnerd6f85422010-05-05 23:33:33 +00003419 /* spawnvp has three arguments: (mode, path, argv), where
3420 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003421
Victor Stinnerd6f85422010-05-05 23:33:33 +00003422 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3423 Py_FileSystemDefaultEncoding,
3424 &path, &argv))
3425 return NULL;
3426 if (PyList_Check(argv)) {
3427 argc = PyList_Size(argv);
3428 getitem = PyList_GetItem;
3429 }
3430 else if (PyTuple_Check(argv)) {
3431 argc = PyTuple_Size(argv);
3432 getitem = PyTuple_GetItem;
3433 }
3434 else {
3435 PyErr_SetString(PyExc_TypeError,
3436 "spawnvp() arg 2 must be a tuple or list");
3437 PyMem_Free(path);
3438 return NULL;
3439 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003440
Victor Stinnerd6f85422010-05-05 23:33:33 +00003441 argvlist = PyMem_NEW(char *, argc+1);
3442 if (argvlist == NULL) {
3443 PyMem_Free(path);
3444 return PyErr_NoMemory();
3445 }
3446 for (i = 0; i < argc; i++) {
3447 if (!PyArg_Parse((*getitem)(argv, i), "et",
3448 Py_FileSystemDefaultEncoding,
3449 &argvlist[i])) {
3450 free_string_array(argvlist, i);
3451 PyErr_SetString(
3452 PyExc_TypeError,
3453 "spawnvp() arg 2 must contain only strings");
3454 PyMem_Free(path);
3455 return NULL;
3456 }
3457 }
3458 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003459
Victor Stinnerd6f85422010-05-05 23:33:33 +00003460 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003461#if defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003462 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003463#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003464 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003465#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003466 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003467
Victor Stinnerd6f85422010-05-05 23:33:33 +00003468 free_string_array(argvlist, argc);
3469 PyMem_Free(path);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003470
Victor Stinnerd6f85422010-05-05 23:33:33 +00003471 if (spawnval == -1)
3472 return posix_error();
3473 else
3474 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003475}
3476
3477
3478PyDoc_STRVAR(posix_spawnvpe__doc__,
3479"spawnvpe(mode, file, args, env)\n\n\
3480Execute the program 'file' in a new process, using the environment\n\
3481search path to find the file.\n\
3482\n\
Victor Stinnerd6f85422010-05-05 23:33:33 +00003483 mode: mode of process creation\n\
3484 file: executable file name\n\
3485 args: tuple or list of arguments\n\
3486 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003487
3488static PyObject *
3489posix_spawnvpe(PyObject *self, PyObject *args)
3490{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003491 char *path;
3492 PyObject *argv, *env;
3493 char **argvlist;
3494 char **envlist;
3495 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3496 int mode, i, pos, argc, envc;
3497 Py_intptr_t spawnval;
3498 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3499 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003500
Victor Stinnerd6f85422010-05-05 23:33:33 +00003501 /* spawnvpe has four arguments: (mode, path, argv, env), where
3502 argv is a list or tuple of strings and env is a dictionary
3503 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003504
Victor Stinnerd6f85422010-05-05 23:33:33 +00003505 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3506 Py_FileSystemDefaultEncoding,
3507 &path, &argv, &env))
3508 return NULL;
3509 if (PyList_Check(argv)) {
3510 argc = PyList_Size(argv);
3511 getitem = PyList_GetItem;
3512 }
3513 else if (PyTuple_Check(argv)) {
3514 argc = PyTuple_Size(argv);
3515 getitem = PyTuple_GetItem;
3516 }
3517 else {
3518 PyErr_SetString(PyExc_TypeError,
3519 "spawnvpe() arg 2 must be a tuple or list");
3520 goto fail_0;
3521 }
3522 if (!PyMapping_Check(env)) {
3523 PyErr_SetString(PyExc_TypeError,
3524 "spawnvpe() arg 3 must be a mapping object");
3525 goto fail_0;
3526 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003527
Victor Stinnerd6f85422010-05-05 23:33:33 +00003528 argvlist = PyMem_NEW(char *, argc+1);
3529 if (argvlist == NULL) {
3530 PyErr_NoMemory();
3531 goto fail_0;
3532 }
3533 for (i = 0; i < argc; i++) {
3534 if (!PyArg_Parse((*getitem)(argv, i),
3535 "et;spawnvpe() arg 2 must contain only strings",
3536 Py_FileSystemDefaultEncoding,
3537 &argvlist[i]))
3538 {
3539 lastarg = i;
3540 goto fail_1;
3541 }
3542 }
3543 lastarg = argc;
3544 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003545
Victor Stinnerd6f85422010-05-05 23:33:33 +00003546 i = PyMapping_Size(env);
3547 if (i < 0)
3548 goto fail_1;
3549 envlist = PyMem_NEW(char *, i + 1);
3550 if (envlist == NULL) {
3551 PyErr_NoMemory();
3552 goto fail_1;
3553 }
3554 envc = 0;
3555 keys = PyMapping_Keys(env);
3556 vals = PyMapping_Values(env);
3557 if (!keys || !vals)
3558 goto fail_2;
3559 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3560 PyErr_SetString(PyExc_TypeError,
3561 "spawnvpe(): env.keys() or env.values() is not a list");
3562 goto fail_2;
3563 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003564
Victor Stinnerd6f85422010-05-05 23:33:33 +00003565 for (pos = 0; pos < i; pos++) {
3566 char *p, *k, *v;
3567 size_t len;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003568
Victor Stinnerd6f85422010-05-05 23:33:33 +00003569 key = PyList_GetItem(keys, pos);
3570 val = PyList_GetItem(vals, pos);
3571 if (!key || !val)
3572 goto fail_2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003573
Victor Stinnerd6f85422010-05-05 23:33:33 +00003574 if (!PyArg_Parse(
3575 key,
3576 "s;spawnvpe() arg 3 contains a non-string key",
3577 &k) ||
3578 !PyArg_Parse(
3579 val,
3580 "s;spawnvpe() arg 3 contains a non-string value",
3581 &v))
3582 {
3583 goto fail_2;
3584 }
3585 len = PyString_Size(key) + PyString_Size(val) + 2;
3586 p = PyMem_NEW(char, len);
3587 if (p == NULL) {
3588 PyErr_NoMemory();
3589 goto fail_2;
3590 }
3591 PyOS_snprintf(p, len, "%s=%s", k, v);
3592 envlist[envc++] = p;
3593 }
3594 envlist[envc] = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003595
Victor Stinnerd6f85422010-05-05 23:33:33 +00003596 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003597#if defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003598 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003599#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003600 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003601#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003602 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003603
Victor Stinnerd6f85422010-05-05 23:33:33 +00003604 if (spawnval == -1)
3605 (void) posix_error();
3606 else
3607 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003608
3609 fail_2:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003610 while (--envc >= 0)
3611 PyMem_DEL(envlist[envc]);
3612 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003613 fail_1:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003614 free_string_array(argvlist, lastarg);
3615 Py_XDECREF(vals);
3616 Py_XDECREF(keys);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003617 fail_0:
Victor Stinnerd6f85422010-05-05 23:33:33 +00003618 PyMem_Free(path);
3619 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003620}
3621#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003622#endif /* HAVE_SPAWNV */
3623
3624
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003625#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003626PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003627"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003628Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3629\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003630Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003631
3632static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003633posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003634{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003635 pid_t pid;
3636 int result = 0;
3637 _PyImport_AcquireLock();
3638 pid = fork1();
3639 if (pid == 0) {
3640 /* child: this clobbers and resets the import lock. */
3641 PyOS_AfterFork();
3642 } else {
3643 /* parent: release the import lock. */
3644 result = _PyImport_ReleaseLock();
3645 }
3646 if (pid == -1)
3647 return posix_error();
3648 if (result < 0) {
3649 /* Don't clobber the OSError if the fork failed. */
3650 PyErr_SetString(PyExc_RuntimeError,
3651 "not holding the import lock");
3652 return NULL;
3653 }
3654 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003655}
3656#endif
3657
3658
Guido van Rossumad0ee831995-03-01 10:34:45 +00003659#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003660PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003661"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003662Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003663Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003664
Barry Warsaw53699e91996-12-10 23:23:01 +00003665static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003666posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003667{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003668 pid_t pid;
3669 int result = 0;
3670 _PyImport_AcquireLock();
3671 pid = fork();
3672 if (pid == 0) {
3673 /* child: this clobbers and resets the import lock. */
3674 PyOS_AfterFork();
3675 } else {
3676 /* parent: release the import lock. */
3677 result = _PyImport_ReleaseLock();
3678 }
3679 if (pid == -1)
3680 return posix_error();
3681 if (result < 0) {
3682 /* Don't clobber the OSError if the fork failed. */
3683 PyErr_SetString(PyExc_RuntimeError,
3684 "not holding the import lock");
3685 return NULL;
3686 }
3687 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003688}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003689#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003690
Neal Norwitzb59798b2003-03-21 01:43:31 +00003691/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003692/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3693#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003694#define DEV_PTY_FILE "/dev/ptc"
3695#define HAVE_DEV_PTMX
3696#else
3697#define DEV_PTY_FILE "/dev/ptmx"
3698#endif
3699
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003700#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003701#ifdef HAVE_PTY_H
3702#include <pty.h>
3703#else
3704#ifdef HAVE_LIBUTIL_H
3705#include <libutil.h>
Ronald Oussorena55af9a2010-01-17 16:25:57 +00003706#else
3707#ifdef HAVE_UTIL_H
3708#include <util.h>
3709#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003710#endif /* HAVE_LIBUTIL_H */
3711#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003712#ifdef HAVE_STROPTS_H
3713#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003714#endif
3715#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003716
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003717#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003718PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003719"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003720Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003721
3722static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003723posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003724{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003725 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003726#ifndef HAVE_OPENPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00003727 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003728#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003729#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003730 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003731#ifdef sun
Victor Stinnerd6f85422010-05-05 23:33:33 +00003732 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003733#endif
3734#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003735
Thomas Wouters70c21a12000-07-14 14:28:33 +00003736#ifdef HAVE_OPENPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00003737 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3738 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003739#elif defined(HAVE__GETPTY)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003740 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3741 if (slave_name == NULL)
3742 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003743
Victor Stinnerd6f85422010-05-05 23:33:33 +00003744 slave_fd = open(slave_name, O_RDWR);
3745 if (slave_fd < 0)
3746 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003747#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003748 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3749 if (master_fd < 0)
3750 return posix_error();
3751 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3752 /* change permission of slave */
3753 if (grantpt(master_fd) < 0) {
3754 PyOS_setsig(SIGCHLD, sig_saved);
3755 return posix_error();
3756 }
3757 /* unlock slave */
3758 if (unlockpt(master_fd) < 0) {
3759 PyOS_setsig(SIGCHLD, sig_saved);
3760 return posix_error();
3761 }
3762 PyOS_setsig(SIGCHLD, sig_saved);
3763 slave_name = ptsname(master_fd); /* get name of slave */
3764 if (slave_name == NULL)
3765 return posix_error();
3766 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3767 if (slave_fd < 0)
3768 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003769#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00003770 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3771 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003772#ifndef __hpux
Victor Stinnerd6f85422010-05-05 23:33:33 +00003773 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003774#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003775#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003776#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003777
Victor Stinnerd6f85422010-05-05 23:33:33 +00003778 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003779
Fred Drake8cef4cf2000-06-28 16:40:38 +00003780}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003781#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003782
3783#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003784PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003785"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003786Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3787Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003788To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003789
3790static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003791posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003792{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003793 int master_fd = -1, result = 0;
3794 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003795
Victor Stinnerd6f85422010-05-05 23:33:33 +00003796 _PyImport_AcquireLock();
3797 pid = forkpty(&master_fd, NULL, NULL, NULL);
3798 if (pid == 0) {
3799 /* child: this clobbers and resets the import lock. */
3800 PyOS_AfterFork();
3801 } else {
3802 /* parent: release the import lock. */
3803 result = _PyImport_ReleaseLock();
3804 }
3805 if (pid == -1)
3806 return posix_error();
3807 if (result < 0) {
3808 /* Don't clobber the OSError if the fork failed. */
3809 PyErr_SetString(PyExc_RuntimeError,
3810 "not holding the import lock");
3811 return NULL;
3812 }
3813 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003814}
3815#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003816
Guido van Rossumad0ee831995-03-01 10:34:45 +00003817#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003818PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003819"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003820Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003821
Barry Warsaw53699e91996-12-10 23:23:01 +00003822static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003823posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003824{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003825 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003826}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003827#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003828
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003829
Guido van Rossumad0ee831995-03-01 10:34:45 +00003830#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003831PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003832"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003833Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003834
Barry Warsaw53699e91996-12-10 23:23:01 +00003835static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003836posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003837{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003838 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003839}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003840#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003841
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003842
Guido van Rossumad0ee831995-03-01 10:34:45 +00003843#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003844PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003845"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003846Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003847
Barry Warsaw53699e91996-12-10 23:23:01 +00003848static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003849posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003850{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003851 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003852}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003853#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003854
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003855
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003856PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003857"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003858Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003859
Barry Warsaw53699e91996-12-10 23:23:01 +00003860static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003861posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003862{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003863 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003864}
3865
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003866
Fred Drakec9680921999-12-13 16:37:25 +00003867#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003868PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003869"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003870Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003871
3872static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003873posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003874{
3875 PyObject *result = NULL;
3876
Fred Drakec9680921999-12-13 16:37:25 +00003877#ifdef NGROUPS_MAX
3878#define MAX_GROUPS NGROUPS_MAX
3879#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00003880 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00003881#define MAX_GROUPS 64
3882#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00003883 gid_t grouplist[MAX_GROUPS];
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00003884
3885 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
3886 * This is a helper variable to store the intermediate result when
3887 * that happens.
3888 *
3889 * To keep the code readable the OSX behaviour is unconditional,
3890 * according to the POSIX spec this should be safe on all unix-y
3891 * systems.
3892 */
3893 gid_t* alt_grouplist = grouplist;
Victor Stinnerd6f85422010-05-05 23:33:33 +00003894 int n;
Fred Drakec9680921999-12-13 16:37:25 +00003895
Victor Stinnerd6f85422010-05-05 23:33:33 +00003896 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00003897 if (n < 0) {
3898 if (errno == EINVAL) {
3899 n = getgroups(0, NULL);
3900 if (n == -1) {
3901 return posix_error();
3902 }
3903 if (n == 0) {
3904 /* Avoid malloc(0) */
3905 alt_grouplist = grouplist;
3906 } else {
3907 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
3908 if (alt_grouplist == NULL) {
3909 errno = EINVAL;
3910 return posix_error();
3911 }
3912 n = getgroups(n, alt_grouplist);
3913 if (n == -1) {
3914 PyMem_Free(alt_grouplist);
3915 return posix_error();
3916 }
3917 }
3918 } else {
3919 return posix_error();
3920 }
3921 }
3922 result = PyList_New(n);
3923 if (result != NULL) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00003924 int i;
3925 for (i = 0; i < n; ++i) {
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00003926 PyObject *o = PyInt_FromLong((long)alt_grouplist[i]);
Victor Stinnerd6f85422010-05-05 23:33:33 +00003927 if (o == NULL) {
3928 Py_DECREF(result);
3929 result = NULL;
3930 break;
Fred Drakec9680921999-12-13 16:37:25 +00003931 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00003932 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00003933 }
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00003934 }
3935
3936 if (alt_grouplist != grouplist) {
3937 PyMem_Free(alt_grouplist);
Victor Stinnerd6f85422010-05-05 23:33:33 +00003938 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003939
Fred Drakec9680921999-12-13 16:37:25 +00003940 return result;
3941}
3942#endif
3943
Antoine Pitrou30b3b352009-12-02 20:37:54 +00003944#ifdef HAVE_INITGROUPS
3945PyDoc_STRVAR(posix_initgroups__doc__,
3946"initgroups(username, gid) -> None\n\n\
3947Call the system initgroups() to initialize the group access list with all of\n\
3948the groups of which the specified username is a member, plus the specified\n\
3949group id.");
3950
3951static PyObject *
3952posix_initgroups(PyObject *self, PyObject *args)
3953{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003954 char *username;
3955 long gid;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00003956
Victor Stinnerd6f85422010-05-05 23:33:33 +00003957 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
3958 return NULL;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00003959
Victor Stinnerd6f85422010-05-05 23:33:33 +00003960 if (initgroups(username, (gid_t) gid) == -1)
3961 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou30b3b352009-12-02 20:37:54 +00003962
Victor Stinnerd6f85422010-05-05 23:33:33 +00003963 Py_INCREF(Py_None);
3964 return Py_None;
Antoine Pitrou30b3b352009-12-02 20:37:54 +00003965}
3966#endif
3967
Martin v. Löwis606edc12002-06-13 21:09:11 +00003968#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003969PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003970"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003971Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003972
3973static PyObject *
3974posix_getpgid(PyObject *self, PyObject *args)
3975{
Victor Stinnerd6f85422010-05-05 23:33:33 +00003976 pid_t pid, pgid;
3977 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
3978 return NULL;
3979 pgid = getpgid(pid);
3980 if (pgid < 0)
3981 return posix_error();
3982 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003983}
3984#endif /* HAVE_GETPGID */
3985
3986
Guido van Rossumb6775db1994-08-01 11:34:53 +00003987#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003988PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003989"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003990Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003991
Barry Warsaw53699e91996-12-10 23:23:01 +00003992static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003993posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003994{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003995#ifdef GETPGRP_HAVE_ARG
Victor Stinnerd6f85422010-05-05 23:33:33 +00003996 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003997#else /* GETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00003998 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003999#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004000}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004001#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004002
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004003
Guido van Rossumb6775db1994-08-01 11:34:53 +00004004#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004005PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004006"setpgrp()\n\n\
Senthil Kumaran0d6908b2010-06-17 16:38:34 +00004007Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004008
Barry Warsaw53699e91996-12-10 23:23:01 +00004009static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004010posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004011{
Guido van Rossum64933891994-10-20 21:56:42 +00004012#ifdef SETPGRP_HAVE_ARG
Victor Stinnerd6f85422010-05-05 23:33:33 +00004013 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004014#else /* SETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004015 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004016#endif /* SETPGRP_HAVE_ARG */
Victor Stinnerd6f85422010-05-05 23:33:33 +00004017 return posix_error();
4018 Py_INCREF(Py_None);
4019 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004020}
4021
Guido van Rossumb6775db1994-08-01 11:34:53 +00004022#endif /* HAVE_SETPGRP */
4023
Guido van Rossumad0ee831995-03-01 10:34:45 +00004024#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004025PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004026"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004027Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004028
Barry Warsaw53699e91996-12-10 23:23:01 +00004029static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004030posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004031{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004032 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004033}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004034#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004035
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004036
Fred Drake12c6e2d1999-12-14 21:25:03 +00004037#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004038PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004039"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004040Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004041
4042static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004043posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004044{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004045 PyObject *result = NULL;
4046 char *name;
4047 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004048
Victor Stinnerd6f85422010-05-05 23:33:33 +00004049 errno = 0;
4050 name = getlogin();
4051 if (name == NULL) {
4052 if (errno)
4053 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004054 else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004055 PyErr_SetString(PyExc_OSError,
4056 "unable to determine login name");
4057 }
4058 else
4059 result = PyString_FromString(name);
4060 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004061
Fred Drake12c6e2d1999-12-14 21:25:03 +00004062 return result;
4063}
4064#endif
4065
Guido van Rossumad0ee831995-03-01 10:34:45 +00004066#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004067PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004068"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004069Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004070
Barry Warsaw53699e91996-12-10 23:23:01 +00004071static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004072posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004073{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004074 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004075}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004076#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004077
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004078
Guido van Rossumad0ee831995-03-01 10:34:45 +00004079#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004080PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004081"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004082Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004083
Barry Warsaw53699e91996-12-10 23:23:01 +00004084static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004085posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004086{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004087 pid_t pid;
4088 int sig;
4089 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
4090 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004091#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004092 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4093 APIRET rc;
4094 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004095 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004096
4097 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4098 APIRET rc;
4099 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004100 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004101
4102 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004103 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004104#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00004105 if (kill(pid, sig) == -1)
4106 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004107#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004108 Py_INCREF(Py_None);
4109 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004110}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004111#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004112
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004113#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004114PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004115"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004116Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004117
4118static PyObject *
4119posix_killpg(PyObject *self, PyObject *args)
4120{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004121 int sig;
4122 pid_t pgid;
4123 /* XXX some man pages make the `pgid` parameter an int, others
4124 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4125 take the same type. Moreover, pid_t is always at least as wide as
4126 int (else compilation of this module fails), which is safe. */
4127 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
4128 return NULL;
4129 if (killpg(pgid, sig) == -1)
4130 return posix_error();
4131 Py_INCREF(Py_None);
4132 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004133}
4134#endif
4135
Brian Curtine5aa8862010-04-02 23:26:06 +00004136#ifdef MS_WINDOWS
4137PyDoc_STRVAR(win32_kill__doc__,
4138"kill(pid, sig)\n\n\
4139Kill a process with a signal.");
4140
4141static PyObject *
4142win32_kill(PyObject *self, PyObject *args)
4143{
Amaury Forgeot d'Arc03acec22010-05-15 21:45:30 +00004144 PyObject *result;
Victor Stinnerd6f85422010-05-05 23:33:33 +00004145 DWORD pid, sig, err;
4146 HANDLE handle;
Brian Curtine5aa8862010-04-02 23:26:06 +00004147
Victor Stinnerd6f85422010-05-05 23:33:33 +00004148 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4149 return NULL;
Brian Curtine5aa8862010-04-02 23:26:06 +00004150
Victor Stinnerd6f85422010-05-05 23:33:33 +00004151 /* Console processes which share a common console can be sent CTRL+C or
4152 CTRL+BREAK events, provided they handle said events. */
4153 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4154 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4155 err = GetLastError();
4156 return PyErr_SetFromWindowsErr(err);
4157 }
4158 else
4159 Py_RETURN_NONE;
4160 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004161
Victor Stinnerd6f85422010-05-05 23:33:33 +00004162 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4163 attempt to open and terminate the process. */
4164 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4165 if (handle == NULL) {
4166 err = GetLastError();
4167 return PyErr_SetFromWindowsErr(err);
4168 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004169
Victor Stinnerd6f85422010-05-05 23:33:33 +00004170 if (TerminateProcess(handle, sig) == 0) {
4171 err = GetLastError();
4172 result = PyErr_SetFromWindowsErr(err);
4173 } else {
4174 Py_INCREF(Py_None);
4175 result = Py_None;
4176 }
Brian Curtine5aa8862010-04-02 23:26:06 +00004177
Victor Stinnerd6f85422010-05-05 23:33:33 +00004178 CloseHandle(handle);
4179 return result;
Brian Curtine5aa8862010-04-02 23:26:06 +00004180}
4181#endif /* MS_WINDOWS */
4182
Guido van Rossumc0125471996-06-28 18:55:32 +00004183#ifdef HAVE_PLOCK
4184
4185#ifdef HAVE_SYS_LOCK_H
4186#include <sys/lock.h>
4187#endif
4188
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004189PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004190"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004191Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004192
Barry Warsaw53699e91996-12-10 23:23:01 +00004193static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004194posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004195{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004196 int op;
4197 if (!PyArg_ParseTuple(args, "i:plock", &op))
4198 return NULL;
4199 if (plock(op) == -1)
4200 return posix_error();
4201 Py_INCREF(Py_None);
4202 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004203}
4204#endif
4205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004206
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004207#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004208PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004209"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004210Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004211
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004212#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004213#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004214static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004215async_system(const char *command)
4216{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004217 char errormsg[256], args[1024];
4218 RESULTCODES rcodes;
4219 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004220
Victor Stinnerd6f85422010-05-05 23:33:33 +00004221 char *shell = getenv("COMSPEC");
4222 if (!shell)
4223 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004224
Victor Stinnerd6f85422010-05-05 23:33:33 +00004225 /* avoid overflowing the argument buffer */
4226 if (strlen(shell) + 3 + strlen(command) >= 1024)
4227 return ERROR_NOT_ENOUGH_MEMORY
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004228
Victor Stinnerd6f85422010-05-05 23:33:33 +00004229 args[0] = '\0';
4230 strcat(args, shell);
4231 strcat(args, "/c ");
4232 strcat(args, command);
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004233
Victor Stinnerd6f85422010-05-05 23:33:33 +00004234 /* execute asynchronously, inheriting the environment */
4235 rc = DosExecPgm(errormsg,
4236 sizeof(errormsg),
4237 EXEC_ASYNC,
4238 args,
4239 NULL,
4240 &rcodes,
4241 shell);
4242 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004243}
4244
Guido van Rossumd48f2521997-12-05 22:19:34 +00004245static FILE *
4246popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004247{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004248 int oldfd, tgtfd;
4249 HFILE pipeh[2];
4250 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004251
Victor Stinnerd6f85422010-05-05 23:33:33 +00004252 /* mode determines which of stdin or stdout is reconnected to
4253 * the pipe to the child
4254 */
4255 if (strchr(mode, 'r') != NULL) {
4256 tgt_fd = 1; /* stdout */
4257 } else if (strchr(mode, 'w')) {
4258 tgt_fd = 0; /* stdin */
4259 } else {
4260 *err = ERROR_INVALID_ACCESS;
4261 return NULL;
4262 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004263
Victor Stinnerd6f85422010-05-05 23:33:33 +00004264 /* setup the pipe */
4265 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4266 *err = rc;
4267 return NULL;
4268 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004269
Victor Stinnerd6f85422010-05-05 23:33:33 +00004270 /* prevent other threads accessing stdio */
4271 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004272
Victor Stinnerd6f85422010-05-05 23:33:33 +00004273 /* reconnect stdio and execute child */
4274 oldfd = dup(tgtfd);
4275 close(tgtfd);
4276 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4277 DosClose(pipeh[tgtfd]);
4278 rc = async_system(command);
4279 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004280
Victor Stinnerd6f85422010-05-05 23:33:33 +00004281 /* restore stdio */
4282 dup2(oldfd, tgtfd);
4283 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004284
Victor Stinnerd6f85422010-05-05 23:33:33 +00004285 /* allow other threads access to stdio */
4286 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004287
Victor Stinnerd6f85422010-05-05 23:33:33 +00004288 /* if execution of child was successful return file stream */
4289 if (rc == NO_ERROR)
4290 return fdopen(pipeh[1 - tgtfd], mode);
4291 else {
4292 DosClose(pipeh[1 - tgtfd]);
4293 *err = rc;
4294 return NULL;
4295 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004296}
4297
4298static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004299posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004300{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004301 char *name;
4302 char *mode = "r";
4303 int err, bufsize = -1;
4304 FILE *fp;
4305 PyObject *f;
4306 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4307 return NULL;
4308 Py_BEGIN_ALLOW_THREADS
4309 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4310 Py_END_ALLOW_THREADS
4311 if (fp == NULL)
4312 return os2_error(err);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004313
Victor Stinnerd6f85422010-05-05 23:33:33 +00004314 f = PyFile_FromFile(fp, name, mode, fclose);
4315 if (f != NULL)
4316 PyFile_SetBufSize(f, bufsize);
4317 return f;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004318}
4319
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004320#elif defined(PYCC_GCC)
4321
4322/* standard posix version of popen() support */
4323static PyObject *
4324posix_popen(PyObject *self, PyObject *args)
4325{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004326 char *name;
4327 char *mode = "r";
4328 int bufsize = -1;
4329 FILE *fp;
4330 PyObject *f;
4331 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4332 return NULL;
4333 Py_BEGIN_ALLOW_THREADS
4334 fp = popen(name, mode);
4335 Py_END_ALLOW_THREADS
4336 if (fp == NULL)
4337 return posix_error();
4338 f = PyFile_FromFile(fp, name, mode, pclose);
4339 if (f != NULL)
4340 PyFile_SetBufSize(f, bufsize);
4341 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004342}
4343
4344/* fork() under OS/2 has lots'o'warts
4345 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4346 * most of this code is a ripoff of the win32 code, but using the
4347 * capabilities of EMX's C library routines
4348 */
4349
4350/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4351#define POPEN_1 1
4352#define POPEN_2 2
4353#define POPEN_3 3
4354#define POPEN_4 4
4355
4356static PyObject *_PyPopen(char *, int, int, int);
4357static int _PyPclose(FILE *file);
4358
4359/*
4360 * Internal dictionary mapping popen* file pointers to process handles,
4361 * for use when retrieving the process exit code. See _PyPclose() below
4362 * for more information on this dictionary's use.
4363 */
4364static PyObject *_PyPopenProcs = NULL;
4365
4366/* os2emx version of popen2()
4367 *
4368 * The result of this function is a pipe (file) connected to the
4369 * process's stdin, and a pipe connected to the process's stdout.
4370 */
4371
4372static PyObject *
4373os2emx_popen2(PyObject *self, PyObject *args)
4374{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004375 PyObject *f;
4376 int tm=0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004377
Victor Stinnerd6f85422010-05-05 23:33:33 +00004378 char *cmdstring;
4379 char *mode = "t";
4380 int bufsize = -1;
4381 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4382 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004383
Victor Stinnerd6f85422010-05-05 23:33:33 +00004384 if (*mode == 't')
4385 tm = O_TEXT;
4386 else if (*mode != 'b') {
4387 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4388 return NULL;
4389 } else
4390 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004391
Victor Stinnerd6f85422010-05-05 23:33:33 +00004392 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004393
Victor Stinnerd6f85422010-05-05 23:33:33 +00004394 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004395}
4396
4397/*
4398 * Variation on os2emx.popen2
4399 *
4400 * The result of this function is 3 pipes - the process's stdin,
4401 * stdout and stderr
4402 */
4403
4404static PyObject *
4405os2emx_popen3(PyObject *self, PyObject *args)
4406{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004407 PyObject *f;
4408 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004409
Victor Stinnerd6f85422010-05-05 23:33:33 +00004410 char *cmdstring;
4411 char *mode = "t";
4412 int bufsize = -1;
4413 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4414 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004415
Victor Stinnerd6f85422010-05-05 23:33:33 +00004416 if (*mode == 't')
4417 tm = O_TEXT;
4418 else if (*mode != 'b') {
4419 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4420 return NULL;
4421 } else
4422 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004423
Victor Stinnerd6f85422010-05-05 23:33:33 +00004424 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004425
Victor Stinnerd6f85422010-05-05 23:33:33 +00004426 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004427}
4428
4429/*
4430 * Variation on os2emx.popen2
4431 *
Tim Peters11b23062003-04-23 02:39:17 +00004432 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004433 * and stdout+stderr combined as a single pipe.
4434 */
4435
4436static PyObject *
4437os2emx_popen4(PyObject *self, PyObject *args)
4438{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004439 PyObject *f;
4440 int tm = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004441
Victor Stinnerd6f85422010-05-05 23:33:33 +00004442 char *cmdstring;
4443 char *mode = "t";
4444 int bufsize = -1;
4445 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4446 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004447
Victor Stinnerd6f85422010-05-05 23:33:33 +00004448 if (*mode == 't')
4449 tm = O_TEXT;
4450 else if (*mode != 'b') {
4451 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4452 return NULL;
4453 } else
4454 tm = O_BINARY;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004455
Victor Stinnerd6f85422010-05-05 23:33:33 +00004456 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004457
Victor Stinnerd6f85422010-05-05 23:33:33 +00004458 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004459}
4460
4461/* a couple of structures for convenient handling of multiple
4462 * file handles and pipes
4463 */
4464struct file_ref
4465{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004466 int handle;
4467 int flags;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004468};
4469
4470struct pipe_ref
4471{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004472 int rd;
4473 int wr;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004474};
4475
4476/* The following code is derived from the win32 code */
4477
4478static PyObject *
4479_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4480{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004481 struct file_ref stdio[3];
4482 struct pipe_ref p_fd[3];
4483 FILE *p_s[3];
4484 int file_count, i, pipe_err;
4485 pid_t pipe_pid;
4486 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4487 PyObject *f, *p_f[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004488
Victor Stinnerd6f85422010-05-05 23:33:33 +00004489 /* file modes for subsequent fdopen's on pipe handles */
4490 if (mode == O_TEXT)
4491 {
4492 rd_mode = "rt";
4493 wr_mode = "wt";
4494 }
4495 else
4496 {
4497 rd_mode = "rb";
4498 wr_mode = "wb";
4499 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004500
Victor Stinnerd6f85422010-05-05 23:33:33 +00004501 /* prepare shell references */
4502 if ((shell = getenv("EMXSHELL")) == NULL)
4503 if ((shell = getenv("COMSPEC")) == NULL)
4504 {
4505 errno = ENOENT;
4506 return posix_error();
4507 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004508
Victor Stinnerd6f85422010-05-05 23:33:33 +00004509 sh_name = _getname(shell);
4510 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4511 opt = "/c";
4512 else
4513 opt = "-c";
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004514
Victor Stinnerd6f85422010-05-05 23:33:33 +00004515 /* save current stdio fds + their flags, and set not inheritable */
4516 i = pipe_err = 0;
4517 while (pipe_err >= 0 && i < 3)
4518 {
4519 pipe_err = stdio[i].handle = dup(i);
4520 stdio[i].flags = fcntl(i, F_GETFD, 0);
4521 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4522 i++;
4523 }
4524 if (pipe_err < 0)
4525 {
4526 /* didn't get them all saved - clean up and bail out */
4527 int saved_err = errno;
4528 while (i-- > 0)
4529 {
4530 close(stdio[i].handle);
4531 }
4532 errno = saved_err;
4533 return posix_error();
4534 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004535
Victor Stinnerd6f85422010-05-05 23:33:33 +00004536 /* create pipe ends */
4537 file_count = 2;
4538 if (n == POPEN_3)
4539 file_count = 3;
4540 i = pipe_err = 0;
4541 while ((pipe_err == 0) && (i < file_count))
4542 pipe_err = pipe((int *)&p_fd[i++]);
4543 if (pipe_err < 0)
4544 {
4545 /* didn't get them all made - clean up and bail out */
4546 while (i-- > 0)
4547 {
4548 close(p_fd[i].wr);
4549 close(p_fd[i].rd);
4550 }
4551 errno = EPIPE;
4552 return posix_error();
4553 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004554
Victor Stinnerd6f85422010-05-05 23:33:33 +00004555 /* change the actual standard IO streams over temporarily,
4556 * making the retained pipe ends non-inheritable
4557 */
4558 pipe_err = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004559
Victor Stinnerd6f85422010-05-05 23:33:33 +00004560 /* - stdin */
4561 if (dup2(p_fd[0].rd, 0) == 0)
4562 {
4563 close(p_fd[0].rd);
4564 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4565 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4566 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4567 {
4568 close(p_fd[0].wr);
4569 pipe_err = -1;
4570 }
4571 }
4572 else
4573 {
4574 pipe_err = -1;
4575 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004576
Victor Stinnerd6f85422010-05-05 23:33:33 +00004577 /* - stdout */
4578 if (pipe_err == 0)
4579 {
4580 if (dup2(p_fd[1].wr, 1) == 1)
4581 {
4582 close(p_fd[1].wr);
4583 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4584 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4585 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4586 {
4587 close(p_fd[1].rd);
4588 pipe_err = -1;
4589 }
4590 }
4591 else
4592 {
4593 pipe_err = -1;
4594 }
4595 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004596
Victor Stinnerd6f85422010-05-05 23:33:33 +00004597 /* - stderr, as required */
4598 if (pipe_err == 0)
4599 switch (n)
4600 {
4601 case POPEN_3:
4602 {
4603 if (dup2(p_fd[2].wr, 2) == 2)
4604 {
4605 close(p_fd[2].wr);
4606 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4607 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4608 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4609 {
4610 close(p_fd[2].rd);
4611 pipe_err = -1;
4612 }
4613 }
4614 else
4615 {
4616 pipe_err = -1;
4617 }
4618 break;
4619 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004620
Victor Stinnerd6f85422010-05-05 23:33:33 +00004621 case POPEN_4:
4622 {
4623 if (dup2(1, 2) != 2)
4624 {
4625 pipe_err = -1;
4626 }
4627 break;
4628 }
4629 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004630
Victor Stinnerd6f85422010-05-05 23:33:33 +00004631 /* spawn the child process */
4632 if (pipe_err == 0)
4633 {
4634 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4635 if (pipe_pid == -1)
4636 {
4637 pipe_err = -1;
4638 }
4639 else
4640 {
4641 /* save the PID into the FILE structure
4642 * NOTE: this implementation doesn't actually
4643 * take advantage of this, but do it for
4644 * completeness - AIM Apr01
4645 */
4646 for (i = 0; i < file_count; i++)
4647 p_s[i]->_pid = pipe_pid;
4648 }
4649 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004650
Victor Stinnerd6f85422010-05-05 23:33:33 +00004651 /* reset standard IO to normal */
4652 for (i = 0; i < 3; i++)
4653 {
4654 dup2(stdio[i].handle, i);
4655 fcntl(i, F_SETFD, stdio[i].flags);
4656 close(stdio[i].handle);
4657 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004658
Victor Stinnerd6f85422010-05-05 23:33:33 +00004659 /* if any remnant problems, clean up and bail out */
4660 if (pipe_err < 0)
4661 {
4662 for (i = 0; i < 3; i++)
4663 {
4664 close(p_fd[i].rd);
4665 close(p_fd[i].wr);
4666 }
4667 errno = EPIPE;
4668 return posix_error_with_filename(cmdstring);
4669 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004670
Victor Stinnerd6f85422010-05-05 23:33:33 +00004671 /* build tuple of file objects to return */
4672 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4673 PyFile_SetBufSize(p_f[0], bufsize);
4674 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4675 PyFile_SetBufSize(p_f[1], bufsize);
4676 if (n == POPEN_3)
4677 {
4678 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4679 PyFile_SetBufSize(p_f[0], bufsize);
4680 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4681 }
4682 else
4683 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004684
Victor Stinnerd6f85422010-05-05 23:33:33 +00004685 /*
4686 * Insert the files we've created into the process dictionary
4687 * all referencing the list with the process handle and the
4688 * initial number of files (see description below in _PyPclose).
4689 * Since if _PyPclose later tried to wait on a process when all
4690 * handles weren't closed, it could create a deadlock with the
4691 * child, we spend some energy here to try to ensure that we
4692 * either insert all file handles into the dictionary or none
4693 * at all. It's a little clumsy with the various popen modes
4694 * and variable number of files involved.
4695 */
4696 if (!_PyPopenProcs)
4697 {
4698 _PyPopenProcs = PyDict_New();
4699 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004700
Victor Stinnerd6f85422010-05-05 23:33:33 +00004701 if (_PyPopenProcs)
4702 {
4703 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4704 int ins_rc[3];
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004705
Victor Stinnerd6f85422010-05-05 23:33:33 +00004706 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4707 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004708
Victor Stinnerd6f85422010-05-05 23:33:33 +00004709 procObj = PyList_New(2);
4710 pidObj = PyLong_FromPid(pipe_pid);
4711 intObj = PyInt_FromLong((long) file_count);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004712
Victor Stinnerd6f85422010-05-05 23:33:33 +00004713 if (procObj && pidObj && intObj)
4714 {
4715 PyList_SetItem(procObj, 0, pidObj);
4716 PyList_SetItem(procObj, 1, intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004717
Victor Stinnerd6f85422010-05-05 23:33:33 +00004718 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4719 if (fileObj[0])
4720 {
4721 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4722 fileObj[0],
4723 procObj);
4724 }
4725 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4726 if (fileObj[1])
4727 {
4728 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4729 fileObj[1],
4730 procObj);
4731 }
4732 if (file_count >= 3)
4733 {
4734 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4735 if (fileObj[2])
4736 {
4737 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4738 fileObj[2],
4739 procObj);
4740 }
4741 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004742
Victor Stinnerd6f85422010-05-05 23:33:33 +00004743 if (ins_rc[0] < 0 || !fileObj[0] ||
4744 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4745 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4746 {
4747 /* Something failed - remove any dictionary
4748 * entries that did make it.
4749 */
4750 if (!ins_rc[0] && fileObj[0])
4751 {
4752 PyDict_DelItem(_PyPopenProcs,
4753 fileObj[0]);
4754 }
4755 if (!ins_rc[1] && fileObj[1])
4756 {
4757 PyDict_DelItem(_PyPopenProcs,
4758 fileObj[1]);
4759 }
4760 if (!ins_rc[2] && fileObj[2])
4761 {
4762 PyDict_DelItem(_PyPopenProcs,
4763 fileObj[2]);
4764 }
4765 }
4766 }
Tim Peters11b23062003-04-23 02:39:17 +00004767
Victor Stinnerd6f85422010-05-05 23:33:33 +00004768 /*
4769 * Clean up our localized references for the dictionary keys
4770 * and value since PyDict_SetItem will Py_INCREF any copies
4771 * that got placed in the dictionary.
4772 */
4773 Py_XDECREF(procObj);
4774 Py_XDECREF(fileObj[0]);
4775 Py_XDECREF(fileObj[1]);
4776 Py_XDECREF(fileObj[2]);
4777 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004778
Victor Stinnerd6f85422010-05-05 23:33:33 +00004779 /* Child is launched. */
4780 return f;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004781}
4782
4783/*
4784 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4785 * exit code for the child process and return as a result of the close.
4786 *
4787 * This function uses the _PyPopenProcs dictionary in order to map the
4788 * input file pointer to information about the process that was
4789 * originally created by the popen* call that created the file pointer.
4790 * The dictionary uses the file pointer as a key (with one entry
4791 * inserted for each file returned by the original popen* call) and a
4792 * single list object as the value for all files from a single call.
4793 * The list object contains the Win32 process handle at [0], and a file
4794 * count at [1], which is initialized to the total number of file
4795 * handles using that list.
4796 *
4797 * This function closes whichever handle it is passed, and decrements
4798 * the file count in the dictionary for the process handle pointed to
4799 * by this file. On the last close (when the file count reaches zero),
4800 * this function will wait for the child process and then return its
4801 * exit code as the result of the close() operation. This permits the
4802 * files to be closed in any order - it is always the close() of the
4803 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004804 *
4805 * NOTE: This function is currently called with the GIL released.
4806 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004807 */
4808
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004809static int _PyPclose(FILE *file)
4810{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004811 int result;
4812 int exit_code;
4813 pid_t pipe_pid;
4814 PyObject *procObj, *pidObj, *intObj, *fileObj;
4815 int file_count;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004816#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00004817 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004818#endif
4819
Victor Stinnerd6f85422010-05-05 23:33:33 +00004820 /* Close the file handle first, to ensure it can't block the
4821 * child from exiting if it's the last handle.
4822 */
4823 result = fclose(file);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004824
4825#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00004826 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004827#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004828 if (_PyPopenProcs)
4829 {
4830 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4831 (procObj = PyDict_GetItem(_PyPopenProcs,
4832 fileObj)) != NULL &&
4833 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4834 (intObj = PyList_GetItem(procObj,1)) != NULL)
4835 {
4836 pipe_pid = (pid_t) PyLong_AsPid(pidObj);
4837 file_count = (int) PyInt_AsLong(intObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004838
Victor Stinnerd6f85422010-05-05 23:33:33 +00004839 if (file_count > 1)
4840 {
4841 /* Still other files referencing process */
4842 file_count--;
4843 PyList_SetItem(procObj,1,
4844 PyInt_FromLong((long) file_count));
4845 }
4846 else
4847 {
4848 /* Last file for this process */
4849 if (result != EOF &&
4850 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4851 {
4852 /* extract exit status */
4853 if (WIFEXITED(exit_code))
4854 {
4855 result = WEXITSTATUS(exit_code);
4856 }
4857 else
4858 {
4859 errno = EPIPE;
4860 result = -1;
4861 }
4862 }
4863 else
4864 {
4865 /* Indicate failure - this will cause the file object
4866 * to raise an I/O error and translate the last
4867 * error code from errno. We do have a problem with
4868 * last errors that overlap the normal errno table,
4869 * but that's a consistent problem with the file object.
4870 */
4871 result = -1;
4872 }
4873 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004874
Victor Stinnerd6f85422010-05-05 23:33:33 +00004875 /* Remove this file pointer from dictionary */
4876 PyDict_DelItem(_PyPopenProcs, fileObj);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004877
Victor Stinnerd6f85422010-05-05 23:33:33 +00004878 if (PyDict_Size(_PyPopenProcs) == 0)
4879 {
4880 Py_DECREF(_PyPopenProcs);
4881 _PyPopenProcs = NULL;
4882 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004883
Victor Stinnerd6f85422010-05-05 23:33:33 +00004884 } /* if object retrieval ok */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004885
Victor Stinnerd6f85422010-05-05 23:33:33 +00004886 Py_XDECREF(fileObj);
4887 } /* if _PyPopenProcs */
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004888
4889#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00004890 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004891#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00004892 return result;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004893}
4894
4895#endif /* PYCC_??? */
4896
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004897#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004898
4899/*
4900 * Portable 'popen' replacement for Win32.
4901 *
4902 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4903 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004904 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004905 */
4906
4907#include <malloc.h>
4908#include <io.h>
4909#include <fcntl.h>
4910
4911/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4912#define POPEN_1 1
4913#define POPEN_2 2
4914#define POPEN_3 3
4915#define POPEN_4 4
4916
4917static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004918static int _PyPclose(FILE *file);
4919
4920/*
4921 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004922 * for use when retrieving the process exit code. See _PyPclose() below
4923 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004924 */
4925static PyObject *_PyPopenProcs = NULL;
4926
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004927
4928/* popen that works from a GUI.
4929 *
4930 * The result of this function is a pipe (file) connected to the
4931 * processes stdin or stdout, depending on the requested mode.
4932 */
4933
4934static PyObject *
4935posix_popen(PyObject *self, PyObject *args)
4936{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004937 PyObject *f;
4938 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004939
Victor Stinnerd6f85422010-05-05 23:33:33 +00004940 char *cmdstring;
4941 char *mode = "r";
4942 int bufsize = -1;
4943 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
4944 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004945
Victor Stinnerd6f85422010-05-05 23:33:33 +00004946 if (*mode == 'r')
4947 tm = _O_RDONLY;
4948 else if (*mode != 'w') {
4949 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
4950 return NULL;
4951 } else
4952 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004953
Victor Stinnerd6f85422010-05-05 23:33:33 +00004954 if (bufsize != -1) {
4955 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
4956 return NULL;
4957 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004958
Victor Stinnerd6f85422010-05-05 23:33:33 +00004959 if (*(mode+1) == 't')
4960 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4961 else if (*(mode+1) == 'b')
4962 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
4963 else
4964 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004965
Victor Stinnerd6f85422010-05-05 23:33:33 +00004966 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004967}
4968
4969/* Variation on win32pipe.popen
4970 *
4971 * The result of this function is a pipe (file) connected to the
4972 * process's stdin, and a pipe connected to the process's stdout.
4973 */
4974
4975static PyObject *
4976win32_popen2(PyObject *self, PyObject *args)
4977{
Victor Stinnerd6f85422010-05-05 23:33:33 +00004978 PyObject *f;
4979 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004980
Victor Stinnerd6f85422010-05-05 23:33:33 +00004981 char *cmdstring;
4982 char *mode = "t";
4983 int bufsize = -1;
4984 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4985 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004986
Victor Stinnerd6f85422010-05-05 23:33:33 +00004987 if (*mode == 't')
4988 tm = _O_TEXT;
4989 else if (*mode != 'b') {
4990 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
4991 return NULL;
4992 } else
4993 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004994
Victor Stinnerd6f85422010-05-05 23:33:33 +00004995 if (bufsize != -1) {
4996 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
4997 return NULL;
4998 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004999
Victor Stinnerd6f85422010-05-05 23:33:33 +00005000 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00005001
Victor Stinnerd6f85422010-05-05 23:33:33 +00005002 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005003}
5004
5005/*
5006 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00005007 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005008 * The result of this function is 3 pipes - the process's stdin,
5009 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005010 */
5011
5012static PyObject *
5013win32_popen3(PyObject *self, PyObject *args)
5014{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005015 PyObject *f;
5016 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005017
Victor Stinnerd6f85422010-05-05 23:33:33 +00005018 char *cmdstring;
5019 char *mode = "t";
5020 int bufsize = -1;
5021 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
5022 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005023
Victor Stinnerd6f85422010-05-05 23:33:33 +00005024 if (*mode == 't')
5025 tm = _O_TEXT;
5026 else if (*mode != 'b') {
5027 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
5028 return NULL;
5029 } else
5030 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00005031
Victor Stinnerd6f85422010-05-05 23:33:33 +00005032 if (bufsize != -1) {
5033 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
5034 return NULL;
5035 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005036
Victor Stinnerd6f85422010-05-05 23:33:33 +00005037 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00005038
Victor Stinnerd6f85422010-05-05 23:33:33 +00005039 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005040}
5041
5042/*
5043 * Variation on win32pipe.popen
5044 *
Tim Peters5aa91602002-01-30 05:46:57 +00005045 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005046 * and stdout+stderr combined as a single pipe.
5047 */
5048
5049static PyObject *
5050win32_popen4(PyObject *self, PyObject *args)
5051{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005052 PyObject *f;
5053 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005054
Victor Stinnerd6f85422010-05-05 23:33:33 +00005055 char *cmdstring;
5056 char *mode = "t";
5057 int bufsize = -1;
5058 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
5059 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005060
Victor Stinnerd6f85422010-05-05 23:33:33 +00005061 if (*mode == 't')
5062 tm = _O_TEXT;
5063 else if (*mode != 'b') {
5064 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
5065 return NULL;
5066 } else
5067 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005068
Victor Stinnerd6f85422010-05-05 23:33:33 +00005069 if (bufsize != -1) {
5070 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
5071 return NULL;
5072 }
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005073
Victor Stinnerd6f85422010-05-05 23:33:33 +00005074 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00005075
Victor Stinnerd6f85422010-05-05 23:33:33 +00005076 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005077}
5078
Mark Hammond08501372001-01-31 07:30:29 +00005079static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00005080_PyPopenCreateProcess(char *cmdstring,
Victor Stinnerd6f85422010-05-05 23:33:33 +00005081 HANDLE hStdin,
5082 HANDLE hStdout,
5083 HANDLE hStderr,
5084 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005085{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005086 PROCESS_INFORMATION piProcInfo;
5087 STARTUPINFO siStartInfo;
5088 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
5089 char *s1,*s2, *s3 = " /c ";
5090 const char *szConsoleSpawn = "w9xpopen.exe";
5091 int i;
5092 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005093
Victor Stinnerd6f85422010-05-05 23:33:33 +00005094 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
5095 char *comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005096
Victor Stinnerd6f85422010-05-05 23:33:33 +00005097 s1 = (char *)alloca(i);
5098 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
5099 /* x < i, so x fits into an integer */
5100 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00005101
Victor Stinnerd6f85422010-05-05 23:33:33 +00005102 /* Explicitly check if we are using COMMAND.COM. If we are
5103 * then use the w9xpopen hack.
5104 */
5105 comshell = s1 + x;
5106 while (comshell >= s1 && *comshell != '\\')
5107 --comshell;
5108 ++comshell;
Tim Peters402d5982001-08-27 06:37:48 +00005109
Victor Stinnerd6f85422010-05-05 23:33:33 +00005110 if (GetVersion() < 0x80000000 &&
5111 _stricmp(comshell, "command.com") != 0) {
5112 /* NT/2000 and not using command.com. */
5113 x = i + strlen(s3) + strlen(cmdstring) + 1;
5114 s2 = (char *)alloca(x);
5115 ZeroMemory(s2, x);
5116 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
5117 }
5118 else {
5119 /*
5120 * Oh gag, we're on Win9x or using COMMAND.COM. Use
5121 * the workaround listed in KB: Q150956
5122 */
5123 char modulepath[_MAX_PATH];
5124 struct stat statinfo;
5125 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
5126 for (x = i = 0; modulepath[i]; i++)
5127 if (modulepath[i] == SEP)
5128 x = i+1;
5129 modulepath[x] = '\0';
5130 /* Create the full-name to w9xpopen, so we can test it exists */
5131 strncat(modulepath,
5132 szConsoleSpawn,
5133 (sizeof(modulepath)/sizeof(modulepath[0]))
5134 -strlen(modulepath));
5135 if (stat(modulepath, &statinfo) != 0) {
5136 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
5137 /* Eeek - file-not-found - possibly an embedding
5138 situation - see if we can locate it in sys.prefix
5139 */
5140 strncpy(modulepath,
5141 Py_GetExecPrefix(),
5142 mplen);
5143 modulepath[mplen-1] = '\0';
5144 if (modulepath[strlen(modulepath)-1] != '\\')
5145 strcat(modulepath, "\\");
5146 strncat(modulepath,
5147 szConsoleSpawn,
5148 mplen-strlen(modulepath));
5149 /* No where else to look - raise an easily identifiable
5150 error, rather than leaving Windows to report
5151 "file not found" - as the user is probably blissfully
5152 unaware this shim EXE is used, and it will confuse them.
5153 (well, it confused me for a while ;-)
5154 */
5155 if (stat(modulepath, &statinfo) != 0) {
5156 PyErr_Format(PyExc_RuntimeError,
5157 "Can not locate '%s' which is needed "
5158 "for popen to work with your shell "
5159 "or platform.",
5160 szConsoleSpawn);
5161 return FALSE;
5162 }
5163 }
5164 x = i + strlen(s3) + strlen(cmdstring) + 1 +
5165 strlen(modulepath) +
5166 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00005167
Victor Stinnerd6f85422010-05-05 23:33:33 +00005168 s2 = (char *)alloca(x);
5169 ZeroMemory(s2, x);
5170 /* To maintain correct argument passing semantics,
5171 we pass the command-line as it stands, and allow
5172 quoting to be applied. w9xpopen.exe will then
5173 use its argv vector, and re-quote the necessary
5174 args for the ultimate child process.
5175 */
5176 PyOS_snprintf(
5177 s2, x,
5178 "\"%s\" %s%s%s",
5179 modulepath,
5180 s1,
5181 s3,
5182 cmdstring);
5183 /* Not passing CREATE_NEW_CONSOLE has been known to
5184 cause random failures on win9x. Specifically a
5185 dialog:
5186 "Your program accessed mem currently in use at xxx"
5187 and a hopeful warning about the stability of your
5188 system.
5189 Cost is Ctrl+C won't kill children, but anyone
5190 who cares can have a go!
5191 */
5192 dwProcessFlags |= CREATE_NEW_CONSOLE;
5193 }
5194 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005195
Victor Stinnerd6f85422010-05-05 23:33:33 +00005196 /* Could be an else here to try cmd.exe / command.com in the path
5197 Now we'll just error out.. */
5198 else {
5199 PyErr_SetString(PyExc_RuntimeError,
5200 "Cannot locate a COMSPEC environment variable to "
5201 "use as the shell");
5202 return FALSE;
5203 }
Tim Peters5aa91602002-01-30 05:46:57 +00005204
Victor Stinnerd6f85422010-05-05 23:33:33 +00005205 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5206 siStartInfo.cb = sizeof(STARTUPINFO);
5207 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5208 siStartInfo.hStdInput = hStdin;
5209 siStartInfo.hStdOutput = hStdout;
5210 siStartInfo.hStdError = hStderr;
5211 siStartInfo.wShowWindow = SW_HIDE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005212
Victor Stinnerd6f85422010-05-05 23:33:33 +00005213 if (CreateProcess(NULL,
5214 s2,
5215 NULL,
5216 NULL,
5217 TRUE,
5218 dwProcessFlags,
5219 NULL,
5220 NULL,
5221 &siStartInfo,
5222 &piProcInfo) ) {
5223 /* Close the handles now so anyone waiting is woken. */
5224 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005225
Victor Stinnerd6f85422010-05-05 23:33:33 +00005226 /* Return process handle */
5227 *hProcess = piProcInfo.hProcess;
5228 return TRUE;
5229 }
5230 win32_error("CreateProcess", s2);
5231 return FALSE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005232}
5233
5234/* The following code is based off of KB: Q190351 */
5235
5236static PyObject *
5237_PyPopen(char *cmdstring, int mode, int n)
5238{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005239 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5240 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
5241 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005242
Victor Stinnerd6f85422010-05-05 23:33:33 +00005243 SECURITY_ATTRIBUTES saAttr;
5244 BOOL fSuccess;
5245 int fd1, fd2, fd3;
5246 FILE *f1, *f2, *f3;
5247 long file_count;
5248 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005249
Victor Stinnerd6f85422010-05-05 23:33:33 +00005250 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5251 saAttr.bInheritHandle = TRUE;
5252 saAttr.lpSecurityDescriptor = NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005253
Victor Stinnerd6f85422010-05-05 23:33:33 +00005254 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5255 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005256
Victor Stinnerd6f85422010-05-05 23:33:33 +00005257 /* Create new output read handle and the input write handle. Set
5258 * the inheritance properties to FALSE. Otherwise, the child inherits
5259 * these handles; resulting in non-closeable handles to the pipes
5260 * being created. */
5261 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
5262 GetCurrentProcess(), &hChildStdinWrDup, 0,
5263 FALSE,
5264 DUPLICATE_SAME_ACCESS);
5265 if (!fSuccess)
5266 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005267
Victor Stinnerd6f85422010-05-05 23:33:33 +00005268 /* Close the inheritable version of ChildStdin
5269 that we're using. */
5270 CloseHandle(hChildStdinWr);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005271
Victor Stinnerd6f85422010-05-05 23:33:33 +00005272 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5273 return win32_error("CreatePipe", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005274
Victor Stinnerd6f85422010-05-05 23:33:33 +00005275 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
5276 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5277 FALSE, DUPLICATE_SAME_ACCESS);
5278 if (!fSuccess)
5279 return win32_error("DuplicateHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005280
Victor Stinnerd6f85422010-05-05 23:33:33 +00005281 /* Close the inheritable version of ChildStdout
5282 that we're using. */
5283 CloseHandle(hChildStdoutRd);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005284
Victor Stinnerd6f85422010-05-05 23:33:33 +00005285 if (n != POPEN_4) {
5286 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5287 return win32_error("CreatePipe", NULL);
5288 fSuccess = DuplicateHandle(GetCurrentProcess(),
5289 hChildStderrRd,
5290 GetCurrentProcess(),
5291 &hChildStderrRdDup, 0,
5292 FALSE, DUPLICATE_SAME_ACCESS);
5293 if (!fSuccess)
5294 return win32_error("DuplicateHandle", NULL);
5295 /* Close the inheritable version of ChildStdErr that we're using. */
5296 CloseHandle(hChildStderrRd);
5297 }
Tim Peters5aa91602002-01-30 05:46:57 +00005298
Victor Stinnerd6f85422010-05-05 23:33:33 +00005299 switch (n) {
5300 case POPEN_1:
5301 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5302 case _O_WRONLY | _O_TEXT:
5303 /* Case for writing to child Stdin in text mode. */
5304 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5305 f1 = _fdopen(fd1, "w");
5306 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5307 PyFile_SetBufSize(f, 0);
5308 /* We don't care about these pipes anymore, so close them. */
5309 CloseHandle(hChildStdoutRdDup);
5310 CloseHandle(hChildStderrRdDup);
5311 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005312
Victor Stinnerd6f85422010-05-05 23:33:33 +00005313 case _O_RDONLY | _O_TEXT:
5314 /* Case for reading from child Stdout in text mode. */
5315 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5316 f1 = _fdopen(fd1, "r");
5317 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5318 PyFile_SetBufSize(f, 0);
5319 /* We don't care about these pipes anymore, so close them. */
5320 CloseHandle(hChildStdinWrDup);
5321 CloseHandle(hChildStderrRdDup);
5322 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005323
Victor Stinnerd6f85422010-05-05 23:33:33 +00005324 case _O_RDONLY | _O_BINARY:
5325 /* Case for readinig from child Stdout in binary mode. */
5326 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5327 f1 = _fdopen(fd1, "rb");
5328 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5329 PyFile_SetBufSize(f, 0);
5330 /* We don't care about these pipes anymore, so close them. */
5331 CloseHandle(hChildStdinWrDup);
5332 CloseHandle(hChildStderrRdDup);
5333 break;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005334
Victor Stinnerd6f85422010-05-05 23:33:33 +00005335 case _O_WRONLY | _O_BINARY:
5336 /* Case for writing to child Stdin in binary mode. */
5337 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5338 f1 = _fdopen(fd1, "wb");
5339 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5340 PyFile_SetBufSize(f, 0);
5341 /* We don't care about these pipes anymore, so close them. */
5342 CloseHandle(hChildStdoutRdDup);
5343 CloseHandle(hChildStderrRdDup);
5344 break;
5345 }
5346 file_count = 1;
5347 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005348
Victor Stinnerd6f85422010-05-05 23:33:33 +00005349 case POPEN_2:
5350 case POPEN_4:
5351 {
5352 char *m1, *m2;
5353 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005354
Victor Stinnerd6f85422010-05-05 23:33:33 +00005355 if (mode & _O_TEXT) {
5356 m1 = "r";
5357 m2 = "w";
5358 } else {
5359 m1 = "rb";
5360 m2 = "wb";
5361 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005362
Victor Stinnerd6f85422010-05-05 23:33:33 +00005363 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5364 f1 = _fdopen(fd1, m2);
5365 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5366 f2 = _fdopen(fd2, m1);
5367 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5368 PyFile_SetBufSize(p1, 0);
5369 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5370 PyFile_SetBufSize(p2, 0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005371
Victor Stinnerd6f85422010-05-05 23:33:33 +00005372 if (n != 4)
5373 CloseHandle(hChildStderrRdDup);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005374
Victor Stinnerd6f85422010-05-05 23:33:33 +00005375 f = PyTuple_Pack(2,p1,p2);
5376 Py_XDECREF(p1);
5377 Py_XDECREF(p2);
5378 file_count = 2;
5379 break;
5380 }
Tim Peters5aa91602002-01-30 05:46:57 +00005381
Victor Stinnerd6f85422010-05-05 23:33:33 +00005382 case POPEN_3:
5383 {
5384 char *m1, *m2;
5385 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005386
Victor Stinnerd6f85422010-05-05 23:33:33 +00005387 if (mode & _O_TEXT) {
5388 m1 = "r";
5389 m2 = "w";
5390 } else {
5391 m1 = "rb";
5392 m2 = "wb";
5393 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005394
Victor Stinnerd6f85422010-05-05 23:33:33 +00005395 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5396 f1 = _fdopen(fd1, m2);
5397 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5398 f2 = _fdopen(fd2, m1);
5399 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5400 f3 = _fdopen(fd3, m1);
5401 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5402 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5403 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5404 PyFile_SetBufSize(p1, 0);
5405 PyFile_SetBufSize(p2, 0);
5406 PyFile_SetBufSize(p3, 0);
5407 f = PyTuple_Pack(3,p1,p2,p3);
5408 Py_XDECREF(p1);
5409 Py_XDECREF(p2);
5410 Py_XDECREF(p3);
5411 file_count = 3;
5412 break;
5413 }
5414 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005415
Victor Stinnerd6f85422010-05-05 23:33:33 +00005416 if (n == POPEN_4) {
5417 if (!_PyPopenCreateProcess(cmdstring,
5418 hChildStdinRd,
5419 hChildStdoutWr,
5420 hChildStdoutWr,
5421 &hProcess))
5422 return NULL;
5423 }
5424 else {
5425 if (!_PyPopenCreateProcess(cmdstring,
5426 hChildStdinRd,
5427 hChildStdoutWr,
5428 hChildStderrWr,
5429 &hProcess))
5430 return NULL;
5431 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005432
Victor Stinnerd6f85422010-05-05 23:33:33 +00005433 /*
5434 * Insert the files we've created into the process dictionary
5435 * all referencing the list with the process handle and the
5436 * initial number of files (see description below in _PyPclose).
5437 * Since if _PyPclose later tried to wait on a process when all
5438 * handles weren't closed, it could create a deadlock with the
5439 * child, we spend some energy here to try to ensure that we
5440 * either insert all file handles into the dictionary or none
5441 * at all. It's a little clumsy with the various popen modes
5442 * and variable number of files involved.
5443 */
5444 if (!_PyPopenProcs) {
5445 _PyPopenProcs = PyDict_New();
5446 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005447
Victor Stinnerd6f85422010-05-05 23:33:33 +00005448 if (_PyPopenProcs) {
5449 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5450 int ins_rc[3];
Mark Hammondb37a3732000-08-14 04:47:33 +00005451
Victor Stinnerd6f85422010-05-05 23:33:33 +00005452 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5453 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
Mark Hammondb37a3732000-08-14 04:47:33 +00005454
Victor Stinnerd6f85422010-05-05 23:33:33 +00005455 procObj = PyList_New(2);
5456 hProcessObj = PyLong_FromVoidPtr(hProcess);
5457 intObj = PyInt_FromLong(file_count);
Mark Hammondb37a3732000-08-14 04:47:33 +00005458
Victor Stinnerd6f85422010-05-05 23:33:33 +00005459 if (procObj && hProcessObj && intObj) {
5460 PyList_SetItem(procObj,0,hProcessObj);
5461 PyList_SetItem(procObj,1,intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005462
Victor Stinnerd6f85422010-05-05 23:33:33 +00005463 fileObj[0] = PyLong_FromVoidPtr(f1);
5464 if (fileObj[0]) {
5465 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5466 fileObj[0],
5467 procObj);
5468 }
5469 if (file_count >= 2) {
5470 fileObj[1] = PyLong_FromVoidPtr(f2);
5471 if (fileObj[1]) {
5472 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5473 fileObj[1],
5474 procObj);
5475 }
5476 }
5477 if (file_count >= 3) {
5478 fileObj[2] = PyLong_FromVoidPtr(f3);
5479 if (fileObj[2]) {
5480 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5481 fileObj[2],
5482 procObj);
5483 }
5484 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005485
Victor Stinnerd6f85422010-05-05 23:33:33 +00005486 if (ins_rc[0] < 0 || !fileObj[0] ||
5487 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5488 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5489 /* Something failed - remove any dictionary
5490 * entries that did make it.
5491 */
5492 if (!ins_rc[0] && fileObj[0]) {
5493 PyDict_DelItem(_PyPopenProcs,
5494 fileObj[0]);
5495 }
5496 if (!ins_rc[1] && fileObj[1]) {
5497 PyDict_DelItem(_PyPopenProcs,
5498 fileObj[1]);
5499 }
5500 if (!ins_rc[2] && fileObj[2]) {
5501 PyDict_DelItem(_PyPopenProcs,
5502 fileObj[2]);
5503 }
5504 }
5505 }
Tim Peters5aa91602002-01-30 05:46:57 +00005506
Victor Stinnerd6f85422010-05-05 23:33:33 +00005507 /*
5508 * Clean up our localized references for the dictionary keys
5509 * and value since PyDict_SetItem will Py_INCREF any copies
5510 * that got placed in the dictionary.
5511 */
5512 Py_XDECREF(procObj);
5513 Py_XDECREF(fileObj[0]);
5514 Py_XDECREF(fileObj[1]);
5515 Py_XDECREF(fileObj[2]);
5516 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005517
Victor Stinnerd6f85422010-05-05 23:33:33 +00005518 /* Child is launched. Close the parents copy of those pipe
5519 * handles that only the child should have open. You need to
5520 * make sure that no handles to the write end of the output pipe
5521 * are maintained in this process or else the pipe will not close
5522 * when the child process exits and the ReadFile will hang. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005523
Victor Stinnerd6f85422010-05-05 23:33:33 +00005524 if (!CloseHandle(hChildStdinRd))
5525 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005526
Victor Stinnerd6f85422010-05-05 23:33:33 +00005527 if (!CloseHandle(hChildStdoutWr))
5528 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005529
Victor Stinnerd6f85422010-05-05 23:33:33 +00005530 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5531 return win32_error("CloseHandle", NULL);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005532
Victor Stinnerd6f85422010-05-05 23:33:33 +00005533 return f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005534}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005535
5536/*
5537 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5538 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005539 *
5540 * This function uses the _PyPopenProcs dictionary in order to map the
5541 * input file pointer to information about the process that was
5542 * originally created by the popen* call that created the file pointer.
5543 * The dictionary uses the file pointer as a key (with one entry
5544 * inserted for each file returned by the original popen* call) and a
5545 * single list object as the value for all files from a single call.
5546 * The list object contains the Win32 process handle at [0], and a file
5547 * count at [1], which is initialized to the total number of file
5548 * handles using that list.
5549 *
5550 * This function closes whichever handle it is passed, and decrements
5551 * the file count in the dictionary for the process handle pointed to
5552 * by this file. On the last close (when the file count reaches zero),
5553 * this function will wait for the child process and then return its
5554 * exit code as the result of the close() operation. This permits the
5555 * files to be closed in any order - it is always the close() of the
5556 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005557 *
5558 * NOTE: This function is currently called with the GIL released.
5559 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005560 */
Tim Peters736aa322000-09-01 06:51:24 +00005561
Fredrik Lundh56055a42000-07-23 19:47:12 +00005562static int _PyPclose(FILE *file)
5563{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005564 int result;
5565 DWORD exit_code;
5566 HANDLE hProcess;
5567 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5568 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005569#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005570 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005571#endif
5572
Victor Stinnerd6f85422010-05-05 23:33:33 +00005573 /* Close the file handle first, to ensure it can't block the
5574 * child from exiting if it's the last handle.
5575 */
5576 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005577#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005578 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005579#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005580 if (_PyPopenProcs) {
5581 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5582 (procObj = PyDict_GetItem(_PyPopenProcs,
5583 fileObj)) != NULL &&
5584 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5585 (intObj = PyList_GetItem(procObj,1)) != NULL) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005586
Victor Stinnerd6f85422010-05-05 23:33:33 +00005587 hProcess = PyLong_AsVoidPtr(hProcessObj);
5588 file_count = PyInt_AsLong(intObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005589
Victor Stinnerd6f85422010-05-05 23:33:33 +00005590 if (file_count > 1) {
5591 /* Still other files referencing process */
5592 file_count--;
5593 PyList_SetItem(procObj,1,
5594 PyInt_FromLong(file_count));
5595 } else {
5596 /* Last file for this process */
5597 if (result != EOF &&
5598 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5599 GetExitCodeProcess(hProcess, &exit_code)) {
5600 /* Possible truncation here in 16-bit environments, but
5601 * real exit codes are just the lower byte in any event.
5602 */
5603 result = exit_code;
5604 } else {
5605 /* Indicate failure - this will cause the file object
5606 * to raise an I/O error and translate the last Win32
5607 * error code from errno. We do have a problem with
5608 * last errors that overlap the normal errno table,
5609 * but that's a consistent problem with the file object.
5610 */
5611 if (result != EOF) {
5612 /* If the error wasn't from the fclose(), then
5613 * set errno for the file object error handling.
5614 */
5615 errno = GetLastError();
5616 }
5617 result = -1;
5618 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005619
Victor Stinnerd6f85422010-05-05 23:33:33 +00005620 /* Free up the native handle at this point */
5621 CloseHandle(hProcess);
5622 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005623
Victor Stinnerd6f85422010-05-05 23:33:33 +00005624 /* Remove this file pointer from dictionary */
5625 PyDict_DelItem(_PyPopenProcs, fileObj);
Mark Hammondb37a3732000-08-14 04:47:33 +00005626
Victor Stinnerd6f85422010-05-05 23:33:33 +00005627 if (PyDict_Size(_PyPopenProcs) == 0) {
5628 Py_DECREF(_PyPopenProcs);
5629 _PyPopenProcs = NULL;
5630 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005631
Victor Stinnerd6f85422010-05-05 23:33:33 +00005632 } /* if object retrieval ok */
Mark Hammondb37a3732000-08-14 04:47:33 +00005633
Victor Stinnerd6f85422010-05-05 23:33:33 +00005634 Py_XDECREF(fileObj);
5635 } /* if _PyPopenProcs */
Fredrik Lundh56055a42000-07-23 19:47:12 +00005636
Tim Peters736aa322000-09-01 06:51:24 +00005637#ifdef WITH_THREAD
Victor Stinnerd6f85422010-05-05 23:33:33 +00005638 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005639#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00005640 return result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005641}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005642
5643#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005644static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005645posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005646{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005647 char *name;
5648 char *mode = "r";
5649 int bufsize = -1;
5650 FILE *fp;
5651 PyObject *f;
5652 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5653 return NULL;
5654 /* Strip mode of binary or text modifiers */
5655 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5656 mode = "r";
5657 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5658 mode = "w";
5659 Py_BEGIN_ALLOW_THREADS
5660 fp = popen(name, mode);
5661 Py_END_ALLOW_THREADS
5662 if (fp == NULL)
5663 return posix_error();
5664 f = PyFile_FromFile(fp, name, mode, pclose);
5665 if (f != NULL)
5666 PyFile_SetBufSize(f, bufsize);
5667 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005668}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005669
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005670#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005671#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005672
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005673
Guido van Rossumb6775db1994-08-01 11:34:53 +00005674#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005675PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005676"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005677Set the current process's user id.");
5678
Barry Warsaw53699e91996-12-10 23:23:01 +00005679static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005680posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005681{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005682 long uid_arg;
5683 uid_t uid;
5684 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5685 return NULL;
5686 uid = uid_arg;
5687 if (uid != uid_arg) {
5688 PyErr_SetString(PyExc_OverflowError, "user id too big");
5689 return NULL;
5690 }
5691 if (setuid(uid) < 0)
5692 return posix_error();
5693 Py_INCREF(Py_None);
5694 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005695}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005696#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005697
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005698
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005699#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005700PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005701"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005702Set the current process's effective user id.");
5703
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005704static PyObject *
5705posix_seteuid (PyObject *self, PyObject *args)
5706{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005707 long euid_arg;
5708 uid_t euid;
5709 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5710 return NULL;
5711 euid = euid_arg;
5712 if (euid != euid_arg) {
5713 PyErr_SetString(PyExc_OverflowError, "user id too big");
5714 return NULL;
5715 }
5716 if (seteuid(euid) < 0) {
5717 return posix_error();
5718 } else {
5719 Py_INCREF(Py_None);
5720 return Py_None;
5721 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005722}
5723#endif /* HAVE_SETEUID */
5724
5725#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005726PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005727"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005728Set the current process's effective group id.");
5729
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005730static PyObject *
5731posix_setegid (PyObject *self, PyObject *args)
5732{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005733 long egid_arg;
5734 gid_t egid;
5735 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5736 return NULL;
5737 egid = egid_arg;
5738 if (egid != egid_arg) {
5739 PyErr_SetString(PyExc_OverflowError, "group id too big");
5740 return NULL;
5741 }
5742 if (setegid(egid) < 0) {
5743 return posix_error();
5744 } else {
5745 Py_INCREF(Py_None);
5746 return Py_None;
5747 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005748}
5749#endif /* HAVE_SETEGID */
5750
5751#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005752PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005753"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005754Set the current process's real and effective user ids.");
5755
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005756static PyObject *
5757posix_setreuid (PyObject *self, PyObject *args)
5758{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005759 long ruid_arg, euid_arg;
5760 uid_t ruid, euid;
5761 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5762 return NULL;
5763 if (ruid_arg == -1)
5764 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5765 else
5766 ruid = ruid_arg; /* otherwise, assign from our long */
5767 if (euid_arg == -1)
5768 euid = (uid_t)-1;
5769 else
5770 euid = euid_arg;
5771 if ((euid_arg != -1 && euid != euid_arg) ||
5772 (ruid_arg != -1 && ruid != ruid_arg)) {
5773 PyErr_SetString(PyExc_OverflowError, "user id too big");
5774 return NULL;
5775 }
5776 if (setreuid(ruid, euid) < 0) {
5777 return posix_error();
5778 } else {
5779 Py_INCREF(Py_None);
5780 return Py_None;
5781 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005782}
5783#endif /* HAVE_SETREUID */
5784
5785#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005786PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005787"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005788Set the current process's real and effective group ids.");
5789
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005790static PyObject *
5791posix_setregid (PyObject *self, PyObject *args)
5792{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005793 long rgid_arg, egid_arg;
5794 gid_t rgid, egid;
5795 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5796 return NULL;
5797 if (rgid_arg == -1)
5798 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5799 else
5800 rgid = rgid_arg; /* otherwise, assign from our long */
5801 if (egid_arg == -1)
5802 egid = (gid_t)-1;
5803 else
5804 egid = egid_arg;
5805 if ((egid_arg != -1 && egid != egid_arg) ||
5806 (rgid_arg != -1 && rgid != rgid_arg)) {
5807 PyErr_SetString(PyExc_OverflowError, "group id too big");
5808 return NULL;
5809 }
5810 if (setregid(rgid, egid) < 0) {
5811 return posix_error();
5812 } else {
5813 Py_INCREF(Py_None);
5814 return Py_None;
5815 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005816}
5817#endif /* HAVE_SETREGID */
5818
Guido van Rossumb6775db1994-08-01 11:34:53 +00005819#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005820PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005821"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005822Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005823
Barry Warsaw53699e91996-12-10 23:23:01 +00005824static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005825posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005826{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005827 long gid_arg;
5828 gid_t gid;
5829 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5830 return NULL;
5831 gid = gid_arg;
5832 if (gid != gid_arg) {
5833 PyErr_SetString(PyExc_OverflowError, "group id too big");
5834 return NULL;
5835 }
5836 if (setgid(gid) < 0)
5837 return posix_error();
5838 Py_INCREF(Py_None);
5839 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005840}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005841#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005842
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005843#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005844PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005845"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005846Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005847
5848static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005849posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005850{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005851 int i, len;
5852 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005853
Victor Stinnerd6f85422010-05-05 23:33:33 +00005854 if (!PySequence_Check(groups)) {
5855 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5856 return NULL;
5857 }
5858 len = PySequence_Size(groups);
5859 if (len > MAX_GROUPS) {
5860 PyErr_SetString(PyExc_ValueError, "too many groups");
5861 return NULL;
5862 }
5863 for(i = 0; i < len; i++) {
5864 PyObject *elem;
5865 elem = PySequence_GetItem(groups, i);
5866 if (!elem)
5867 return NULL;
5868 if (!PyInt_Check(elem)) {
5869 if (!PyLong_Check(elem)) {
5870 PyErr_SetString(PyExc_TypeError,
5871 "groups must be integers");
5872 Py_DECREF(elem);
5873 return NULL;
5874 } else {
5875 unsigned long x = PyLong_AsUnsignedLong(elem);
5876 if (PyErr_Occurred()) {
5877 PyErr_SetString(PyExc_TypeError,
5878 "group id too big");
5879 Py_DECREF(elem);
5880 return NULL;
5881 }
5882 grouplist[i] = x;
5883 /* read back to see if it fits in gid_t */
5884 if (grouplist[i] != x) {
5885 PyErr_SetString(PyExc_TypeError,
5886 "group id too big");
5887 Py_DECREF(elem);
5888 return NULL;
5889 }
5890 }
5891 } else {
5892 long x = PyInt_AsLong(elem);
5893 grouplist[i] = x;
5894 if (grouplist[i] != x) {
5895 PyErr_SetString(PyExc_TypeError,
5896 "group id too big");
5897 Py_DECREF(elem);
5898 return NULL;
5899 }
5900 }
5901 Py_DECREF(elem);
5902 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005903
Victor Stinnerd6f85422010-05-05 23:33:33 +00005904 if (setgroups(len, grouplist) < 0)
5905 return posix_error();
5906 Py_INCREF(Py_None);
5907 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005908}
5909#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005910
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005911#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005912static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00005913wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00005914{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005915 PyObject *result;
5916 static PyObject *struct_rusage;
Neal Norwitz05a45592006-03-20 06:30:08 +00005917
Victor Stinnerd6f85422010-05-05 23:33:33 +00005918 if (pid == -1)
5919 return posix_error();
Neal Norwitz05a45592006-03-20 06:30:08 +00005920
Victor Stinnerd6f85422010-05-05 23:33:33 +00005921 if (struct_rusage == NULL) {
5922 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5923 if (m == NULL)
5924 return NULL;
5925 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5926 Py_DECREF(m);
5927 if (struct_rusage == NULL)
5928 return NULL;
5929 }
Neal Norwitz05a45592006-03-20 06:30:08 +00005930
Victor Stinnerd6f85422010-05-05 23:33:33 +00005931 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5932 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5933 if (!result)
5934 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005935
5936#ifndef doubletime
5937#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5938#endif
5939
Victor Stinnerd6f85422010-05-05 23:33:33 +00005940 PyStructSequence_SET_ITEM(result, 0,
5941 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5942 PyStructSequence_SET_ITEM(result, 1,
5943 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Neal Norwitz05a45592006-03-20 06:30:08 +00005944#define SET_INT(result, index, value)\
Victor Stinnerd6f85422010-05-05 23:33:33 +00005945 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5946 SET_INT(result, 2, ru->ru_maxrss);
5947 SET_INT(result, 3, ru->ru_ixrss);
5948 SET_INT(result, 4, ru->ru_idrss);
5949 SET_INT(result, 5, ru->ru_isrss);
5950 SET_INT(result, 6, ru->ru_minflt);
5951 SET_INT(result, 7, ru->ru_majflt);
5952 SET_INT(result, 8, ru->ru_nswap);
5953 SET_INT(result, 9, ru->ru_inblock);
5954 SET_INT(result, 10, ru->ru_oublock);
5955 SET_INT(result, 11, ru->ru_msgsnd);
5956 SET_INT(result, 12, ru->ru_msgrcv);
5957 SET_INT(result, 13, ru->ru_nsignals);
5958 SET_INT(result, 14, ru->ru_nvcsw);
5959 SET_INT(result, 15, ru->ru_nivcsw);
Neal Norwitz05a45592006-03-20 06:30:08 +00005960#undef SET_INT
5961
Victor Stinnerd6f85422010-05-05 23:33:33 +00005962 if (PyErr_Occurred()) {
5963 Py_DECREF(result);
5964 return NULL;
5965 }
Neal Norwitz05a45592006-03-20 06:30:08 +00005966
Victor Stinnerd6f85422010-05-05 23:33:33 +00005967 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005968}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005969#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005970
5971#ifdef HAVE_WAIT3
5972PyDoc_STRVAR(posix_wait3__doc__,
5973"wait3(options) -> (pid, status, rusage)\n\n\
5974Wait for completion of a child process.");
5975
5976static PyObject *
5977posix_wait3(PyObject *self, PyObject *args)
5978{
Victor Stinnerd6f85422010-05-05 23:33:33 +00005979 pid_t pid;
5980 int options;
5981 struct rusage ru;
5982 WAIT_TYPE status;
5983 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005984
Victor Stinnerd6f85422010-05-05 23:33:33 +00005985 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5986 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00005987
Victor Stinnerd6f85422010-05-05 23:33:33 +00005988 Py_BEGIN_ALLOW_THREADS
5989 pid = wait3(&status, options, &ru);
5990 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00005991
Victor Stinnerd6f85422010-05-05 23:33:33 +00005992 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005993}
5994#endif /* HAVE_WAIT3 */
5995
5996#ifdef HAVE_WAIT4
5997PyDoc_STRVAR(posix_wait4__doc__,
5998"wait4(pid, options) -> (pid, status, rusage)\n\n\
5999Wait for completion of a given child process.");
6000
6001static PyObject *
6002posix_wait4(PyObject *self, PyObject *args)
6003{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006004 pid_t pid;
6005 int options;
6006 struct rusage ru;
6007 WAIT_TYPE status;
6008 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00006009
Victor Stinnerd6f85422010-05-05 23:33:33 +00006010 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
6011 return NULL;
Neal Norwitz05a45592006-03-20 06:30:08 +00006012
Victor Stinnerd6f85422010-05-05 23:33:33 +00006013 Py_BEGIN_ALLOW_THREADS
6014 pid = wait4(pid, &status, options, &ru);
6015 Py_END_ALLOW_THREADS
Neal Norwitz05a45592006-03-20 06:30:08 +00006016
Victor Stinnerd6f85422010-05-05 23:33:33 +00006017 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00006018}
6019#endif /* HAVE_WAIT4 */
6020
Guido van Rossumb6775db1994-08-01 11:34:53 +00006021#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006022PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006023"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006024Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006025
Barry Warsaw53699e91996-12-10 23:23:01 +00006026static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006027posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006028{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006029 pid_t pid;
6030 int options;
6031 WAIT_TYPE status;
6032 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006033
Victor Stinnerd6f85422010-05-05 23:33:33 +00006034 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6035 return NULL;
6036 Py_BEGIN_ALLOW_THREADS
6037 pid = waitpid(pid, &status, options);
6038 Py_END_ALLOW_THREADS
6039 if (pid == -1)
6040 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006041
Victor Stinnerd6f85422010-05-05 23:33:33 +00006042 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006043}
6044
Tim Petersab034fa2002-02-01 11:27:43 +00006045#elif defined(HAVE_CWAIT)
6046
6047/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006048PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006049"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006050"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006051
6052static PyObject *
6053posix_waitpid(PyObject *self, PyObject *args)
6054{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006055 Py_intptr_t pid;
6056 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006057
Victor Stinnerd6f85422010-05-05 23:33:33 +00006058 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
6059 return NULL;
6060 Py_BEGIN_ALLOW_THREADS
6061 pid = _cwait(&status, pid, options);
6062 Py_END_ALLOW_THREADS
6063 if (pid == -1)
6064 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006065
Victor Stinnerd6f85422010-05-05 23:33:33 +00006066 /* shift the status left a byte so this is more like the POSIX waitpid */
6067 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006068}
6069#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006070
Guido van Rossumad0ee831995-03-01 10:34:45 +00006071#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006072PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006073"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006074Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006075
Barry Warsaw53699e91996-12-10 23:23:01 +00006076static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006077posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006078{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006079 pid_t pid;
6080 WAIT_TYPE status;
6081 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006082
Victor Stinnerd6f85422010-05-05 23:33:33 +00006083 Py_BEGIN_ALLOW_THREADS
6084 pid = wait(&status);
6085 Py_END_ALLOW_THREADS
6086 if (pid == -1)
6087 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00006088
Victor Stinnerd6f85422010-05-05 23:33:33 +00006089 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006090}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006091#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006092
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006093
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006094PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006095"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006096Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006097
Barry Warsaw53699e91996-12-10 23:23:01 +00006098static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006099posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006100{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006101#ifdef HAVE_LSTAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00006102 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006103#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006104#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006105 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006106#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006107 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006108#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006109#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006110}
6111
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006112
Guido van Rossumb6775db1994-08-01 11:34:53 +00006113#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006114PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006115"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006116Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006117
Barry Warsaw53699e91996-12-10 23:23:01 +00006118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006119posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006120{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006121 PyObject* v;
6122 char buf[MAXPATHLEN];
6123 char *path;
6124 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006125#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006126 int arg_is_unicode = 0;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006127#endif
6128
Victor Stinnerd6f85422010-05-05 23:33:33 +00006129 if (!PyArg_ParseTuple(args, "et:readlink",
6130 Py_FileSystemDefaultEncoding, &path))
6131 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006132#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006133 v = PySequence_GetItem(args, 0);
6134 if (v == NULL) {
6135 PyMem_Free(path);
6136 return NULL;
6137 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006138
Victor Stinnerd6f85422010-05-05 23:33:33 +00006139 if (PyUnicode_Check(v)) {
6140 arg_is_unicode = 1;
6141 }
6142 Py_DECREF(v);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006143#endif
6144
Victor Stinnerd6f85422010-05-05 23:33:33 +00006145 Py_BEGIN_ALLOW_THREADS
6146 n = readlink(path, buf, (int) sizeof buf);
6147 Py_END_ALLOW_THREADS
6148 if (n < 0)
6149 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006150
Victor Stinnerd6f85422010-05-05 23:33:33 +00006151 PyMem_Free(path);
6152 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00006153#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00006154 if (arg_is_unicode) {
6155 PyObject *w;
Ronald Oussoren10168f22006-10-22 10:45:18 +00006156
Victor Stinnerd6f85422010-05-05 23:33:33 +00006157 w = PyUnicode_FromEncodedObject(v,
6158 Py_FileSystemDefaultEncoding,
6159 "strict");
6160 if (w != NULL) {
6161 Py_DECREF(v);
6162 v = w;
6163 }
6164 else {
6165 /* fall back to the original byte string, as
6166 discussed in patch #683592 */
6167 PyErr_Clear();
6168 }
6169 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00006170#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006171 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006172}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006173#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006174
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006175
Guido van Rossumb6775db1994-08-01 11:34:53 +00006176#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006177PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006178"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006179Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006180
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006181static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006182posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006183{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006184 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006185}
6186#endif /* HAVE_SYMLINK */
6187
6188
6189#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006190#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6191static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006192system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006193{
6194 ULONG value = 0;
6195
6196 Py_BEGIN_ALLOW_THREADS
6197 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6198 Py_END_ALLOW_THREADS
6199
6200 return value;
6201}
6202
6203static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006204posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006205{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006206 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006207 return Py_BuildValue("ddddd",
6208 (double)0 /* t.tms_utime / HZ */,
6209 (double)0 /* t.tms_stime / HZ */,
6210 (double)0 /* t.tms_cutime / HZ */,
6211 (double)0 /* t.tms_cstime / HZ */,
6212 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006213}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006214#else /* not OS2 */
Martin v. Löwis03824e42008-12-29 18:17:34 +00006215#define NEED_TICKS_PER_SECOND
6216static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006217static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006218posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006219{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006220 struct tms t;
6221 clock_t c;
6222 errno = 0;
6223 c = times(&t);
6224 if (c == (clock_t) -1)
6225 return posix_error();
6226 return Py_BuildValue("ddddd",
6227 (double)t.tms_utime / ticks_per_second,
6228 (double)t.tms_stime / ticks_per_second,
6229 (double)t.tms_cutime / ticks_per_second,
6230 (double)t.tms_cstime / ticks_per_second,
6231 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006232}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006233#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006234#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006235
6236
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006237#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006238#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006239static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006240posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006241{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006242 FILETIME create, exit, kernel, user;
6243 HANDLE hProc;
6244 hProc = GetCurrentProcess();
6245 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6246 /* The fields of a FILETIME structure are the hi and lo part
6247 of a 64-bit value expressed in 100 nanosecond units.
6248 1e7 is one second in such units; 1e-7 the inverse.
6249 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6250 */
6251 return Py_BuildValue(
6252 "ddddd",
6253 (double)(user.dwHighDateTime*429.4967296 +
6254 user.dwLowDateTime*1e-7),
6255 (double)(kernel.dwHighDateTime*429.4967296 +
6256 kernel.dwLowDateTime*1e-7),
6257 (double)0,
6258 (double)0,
6259 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006260}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006261#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006262
6263#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006264PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006265"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006266Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006267#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006268
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006269
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006270#ifdef HAVE_GETSID
6271PyDoc_STRVAR(posix_getsid__doc__,
6272"getsid(pid) -> sid\n\n\
6273Call the system call getsid().");
6274
6275static PyObject *
6276posix_getsid(PyObject *self, PyObject *args)
6277{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006278 pid_t pid;
6279 int sid;
6280 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
6281 return NULL;
6282 sid = getsid(pid);
6283 if (sid < 0)
6284 return posix_error();
6285 return PyInt_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006286}
6287#endif /* HAVE_GETSID */
6288
6289
Guido van Rossumb6775db1994-08-01 11:34:53 +00006290#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006291PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006292"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006293Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006294
Barry Warsaw53699e91996-12-10 23:23:01 +00006295static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006296posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006297{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006298 if (setsid() < 0)
6299 return posix_error();
6300 Py_INCREF(Py_None);
6301 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006302}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006303#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006304
Guido van Rossumb6775db1994-08-01 11:34:53 +00006305#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006306PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006307"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006308Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006309
Barry Warsaw53699e91996-12-10 23:23:01 +00006310static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006311posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006312{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006313 pid_t pid;
6314 int pgrp;
6315 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
6316 return NULL;
6317 if (setpgid(pid, pgrp) < 0)
6318 return posix_error();
6319 Py_INCREF(Py_None);
6320 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006321}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006322#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006323
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006324
Guido van Rossumb6775db1994-08-01 11:34:53 +00006325#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006326PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006327"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006328Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006329
Barry Warsaw53699e91996-12-10 23:23:01 +00006330static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006331posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006332{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006333 int fd;
6334 pid_t pgid;
6335 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6336 return NULL;
6337 pgid = tcgetpgrp(fd);
6338 if (pgid < 0)
6339 return posix_error();
6340 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006341}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006342#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006343
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006344
Guido van Rossumb6775db1994-08-01 11:34:53 +00006345#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006346PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006347"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006348Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006349
Barry Warsaw53699e91996-12-10 23:23:01 +00006350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006351posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006352{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006353 int fd;
6354 pid_t pgid;
6355 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
6356 return NULL;
6357 if (tcsetpgrp(fd, pgid) < 0)
6358 return posix_error();
6359 Py_INCREF(Py_None);
6360 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006361}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006362#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006363
Guido van Rossum687dd131993-05-17 08:34:16 +00006364/* Functions acting on file descriptors */
6365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006366PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006367"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006368Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006369
Barry Warsaw53699e91996-12-10 23:23:01 +00006370static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006371posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006372{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006373 char *file = NULL;
6374 int flag;
6375 int mode = 0777;
6376 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006377
6378#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006379 PyUnicodeObject *po;
6380 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6381 Py_BEGIN_ALLOW_THREADS
6382 /* PyUnicode_AS_UNICODE OK without thread
6383 lock as it is a simple dereference. */
6384 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6385 Py_END_ALLOW_THREADS
6386 if (fd < 0)
6387 return posix_error();
6388 return PyInt_FromLong((long)fd);
6389 }
6390 /* Drop the argument parsing error as narrow strings
6391 are also valid. */
6392 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006393#endif
6394
Victor Stinnerd6f85422010-05-05 23:33:33 +00006395 if (!PyArg_ParseTuple(args, "eti|i",
6396 Py_FileSystemDefaultEncoding, &file,
6397 &flag, &mode))
6398 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006399
Victor Stinnerd6f85422010-05-05 23:33:33 +00006400 Py_BEGIN_ALLOW_THREADS
6401 fd = open(file, flag, mode);
6402 Py_END_ALLOW_THREADS
6403 if (fd < 0)
6404 return posix_error_with_allocated_filename(file);
6405 PyMem_Free(file);
6406 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006407}
6408
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006410PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006411"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006412Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006413
Barry Warsaw53699e91996-12-10 23:23:01 +00006414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006415posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006416{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006417 int fd, res;
6418 if (!PyArg_ParseTuple(args, "i:close", &fd))
6419 return NULL;
6420 if (!_PyVerify_fd(fd))
6421 return posix_error();
6422 Py_BEGIN_ALLOW_THREADS
6423 res = close(fd);
6424 Py_END_ALLOW_THREADS
6425 if (res < 0)
6426 return posix_error();
6427 Py_INCREF(Py_None);
6428 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006429}
6430
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006431
Victor Stinnerd6f85422010-05-05 23:33:33 +00006432PyDoc_STRVAR(posix_closerange__doc__,
Georg Brandl309501a2008-01-19 20:22:13 +00006433"closerange(fd_low, fd_high)\n\n\
6434Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6435
6436static PyObject *
6437posix_closerange(PyObject *self, PyObject *args)
6438{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006439 int fd_from, fd_to, i;
6440 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6441 return NULL;
6442 Py_BEGIN_ALLOW_THREADS
6443 for (i = fd_from; i < fd_to; i++)
6444 if (_PyVerify_fd(i))
6445 close(i);
6446 Py_END_ALLOW_THREADS
6447 Py_RETURN_NONE;
Georg Brandl309501a2008-01-19 20:22:13 +00006448}
6449
6450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006451PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006452"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006453Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006454
Barry Warsaw53699e91996-12-10 23:23:01 +00006455static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006456posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006457{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006458 int fd;
6459 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6460 return NULL;
6461 if (!_PyVerify_fd(fd))
6462 return posix_error();
6463 Py_BEGIN_ALLOW_THREADS
6464 fd = dup(fd);
6465 Py_END_ALLOW_THREADS
6466 if (fd < 0)
6467 return posix_error();
6468 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006469}
6470
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006472PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006473"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006474Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006475
Barry Warsaw53699e91996-12-10 23:23:01 +00006476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006477posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006478{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006479 int fd, fd2, res;
6480 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6481 return NULL;
6482 if (!_PyVerify_fd_dup2(fd, fd2))
6483 return posix_error();
6484 Py_BEGIN_ALLOW_THREADS
6485 res = dup2(fd, fd2);
6486 Py_END_ALLOW_THREADS
6487 if (res < 0)
6488 return posix_error();
6489 Py_INCREF(Py_None);
6490 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006491}
6492
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006494PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006495"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006496Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006497
Barry Warsaw53699e91996-12-10 23:23:01 +00006498static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006499posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006500{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006501 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006502#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006503 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006504#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006505 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006506#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006507 PyObject *posobj;
6508 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6509 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006510#ifdef SEEK_SET
Victor Stinnerd6f85422010-05-05 23:33:33 +00006511 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6512 switch (how) {
6513 case 0: how = SEEK_SET; break;
6514 case 1: how = SEEK_CUR; break;
6515 case 2: how = SEEK_END; break;
6516 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006517#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006518
6519#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006520 pos = PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006521#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006522 pos = PyLong_Check(posobj) ?
6523 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006524#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006525 if (PyErr_Occurred())
6526 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006527
Victor Stinnerd6f85422010-05-05 23:33:33 +00006528 if (!_PyVerify_fd(fd))
6529 return posix_error();
6530 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006531#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006532 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006533#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006534 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006535#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006536 Py_END_ALLOW_THREADS
6537 if (res < 0)
6538 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006539
6540#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006541 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006542#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006543 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006544#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006545}
6546
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006548PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006549"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006550Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006551
Barry Warsaw53699e91996-12-10 23:23:01 +00006552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006553posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006554{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006555 int fd, size, n;
6556 PyObject *buffer;
6557 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6558 return NULL;
6559 if (size < 0) {
6560 errno = EINVAL;
6561 return posix_error();
6562 }
6563 buffer = PyString_FromStringAndSize((char *)NULL, size);
6564 if (buffer == NULL)
6565 return NULL;
6566 if (!_PyVerify_fd(fd))
6567 return posix_error();
6568 Py_BEGIN_ALLOW_THREADS
6569 n = read(fd, PyString_AsString(buffer), size);
6570 Py_END_ALLOW_THREADS
6571 if (n < 0) {
6572 Py_DECREF(buffer);
6573 return posix_error();
6574 }
6575 if (n != size)
6576 _PyString_Resize(&buffer, n);
6577 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006578}
6579
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006581PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006582"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006583Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006584
Barry Warsaw53699e91996-12-10 23:23:01 +00006585static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006586posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006587{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006588 Py_buffer pbuf;
6589 int fd;
6590 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006591
Victor Stinnerd6f85422010-05-05 23:33:33 +00006592 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6593 return NULL;
6594 if (!_PyVerify_fd(fd))
6595 return posix_error();
6596 Py_BEGIN_ALLOW_THREADS
6597 size = write(fd, pbuf.buf, (size_t)pbuf.len);
6598 Py_END_ALLOW_THREADS
6599 PyBuffer_Release(&pbuf);
6600 if (size < 0)
6601 return posix_error();
6602 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006603}
6604
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006606PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006607"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006608Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006609
Barry Warsaw53699e91996-12-10 23:23:01 +00006610static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006611posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006612{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006613 int fd;
6614 STRUCT_STAT st;
6615 int res;
6616 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6617 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006618#ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006619 /* on OpenVMS we must ensure that all bytes are written to the file */
6620 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006621#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006622 if (!_PyVerify_fd(fd))
6623 return posix_error();
6624 Py_BEGIN_ALLOW_THREADS
6625 res = FSTAT(fd, &st);
6626 Py_END_ALLOW_THREADS
6627 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006628#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00006629 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006630#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006631 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006632#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006633 }
Tim Peters5aa91602002-01-30 05:46:57 +00006634
Victor Stinnerd6f85422010-05-05 23:33:33 +00006635 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006636}
6637
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006639PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006640"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006641Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006642
Barry Warsaw53699e91996-12-10 23:23:01 +00006643static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006644posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006645{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006646 int fd;
6647 char *orgmode = "r";
6648 int bufsize = -1;
6649 FILE *fp;
6650 PyObject *f;
6651 char *mode;
6652 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6653 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006654
Victor Stinnerd6f85422010-05-05 23:33:33 +00006655 /* Sanitize mode. See fileobject.c */
6656 mode = PyMem_MALLOC(strlen(orgmode)+3);
6657 if (!mode) {
6658 PyErr_NoMemory();
6659 return NULL;
6660 }
6661 strcpy(mode, orgmode);
6662 if (_PyFile_SanitizeMode(mode)) {
6663 PyMem_FREE(mode);
6664 return NULL;
6665 }
6666 if (!_PyVerify_fd(fd))
6667 return posix_error();
6668 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006669#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006670 if (mode[0] == 'a') {
6671 /* try to make sure the O_APPEND flag is set */
6672 int flags;
6673 flags = fcntl(fd, F_GETFL);
6674 if (flags != -1)
6675 fcntl(fd, F_SETFL, flags | O_APPEND);
6676 fp = fdopen(fd, mode);
6677 if (fp == NULL && flags != -1)
6678 /* restore old mode if fdopen failed */
6679 fcntl(fd, F_SETFL, flags);
6680 } else {
6681 fp = fdopen(fd, mode);
6682 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006683#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006684 fp = fdopen(fd, mode);
Georg Brandl644b1e72006-03-31 20:27:22 +00006685#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006686 Py_END_ALLOW_THREADS
6687 PyMem_FREE(mode);
6688 if (fp == NULL)
6689 return posix_error();
6690 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6691 if (f != NULL)
6692 PyFile_SetBufSize(f, bufsize);
6693 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006694}
6695
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006696PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006697"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006698Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006699connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006700
6701static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006702posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006703{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006704 int fd;
6705 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6706 return NULL;
6707 if (!_PyVerify_fd(fd))
6708 return PyBool_FromLong(0);
6709 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006710}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006711
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006712#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006713PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006714"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006715Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006716
Barry Warsaw53699e91996-12-10 23:23:01 +00006717static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006718posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006719{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006720#if defined(PYOS_OS2)
6721 HFILE read, write;
6722 APIRET rc;
6723
Victor Stinnerd6f85422010-05-05 23:33:33 +00006724 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006725 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinnerd6f85422010-05-05 23:33:33 +00006726 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006727 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006728 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006729
6730 return Py_BuildValue("(ii)", read, write);
6731#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006732#if !defined(MS_WINDOWS)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006733 int fds[2];
6734 int res;
6735 Py_BEGIN_ALLOW_THREADS
6736 res = pipe(fds);
6737 Py_END_ALLOW_THREADS
6738 if (res != 0)
6739 return posix_error();
6740 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006741#else /* MS_WINDOWS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00006742 HANDLE read, write;
6743 int read_fd, write_fd;
6744 BOOL ok;
6745 Py_BEGIN_ALLOW_THREADS
6746 ok = CreatePipe(&read, &write, NULL, 0);
6747 Py_END_ALLOW_THREADS
6748 if (!ok)
6749 return win32_error("CreatePipe", NULL);
6750 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6751 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6752 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006753#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006754#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006755}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006756#endif /* HAVE_PIPE */
6757
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006758
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006759#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006760PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006761"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006762Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006763
Barry Warsaw53699e91996-12-10 23:23:01 +00006764static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006765posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006766{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006767 char *filename;
6768 int mode = 0666;
6769 int res;
6770 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6771 return NULL;
6772 Py_BEGIN_ALLOW_THREADS
6773 res = mkfifo(filename, mode);
6774 Py_END_ALLOW_THREADS
6775 if (res < 0)
6776 return posix_error();
6777 Py_INCREF(Py_None);
6778 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006779}
6780#endif
6781
6782
Neal Norwitz11690112002-07-30 01:08:28 +00006783#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006784PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006785"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006786Create a filesystem node (file, device special file or named pipe)\n\
6787named filename. mode specifies both the permissions to use and the\n\
6788type of node to be created, being combined (bitwise OR) with one of\n\
6789S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006790device defines the newly created device special file (probably using\n\
6791os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006792
6793
6794static PyObject *
6795posix_mknod(PyObject *self, PyObject *args)
6796{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006797 char *filename;
6798 int mode = 0600;
6799 int device = 0;
6800 int res;
6801 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6802 return NULL;
6803 Py_BEGIN_ALLOW_THREADS
6804 res = mknod(filename, mode, device);
6805 Py_END_ALLOW_THREADS
6806 if (res < 0)
6807 return posix_error();
6808 Py_INCREF(Py_None);
6809 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006810}
6811#endif
6812
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006813#ifdef HAVE_DEVICE_MACROS
6814PyDoc_STRVAR(posix_major__doc__,
6815"major(device) -> major number\n\
6816Extracts a device major number from a raw device number.");
6817
6818static PyObject *
6819posix_major(PyObject *self, PyObject *args)
6820{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006821 int device;
6822 if (!PyArg_ParseTuple(args, "i:major", &device))
6823 return NULL;
6824 return PyInt_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006825}
6826
6827PyDoc_STRVAR(posix_minor__doc__,
6828"minor(device) -> minor number\n\
6829Extracts a device minor number from a raw device number.");
6830
6831static PyObject *
6832posix_minor(PyObject *self, PyObject *args)
6833{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006834 int device;
6835 if (!PyArg_ParseTuple(args, "i:minor", &device))
6836 return NULL;
6837 return PyInt_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006838}
6839
6840PyDoc_STRVAR(posix_makedev__doc__,
6841"makedev(major, minor) -> device number\n\
6842Composes a raw device number from the major and minor device numbers.");
6843
6844static PyObject *
6845posix_makedev(PyObject *self, PyObject *args)
6846{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006847 int major, minor;
6848 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6849 return NULL;
6850 return PyInt_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006851}
6852#endif /* device macros */
6853
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006854
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006855#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006856PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006857"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006858Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006859
Barry Warsaw53699e91996-12-10 23:23:01 +00006860static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006861posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006862{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006863 int fd;
6864 off_t length;
6865 int res;
6866 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006867
Victor Stinnerd6f85422010-05-05 23:33:33 +00006868 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6869 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006870
6871#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00006872 length = PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006873#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00006874 length = PyLong_Check(lenobj) ?
6875 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006876#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006877 if (PyErr_Occurred())
6878 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006879
Victor Stinnerd6f85422010-05-05 23:33:33 +00006880 Py_BEGIN_ALLOW_THREADS
6881 res = ftruncate(fd, length);
6882 Py_END_ALLOW_THREADS
6883 if (res < 0)
6884 return posix_error();
6885 Py_INCREF(Py_None);
6886 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006887}
6888#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006889
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006890#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006891PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006892"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006893Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006894
Fred Drake762e2061999-08-26 17:23:54 +00006895/* Save putenv() parameters as values here, so we can collect them when they
6896 * get re-set with another call for the same key. */
6897static PyObject *posix_putenv_garbage;
6898
Tim Peters5aa91602002-01-30 05:46:57 +00006899static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006900posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006901{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006902 char *s1, *s2;
6903 char *newenv;
6904 PyObject *newstr;
6905 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006906
Victor Stinnerd6f85422010-05-05 23:33:33 +00006907 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
6908 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006909
6910#if defined(PYOS_OS2)
6911 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6912 APIRET rc;
6913
Guido van Rossumd48f2521997-12-05 22:19:34 +00006914 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6915 if (rc != NO_ERROR)
6916 return os2_error(rc);
6917
6918 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6919 APIRET rc;
6920
Guido van Rossumd48f2521997-12-05 22:19:34 +00006921 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6922 if (rc != NO_ERROR)
6923 return os2_error(rc);
6924 } else {
6925#endif
6926
Victor Stinnerd6f85422010-05-05 23:33:33 +00006927 /* XXX This can leak memory -- not easy to fix :-( */
6928 len = strlen(s1) + strlen(s2) + 2;
6929 /* len includes space for a trailing \0; the size arg to
6930 PyString_FromStringAndSize does not count that */
6931 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
6932 if (newstr == NULL)
6933 return PyErr_NoMemory();
6934 newenv = PyString_AS_STRING(newstr);
6935 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6936 if (putenv(newenv)) {
6937 Py_DECREF(newstr);
6938 posix_error();
6939 return NULL;
6940 }
6941 /* Install the first arg and newstr in posix_putenv_garbage;
6942 * this will cause previous value to be collected. This has to
6943 * happen after the real putenv() call because the old value
6944 * was still accessible until then. */
6945 if (PyDict_SetItem(posix_putenv_garbage,
6946 PyTuple_GET_ITEM(args, 0), newstr)) {
6947 /* really not much we can do; just leak */
6948 PyErr_Clear();
6949 }
6950 else {
6951 Py_DECREF(newstr);
6952 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006953
6954#if defined(PYOS_OS2)
6955 }
6956#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00006957 Py_INCREF(Py_None);
6958 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006959}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006960#endif /* putenv */
6961
Guido van Rossumc524d952001-10-19 01:31:59 +00006962#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006963PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006964"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006965Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006966
6967static PyObject *
6968posix_unsetenv(PyObject *self, PyObject *args)
6969{
Victor Stinnerd6f85422010-05-05 23:33:33 +00006970 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006971
Victor Stinnerd6f85422010-05-05 23:33:33 +00006972 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6973 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00006974
Victor Stinnerd6f85422010-05-05 23:33:33 +00006975 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006976
Victor Stinnerd6f85422010-05-05 23:33:33 +00006977 /* Remove the key from posix_putenv_garbage;
6978 * this will cause it to be collected. This has to
6979 * happen after the real unsetenv() call because the
6980 * old value was still accessible until then.
6981 */
6982 if (PyDict_DelItem(posix_putenv_garbage,
6983 PyTuple_GET_ITEM(args, 0))) {
6984 /* really not much we can do; just leak */
6985 PyErr_Clear();
6986 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006987
Victor Stinnerd6f85422010-05-05 23:33:33 +00006988 Py_INCREF(Py_None);
6989 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00006990}
6991#endif /* unsetenv */
6992
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006993PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006994"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006995Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006996
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006997static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006998posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006999{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007000 int code;
7001 char *message;
7002 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7003 return NULL;
7004 message = strerror(code);
7005 if (message == NULL) {
7006 PyErr_SetString(PyExc_ValueError,
7007 "strerror() argument out of range");
7008 return NULL;
7009 }
7010 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007011}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007012
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007013
Guido van Rossumc9641791998-08-04 15:26:23 +00007014#ifdef HAVE_SYS_WAIT_H
7015
Fred Drake106c1a02002-04-23 15:58:02 +00007016#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007017PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007018"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007019Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007020
7021static PyObject *
7022posix_WCOREDUMP(PyObject *self, PyObject *args)
7023{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007024 WAIT_TYPE status;
7025 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007026
Victor Stinnerd6f85422010-05-05 23:33:33 +00007027 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7028 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007029
Victor Stinnerd6f85422010-05-05 23:33:33 +00007030 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007031}
7032#endif /* WCOREDUMP */
7033
7034#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007035PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007036"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007037Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007038job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007039
7040static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007041posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007042{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007043 WAIT_TYPE status;
7044 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007045
Victor Stinnerd6f85422010-05-05 23:33:33 +00007046 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7047 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007048
Victor Stinnerd6f85422010-05-05 23:33:33 +00007049 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007050}
7051#endif /* WIFCONTINUED */
7052
Guido van Rossumc9641791998-08-04 15:26:23 +00007053#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007054PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007055"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007056Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007057
7058static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007059posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007060{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007061 WAIT_TYPE status;
7062 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007063
Victor Stinnerd6f85422010-05-05 23:33:33 +00007064 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7065 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007066
Victor Stinnerd6f85422010-05-05 23:33:33 +00007067 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007068}
7069#endif /* WIFSTOPPED */
7070
7071#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007072PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007073"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007074Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007075
7076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007077posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007078{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007079 WAIT_TYPE status;
7080 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007081
Victor Stinnerd6f85422010-05-05 23:33:33 +00007082 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7083 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007084
Victor Stinnerd6f85422010-05-05 23:33:33 +00007085 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007086}
7087#endif /* WIFSIGNALED */
7088
7089#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007090PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007091"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007092Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007093system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007094
7095static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007096posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007097{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007098 WAIT_TYPE status;
7099 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007100
Victor Stinnerd6f85422010-05-05 23:33:33 +00007101 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7102 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007103
Victor Stinnerd6f85422010-05-05 23:33:33 +00007104 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007105}
7106#endif /* WIFEXITED */
7107
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007108#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007109PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007110"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007111Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007112
7113static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007114posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007115{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007116 WAIT_TYPE status;
7117 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007118
Victor Stinnerd6f85422010-05-05 23:33:33 +00007119 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7120 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007121
Victor Stinnerd6f85422010-05-05 23:33:33 +00007122 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007123}
7124#endif /* WEXITSTATUS */
7125
7126#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007127PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007128"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007129Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007130value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007131
7132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007133posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007134{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007135 WAIT_TYPE status;
7136 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007137
Victor Stinnerd6f85422010-05-05 23:33:33 +00007138 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7139 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007140
Victor Stinnerd6f85422010-05-05 23:33:33 +00007141 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007142}
7143#endif /* WTERMSIG */
7144
7145#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007146PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007147"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007148Return the signal that stopped the process that provided\n\
7149the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007150
7151static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007152posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007153{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007154 WAIT_TYPE status;
7155 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007156
Victor Stinnerd6f85422010-05-05 23:33:33 +00007157 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7158 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007159
Victor Stinnerd6f85422010-05-05 23:33:33 +00007160 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007161}
7162#endif /* WSTOPSIG */
7163
7164#endif /* HAVE_SYS_WAIT_H */
7165
7166
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007167#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007168#ifdef _SCO_DS
7169/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7170 needed definitions in sys/statvfs.h */
7171#define _SVID3
7172#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007173#include <sys/statvfs.h>
7174
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007175static PyObject*
7176_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007177 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7178 if (v == NULL)
7179 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007180
7181#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007182 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7183 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7184 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
7185 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
7186 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
7187 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
7188 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
7189 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
7190 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7191 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007192#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007193 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
7194 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
7195 PyStructSequence_SET_ITEM(v, 2,
7196 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7197 PyStructSequence_SET_ITEM(v, 3,
7198 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7199 PyStructSequence_SET_ITEM(v, 4,
7200 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7201 PyStructSequence_SET_ITEM(v, 5,
7202 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7203 PyStructSequence_SET_ITEM(v, 6,
7204 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7205 PyStructSequence_SET_ITEM(v, 7,
7206 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7207 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
7208 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007209#endif
7210
Victor Stinnerd6f85422010-05-05 23:33:33 +00007211 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007212}
7213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007214PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007215"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007216Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007217
7218static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007219posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007220{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007221 int fd, res;
7222 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007223
Victor Stinnerd6f85422010-05-05 23:33:33 +00007224 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7225 return NULL;
7226 Py_BEGIN_ALLOW_THREADS
7227 res = fstatvfs(fd, &st);
7228 Py_END_ALLOW_THREADS
7229 if (res != 0)
7230 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007231
Victor Stinnerd6f85422010-05-05 23:33:33 +00007232 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007233}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007234#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007235
7236
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00007237#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007238#include <sys/statvfs.h>
7239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007240PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007241"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007242Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007243
7244static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007245posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007246{
Victor Stinnerd6f85422010-05-05 23:33:33 +00007247 char *path;
7248 int res;
7249 struct statvfs st;
7250 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7251 return NULL;
7252 Py_BEGIN_ALLOW_THREADS
7253 res = statvfs(path, &st);
7254 Py_END_ALLOW_THREADS
7255 if (res != 0)
7256 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007257
Victor Stinnerd6f85422010-05-05 23:33:33 +00007258 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007259}
7260#endif /* HAVE_STATVFS */
7261
7262
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007263#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007264PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007265"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007266Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007267The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007268or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007269
7270static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007271posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007272{
7273 PyObject *result = NULL;
7274 char *dir = NULL;
7275 char *pfx = NULL;
7276 char *name;
7277
7278 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
Victor Stinnerd6f85422010-05-05 23:33:33 +00007279 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007280
7281 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007282 "tempnam is a potential security risk to your program") < 0)
7283 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007284
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007285#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007286 name = _tempnam(dir, pfx);
7287#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007288 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007289#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007290 if (name == NULL)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007291 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007292 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007293 free(name);
7294 return result;
7295}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007296#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007297
7298
7299#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007300PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007301"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007302Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007303
7304static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007305posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007306{
7307 FILE *fp;
7308
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007309 fp = tmpfile();
7310 if (fp == NULL)
Victor Stinnerd6f85422010-05-05 23:33:33 +00007311 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007312 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007313}
7314#endif
7315
7316
7317#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007318PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007319"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007320Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007321
7322static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007323posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007324{
7325 char buffer[L_tmpnam];
7326 char *name;
7327
Skip Montanaro95618b52001-08-18 18:52:10 +00007328 if (PyErr_Warn(PyExc_RuntimeWarning,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007329 "tmpnam is a potential security risk to your program") < 0)
7330 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007331
Greg Wardb48bc172000-03-01 21:51:56 +00007332#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007333 name = tmpnam_r(buffer);
7334#else
7335 name = tmpnam(buffer);
7336#endif
7337 if (name == NULL) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007338 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007339#ifdef USE_TMPNAM_R
Victor Stinnerd6f85422010-05-05 23:33:33 +00007340 "unexpected NULL from tmpnam_r"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007341#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007342 "unexpected NULL from tmpnam"
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007343#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00007344 );
7345 PyErr_SetObject(PyExc_OSError, err);
7346 Py_XDECREF(err);
7347 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007348 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007349 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007350}
7351#endif
7352
7353
Fred Drakec9680921999-12-13 16:37:25 +00007354/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7355 * It maps strings representing configuration variable names to
7356 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007357 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007358 * rarely-used constants. There are three separate tables that use
7359 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007360 *
7361 * This code is always included, even if none of the interfaces that
7362 * need it are included. The #if hackery needed to avoid it would be
7363 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007364 */
7365struct constdef {
7366 char *name;
7367 long value;
7368};
7369
Fred Drake12c6e2d1999-12-14 21:25:03 +00007370static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007371conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Victor Stinnerd6f85422010-05-05 23:33:33 +00007372 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007373{
7374 if (PyInt_Check(arg)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007375 *valuep = PyInt_AS_LONG(arg);
7376 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007377 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007378 if (PyString_Check(arg)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007379 /* look up the value in the table using a binary search */
7380 size_t lo = 0;
7381 size_t mid;
7382 size_t hi = tablesize;
7383 int cmp;
7384 char *confname = PyString_AS_STRING(arg);
7385 while (lo < hi) {
7386 mid = (lo + hi) / 2;
7387 cmp = strcmp(confname, table[mid].name);
7388 if (cmp < 0)
7389 hi = mid;
7390 else if (cmp > 0)
7391 lo = mid + 1;
7392 else {
7393 *valuep = table[mid].value;
7394 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007395 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00007396 }
7397 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007398 }
7399 else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007400 PyErr_SetString(PyExc_TypeError,
7401 "configuration names must be strings or integers");
Fred Drake12c6e2d1999-12-14 21:25:03 +00007402 return 0;
7403}
7404
7405
7406#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7407static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007408#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007409 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007410#endif
7411#ifdef _PC_ABI_ASYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007412 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007413#endif
Fred Drakec9680921999-12-13 16:37:25 +00007414#ifdef _PC_ASYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007415 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007416#endif
7417#ifdef _PC_CHOWN_RESTRICTED
Victor Stinnerd6f85422010-05-05 23:33:33 +00007418 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007419#endif
7420#ifdef _PC_FILESIZEBITS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007421 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007422#endif
7423#ifdef _PC_LAST
Victor Stinnerd6f85422010-05-05 23:33:33 +00007424 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007425#endif
7426#ifdef _PC_LINK_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007427 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007428#endif
7429#ifdef _PC_MAX_CANON
Victor Stinnerd6f85422010-05-05 23:33:33 +00007430 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007431#endif
7432#ifdef _PC_MAX_INPUT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007433 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007434#endif
7435#ifdef _PC_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007436 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007437#endif
7438#ifdef _PC_NO_TRUNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00007439 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007440#endif
7441#ifdef _PC_PATH_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007442 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007443#endif
7444#ifdef _PC_PIPE_BUF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007445 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007446#endif
7447#ifdef _PC_PRIO_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007448 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007449#endif
7450#ifdef _PC_SOCK_MAXBUF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007451 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007452#endif
7453#ifdef _PC_SYNC_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007454 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007455#endif
7456#ifdef _PC_VDISABLE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007457 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007458#endif
7459};
7460
Fred Drakec9680921999-12-13 16:37:25 +00007461static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007462conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007463{
7464 return conv_confname(arg, valuep, posix_constants_pathconf,
7465 sizeof(posix_constants_pathconf)
7466 / sizeof(struct constdef));
7467}
7468#endif
7469
7470#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007471PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007472"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007473Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007474If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007475
7476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007477posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007478{
7479 PyObject *result = NULL;
7480 int name, fd;
7481
Fred Drake12c6e2d1999-12-14 21:25:03 +00007482 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7483 conv_path_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007484 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007485
Victor Stinnerd6f85422010-05-05 23:33:33 +00007486 errno = 0;
7487 limit = fpathconf(fd, name);
7488 if (limit == -1 && errno != 0)
7489 posix_error();
7490 else
7491 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007492 }
7493 return result;
7494}
7495#endif
7496
7497
7498#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007499PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007500"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007501Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007502If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007503
7504static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007505posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007506{
7507 PyObject *result = NULL;
7508 int name;
7509 char *path;
7510
7511 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7512 conv_path_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007513 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007514
Victor Stinnerd6f85422010-05-05 23:33:33 +00007515 errno = 0;
7516 limit = pathconf(path, name);
7517 if (limit == -1 && errno != 0) {
7518 if (errno == EINVAL)
7519 /* could be a path or name problem */
7520 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007521 else
Victor Stinnerd6f85422010-05-05 23:33:33 +00007522 posix_error_with_filename(path);
7523 }
7524 else
7525 result = PyInt_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007526 }
7527 return result;
7528}
7529#endif
7530
7531#ifdef HAVE_CONFSTR
7532static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007533#ifdef _CS_ARCHITECTURE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007534 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007535#endif
7536#ifdef _CS_HOSTNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007537 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007538#endif
7539#ifdef _CS_HW_PROVIDER
Victor Stinnerd6f85422010-05-05 23:33:33 +00007540 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007541#endif
7542#ifdef _CS_HW_SERIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007543 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007544#endif
7545#ifdef _CS_INITTAB_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007546 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007547#endif
Fred Drakec9680921999-12-13 16:37:25 +00007548#ifdef _CS_LFS64_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007549 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007550#endif
7551#ifdef _CS_LFS64_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007552 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007553#endif
7554#ifdef _CS_LFS64_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007555 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007556#endif
7557#ifdef _CS_LFS64_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007558 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007559#endif
7560#ifdef _CS_LFS_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007561 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007562#endif
7563#ifdef _CS_LFS_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007564 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007565#endif
7566#ifdef _CS_LFS_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007567 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007568#endif
7569#ifdef _CS_LFS_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007570 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007571#endif
Fred Draked86ed291999-12-15 15:34:33 +00007572#ifdef _CS_MACHINE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007573 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007574#endif
Fred Drakec9680921999-12-13 16:37:25 +00007575#ifdef _CS_PATH
Victor Stinnerd6f85422010-05-05 23:33:33 +00007576 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007577#endif
Fred Draked86ed291999-12-15 15:34:33 +00007578#ifdef _CS_RELEASE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007579 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007580#endif
7581#ifdef _CS_SRPC_DOMAIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007582 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007583#endif
7584#ifdef _CS_SYSNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007585 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007586#endif
7587#ifdef _CS_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007588 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007589#endif
Fred Drakec9680921999-12-13 16:37:25 +00007590#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007591 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007592#endif
7593#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007594 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007595#endif
7596#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007597 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007598#endif
7599#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007600 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007601#endif
7602#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007603 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007604#endif
7605#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007606 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007607#endif
7608#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007609 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007610#endif
7611#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007612 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007613#endif
7614#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007615 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007616#endif
7617#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007618 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007619#endif
7620#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007621 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007622#endif
7623#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007624 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007625#endif
7626#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007627 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007628#endif
7629#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007630 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007631#endif
7632#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007633 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007634#endif
7635#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007636 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007637#endif
Fred Draked86ed291999-12-15 15:34:33 +00007638#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007639 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007640#endif
7641#ifdef _MIPS_CS_BASE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007642 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007643#endif
7644#ifdef _MIPS_CS_HOSTID
Victor Stinnerd6f85422010-05-05 23:33:33 +00007645 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007646#endif
7647#ifdef _MIPS_CS_HW_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007648 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007649#endif
7650#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007651 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007652#endif
7653#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007654 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007655#endif
7656#ifdef _MIPS_CS_OSREL_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007657 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007658#endif
7659#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinnerd6f85422010-05-05 23:33:33 +00007660 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007661#endif
7662#ifdef _MIPS_CS_OS_NAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00007663 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007664#endif
7665#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinnerd6f85422010-05-05 23:33:33 +00007666 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007667#endif
7668#ifdef _MIPS_CS_PROCESSORS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007669 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007670#endif
7671#ifdef _MIPS_CS_SERIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007672 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007673#endif
7674#ifdef _MIPS_CS_VENDOR
Victor Stinnerd6f85422010-05-05 23:33:33 +00007675 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007676#endif
Fred Drakec9680921999-12-13 16:37:25 +00007677};
7678
7679static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007680conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007681{
7682 return conv_confname(arg, valuep, posix_constants_confstr,
7683 sizeof(posix_constants_confstr)
7684 / sizeof(struct constdef));
7685}
7686
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007687PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007688"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007689Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007690
7691static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007692posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007693{
7694 PyObject *result = NULL;
7695 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007696 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007697
7698 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007699 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007700
Victor Stinnerd6f85422010-05-05 23:33:33 +00007701 errno = 0;
7702 len = confstr(name, buffer, sizeof(buffer));
7703 if (len == 0) {
7704 if (errno) {
7705 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007706 }
7707 else {
Victor Stinnerd6f85422010-05-05 23:33:33 +00007708 result = Py_None;
7709 Py_INCREF(Py_None);
Fred Drakec9680921999-12-13 16:37:25 +00007710 }
7711 }
Victor Stinnerd6f85422010-05-05 23:33:33 +00007712 else {
7713 if ((unsigned int)len >= sizeof(buffer)) {
7714 result = PyString_FromStringAndSize(NULL, len-1);
7715 if (result != NULL)
7716 confstr(name, PyString_AS_STRING(result), len);
7717 }
7718 else
7719 result = PyString_FromStringAndSize(buffer, len-1);
7720 }
7721 }
Fred Drakec9680921999-12-13 16:37:25 +00007722 return result;
7723}
7724#endif
7725
7726
7727#ifdef HAVE_SYSCONF
7728static struct constdef posix_constants_sysconf[] = {
7729#ifdef _SC_2_CHAR_TERM
Victor Stinnerd6f85422010-05-05 23:33:33 +00007730 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007731#endif
7732#ifdef _SC_2_C_BIND
Victor Stinnerd6f85422010-05-05 23:33:33 +00007733 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007734#endif
7735#ifdef _SC_2_C_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007736 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007737#endif
7738#ifdef _SC_2_C_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007739 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007740#endif
7741#ifdef _SC_2_FORT_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007742 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007743#endif
7744#ifdef _SC_2_FORT_RUN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007745 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007746#endif
7747#ifdef _SC_2_LOCALEDEF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007748 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007749#endif
7750#ifdef _SC_2_SW_DEV
Victor Stinnerd6f85422010-05-05 23:33:33 +00007751 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007752#endif
7753#ifdef _SC_2_UPE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007754 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007755#endif
7756#ifdef _SC_2_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007757 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007758#endif
Fred Draked86ed291999-12-15 15:34:33 +00007759#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007760 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007761#endif
7762#ifdef _SC_ACL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007763 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007764#endif
Fred Drakec9680921999-12-13 16:37:25 +00007765#ifdef _SC_AIO_LISTIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007766 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007767#endif
Fred Drakec9680921999-12-13 16:37:25 +00007768#ifdef _SC_AIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007769 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007770#endif
7771#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007772 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007773#endif
7774#ifdef _SC_ARG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007775 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007776#endif
7777#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007778 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007779#endif
7780#ifdef _SC_ATEXIT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007781 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007782#endif
Fred Draked86ed291999-12-15 15:34:33 +00007783#ifdef _SC_AUDIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007784 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007785#endif
Fred Drakec9680921999-12-13 16:37:25 +00007786#ifdef _SC_AVPHYS_PAGES
Victor Stinnerd6f85422010-05-05 23:33:33 +00007787 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007788#endif
7789#ifdef _SC_BC_BASE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007790 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007791#endif
7792#ifdef _SC_BC_DIM_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007793 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007794#endif
7795#ifdef _SC_BC_SCALE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007796 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007797#endif
7798#ifdef _SC_BC_STRING_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007799 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007800#endif
Fred Draked86ed291999-12-15 15:34:33 +00007801#ifdef _SC_CAP
Victor Stinnerd6f85422010-05-05 23:33:33 +00007802 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007803#endif
Fred Drakec9680921999-12-13 16:37:25 +00007804#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007805 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007806#endif
7807#ifdef _SC_CHAR_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007808 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007809#endif
7810#ifdef _SC_CHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007811 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007812#endif
7813#ifdef _SC_CHAR_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007814 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007815#endif
7816#ifdef _SC_CHILD_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007817 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007818#endif
7819#ifdef _SC_CLK_TCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00007820 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007821#endif
7822#ifdef _SC_COHER_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007823 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007824#endif
7825#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007826 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007827#endif
7828#ifdef _SC_DCACHE_ASSOC
Victor Stinnerd6f85422010-05-05 23:33:33 +00007829 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007830#endif
7831#ifdef _SC_DCACHE_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007832 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007833#endif
7834#ifdef _SC_DCACHE_LINESZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007835 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007836#endif
7837#ifdef _SC_DCACHE_SZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007838 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007839#endif
7840#ifdef _SC_DCACHE_TBLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007841 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007842#endif
7843#ifdef _SC_DELAYTIMER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007844 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007845#endif
7846#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007847 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007848#endif
7849#ifdef _SC_EXPR_NEST_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007850 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007851#endif
7852#ifdef _SC_FSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00007853 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007854#endif
7855#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007856 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007857#endif
7858#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007859 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007860#endif
7861#ifdef _SC_ICACHE_ASSOC
Victor Stinnerd6f85422010-05-05 23:33:33 +00007862 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007863#endif
7864#ifdef _SC_ICACHE_BLKSZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007865 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007866#endif
7867#ifdef _SC_ICACHE_LINESZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007868 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007869#endif
7870#ifdef _SC_ICACHE_SZ
Victor Stinnerd6f85422010-05-05 23:33:33 +00007871 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007872#endif
Fred Draked86ed291999-12-15 15:34:33 +00007873#ifdef _SC_INF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007874 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007875#endif
Fred Drakec9680921999-12-13 16:37:25 +00007876#ifdef _SC_INT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007877 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007878#endif
7879#ifdef _SC_INT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007880 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007881#endif
7882#ifdef _SC_IOV_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007883 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007884#endif
Fred Draked86ed291999-12-15 15:34:33 +00007885#ifdef _SC_IP_SECOPTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007886 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007887#endif
Fred Drakec9680921999-12-13 16:37:25 +00007888#ifdef _SC_JOB_CONTROL
Victor Stinnerd6f85422010-05-05 23:33:33 +00007889 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007890#endif
Fred Draked86ed291999-12-15 15:34:33 +00007891#ifdef _SC_KERN_POINTERS
Victor Stinnerd6f85422010-05-05 23:33:33 +00007892 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007893#endif
7894#ifdef _SC_KERN_SIM
Victor Stinnerd6f85422010-05-05 23:33:33 +00007895 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007896#endif
Fred Drakec9680921999-12-13 16:37:25 +00007897#ifdef _SC_LINE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007898 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007899#endif
7900#ifdef _SC_LOGIN_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007901 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007902#endif
7903#ifdef _SC_LOGNAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007904 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007905#endif
7906#ifdef _SC_LONG_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007907 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007908#endif
Fred Draked86ed291999-12-15 15:34:33 +00007909#ifdef _SC_MAC
Victor Stinnerd6f85422010-05-05 23:33:33 +00007910 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007911#endif
Fred Drakec9680921999-12-13 16:37:25 +00007912#ifdef _SC_MAPPED_FILES
Victor Stinnerd6f85422010-05-05 23:33:33 +00007913 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007914#endif
7915#ifdef _SC_MAXPID
Victor Stinnerd6f85422010-05-05 23:33:33 +00007916 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007917#endif
7918#ifdef _SC_MB_LEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007919 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007920#endif
7921#ifdef _SC_MEMLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00007922 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007923#endif
7924#ifdef _SC_MEMLOCK_RANGE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007925 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007926#endif
7927#ifdef _SC_MEMORY_PROTECTION
Victor Stinnerd6f85422010-05-05 23:33:33 +00007928 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007929#endif
7930#ifdef _SC_MESSAGE_PASSING
Victor Stinnerd6f85422010-05-05 23:33:33 +00007931 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007932#endif
Fred Draked86ed291999-12-15 15:34:33 +00007933#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinnerd6f85422010-05-05 23:33:33 +00007934 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007935#endif
Fred Drakec9680921999-12-13 16:37:25 +00007936#ifdef _SC_MQ_OPEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007937 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007938#endif
7939#ifdef _SC_MQ_PRIO_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007940 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007941#endif
Fred Draked86ed291999-12-15 15:34:33 +00007942#ifdef _SC_NACLS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007943 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007944#endif
Fred Drakec9680921999-12-13 16:37:25 +00007945#ifdef _SC_NGROUPS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007946 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007947#endif
7948#ifdef _SC_NL_ARGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007949 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007950#endif
7951#ifdef _SC_NL_LANGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007952 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007953#endif
7954#ifdef _SC_NL_MSGMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007955 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007956#endif
7957#ifdef _SC_NL_NMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007958 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007959#endif
7960#ifdef _SC_NL_SETMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007961 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007962#endif
7963#ifdef _SC_NL_TEXTMAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007964 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007965#endif
7966#ifdef _SC_NPROCESSORS_CONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007967 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007968#endif
7969#ifdef _SC_NPROCESSORS_ONLN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007970 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007971#endif
Fred Draked86ed291999-12-15 15:34:33 +00007972#ifdef _SC_NPROC_CONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00007973 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007974#endif
7975#ifdef _SC_NPROC_ONLN
Victor Stinnerd6f85422010-05-05 23:33:33 +00007976 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007977#endif
Fred Drakec9680921999-12-13 16:37:25 +00007978#ifdef _SC_NZERO
Victor Stinnerd6f85422010-05-05 23:33:33 +00007979 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007980#endif
7981#ifdef _SC_OPEN_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007982 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007983#endif
7984#ifdef _SC_PAGESIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007985 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007986#endif
7987#ifdef _SC_PAGE_SIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00007988 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007989#endif
7990#ifdef _SC_PASS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00007991 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007992#endif
7993#ifdef _SC_PHYS_PAGES
Victor Stinnerd6f85422010-05-05 23:33:33 +00007994 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007995#endif
7996#ifdef _SC_PII
Victor Stinnerd6f85422010-05-05 23:33:33 +00007997 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007998#endif
7999#ifdef _SC_PII_INTERNET
Victor Stinnerd6f85422010-05-05 23:33:33 +00008000 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008001#endif
8002#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008003 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
8005#ifdef _SC_PII_INTERNET_STREAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008006 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008007#endif
8008#ifdef _SC_PII_OSI
Victor Stinnerd6f85422010-05-05 23:33:33 +00008009 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008010#endif
8011#ifdef _SC_PII_OSI_CLTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008012 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008013#endif
8014#ifdef _SC_PII_OSI_COTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008015 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008016#endif
8017#ifdef _SC_PII_OSI_M
Victor Stinnerd6f85422010-05-05 23:33:33 +00008018 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008019#endif
8020#ifdef _SC_PII_SOCKET
Victor Stinnerd6f85422010-05-05 23:33:33 +00008021 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008022#endif
8023#ifdef _SC_PII_XTI
Victor Stinnerd6f85422010-05-05 23:33:33 +00008024 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008025#endif
8026#ifdef _SC_POLL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008027 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008028#endif
8029#ifdef _SC_PRIORITIZED_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008030 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008031#endif
8032#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008033 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008034#endif
8035#ifdef _SC_REALTIME_SIGNALS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008036 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008037#endif
8038#ifdef _SC_RE_DUP_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008039 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008040#endif
8041#ifdef _SC_RTSIG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008042 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef _SC_SAVED_IDS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008045 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef _SC_SCHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008048 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
8050#ifdef _SC_SCHAR_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008051 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008052#endif
8053#ifdef _SC_SELECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008054 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008055#endif
8056#ifdef _SC_SEMAPHORES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008057 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008058#endif
8059#ifdef _SC_SEM_NSEMS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008060 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008061#endif
8062#ifdef _SC_SEM_VALUE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008063 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008064#endif
8065#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008066 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008067#endif
8068#ifdef _SC_SHRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008069 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008070#endif
8071#ifdef _SC_SHRT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008072 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008073#endif
8074#ifdef _SC_SIGQUEUE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008075 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008076#endif
8077#ifdef _SC_SIGRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008078 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008079#endif
8080#ifdef _SC_SIGRT_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008081 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008082#endif
Fred Draked86ed291999-12-15 15:34:33 +00008083#ifdef _SC_SOFTPOWER
Victor Stinnerd6f85422010-05-05 23:33:33 +00008084 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008085#endif
Fred Drakec9680921999-12-13 16:37:25 +00008086#ifdef _SC_SPLIT_CACHE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008087 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef _SC_SSIZE_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008090 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
8092#ifdef _SC_STACK_PROT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008093 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008094#endif
8095#ifdef _SC_STREAM_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008096 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008097#endif
8098#ifdef _SC_SYNCHRONIZED_IO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008099 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008100#endif
8101#ifdef _SC_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008102 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008103#endif
8104#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinnerd6f85422010-05-05 23:33:33 +00008105 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008106#endif
8107#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008108 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008109#endif
8110#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008111 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008112#endif
8113#ifdef _SC_THREAD_KEYS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008114 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008115#endif
8116#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinnerd6f85422010-05-05 23:33:33 +00008117 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
8119#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008120 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008121#endif
8122#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008123 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008126 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
8128#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008129 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008130#endif
8131#ifdef _SC_THREAD_STACK_MIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008132 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
8134#ifdef _SC_THREAD_THREADS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008135 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef _SC_TIMERS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008138 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef _SC_TIMER_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008141 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef _SC_TTY_NAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008144 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
8146#ifdef _SC_TZNAME_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008147 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008148#endif
8149#ifdef _SC_T_IOV_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008150 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008151#endif
8152#ifdef _SC_UCHAR_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008153 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008154#endif
8155#ifdef _SC_UINT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008156 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008157#endif
8158#ifdef _SC_UIO_MAXIOV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008159 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008160#endif
8161#ifdef _SC_ULONG_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008162 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008163#endif
8164#ifdef _SC_USHRT_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008165 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008166#endif
8167#ifdef _SC_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008168 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008169#endif
8170#ifdef _SC_WORD_BIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008171 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008172#endif
8173#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinnerd6f85422010-05-05 23:33:33 +00008174 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008175#endif
8176#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008177 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008178#endif
8179#ifdef _SC_XBS5_LP64_OFF64
Victor Stinnerd6f85422010-05-05 23:33:33 +00008180 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008181#endif
8182#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008183 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008184#endif
8185#ifdef _SC_XOPEN_CRYPT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008186 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008187#endif
8188#ifdef _SC_XOPEN_ENH_I18N
Victor Stinnerd6f85422010-05-05 23:33:33 +00008189 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008190#endif
8191#ifdef _SC_XOPEN_LEGACY
Victor Stinnerd6f85422010-05-05 23:33:33 +00008192 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008193#endif
8194#ifdef _SC_XOPEN_REALTIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008195 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008196#endif
8197#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008198 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008199#endif
8200#ifdef _SC_XOPEN_SHM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008201 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008202#endif
8203#ifdef _SC_XOPEN_UNIX
Victor Stinnerd6f85422010-05-05 23:33:33 +00008204 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008205#endif
8206#ifdef _SC_XOPEN_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008207 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008208#endif
8209#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinnerd6f85422010-05-05 23:33:33 +00008210 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008211#endif
8212#ifdef _SC_XOPEN_XPG2
Victor Stinnerd6f85422010-05-05 23:33:33 +00008213 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008214#endif
8215#ifdef _SC_XOPEN_XPG3
Victor Stinnerd6f85422010-05-05 23:33:33 +00008216 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008217#endif
8218#ifdef _SC_XOPEN_XPG4
Victor Stinnerd6f85422010-05-05 23:33:33 +00008219 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008220#endif
8221};
8222
8223static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008224conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008225{
8226 return conv_confname(arg, valuep, posix_constants_sysconf,
8227 sizeof(posix_constants_sysconf)
8228 / sizeof(struct constdef));
8229}
8230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008231PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008232"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008233Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008234
8235static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008236posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008237{
8238 PyObject *result = NULL;
8239 int name;
8240
8241 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
Victor Stinner862490a2010-05-06 00:03:44 +00008242 int value;
Fred Drakec9680921999-12-13 16:37:25 +00008243
Victor Stinner862490a2010-05-06 00:03:44 +00008244 errno = 0;
8245 value = sysconf(name);
8246 if (value == -1 && errno != 0)
8247 posix_error();
8248 else
8249 result = PyInt_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008250 }
8251 return result;
8252}
8253#endif
8254
8255
Fred Drakebec628d1999-12-15 18:31:10 +00008256/* This code is used to ensure that the tables of configuration value names
8257 * are in sorted order as required by conv_confname(), and also to build the
8258 * the exported dictionaries that are used to publish information about the
8259 * names available on the host platform.
8260 *
8261 * Sorting the table at runtime ensures that the table is properly ordered
8262 * when used, even for platforms we're not able to test on. It also makes
8263 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008264 */
Fred Drakebec628d1999-12-15 18:31:10 +00008265
8266static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008267cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008268{
8269 const struct constdef *c1 =
Victor Stinnerd6f85422010-05-05 23:33:33 +00008270 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008271 const struct constdef *c2 =
Victor Stinnerd6f85422010-05-05 23:33:33 +00008272 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008273
8274 return strcmp(c1->name, c2->name);
8275}
8276
8277static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008278setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinnerd6f85422010-05-05 23:33:33 +00008279 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008280{
Fred Drakebec628d1999-12-15 18:31:10 +00008281 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008282 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008283
8284 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8285 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008286 if (d == NULL)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008287 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008288
Barry Warsaw3155db32000-04-13 15:20:40 +00008289 for (i=0; i < tablesize; ++i) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008290 PyObject *o = PyInt_FromLong(table[i].value);
8291 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8292 Py_XDECREF(o);
8293 Py_DECREF(d);
8294 return -1;
8295 }
8296 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008297 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008298 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008299}
8300
Fred Drakebec628d1999-12-15 18:31:10 +00008301/* Return -1 on failure, 0 on success. */
8302static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008303setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008304{
8305#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008306 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008307 sizeof(posix_constants_pathconf)
8308 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008309 "pathconf_names", module))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008310 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008311#endif
8312#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008313 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008314 sizeof(posix_constants_confstr)
8315 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008316 "confstr_names", module))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008317 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008318#endif
8319#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008320 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008321 sizeof(posix_constants_sysconf)
8322 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008323 "sysconf_names", module))
Victor Stinnerd6f85422010-05-05 23:33:33 +00008324 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008325#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008326 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008327}
Fred Draked86ed291999-12-15 15:34:33 +00008328
8329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008330PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008331"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008332Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008333in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008334
8335static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008336posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008337{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008338 abort();
8339 /*NOTREACHED*/
8340 Py_FatalError("abort() called from Python code didn't abort!");
8341 return NULL;
8342}
Fred Drakebec628d1999-12-15 18:31:10 +00008343
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008344#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008345PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008346"startfile(filepath [, operation]) - Start a file with its associated\n\
8347application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008348\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008349When \"operation\" is not specified or \"open\", this acts like\n\
8350double-clicking the file in Explorer, or giving the file name as an\n\
8351argument to the DOS \"start\" command: the file is opened with whatever\n\
8352application (if any) its extension is associated.\n\
8353When another \"operation\" is given, it specifies what should be done with\n\
8354the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008355\n\
8356startfile returns as soon as the associated application is launched.\n\
8357There is no option to wait for the application to close, and no way\n\
8358to retrieve the application's exit status.\n\
8359\n\
8360The filepath is relative to the current directory. If you want to use\n\
8361an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008362the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008363
8364static PyObject *
8365win32_startfile(PyObject *self, PyObject *args)
8366{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008367 char *filepath;
8368 char *operation = NULL;
8369 HINSTANCE rc;
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00008370
Victor Stinnerd6f85422010-05-05 23:33:33 +00008371 PyObject *unipath, *woperation = NULL;
8372 if (!PyArg_ParseTuple(args, "U|s:startfile",
8373 &unipath, &operation)) {
8374 PyErr_Clear();
8375 goto normal;
8376 }
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00008377
Victor Stinnerd6f85422010-05-05 23:33:33 +00008378 if (operation) {
8379 woperation = PyUnicode_DecodeASCII(operation,
8380 strlen(operation), NULL);
8381 if (!woperation) {
8382 PyErr_Clear();
8383 operation = NULL;
8384 goto normal;
8385 }
8386 }
Hirokazu Yamamotoa3c56092009-06-28 10:23:00 +00008387
Victor Stinnerd6f85422010-05-05 23:33:33 +00008388 Py_BEGIN_ALLOW_THREADS
8389 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8390 PyUnicode_AS_UNICODE(unipath),
8391 NULL, NULL, SW_SHOWNORMAL);
8392 Py_END_ALLOW_THREADS
8393
8394 Py_XDECREF(woperation);
8395 if (rc <= (HINSTANCE)32) {
8396 PyObject *errval = win32_error_unicode("startfile",
8397 PyUnicode_AS_UNICODE(unipath));
8398 return errval;
8399 }
8400 Py_INCREF(Py_None);
8401 return Py_None;
Georg Brandlad89dc82006-04-03 12:26:26 +00008402
8403normal:
Victor Stinnerd6f85422010-05-05 23:33:33 +00008404 if (!PyArg_ParseTuple(args, "et|s:startfile",
8405 Py_FileSystemDefaultEncoding, &filepath,
8406 &operation))
8407 return NULL;
8408 Py_BEGIN_ALLOW_THREADS
8409 rc = ShellExecute((HWND)0, operation, filepath,
8410 NULL, NULL, SW_SHOWNORMAL);
8411 Py_END_ALLOW_THREADS
8412 if (rc <= (HINSTANCE)32) {
8413 PyObject *errval = win32_error("startfile", filepath);
8414 PyMem_Free(filepath);
8415 return errval;
8416 }
8417 PyMem_Free(filepath);
8418 Py_INCREF(Py_None);
8419 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008420}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +00008421#endif /* MS_WINDOWS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008422
Martin v. Löwis438b5342002-12-27 10:16:42 +00008423#ifdef HAVE_GETLOADAVG
8424PyDoc_STRVAR(posix_getloadavg__doc__,
8425"getloadavg() -> (float, float, float)\n\n\
8426Return the number of processes in the system run queue averaged over\n\
8427the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8428was unobtainable");
8429
8430static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008431posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008432{
8433 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008434 if (getloadavg(loadavg, 3)!=3) {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008435 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8436 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008437 } else
Victor Stinnerd6f85422010-05-05 23:33:33 +00008438 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008439}
8440#endif
8441
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008442#ifdef MS_WINDOWS
8443
8444PyDoc_STRVAR(win32_urandom__doc__,
8445"urandom(n) -> str\n\n\
8446Return a string of n random bytes suitable for cryptographic use.");
8447
8448typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8449 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8450 DWORD dwFlags );
8451typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8452 BYTE *pbBuffer );
8453
8454static CRYPTGENRANDOM pCryptGenRandom = NULL;
Martin v. Löwisaf699dd2007-09-04 09:51:57 +00008455/* This handle is never explicitly released. Instead, the operating
8456 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008457static HCRYPTPROV hCryptProv = 0;
8458
Tim Peters4ad82172004-08-30 17:02:04 +00008459static PyObject*
8460win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008461{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008462 int howMany;
8463 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008464
Victor Stinnerd6f85422010-05-05 23:33:33 +00008465 /* Read arguments */
8466 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8467 return NULL;
8468 if (howMany < 0)
8469 return PyErr_Format(PyExc_ValueError,
8470 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008471
Victor Stinnerd6f85422010-05-05 23:33:33 +00008472 if (hCryptProv == 0) {
8473 HINSTANCE hAdvAPI32 = NULL;
8474 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008475
Victor Stinnerd6f85422010-05-05 23:33:33 +00008476 /* Obtain handle to the DLL containing CryptoAPI
8477 This should not fail */
8478 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8479 if(hAdvAPI32 == NULL)
8480 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008481
Victor Stinnerd6f85422010-05-05 23:33:33 +00008482 /* Obtain pointers to the CryptoAPI functions
8483 This will fail on some early versions of Win95 */
8484 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8485 hAdvAPI32,
8486 "CryptAcquireContextA");
8487 if (pCryptAcquireContext == NULL)
8488 return PyErr_Format(PyExc_NotImplementedError,
8489 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008490
Victor Stinnerd6f85422010-05-05 23:33:33 +00008491 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8492 hAdvAPI32, "CryptGenRandom");
8493 if (pCryptGenRandom == NULL)
8494 return PyErr_Format(PyExc_NotImplementedError,
8495 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008496
Victor Stinnerd6f85422010-05-05 23:33:33 +00008497 /* Acquire context */
8498 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8499 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8500 return win32_error("CryptAcquireContext", NULL);
8501 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008502
Victor Stinnerd6f85422010-05-05 23:33:33 +00008503 /* Allocate bytes */
8504 result = PyString_FromStringAndSize(NULL, howMany);
8505 if (result != NULL) {
8506 /* Get random data */
8507 memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
8508 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8509 PyString_AS_STRING(result))) {
8510 Py_DECREF(result);
8511 return win32_error("CryptGenRandom", NULL);
8512 }
8513 }
8514 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008515}
8516#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008517
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008518#ifdef __VMS
8519/* Use openssl random routine */
8520#include <openssl/rand.h>
8521PyDoc_STRVAR(vms_urandom__doc__,
8522"urandom(n) -> str\n\n\
8523Return a string of n random bytes suitable for cryptographic use.");
8524
8525static PyObject*
8526vms_urandom(PyObject *self, PyObject *args)
8527{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008528 int howMany;
8529 PyObject* result;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008530
Victor Stinnerd6f85422010-05-05 23:33:33 +00008531 /* Read arguments */
8532 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8533 return NULL;
8534 if (howMany < 0)
8535 return PyErr_Format(PyExc_ValueError,
8536 "negative argument not allowed");
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008537
Victor Stinnerd6f85422010-05-05 23:33:33 +00008538 /* Allocate bytes */
8539 result = PyString_FromStringAndSize(NULL, howMany);
8540 if (result != NULL) {
8541 /* Get random data */
8542 if (RAND_pseudo_bytes((unsigned char*)
8543 PyString_AS_STRING(result),
8544 howMany) < 0) {
8545 Py_DECREF(result);
8546 return PyErr_Format(PyExc_ValueError,
8547 "RAND_pseudo_bytes");
8548 }
8549 }
8550 return result;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008551}
8552#endif
8553
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008554#ifdef HAVE_SETRESUID
8555PyDoc_STRVAR(posix_setresuid__doc__,
8556"setresuid(ruid, euid, suid)\n\n\
8557Set the current process's real, effective, and saved user ids.");
8558
8559static PyObject*
8560posix_setresuid (PyObject *self, PyObject *args)
8561{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008562 /* We assume uid_t is no larger than a long. */
8563 long ruid, euid, suid;
8564 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
8565 return NULL;
8566 if (setresuid(ruid, euid, suid) < 0)
8567 return posix_error();
8568 Py_RETURN_NONE;
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008569}
8570#endif
8571
8572#ifdef HAVE_SETRESGID
8573PyDoc_STRVAR(posix_setresgid__doc__,
8574"setresgid(rgid, egid, sgid)\n\n\
8575Set the current process's real, effective, and saved group ids.");
8576
8577static PyObject*
8578posix_setresgid (PyObject *self, PyObject *args)
8579{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008580 /* We assume uid_t is no larger than a long. */
8581 long rgid, egid, sgid;
8582 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
8583 return NULL;
8584 if (setresgid(rgid, egid, sgid) < 0)
8585 return posix_error();
8586 Py_RETURN_NONE;
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008587}
8588#endif
8589
8590#ifdef HAVE_GETRESUID
8591PyDoc_STRVAR(posix_getresuid__doc__,
8592"getresuid() -> (ruid, euid, suid)\n\n\
8593Get tuple of the current process's real, effective, and saved user ids.");
8594
8595static PyObject*
8596posix_getresuid (PyObject *self, PyObject *noargs)
8597{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008598 uid_t ruid, euid, suid;
8599 long l_ruid, l_euid, l_suid;
8600 if (getresuid(&ruid, &euid, &suid) < 0)
8601 return posix_error();
8602 /* Force the values into long's as we don't know the size of uid_t. */
8603 l_ruid = ruid;
8604 l_euid = euid;
8605 l_suid = suid;
8606 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008607}
8608#endif
8609
8610#ifdef HAVE_GETRESGID
8611PyDoc_STRVAR(posix_getresgid__doc__,
8612"getresgid() -> (rgid, egid, sgid)\n\n\
8613Get tuple of the current process's real, effective, and saved user ids.");
8614
8615static PyObject*
8616posix_getresgid (PyObject *self, PyObject *noargs)
8617{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008618 uid_t rgid, egid, sgid;
8619 long l_rgid, l_egid, l_sgid;
8620 if (getresgid(&rgid, &egid, &sgid) < 0)
8621 return posix_error();
8622 /* Force the values into long's as we don't know the size of uid_t. */
8623 l_rgid = rgid;
8624 l_egid = egid;
8625 l_sgid = sgid;
8626 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008627}
8628#endif
8629
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008630static PyMethodDef posix_methods[] = {
Victor Stinnerd6f85422010-05-05 23:33:33 +00008631 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008632#ifdef HAVE_TTYNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008633 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008634#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008635 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008636#ifdef HAVE_CHFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008637 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008638#endif /* HAVE_CHFLAGS */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008639 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008640#ifdef HAVE_FCHMOD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008641 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008642#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008643#ifdef HAVE_CHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008644 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008645#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008646#ifdef HAVE_LCHMOD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008647 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008648#endif /* HAVE_LCHMOD */
8649#ifdef HAVE_FCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008650 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008651#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008652#ifdef HAVE_LCHFLAGS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008653 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008654#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008655#ifdef HAVE_LCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008656 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008657#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008658#ifdef HAVE_CHROOT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008659 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008660#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008661#ifdef HAVE_CTERMID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008662 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008663#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008664#ifdef HAVE_GETCWD
Victor Stinnerd6f85422010-05-05 23:33:33 +00008665 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008666#ifdef Py_USING_UNICODE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008667 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008668#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008669#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008670#ifdef HAVE_LINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008671 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008672#endif /* HAVE_LINK */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008673 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8674 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8675 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008676#ifdef HAVE_NICE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008677 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008678#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008679#ifdef HAVE_READLINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008680 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008681#endif /* HAVE_READLINK */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008682 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8683 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8684 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8685 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008686#ifdef HAVE_SYMLINK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008687 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008688#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008689#ifdef HAVE_SYSTEM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008690 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008691#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008692 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008693#ifdef HAVE_UNAME
Victor Stinnerd6f85422010-05-05 23:33:33 +00008694 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008695#endif /* HAVE_UNAME */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008696 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8697 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8698 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008699#ifdef HAVE_TIMES
Victor Stinnerd6f85422010-05-05 23:33:33 +00008700 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008701#endif /* HAVE_TIMES */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008702 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008703#ifdef HAVE_EXECV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008704 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8705 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008706#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008707#ifdef HAVE_SPAWNV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008708 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8709 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008710#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008711 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8712 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008713#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008714#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008715#ifdef HAVE_FORK1
Victor Stinnerd6f85422010-05-05 23:33:33 +00008716 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008717#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008718#ifdef HAVE_FORK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008719 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008720#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008721#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008722 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008723#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008724#ifdef HAVE_FORKPTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00008725 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008726#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008727#ifdef HAVE_GETEGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008728 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008729#endif /* HAVE_GETEGID */
8730#ifdef HAVE_GETEUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008731 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008732#endif /* HAVE_GETEUID */
8733#ifdef HAVE_GETGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008734 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008735#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008736#ifdef HAVE_GETGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008737 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008738#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008739 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008740#ifdef HAVE_GETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008741 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008742#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008743#ifdef HAVE_GETPPID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008744 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008745#endif /* HAVE_GETPPID */
8746#ifdef HAVE_GETUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008747 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008748#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008749#ifdef HAVE_GETLOGIN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008750 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008751#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008752#ifdef HAVE_KILL
Victor Stinnerd6f85422010-05-05 23:33:33 +00008753 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008754#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008755#ifdef HAVE_KILLPG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008756 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008757#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008758#ifdef HAVE_PLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00008759 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008760#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008761#ifdef HAVE_POPEN
Victor Stinnerd6f85422010-05-05 23:33:33 +00008762 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008763#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008764 {"popen2", win32_popen2, METH_VARARGS},
8765 {"popen3", win32_popen3, METH_VARARGS},
8766 {"popen4", win32_popen4, METH_VARARGS},
8767 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8768 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008769#else
8770#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008771 {"popen2", os2emx_popen2, METH_VARARGS},
8772 {"popen3", os2emx_popen3, METH_VARARGS},
8773 {"popen4", os2emx_popen4, METH_VARARGS},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008774#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008775#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008776#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008777#ifdef HAVE_SETUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008778 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008779#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008780#ifdef HAVE_SETEUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008781 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008782#endif /* HAVE_SETEUID */
8783#ifdef HAVE_SETEGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008784 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008785#endif /* HAVE_SETEGID */
8786#ifdef HAVE_SETREUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008787 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008788#endif /* HAVE_SETREUID */
8789#ifdef HAVE_SETREGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008790 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008791#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008792#ifdef HAVE_SETGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008793 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008794#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008795#ifdef HAVE_SETGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008796 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008797#endif /* HAVE_SETGROUPS */
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008798#ifdef HAVE_INITGROUPS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008799 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008800#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008801#ifdef HAVE_GETPGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008802 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008803#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008804#ifdef HAVE_SETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008805 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008806#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008807#ifdef HAVE_WAIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00008808 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008809#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008810#ifdef HAVE_WAIT3
Victor Stinnerd6f85422010-05-05 23:33:33 +00008811 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008812#endif /* HAVE_WAIT3 */
8813#ifdef HAVE_WAIT4
Victor Stinnerd6f85422010-05-05 23:33:33 +00008814 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Neal Norwitz05a45592006-03-20 06:30:08 +00008815#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008816#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008817 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008818#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008819#ifdef HAVE_GETSID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008820 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008821#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008822#ifdef HAVE_SETSID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008823 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008824#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008825#ifdef HAVE_SETPGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008826 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008827#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008828#ifdef HAVE_TCGETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008829 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008830#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008831#ifdef HAVE_TCSETPGRP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008832 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008833#endif /* HAVE_TCSETPGRP */
Victor Stinnerd6f85422010-05-05 23:33:33 +00008834 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8835 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8836 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8837 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8838 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8839 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8840 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8841 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8842 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8843 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8844 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008845#ifdef HAVE_PIPE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008846 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008847#endif
8848#ifdef HAVE_MKFIFO
Victor Stinnerd6f85422010-05-05 23:33:33 +00008849 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008850#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008851#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008852 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008853#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008854#ifdef HAVE_DEVICE_MACROS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008855 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8856 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8857 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008858#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008859#ifdef HAVE_FTRUNCATE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008860 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008861#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008862#ifdef HAVE_PUTENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008863 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008864#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008865#ifdef HAVE_UNSETENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00008866 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008867#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008868 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008869#ifdef HAVE_FCHDIR
Victor Stinnerd6f85422010-05-05 23:33:33 +00008870 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008871#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008872#ifdef HAVE_FSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008873 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008874#endif
8875#ifdef HAVE_FDATASYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00008876 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008877#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008878#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008879#ifdef WCOREDUMP
Victor Stinnerd6f85422010-05-05 23:33:33 +00008880 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008881#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008882#ifdef WIFCONTINUED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008883 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008884#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008885#ifdef WIFSTOPPED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008886 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008887#endif /* WIFSTOPPED */
8888#ifdef WIFSIGNALED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008889 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008890#endif /* WIFSIGNALED */
8891#ifdef WIFEXITED
Victor Stinnerd6f85422010-05-05 23:33:33 +00008892 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008893#endif /* WIFEXITED */
8894#ifdef WEXITSTATUS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008895 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008896#endif /* WEXITSTATUS */
8897#ifdef WTERMSIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008898 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008899#endif /* WTERMSIG */
8900#ifdef WSTOPSIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008901 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008902#endif /* WSTOPSIG */
8903#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008904#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008905 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008906#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008907#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinnerd6f85422010-05-05 23:33:33 +00008908 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008909#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008910#ifdef HAVE_TMPFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00008911 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008912#endif
8913#ifdef HAVE_TEMPNAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008914 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008915#endif
8916#ifdef HAVE_TMPNAM
Victor Stinnerd6f85422010-05-05 23:33:33 +00008917 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008918#endif
Fred Drakec9680921999-12-13 16:37:25 +00008919#ifdef HAVE_CONFSTR
Victor Stinnerd6f85422010-05-05 23:33:33 +00008920 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008921#endif
8922#ifdef HAVE_SYSCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008923 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008924#endif
8925#ifdef HAVE_FPATHCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008926 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008927#endif
8928#ifdef HAVE_PATHCONF
Victor Stinnerd6f85422010-05-05 23:33:33 +00008929 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008930#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00008931 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008932#ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008933 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008934#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008935#ifdef HAVE_GETLOADAVG
Victor Stinnerd6f85422010-05-05 23:33:33 +00008936 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008937#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008938 #ifdef MS_WINDOWS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008939 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008940 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008941 #ifdef __VMS
Victor Stinnerd6f85422010-05-05 23:33:33 +00008942 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008943 #endif
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008944#ifdef HAVE_SETRESUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008945 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008946#endif
8947#ifdef HAVE_SETRESGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008948 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008949#endif
8950#ifdef HAVE_GETRESUID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008951 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008952#endif
8953#ifdef HAVE_GETRESGID
Victor Stinnerd6f85422010-05-05 23:33:33 +00008954 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis50ea4562009-11-27 13:56:01 +00008955#endif
8956
Victor Stinnerd6f85422010-05-05 23:33:33 +00008957 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008958};
8959
8960
Barry Warsaw4a342091996-12-19 23:50:02 +00008961static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008962ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008963{
Victor Stinnerd6f85422010-05-05 23:33:33 +00008964 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008965}
8966
Guido van Rossumd48f2521997-12-05 22:19:34 +00008967#if defined(PYOS_OS2)
8968/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008969static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008970{
8971 APIRET rc;
8972 ULONG values[QSV_MAX+1];
8973 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008974 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008975
8976 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008977 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008978 Py_END_ALLOW_THREADS
8979
8980 if (rc != NO_ERROR) {
8981 os2_error(rc);
8982 return -1;
8983 }
8984
Fred Drake4d1e64b2002-04-15 19:40:07 +00008985 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8986 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8987 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8988 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8989 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8990 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8991 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008992
8993 switch (values[QSV_VERSION_MINOR]) {
8994 case 0: ver = "2.00"; break;
8995 case 10: ver = "2.10"; break;
8996 case 11: ver = "2.11"; break;
8997 case 30: ver = "3.00"; break;
8998 case 40: ver = "4.00"; break;
8999 case 50: ver = "5.00"; break;
9000 default:
Tim Peters885d4572001-11-28 20:27:42 +00009001 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinnerd6f85422010-05-05 23:33:33 +00009002 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009003 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009004 ver = &tmp[0];
9005 }
9006
9007 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009008 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009009 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009010
9011 /* Add Indicator of Which Drive was Used to Boot the System */
9012 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9013 tmp[1] = ':';
9014 tmp[2] = '\0';
9015
Fred Drake4d1e64b2002-04-15 19:40:07 +00009016 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009017}
9018#endif
9019
Barry Warsaw4a342091996-12-19 23:50:02 +00009020static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009021all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009022{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009023#ifdef F_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009024 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009025#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009026#ifdef R_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009027 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009028#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009029#ifdef W_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009030 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009031#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009032#ifdef X_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009033 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009034#endif
Fred Drakec9680921999-12-13 16:37:25 +00009035#ifdef NGROUPS_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00009036 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009037#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009038#ifdef TMP_MAX
Victor Stinnerd6f85422010-05-05 23:33:33 +00009039 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009040#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009041#ifdef WCONTINUED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009042 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009043#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009044#ifdef WNOHANG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009045 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009046#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009047#ifdef WUNTRACED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009048 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009049#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009050#ifdef O_RDONLY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009051 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009052#endif
9053#ifdef O_WRONLY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009054 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009055#endif
9056#ifdef O_RDWR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009057 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009058#endif
9059#ifdef O_NDELAY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009060 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009061#endif
9062#ifdef O_NONBLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009063 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009064#endif
9065#ifdef O_APPEND
Victor Stinnerd6f85422010-05-05 23:33:33 +00009066 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009067#endif
9068#ifdef O_DSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009069 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009070#endif
9071#ifdef O_RSYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009072 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009073#endif
9074#ifdef O_SYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009075 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009076#endif
9077#ifdef O_NOCTTY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009078 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009079#endif
9080#ifdef O_CREAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009081 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009082#endif
9083#ifdef O_EXCL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009084 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009085#endif
9086#ifdef O_TRUNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009087 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009088#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009089#ifdef O_BINARY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009090 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009091#endif
9092#ifdef O_TEXT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009093 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009094#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009095#ifdef O_LARGEFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009096 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009097#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009098#ifdef O_SHLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009099 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009100#endif
9101#ifdef O_EXLOCK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009102 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009103#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009104
Tim Peters5aa91602002-01-30 05:46:57 +00009105/* MS Windows */
9106#ifdef O_NOINHERIT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009107 /* Don't inherit in child processes. */
9108 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009109#endif
9110#ifdef _O_SHORT_LIVED
Victor Stinnerd6f85422010-05-05 23:33:33 +00009111 /* Optimize for short life (keep in memory). */
9112 /* MS forgot to define this one with a non-underscore form too. */
9113 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009114#endif
9115#ifdef O_TEMPORARY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009116 /* Automatically delete when last handle is closed. */
9117 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009118#endif
9119#ifdef O_RANDOM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009120 /* Optimize for random access. */
9121 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009122#endif
9123#ifdef O_SEQUENTIAL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009124 /* Optimize for sequential access. */
9125 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009126#endif
9127
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009128/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00009129#ifdef O_ASYNC
Victor Stinnerd6f85422010-05-05 23:33:33 +00009130 /* Send a SIGIO signal whenever input or output
9131 becomes available on file descriptor */
9132 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Georg Brandl5049a852008-05-16 13:10:15 +00009133#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009134#ifdef O_DIRECT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009135 /* Direct disk access. */
9136 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009137#endif
9138#ifdef O_DIRECTORY
Victor Stinnerd6f85422010-05-05 23:33:33 +00009139 /* Must be a directory. */
9140 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009141#endif
9142#ifdef O_NOFOLLOW
Victor Stinnerd6f85422010-05-05 23:33:33 +00009143 /* Do not follow links. */
9144 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009145#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00009146#ifdef O_NOATIME
Victor Stinnerd6f85422010-05-05 23:33:33 +00009147 /* Do not update the access time. */
9148 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Georg Brandlb67da6e2007-11-24 13:56:09 +00009149#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009150
Victor Stinnerd6f85422010-05-05 23:33:33 +00009151 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009152#ifdef EX_OK
Victor Stinnerd6f85422010-05-05 23:33:33 +00009153 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009154#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009155#ifdef EX_USAGE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009156 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009157#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009158#ifdef EX_DATAERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009159 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009160#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009161#ifdef EX_NOINPUT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009162 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009163#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009164#ifdef EX_NOUSER
Victor Stinnerd6f85422010-05-05 23:33:33 +00009165 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009166#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009167#ifdef EX_NOHOST
Victor Stinnerd6f85422010-05-05 23:33:33 +00009168 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009169#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009170#ifdef EX_UNAVAILABLE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009171 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009172#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009173#ifdef EX_SOFTWARE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009174 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009175#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009176#ifdef EX_OSERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009177 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009178#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009179#ifdef EX_OSFILE
Victor Stinnerd6f85422010-05-05 23:33:33 +00009180 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009181#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009182#ifdef EX_CANTCREAT
Victor Stinnerd6f85422010-05-05 23:33:33 +00009183 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009184#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009185#ifdef EX_IOERR
Victor Stinnerd6f85422010-05-05 23:33:33 +00009186 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009187#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009188#ifdef EX_TEMPFAIL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009189 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009190#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009191#ifdef EX_PROTOCOL
Victor Stinnerd6f85422010-05-05 23:33:33 +00009192 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009193#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009194#ifdef EX_NOPERM
Victor Stinnerd6f85422010-05-05 23:33:33 +00009195 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009196#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009197#ifdef EX_CONFIG
Victor Stinnerd6f85422010-05-05 23:33:33 +00009198 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009199#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009200#ifdef EX_NOTFOUND
Victor Stinnerd6f85422010-05-05 23:33:33 +00009201 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009202#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009203
Guido van Rossum246bc171999-02-01 23:54:31 +00009204#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009205#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009206 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
9207 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
9208 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
9209 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
9210 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
9211 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
9212 if (ins(d, "P_PM", (long)P_PM)) return -1;
9213 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
9214 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
9215 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
9216 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
9217 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
9218 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
9219 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
9220 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
9221 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
9222 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
9223 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
9224 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
9225 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009226#else
Victor Stinnerd6f85422010-05-05 23:33:33 +00009227 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
9228 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
9229 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
9230 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
9231 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00009232#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009233#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00009234
Guido van Rossumd48f2521997-12-05 22:19:34 +00009235#if defined(PYOS_OS2)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009236 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009237#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009238 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00009239}
9240
9241
Tim Peters5aa91602002-01-30 05:46:57 +00009242#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009243#define INITFUNC initnt
9244#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009245
9246#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009247#define INITFUNC initos2
9248#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009249
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009250#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009251#define INITFUNC initposix
9252#define MODNAME "posix"
9253#endif
9254
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009255PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009256INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009257{
Victor Stinnerd6f85422010-05-05 23:33:33 +00009258 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009259
Victor Stinnerd6f85422010-05-05 23:33:33 +00009260 m = Py_InitModule3(MODNAME,
9261 posix_methods,
9262 posix__doc__);
9263 if (m == NULL)
9264 return;
Tim Peters5aa91602002-01-30 05:46:57 +00009265
Victor Stinnerd6f85422010-05-05 23:33:33 +00009266 /* Initialize environ dictionary */
9267 v = convertenviron();
9268 Py_XINCREF(v);
9269 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9270 return;
9271 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009272
Victor Stinnerd6f85422010-05-05 23:33:33 +00009273 if (all_ins(m))
9274 return;
Barry Warsaw4a342091996-12-19 23:50:02 +00009275
Victor Stinnerd6f85422010-05-05 23:33:33 +00009276 if (setup_confname_tables(m))
9277 return;
Fred Drakebec628d1999-12-15 18:31:10 +00009278
Victor Stinnerd6f85422010-05-05 23:33:33 +00009279 Py_INCREF(PyExc_OSError);
9280 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009281
Guido van Rossumb3d39562000-01-31 18:41:26 +00009282#ifdef HAVE_PUTENV
Victor Stinnerd6f85422010-05-05 23:33:33 +00009283 if (posix_putenv_garbage == NULL)
9284 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009285#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009286
Victor Stinnerd6f85422010-05-05 23:33:33 +00009287 if (!initialized) {
9288 stat_result_desc.name = MODNAME ".stat_result";
9289 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9290 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9291 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9292 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9293 structseq_new = StatResultType.tp_new;
9294 StatResultType.tp_new = statresult_new;
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00009295
Victor Stinnerd6f85422010-05-05 23:33:33 +00009296 statvfs_result_desc.name = MODNAME ".statvfs_result";
9297 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis03824e42008-12-29 18:17:34 +00009298#ifdef NEED_TICKS_PER_SECOND
9299# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009300 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis03824e42008-12-29 18:17:34 +00009301# elif defined(HZ)
Victor Stinnerd6f85422010-05-05 23:33:33 +00009302 ticks_per_second = HZ;
Martin v. Löwis03824e42008-12-29 18:17:34 +00009303# else
Victor Stinnerd6f85422010-05-05 23:33:33 +00009304 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis03824e42008-12-29 18:17:34 +00009305# endif
9306#endif
Victor Stinnerd6f85422010-05-05 23:33:33 +00009307 }
9308 Py_INCREF((PyObject*) &StatResultType);
9309 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9310 Py_INCREF((PyObject*) &StatVFSResultType);
9311 PyModule_AddObject(m, "statvfs_result",
9312 (PyObject*) &StatVFSResultType);
9313 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009314
9315#ifdef __APPLE__
Victor Stinnerd6f85422010-05-05 23:33:33 +00009316 /*
9317 * Step 2 of weak-linking support on Mac OS X.
9318 *
9319 * The code below removes functions that are not available on the
9320 * currently active platform.
9321 *
9322 * This block allow one to use a python binary that was build on
9323 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9324 * OSX 10.4.
9325 */
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009326#ifdef HAVE_FSTATVFS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009327 if (fstatvfs == NULL) {
9328 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9329 return;
9330 }
9331 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009332#endif /* HAVE_FSTATVFS */
9333
9334#ifdef HAVE_STATVFS
Victor Stinnerd6f85422010-05-05 23:33:33 +00009335 if (statvfs == NULL) {
9336 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9337 return;
9338 }
9339 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009340#endif /* HAVE_STATVFS */
9341
9342# ifdef HAVE_LCHOWN
Victor Stinnerd6f85422010-05-05 23:33:33 +00009343 if (lchown == NULL) {
9344 if (PyObject_DelAttrString(m, "lchown") == -1) {
9345 return;
9346 }
9347 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +00009348#endif /* HAVE_LCHOWN */
9349
9350
9351#endif /* __APPLE__ */
9352
Guido van Rossumb6775db1994-08-01 11:34:53 +00009353}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009354
9355#ifdef __cplusplus
9356}
9357#endif
9358
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009359