blob: 22b637ee7aa5c7b52f08d39e76cf57e631ae761b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
72#include <sys/wait.h> /* For WNOHANG */
73#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +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
107#define HAVE_SYSTEM 1
108#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
114#ifdef __BORLANDC__ /* Borland compiler */
115#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
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#else
122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000128#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define HAVE_FSYNC 1
130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#else /* all other compilers */
135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_SYSTEM 1
151#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000189/*#ifdef HAVE_FCHMOD
190extern int fchmod(int, mode_t);
191#endif*/
192/*#ifdef HAVE_LCHMOD
193extern int lchmod(const char *, mode_t);
194#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int chown(const char *, uid_t, gid_t);
196extern char *getcwd(char *, int);
197extern char *strerror(int);
198extern int link(const char *, const char *);
199extern int rename(const char *, const char *);
200extern int stat(const char *, struct stat *);
201extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000266#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000268#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
Guido van Rossumd48f2521997-12-05 22:19:34 +0000271#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000276#if defined(PATH_MAX) && PATH_MAX > 1024
277#define MAXPATHLEN PATH_MAX
278#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000279#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000281#endif /* MAXPATHLEN */
282
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000283#ifdef UNION_WAIT
284/* Emulate some macros on systems that have a union instead of macros */
285
286#ifndef WIFEXITED
287#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
288#endif
289
290#ifndef WEXITSTATUS
291#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
292#endif
293
294#ifndef WTERMSIG
295#define WTERMSIG(u_wait) ((u_wait).w_termsig)
296#endif
297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298#define WAIT_TYPE union wait
299#define WAIT_STATUS_INT(s) (s.w_status)
300
301#else /* !UNION_WAIT */
302#define WAIT_TYPE int
303#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000304#endif /* UNION_WAIT */
305
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000306/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou7852c422009-05-24 11:58:35 +0000307#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000308#define PARSE_PID "i"
309#define PyLong_FromPid PyLong_FromLong
310#define PyLong_AsPid PyLong_AsLong
311#elif SIZEOF_PID_T == SIZEOF_LONG
312#define PARSE_PID "l"
313#define PyLong_FromPid PyLong_FromLong
314#define PyLong_AsPid PyLong_AsLong
315#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
316#define PARSE_PID "L"
317#define PyLong_FromPid PyLong_FromLongLong
318#define PyLong_AsPid PyLong_AsLongLong
319#else
320#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000321#endif /* SIZEOF_PID_T */
322
Greg Wardb48bc172000-03-01 21:51:56 +0000323/* Don't use the "_r" form if we don't need it (also, won't have a
324 prototype for it, at least on Solaris -- maybe others as well?). */
325#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
326#define USE_CTERMID_R
327#endif
328
Fred Drake699f3522000-06-29 21:12:41 +0000329/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000330#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000331#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000332# define STAT win32_stat
333# define FSTAT win32_fstat
334# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000335#else
336# define STAT stat
337# define FSTAT fstat
338# define STRUCT_STAT struct stat
339#endif
340
Tim Peters11b23062003-04-23 02:39:17 +0000341#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000342#include <sys/mkdev.h>
343#else
344#if defined(MAJOR_IN_SYSMACROS)
345#include <sys/sysmacros.h>
346#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000347#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
348#include <sys/mkdev.h>
349#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000350#endif
Fred Drake699f3522000-06-29 21:12:41 +0000351
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000352#if defined _MSC_VER && _MSC_VER >= 1400
353/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
354 * valid and throw an assertion if it isn't.
355 * Normally, an invalid fd is likely to be a C program error and therefore
356 * an assertion can be useful, but it does contradict the POSIX standard
357 * which for write(2) states:
358 * "Otherwise, -1 shall be returned and errno set to indicate the error."
359 * "[EBADF] The fildes argument is not a valid file descriptor open for
360 * writing."
361 * Furthermore, python allows the user to enter any old integer
362 * as a fd and should merely raise a python exception on error.
363 * The Microsoft CRT doesn't provide an official way to check for the
364 * validity of a file descriptor, but we can emulate its internal behaviour
365 * by using the exported __pinfo data member and knowledge of the
366 * internal structures involved.
367 * The structures below must be updated for each version of visual studio
368 * according to the file internal.h in the CRT source, until MS comes
369 * up with a less hacky way to do this.
370 * (all of this is to avoid globally modifying the CRT behaviour using
371 * _set_invalid_parameter_handler() and _CrtSetReportMode())
372 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000373/* The actual size of the structure is determined at runtime.
374 * Only the first items must be present.
375 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000376typedef struct {
377 intptr_t osfhnd;
378 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000381extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000382#define IOINFO_L2E 5
383#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
384#define IOINFO_ARRAYS 64
385#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
386#define FOPEN 0x01
387#define _NO_CONSOLE_FILENO (intptr_t)-2
388
389/* This function emulates what the windows CRT does to validate file handles */
390int
391_PyVerify_fd(int fd)
392{
393 const int i1 = fd >> IOINFO_L2E;
394 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000395
396 static int sizeof_ioinfo = 0;
397
398 /* Determine the actual size of the ioinfo structure,
399 * as used by the CRT loaded in memory
400 */
401 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
402 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
403 }
404 if (sizeof_ioinfo == 0) {
405 /* This should not happen... */
406 goto fail;
407 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408
409 /* See that it isn't a special CLEAR fileno */
410 if (fd != _NO_CONSOLE_FILENO) {
411 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
412 * we check pointer validity and other info
413 */
414 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
415 /* finally, check that the file is open */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000416 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
417 if (info->osfile & FOPEN) {
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000418 return 1;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000419 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000420 }
421 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000422 fail:
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000423 errno = EBADF;
424 return 0;
425}
426
427/* the special case of checking dup2. The target fd must be in a sensible range */
428static int
429_PyVerify_fd_dup2(int fd1, int fd2)
430{
431 if (!_PyVerify_fd(fd1))
432 return 0;
433 if (fd2 == _NO_CONSOLE_FILENO)
434 return 0;
435 if ((unsigned)fd2 < _NHANDLE_)
436 return 1;
437 else
438 return 0;
439}
440#else
441/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
442#define _PyVerify_fd_dup2(A, B) (1)
443#endif
444
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000446#ifdef WITH_NEXT_FRAMEWORK
447/* On Darwin/MacOSX a shared library or framework has no access to
448** environ directly, we must obtain it with _NSGetEnviron().
449*/
450#include <crt_externs.h>
451static char **environ;
452#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000454#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455
Barry Warsaw53699e91996-12-10 23:23:01 +0000456static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000457convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458{
Barry Warsaw53699e91996-12-10 23:23:01 +0000459 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000460#ifdef MS_WINDOWS
461 wchar_t **e;
462#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000464#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000465 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466 if (d == NULL)
467 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000468#ifdef WITH_NEXT_FRAMEWORK
469 if (environ == NULL)
470 environ = *_NSGetEnviron();
471#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000472#ifdef MS_WINDOWS
473 /* _wenviron must be initialized in this way if the program is started
474 through main() instead of wmain(). */
475 _wgetenv(L"");
476 if (_wenviron == NULL)
477 return d;
478 /* This part ignores errors */
479 for (e = _wenviron; *e != NULL; e++) {
480 PyObject *k;
481 PyObject *v;
482 wchar_t *p = wcschr(*e, L'=');
483 if (p == NULL)
484 continue;
485 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
486 if (k == NULL) {
487 PyErr_Clear();
488 continue;
489 }
490 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
491 if (v == NULL) {
492 PyErr_Clear();
493 Py_DECREF(k);
494 continue;
495 }
496 if (PyDict_GetItem(d, k) == NULL) {
497 if (PyDict_SetItem(d, k, v) != 0)
498 PyErr_Clear();
499 }
500 Py_DECREF(k);
501 Py_DECREF(v);
502 }
503#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000504 if (environ == NULL)
505 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000506 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000508 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000509 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510 char *p = strchr(*e, '=');
511 if (p == NULL)
512 continue;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000513 k = PyUnicode_Decode(*e, (int)(p-*e),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000514 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000515 if (k == NULL) {
516 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000518 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000519 v = PyUnicode_Decode(p+1, strlen(p+1),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000520 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000521 if (v == NULL) {
522 PyErr_Clear();
523 Py_DECREF(k);
524 continue;
525 }
526 if (PyDict_GetItem(d, k) == NULL) {
527 if (PyDict_SetItem(d, k, v) != 0)
528 PyErr_Clear();
529 }
530 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000531 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000533#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000534#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000535 {
536 APIRET rc;
537 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
538
539 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000540 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000541 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000542 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000543 Py_DECREF(v);
544 }
545 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
546 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000547 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000548 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000549 Py_DECREF(v);
550 }
551 }
552#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553 return d;
554}
555
Martin v. Löwis011e8422009-05-05 04:43:17 +0000556/* Convert a bytes object to a char*. Optionally lock the buffer if it is a
557 bytes array. */
558
559static char*
560bytes2str(PyObject* o, int lock)
561{
562 if(PyBytes_Check(o))
563 return PyBytes_AsString(o);
564 else if(PyByteArray_Check(o)) {
565 if (lock && PyObject_GetBuffer(o, NULL, 0) < 0)
566 /* On a bytearray, this should not fail. */
567 PyErr_BadInternalCall();
568 return PyByteArray_AsString(o);
569 } else {
570 /* The FS converter should have verified that this
571 is either bytes or bytearray. */
572 Py_FatalError("bad object passed to bytes2str");
573 /* not reached. */
574 return "";
575 }
576}
577
578/* Release the lock, decref the object. */
579static void
580release_bytes(PyObject* o)
581{
582 if (PyByteArray_Check(o))
583 o->ob_type->tp_as_buffer->bf_releasebuffer(NULL, 0);
584 Py_DECREF(o);
585}
586
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588/* Set a POSIX-specific error from errno, and return NULL */
589
Barry Warsawd58d7641998-07-23 16:14:40 +0000590static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000591posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000592{
Barry Warsawca74da41999-02-09 19:31:45 +0000593 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594}
Barry Warsawd58d7641998-07-23 16:14:40 +0000595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000596posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000597{
Barry Warsawca74da41999-02-09 19:31:45 +0000598 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000599}
600
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000601#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000602static PyObject *
603posix_error_with_unicode_filename(Py_UNICODE* name)
604{
605 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
606}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000607#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000608
609
Mark Hammondef8b6542001-05-13 08:04:26 +0000610static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000611posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000612{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000613 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
614 bytes2str(name, 0));
615 release_bytes(name);
Mark Hammondef8b6542001-05-13 08:04:26 +0000616 return rc;
617}
618
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000619#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000620static PyObject *
621win32_error(char* function, char* filename)
622{
Mark Hammond33a6da92000-08-15 00:46:38 +0000623 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000624 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000625 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000626 Windows error object, which is non-trivial.
627 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000628 errno = GetLastError();
629 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000630 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000631 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000632 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000633}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000634
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000635static PyObject *
636win32_error_unicode(char* function, Py_UNICODE* filename)
637{
638 /* XXX - see win32_error for comments on 'function' */
639 errno = GetLastError();
640 if (filename)
641 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
642 else
643 return PyErr_SetFromWindowsErr(errno);
644}
645
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000647convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000649 if (PyUnicode_CheckExact(*param))
650 Py_INCREF(*param);
651 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000652 /* For a Unicode subtype that's not a Unicode object,
653 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000654 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
655 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000657 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 Py_FileSystemDefaultEncoding,
659 "strict");
660 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000661}
662
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000663#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664
Guido van Rossumd48f2521997-12-05 22:19:34 +0000665#if defined(PYOS_OS2)
666/**********************************************************************
667 * Helper Function to Trim and Format OS/2 Messages
668 **********************************************************************/
669 static void
670os2_formatmsg(char *msgbuf, int msglen, char *reason)
671{
672 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
673
674 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
675 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
676
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000677 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000678 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
679 }
680
681 /* Add Optional Reason Text */
682 if (reason) {
683 strcat(msgbuf, " : ");
684 strcat(msgbuf, reason);
685 }
686}
687
688/**********************************************************************
689 * Decode an OS/2 Operating System Error Code
690 *
691 * A convenience function to lookup an OS/2 error code and return a
692 * text message we can use to raise a Python exception.
693 *
694 * Notes:
695 * The messages for errors returned from the OS/2 kernel reside in
696 * the file OSO001.MSG in the \OS2 directory hierarchy.
697 *
698 **********************************************************************/
699 static char *
700os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
701{
702 APIRET rc;
703 ULONG msglen;
704
705 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
706 Py_BEGIN_ALLOW_THREADS
707 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
708 errorcode, "oso001.msg", &msglen);
709 Py_END_ALLOW_THREADS
710
711 if (rc == NO_ERROR)
712 os2_formatmsg(msgbuf, msglen, reason);
713 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000714 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000715 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000716
717 return msgbuf;
718}
719
720/* Set an OS/2-specific error and return NULL. OS/2 kernel
721 errors are not in a global variable e.g. 'errno' nor are
722 they congruent with posix error numbers. */
723
724static PyObject * os2_error(int code)
725{
726 char text[1024];
727 PyObject *v;
728
729 os2_strerror(text, sizeof(text), code, "");
730
731 v = Py_BuildValue("(is)", code, text);
732 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000733 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000734 Py_DECREF(v);
735 }
736 return NULL; /* Signal to Python that an Exception is Pending */
737}
738
739#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000740
741/* POSIX generic methods */
742
Barry Warsaw53699e91996-12-10 23:23:01 +0000743static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000744posix_fildes(PyObject *fdobj, int (*func)(int))
745{
746 int fd;
747 int res;
748 fd = PyObject_AsFileDescriptor(fdobj);
749 if (fd < 0)
750 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000751 if (!_PyVerify_fd(fd))
752 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000753 Py_BEGIN_ALLOW_THREADS
754 res = (*func)(fd);
755 Py_END_ALLOW_THREADS
756 if (res < 0)
757 return posix_error();
758 Py_INCREF(Py_None);
759 return Py_None;
760}
Guido van Rossum21142a01999-01-08 21:05:37 +0000761
762static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000763posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000765 PyObject *opath1 = NULL;
766 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000767 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000768 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000769 PyUnicode_FSConverter, &opath1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000771 path1 = bytes2str(opath1, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000772 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000773 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000774 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000775 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000776 return posix_error_with_allocated_filename(opath1);
777 release_bytes(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000778 Py_INCREF(Py_None);
779 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780}
781
Barry Warsaw53699e91996-12-10 23:23:01 +0000782static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000783posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000784 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000786{
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000787 PyObject *opath1 = NULL, *opath2 = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000788 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000789 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000790 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000791 PyUnicode_FSConverter, &opath1,
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000792 PyUnicode_FSConverter, &opath2)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000793 return NULL;
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000794 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000795 path1 = bytes2str(opath1, 1);
796 path2 = bytes2str(opath2, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000797 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000798 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000799 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +0000800 release_bytes(opath1);
801 release_bytes(opath2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000802 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000803 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000805 Py_INCREF(Py_None);
806 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000807}
808
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000809#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000810static PyObject*
811win32_1str(PyObject* args, char* func,
812 char* format, BOOL (__stdcall *funcA)(LPCSTR),
813 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
814{
815 PyObject *uni;
816 char *ansi;
817 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000818
819 if (!PyArg_ParseTuple(args, wformat, &uni))
820 PyErr_Clear();
821 else {
822 Py_BEGIN_ALLOW_THREADS
823 result = funcW(PyUnicode_AsUnicode(uni));
824 Py_END_ALLOW_THREADS
825 if (!result)
826 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
827 Py_INCREF(Py_None);
828 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000829 }
830 if (!PyArg_ParseTuple(args, format, &ansi))
831 return NULL;
832 Py_BEGIN_ALLOW_THREADS
833 result = funcA(ansi);
834 Py_END_ALLOW_THREADS
835 if (!result)
836 return win32_error(func, ansi);
837 Py_INCREF(Py_None);
838 return Py_None;
839
840}
841
842/* This is a reimplementation of the C library's chdir function,
843 but one that produces Win32 errors instead of DOS error codes.
844 chdir is essentially a wrapper around SetCurrentDirectory; however,
845 it also needs to set "magic" environment variables indicating
846 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000847static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000848win32_chdir(LPCSTR path)
849{
850 char new_path[MAX_PATH+1];
851 int result;
852 char env[4] = "=x:";
853
854 if(!SetCurrentDirectoryA(path))
855 return FALSE;
856 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
857 if (!result)
858 return FALSE;
859 /* In the ANSI API, there should not be any paths longer
860 than MAX_PATH. */
861 assert(result <= MAX_PATH+1);
862 if (strncmp(new_path, "\\\\", 2) == 0 ||
863 strncmp(new_path, "//", 2) == 0)
864 /* UNC path, nothing to do. */
865 return TRUE;
866 env[1] = new_path[0];
867 return SetEnvironmentVariableA(env, new_path);
868}
869
870/* The Unicode version differs from the ANSI version
871 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000872static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000873win32_wchdir(LPCWSTR path)
874{
875 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
876 int result;
877 wchar_t env[4] = L"=x:";
878
879 if(!SetCurrentDirectoryW(path))
880 return FALSE;
881 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
882 if (!result)
883 return FALSE;
884 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000885 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000886 if (!new_path) {
887 SetLastError(ERROR_OUTOFMEMORY);
888 return FALSE;
889 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000890 result = GetCurrentDirectoryW(result, new_path);
891 if (!result) {
892 free(new_path);
893 return FALSE;
894 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000895 }
896 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
897 wcsncmp(new_path, L"//", 2) == 0)
898 /* UNC path, nothing to do. */
899 return TRUE;
900 env[1] = new_path[0];
901 result = SetEnvironmentVariableW(env, new_path);
902 if (new_path != _new_path)
903 free(new_path);
904 return result;
905}
906#endif
907
Martin v. Löwis14694662006-02-03 12:54:16 +0000908#ifdef MS_WINDOWS
909/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
910 - time stamps are restricted to second resolution
911 - file modification times suffer from forth-and-back conversions between
912 UTC and local time
913 Therefore, we implement our own stat, based on the Win32 API directly.
914*/
915#define HAVE_STAT_NSEC 1
916
917struct win32_stat{
918 int st_dev;
919 __int64 st_ino;
920 unsigned short st_mode;
921 int st_nlink;
922 int st_uid;
923 int st_gid;
924 int st_rdev;
925 __int64 st_size;
926 int st_atime;
927 int st_atime_nsec;
928 int st_mtime;
929 int st_mtime_nsec;
930 int st_ctime;
931 int st_ctime_nsec;
932};
933
934static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
935
936static void
937FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
938{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
940 /* Cannot simply cast and dereference in_ptr,
941 since it might not be aligned properly */
942 __int64 in;
943 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000944 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
945 /* XXX Win32 supports time stamps past 2038; we currently don't */
946 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
947}
948
Thomas Wouters477c8d52006-05-27 19:21:47 +0000949static void
950time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
951{
952 /* XXX endianness */
953 __int64 out;
954 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000955 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 memcpy(out_ptr, &out, sizeof(out));
957}
958
Martin v. Löwis14694662006-02-03 12:54:16 +0000959/* Below, we *know* that ugo+r is 0444 */
960#if _S_IREAD != 0400
961#error Unsupported C library
962#endif
963static int
964attributes_to_mode(DWORD attr)
965{
966 int m = 0;
967 if (attr & FILE_ATTRIBUTE_DIRECTORY)
968 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
969 else
970 m |= _S_IFREG;
971 if (attr & FILE_ATTRIBUTE_READONLY)
972 m |= 0444;
973 else
974 m |= 0666;
975 return m;
976}
977
978static int
979attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
980{
981 memset(result, 0, sizeof(*result));
982 result->st_mode = attributes_to_mode(info->dwFileAttributes);
983 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
984 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
985 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
986 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
987
988 return 0;
989}
990
Guido van Rossumd8faa362007-04-27 19:54:29 +0000991static BOOL
992attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
993{
994 HANDLE hFindFile;
995 WIN32_FIND_DATAA FileData;
996 hFindFile = FindFirstFileA(pszFile, &FileData);
997 if (hFindFile == INVALID_HANDLE_VALUE)
998 return FALSE;
999 FindClose(hFindFile);
1000 pfad->dwFileAttributes = FileData.dwFileAttributes;
1001 pfad->ftCreationTime = FileData.ftCreationTime;
1002 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1003 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1004 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1005 pfad->nFileSizeLow = FileData.nFileSizeLow;
1006 return TRUE;
1007}
1008
1009static BOOL
1010attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1011{
1012 HANDLE hFindFile;
1013 WIN32_FIND_DATAW FileData;
1014 hFindFile = FindFirstFileW(pszFile, &FileData);
1015 if (hFindFile == INVALID_HANDLE_VALUE)
1016 return FALSE;
1017 FindClose(hFindFile);
1018 pfad->dwFileAttributes = FileData.dwFileAttributes;
1019 pfad->ftCreationTime = FileData.ftCreationTime;
1020 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1021 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1022 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1023 pfad->nFileSizeLow = FileData.nFileSizeLow;
1024 return TRUE;
1025}
1026
Martin v. Löwis14694662006-02-03 12:54:16 +00001027static int
1028win32_stat(const char* path, struct win32_stat *result)
1029{
1030 WIN32_FILE_ATTRIBUTE_DATA info;
1031 int code;
1032 char *dot;
Hirokazu Yamamoto6fbdfda2009-06-29 11:37:19 +00001033 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001034 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1035 /* Protocol violation: we explicitly clear errno, instead of
1036 setting it to a POSIX error. Callers should use GetLastError. */
1037 errno = 0;
1038 return -1;
1039 } else {
1040 /* Could not get attributes on open file. Fall back to
1041 reading the directory. */
1042 if (!attributes_from_dir(path, &info)) {
1043 /* Very strange. This should not fail now */
1044 errno = 0;
1045 return -1;
1046 }
1047 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001048 }
1049 code = attribute_data_to_stat(&info, result);
1050 if (code != 0)
1051 return code;
1052 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1053 dot = strrchr(path, '.');
1054 if (dot) {
1055 if (stricmp(dot, ".bat") == 0 ||
1056 stricmp(dot, ".cmd") == 0 ||
1057 stricmp(dot, ".exe") == 0 ||
1058 stricmp(dot, ".com") == 0)
1059 result->st_mode |= 0111;
1060 }
1061 return code;
1062}
1063
1064static int
1065win32_wstat(const wchar_t* path, struct win32_stat *result)
1066{
1067 int code;
1068 const wchar_t *dot;
1069 WIN32_FILE_ATTRIBUTE_DATA info;
Hirokazu Yamamoto6fbdfda2009-06-29 11:37:19 +00001070 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001071 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1072 /* Protocol violation: we explicitly clear errno, instead of
1073 setting it to a POSIX error. Callers should use GetLastError. */
1074 errno = 0;
1075 return -1;
1076 } else {
1077 /* Could not get attributes on open file. Fall back to
1078 reading the directory. */
1079 if (!attributes_from_dir_w(path, &info)) {
1080 /* Very strange. This should not fail now */
1081 errno = 0;
1082 return -1;
1083 }
1084 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001085 }
1086 code = attribute_data_to_stat(&info, result);
1087 if (code < 0)
1088 return code;
1089 /* Set IFEXEC if it is an .exe, .bat, ... */
1090 dot = wcsrchr(path, '.');
1091 if (dot) {
1092 if (_wcsicmp(dot, L".bat") == 0 ||
1093 _wcsicmp(dot, L".cmd") == 0 ||
1094 _wcsicmp(dot, L".exe") == 0 ||
1095 _wcsicmp(dot, L".com") == 0)
1096 result->st_mode |= 0111;
1097 }
1098 return code;
1099}
1100
1101static int
1102win32_fstat(int file_number, struct win32_stat *result)
1103{
1104 BY_HANDLE_FILE_INFORMATION info;
1105 HANDLE h;
1106 int type;
1107
1108 h = (HANDLE)_get_osfhandle(file_number);
1109
1110 /* Protocol violation: we explicitly clear errno, instead of
1111 setting it to a POSIX error. Callers should use GetLastError. */
1112 errno = 0;
1113
1114 if (h == INVALID_HANDLE_VALUE) {
1115 /* This is really a C library error (invalid file handle).
1116 We set the Win32 error to the closes one matching. */
1117 SetLastError(ERROR_INVALID_HANDLE);
1118 return -1;
1119 }
1120 memset(result, 0, sizeof(*result));
1121
1122 type = GetFileType(h);
1123 if (type == FILE_TYPE_UNKNOWN) {
1124 DWORD error = GetLastError();
1125 if (error != 0) {
1126 return -1;
1127 }
1128 /* else: valid but unknown file */
1129 }
1130
1131 if (type != FILE_TYPE_DISK) {
1132 if (type == FILE_TYPE_CHAR)
1133 result->st_mode = _S_IFCHR;
1134 else if (type == FILE_TYPE_PIPE)
1135 result->st_mode = _S_IFIFO;
1136 return 0;
1137 }
1138
1139 if (!GetFileInformationByHandle(h, &info)) {
1140 return -1;
1141 }
1142
1143 /* similar to stat() */
1144 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1145 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1146 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1147 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1148 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1149 /* specific to fstat() */
1150 result->st_nlink = info.nNumberOfLinks;
1151 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1152 return 0;
1153}
1154
1155#endif /* MS_WINDOWS */
1156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001157PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001158"stat_result: Result from stat or lstat.\n\n\
1159This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001160 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001161or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1162\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001163Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1164or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001165\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001166See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001167
1168static PyStructSequence_Field stat_result_fields[] = {
1169 {"st_mode", "protection bits"},
1170 {"st_ino", "inode"},
1171 {"st_dev", "device"},
1172 {"st_nlink", "number of hard links"},
1173 {"st_uid", "user ID of owner"},
1174 {"st_gid", "group ID of owner"},
1175 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001176 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1177 {NULL, "integer time of last access"},
1178 {NULL, "integer time of last modification"},
1179 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001180 {"st_atime", "time of last access"},
1181 {"st_mtime", "time of last modification"},
1182 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001183#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001184 {"st_blksize", "blocksize for filesystem I/O"},
1185#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001186#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001187 {"st_blocks", "number of blocks allocated"},
1188#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001189#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001190 {"st_rdev", "device type (if inode device)"},
1191#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001192#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1193 {"st_flags", "user defined flags for file"},
1194#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001195#ifdef HAVE_STRUCT_STAT_ST_GEN
1196 {"st_gen", "generation number"},
1197#endif
1198#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1199 {"st_birthtime", "time of creation"},
1200#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001201 {0}
1202};
1203
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001204#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001205#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001206#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001207#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001208#endif
1209
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001210#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001211#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1212#else
1213#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1214#endif
1215
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001216#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001217#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1218#else
1219#define ST_RDEV_IDX ST_BLOCKS_IDX
1220#endif
1221
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001222#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1223#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1224#else
1225#define ST_FLAGS_IDX ST_RDEV_IDX
1226#endif
1227
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001228#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001229#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001230#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001231#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001232#endif
1233
1234#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1235#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1236#else
1237#define ST_BIRTHTIME_IDX ST_GEN_IDX
1238#endif
1239
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001240static PyStructSequence_Desc stat_result_desc = {
1241 "stat_result", /* name */
1242 stat_result__doc__, /* doc */
1243 stat_result_fields,
1244 10
1245};
1246
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001247PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001248"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1249This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001250 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001251or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001252\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001253See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001254
1255static PyStructSequence_Field statvfs_result_fields[] = {
1256 {"f_bsize", },
1257 {"f_frsize", },
1258 {"f_blocks", },
1259 {"f_bfree", },
1260 {"f_bavail", },
1261 {"f_files", },
1262 {"f_ffree", },
1263 {"f_favail", },
1264 {"f_flag", },
1265 {"f_namemax",},
1266 {0}
1267};
1268
1269static PyStructSequence_Desc statvfs_result_desc = {
1270 "statvfs_result", /* name */
1271 statvfs_result__doc__, /* doc */
1272 statvfs_result_fields,
1273 10
1274};
1275
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001276static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001277static PyTypeObject StatResultType;
1278static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001279static newfunc structseq_new;
1280
1281static PyObject *
1282statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1283{
1284 PyStructSequence *result;
1285 int i;
1286
1287 result = (PyStructSequence*)structseq_new(type, args, kwds);
1288 if (!result)
1289 return NULL;
1290 /* If we have been initialized from a tuple,
1291 st_?time might be set to None. Initialize it
1292 from the int slots. */
1293 for (i = 7; i <= 9; i++) {
1294 if (result->ob_item[i+3] == Py_None) {
1295 Py_DECREF(Py_None);
1296 Py_INCREF(result->ob_item[i]);
1297 result->ob_item[i+3] = result->ob_item[i];
1298 }
1299 }
1300 return (PyObject*)result;
1301}
1302
1303
1304
1305/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001306static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001307
1308PyDoc_STRVAR(stat_float_times__doc__,
1309"stat_float_times([newval]) -> oldval\n\n\
1310Determine whether os.[lf]stat represents time stamps as float objects.\n\
1311If newval is True, future calls to stat() return floats, if it is False,\n\
1312future calls return ints. \n\
1313If newval is omitted, return the current setting.\n");
1314
1315static PyObject*
1316stat_float_times(PyObject* self, PyObject *args)
1317{
1318 int newval = -1;
1319 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1320 return NULL;
1321 if (newval == -1)
1322 /* Return old value */
1323 return PyBool_FromLong(_stat_float_times);
1324 _stat_float_times = newval;
1325 Py_INCREF(Py_None);
1326 return Py_None;
1327}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001328
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001329static void
1330fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1331{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001332 PyObject *fval,*ival;
1333#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001334 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001335#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001336 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001337#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001338 if (!ival)
1339 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001340 if (_stat_float_times) {
1341 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1342 } else {
1343 fval = ival;
1344 Py_INCREF(fval);
1345 }
1346 PyStructSequence_SET_ITEM(v, index, ival);
1347 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001348}
1349
Tim Peters5aa91602002-01-30 05:46:57 +00001350/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001351 (used by posix_stat() and posix_fstat()) */
1352static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001353_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001354{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001355 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001356 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001357 if (v == NULL)
1358 return NULL;
1359
Christian Heimes217cfd12007-12-02 14:31:20 +00001360 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001361#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001362 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001363 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001364#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001365 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001366#endif
1367#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001368 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001369 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001370#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001371 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001372#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001373 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1374 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1375 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001376#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001377 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001378 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001379#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001380 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001381#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001382
Martin v. Löwis14694662006-02-03 12:54:16 +00001383#if defined(HAVE_STAT_TV_NSEC)
1384 ansec = st->st_atim.tv_nsec;
1385 mnsec = st->st_mtim.tv_nsec;
1386 cnsec = st->st_ctim.tv_nsec;
1387#elif defined(HAVE_STAT_TV_NSEC2)
1388 ansec = st->st_atimespec.tv_nsec;
1389 mnsec = st->st_mtimespec.tv_nsec;
1390 cnsec = st->st_ctimespec.tv_nsec;
1391#elif defined(HAVE_STAT_NSEC)
1392 ansec = st->st_atime_nsec;
1393 mnsec = st->st_mtime_nsec;
1394 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001395#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001396 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001397#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001398 fill_time(v, 7, st->st_atime, ansec);
1399 fill_time(v, 8, st->st_mtime, mnsec);
1400 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001401
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001402#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001403 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001404 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001405#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001406#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001407 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001408 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001409#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001410#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001411 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001412 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001413#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001414#ifdef HAVE_STRUCT_STAT_ST_GEN
1415 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001416 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001417#endif
1418#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1419 {
1420 PyObject *val;
1421 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001422 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001423#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001424 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001425#else
1426 bnsec = 0;
1427#endif
1428 if (_stat_float_times) {
1429 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1430 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001431 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001432 }
1433 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1434 val);
1435 }
1436#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001437#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1438 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001439 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001440#endif
Fred Drake699f3522000-06-29 21:12:41 +00001441
1442 if (PyErr_Occurred()) {
1443 Py_DECREF(v);
1444 return NULL;
1445 }
1446
1447 return v;
1448}
1449
Martin v. Löwisd8948722004-06-02 09:57:56 +00001450#ifdef MS_WINDOWS
1451
1452/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1453 where / can be used in place of \ and the trailing slash is optional.
1454 Both SERVER and SHARE must have at least one character.
1455*/
1456
1457#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1458#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001459#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001460#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001461#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001462
Tim Peters4ad82172004-08-30 17:02:04 +00001463static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001464IsUNCRootA(char *path, int pathlen)
1465{
1466 #define ISSLASH ISSLASHA
1467
1468 int i, share;
1469
1470 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1471 /* minimum UNCRoot is \\x\y */
1472 return FALSE;
1473 for (i = 2; i < pathlen ; i++)
1474 if (ISSLASH(path[i])) break;
1475 if (i == 2 || i == pathlen)
1476 /* do not allow \\\SHARE or \\SERVER */
1477 return FALSE;
1478 share = i+1;
1479 for (i = share; i < pathlen; i++)
1480 if (ISSLASH(path[i])) break;
1481 return (i != share && (i == pathlen || i == pathlen-1));
1482
1483 #undef ISSLASH
1484}
1485
Tim Peters4ad82172004-08-30 17:02:04 +00001486static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001487IsUNCRootW(Py_UNICODE *path, int pathlen)
1488{
1489 #define ISSLASH ISSLASHW
1490
1491 int i, share;
1492
1493 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1494 /* minimum UNCRoot is \\x\y */
1495 return FALSE;
1496 for (i = 2; i < pathlen ; i++)
1497 if (ISSLASH(path[i])) break;
1498 if (i == 2 || i == pathlen)
1499 /* do not allow \\\SHARE or \\SERVER */
1500 return FALSE;
1501 share = i+1;
1502 for (i = share; i < pathlen; i++)
1503 if (ISSLASH(path[i])) break;
1504 return (i != share && (i == pathlen || i == pathlen-1));
1505
1506 #undef ISSLASH
1507}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001508#endif /* MS_WINDOWS */
1509
Barry Warsaw53699e91996-12-10 23:23:01 +00001510static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001511posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001512 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001513#ifdef __VMS
1514 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1515#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001516 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001517#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001518 char *wformat,
1519 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001520{
Fred Drake699f3522000-06-29 21:12:41 +00001521 STRUCT_STAT st;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001522 PyObject *opath;
1523 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001524 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001525 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001526
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001527#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001528 PyUnicodeObject *po;
1529 if (PyArg_ParseTuple(args, wformat, &po)) {
1530 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001531
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001532 Py_BEGIN_ALLOW_THREADS
1533 /* PyUnicode_AS_UNICODE result OK without
1534 thread lock as it is a simple dereference. */
1535 res = wstatfunc(wpath, &st);
1536 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001537
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001538 if (res != 0)
1539 return win32_error_unicode("stat", wpath);
1540 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001541 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001542 /* Drop the argument parsing error as narrow strings
1543 are also valid. */
1544 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001545#endif
1546
Tim Peters5aa91602002-01-30 05:46:57 +00001547 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +00001548 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001549 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001550 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00001551 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001552 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001553 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001554
1555 if (res != 0) {
1556#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001557 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001558#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001559 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001560#endif
1561 }
1562 else
1563 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001564
Martin v. Löwis011e8422009-05-05 04:43:17 +00001565 release_bytes(opath);
Martin v. Löwis14694662006-02-03 12:54:16 +00001566 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001567}
1568
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001569/* POSIX methods */
1570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001571PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001572"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001573Use the real uid/gid to test for access to a path. Note that most\n\
1574operations will use the effective uid/gid, therefore this routine can\n\
1575be used in a suid/sgid environment to test if the invoking user has the\n\
1576specified access to the path. The mode argument can be F_OK to test\n\
1577existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001578
1579static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001580posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001581{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001582 PyObject *opath;
Guido van Rossum015f22a1999-01-06 22:52:38 +00001583 char *path;
1584 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001585
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001586#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001587 DWORD attr;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001588 PyUnicodeObject *po;
1589 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1590 Py_BEGIN_ALLOW_THREADS
1591 /* PyUnicode_AS_UNICODE OK without thread lock as
1592 it is a simple dereference. */
1593 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1594 Py_END_ALLOW_THREADS
1595 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001596 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001597 /* Drop the argument parsing error as narrow strings
1598 are also valid. */
1599 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00001600 if (!PyArg_ParseTuple(args, "O&i:access",
1601 PyUnicode_FSConverter, &opath, &mode))
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001602 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001603 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001604 Py_BEGIN_ALLOW_THREADS
1605 attr = GetFileAttributesA(path);
1606 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001607 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001608finish:
1609 if (attr == 0xFFFFFFFF)
1610 /* File does not exist, or cannot read attributes */
1611 return PyBool_FromLong(0);
1612 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001613 the file isn't read-only, or if it's a directory, as there are
1614 no read-only directories on Windows. */
1615 return PyBool_FromLong(!(mode & 2)
1616 || !(attr & FILE_ATTRIBUTE_READONLY)
1617 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618#else
1619 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001620 if (!PyArg_ParseTuple(args, "O&i:access",
1621 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001622 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001623 path = bytes2str(opath, 1);
Guido van Rossum015f22a1999-01-06 22:52:38 +00001624 Py_BEGIN_ALLOW_THREADS
1625 res = access(path, mode);
1626 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001627 release_bytes(opath);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001628 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001629#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001630}
1631
Guido van Rossumd371ff11999-01-25 16:12:23 +00001632#ifndef F_OK
1633#define F_OK 0
1634#endif
1635#ifndef R_OK
1636#define R_OK 4
1637#endif
1638#ifndef W_OK
1639#define W_OK 2
1640#endif
1641#ifndef X_OK
1642#define X_OK 1
1643#endif
1644
1645#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001646PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001647"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001648Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001649
1650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001651posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001652{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001653 int id;
1654 char *ret;
1655
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001656 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001657 return NULL;
1658
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001659#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001660 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001661 if (id == 0) {
1662 ret = ttyname();
1663 }
1664 else {
1665 ret = NULL;
1666 }
1667#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001668 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001669#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001670 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001671 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001672 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001673}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001674#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001675
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001676#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001677PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001678"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001679Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001680
1681static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001682posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001683{
1684 char *ret;
1685 char buffer[L_ctermid];
1686
Greg Wardb48bc172000-03-01 21:51:56 +00001687#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001688 ret = ctermid_r(buffer);
1689#else
1690 ret = ctermid(buffer);
1691#endif
1692 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001693 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001694 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001695}
1696#endif
1697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001698PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001699"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001700Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001701
Barry Warsaw53699e91996-12-10 23:23:01 +00001702static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001703posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001704{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001705#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001706 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001707#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001708 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001709#elif defined(__VMS)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001710 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001711#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001712 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001713#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001714}
1715
Fred Drake4d1e64b2002-04-15 19:40:07 +00001716#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001718"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001719Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001720opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001721
1722static PyObject *
1723posix_fchdir(PyObject *self, PyObject *fdobj)
1724{
1725 return posix_fildes(fdobj, fchdir);
1726}
1727#endif /* HAVE_FCHDIR */
1728
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001730PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001731"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001732Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001733
Barry Warsaw53699e91996-12-10 23:23:01 +00001734static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001735posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001736{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001737 PyObject *opath = NULL;
Mark Hammondef8b6542001-05-13 08:04:26 +00001738 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001739 int i;
1740 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001741#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001742 DWORD attr;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001743 PyUnicodeObject *po;
1744 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1745 Py_BEGIN_ALLOW_THREADS
1746 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1747 if (attr != 0xFFFFFFFF) {
1748 if (i & _S_IWRITE)
1749 attr &= ~FILE_ATTRIBUTE_READONLY;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001750 else
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001751 attr |= FILE_ATTRIBUTE_READONLY;
1752 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
Mark Hammond817c9292003-12-03 01:22:38 +00001753 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001754 else
1755 res = 0;
1756 Py_END_ALLOW_THREADS
1757 if (!res)
1758 return win32_error_unicode("chmod",
1759 PyUnicode_AS_UNICODE(po));
1760 Py_INCREF(Py_None);
1761 return Py_None;
Mark Hammond817c9292003-12-03 01:22:38 +00001762 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001763 /* Drop the argument parsing error as narrow strings
1764 are also valid. */
1765 PyErr_Clear();
1766
Martin v. Löwis011e8422009-05-05 04:43:17 +00001767 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1768 &opath, &i))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001769 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001770 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001771 Py_BEGIN_ALLOW_THREADS
1772 attr = GetFileAttributesA(path);
1773 if (attr != 0xFFFFFFFF) {
1774 if (i & _S_IWRITE)
1775 attr &= ~FILE_ATTRIBUTE_READONLY;
1776 else
1777 attr |= FILE_ATTRIBUTE_READONLY;
1778 res = SetFileAttributesA(path, attr);
1779 }
1780 else
1781 res = 0;
1782 Py_END_ALLOW_THREADS
1783 if (!res) {
1784 win32_error("chmod", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00001785 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001786 return NULL;
1787 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001788 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001789 Py_INCREF(Py_None);
1790 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001791#else /* MS_WINDOWS */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001792 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1793 &opath, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001794 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001795 path = bytes2str(opath, 1);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001796 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001797 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001798 Py_END_ALLOW_THREADS
1799 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001800 return posix_error_with_allocated_filename(opath);
1801 release_bytes(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001802 Py_INCREF(Py_None);
1803 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001804#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001805}
1806
Christian Heimes4e30a842007-11-30 22:12:06 +00001807#ifdef HAVE_FCHMOD
1808PyDoc_STRVAR(posix_fchmod__doc__,
1809"fchmod(fd, mode)\n\n\
1810Change the access permissions of the file given by file\n\
1811descriptor fd.");
1812
1813static PyObject *
1814posix_fchmod(PyObject *self, PyObject *args)
1815{
1816 int fd, mode, res;
1817 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1818 return NULL;
1819 Py_BEGIN_ALLOW_THREADS
1820 res = fchmod(fd, mode);
1821 Py_END_ALLOW_THREADS
1822 if (res < 0)
1823 return posix_error();
1824 Py_RETURN_NONE;
1825}
1826#endif /* HAVE_FCHMOD */
1827
1828#ifdef HAVE_LCHMOD
1829PyDoc_STRVAR(posix_lchmod__doc__,
1830"lchmod(path, mode)\n\n\
1831Change the access permissions of a file. If path is a symlink, this\n\
1832affects the link itself rather than the target.");
1833
1834static PyObject *
1835posix_lchmod(PyObject *self, PyObject *args)
1836{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001837 PyObject *opath;
1838 char *path;
Christian Heimes4e30a842007-11-30 22:12:06 +00001839 int i;
1840 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001841 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1842 &opath, &i))
Christian Heimes4e30a842007-11-30 22:12:06 +00001843 return NULL;
Eric Smith86a05ec2009-05-05 13:07:30 +00001844 path = bytes2str(opath, 1);
Christian Heimes4e30a842007-11-30 22:12:06 +00001845 Py_BEGIN_ALLOW_THREADS
1846 res = lchmod(path, i);
1847 Py_END_ALLOW_THREADS
1848 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001849 return posix_error_with_allocated_filename(opath);
1850 release_bytes(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001851 Py_RETURN_NONE;
1852}
1853#endif /* HAVE_LCHMOD */
1854
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001855
Thomas Wouterscf297e42007-02-23 15:07:44 +00001856#ifdef HAVE_CHFLAGS
1857PyDoc_STRVAR(posix_chflags__doc__,
1858"chflags(path, flags)\n\n\
1859Set file flags.");
1860
1861static PyObject *
1862posix_chflags(PyObject *self, PyObject *args)
1863{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001864 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001865 char *path;
1866 unsigned long flags;
1867 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001868 if (!PyArg_ParseTuple(args, "O&k:chflags",
1869 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001870 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001871 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001872 Py_BEGIN_ALLOW_THREADS
1873 res = chflags(path, flags);
1874 Py_END_ALLOW_THREADS
1875 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001876 return posix_error_with_allocated_filename(opath);
1877 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001878 Py_INCREF(Py_None);
1879 return Py_None;
1880}
1881#endif /* HAVE_CHFLAGS */
1882
1883#ifdef HAVE_LCHFLAGS
1884PyDoc_STRVAR(posix_lchflags__doc__,
1885"lchflags(path, flags)\n\n\
1886Set file flags.\n\
1887This function will not follow symbolic links.");
1888
1889static PyObject *
1890posix_lchflags(PyObject *self, PyObject *args)
1891{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001892 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001893 char *path;
1894 unsigned long flags;
1895 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001896 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Martin v. Löwis4adbc342009-05-05 17:17:55 +00001897 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001898 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001899 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001900 Py_BEGIN_ALLOW_THREADS
1901 res = lchflags(path, flags);
1902 Py_END_ALLOW_THREADS
1903 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001904 return posix_error_with_allocated_filename(opath);
1905 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001906 Py_INCREF(Py_None);
1907 return Py_None;
1908}
1909#endif /* HAVE_LCHFLAGS */
1910
Martin v. Löwis244edc82001-10-04 22:44:26 +00001911#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001913"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001915
1916static PyObject *
1917posix_chroot(PyObject *self, PyObject *args)
1918{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001919 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001920}
1921#endif
1922
Guido van Rossum21142a01999-01-08 21:05:37 +00001923#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001926force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001927
1928static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001929posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001930{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001931 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001932}
1933#endif /* HAVE_FSYNC */
1934
1935#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001936
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001937#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001938extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1939#endif
1940
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001941PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001942"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001943force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001944 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001945
1946static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001947posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001948{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001949 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001950}
1951#endif /* HAVE_FDATASYNC */
1952
1953
Fredrik Lundh10723342000-07-10 16:38:09 +00001954#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001956"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001958
Barry Warsaw53699e91996-12-10 23:23:01 +00001959static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001960posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001961{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001962 PyObject *opath;
1963 char *path;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001964 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001965 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001966 if (!PyArg_ParseTuple(args, "O&ll:chown",
1967 PyUnicode_FSConverter, &opath,
Mark Hammondef8b6542001-05-13 08:04:26 +00001968 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001969 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001970 path = bytes2str(opath, 1);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001971 Py_BEGIN_ALLOW_THREADS
1972 res = chown(path, (uid_t) uid, (gid_t) gid);
1973 Py_END_ALLOW_THREADS
1974 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001975 return posix_error_with_allocated_filename(opath);
1976 release_bytes(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001977 Py_INCREF(Py_None);
1978 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001979}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001980#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001981
Christian Heimes4e30a842007-11-30 22:12:06 +00001982#ifdef HAVE_FCHOWN
1983PyDoc_STRVAR(posix_fchown__doc__,
1984"fchown(fd, uid, gid)\n\n\
1985Change the owner and group id of the file given by file descriptor\n\
1986fd to the numeric uid and gid.");
1987
1988static PyObject *
1989posix_fchown(PyObject *self, PyObject *args)
1990{
1991 int fd, uid, gid;
1992 int res;
1993 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1994 return NULL;
1995 Py_BEGIN_ALLOW_THREADS
1996 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1997 Py_END_ALLOW_THREADS
1998 if (res < 0)
1999 return posix_error();
2000 Py_RETURN_NONE;
2001}
2002#endif /* HAVE_FCHOWN */
2003
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002004#ifdef HAVE_LCHOWN
2005PyDoc_STRVAR(posix_lchown__doc__,
2006"lchown(path, uid, gid)\n\n\
2007Change the owner and group id of path to the numeric uid and gid.\n\
2008This function will not follow symbolic links.");
2009
2010static PyObject *
2011posix_lchown(PyObject *self, PyObject *args)
2012{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002013 PyObject *opath;
2014 char *path;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002015 int uid, gid;
2016 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002017 if (!PyArg_ParseTuple(args, "O&ii:lchown",
2018 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002019 &uid, &gid))
2020 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002021 path = bytes2str(opath, 1);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002022 Py_BEGIN_ALLOW_THREADS
2023 res = lchown(path, (uid_t) uid, (gid_t) gid);
2024 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00002025 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002026 return posix_error_with_allocated_filename(opath);
2027 release_bytes(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002028 Py_INCREF(Py_None);
2029 return Py_None;
2030}
2031#endif /* HAVE_LCHOWN */
2032
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002033
Guido van Rossum36bc6801995-06-14 22:54:23 +00002034#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002035static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002036posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002037{
2038 char buf[1026];
2039 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002040
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002041#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002042 if (!use_bytes) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002043 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002044 wchar_t *wbuf2 = wbuf;
2045 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002046 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002047 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002048 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2049 /* If the buffer is large enough, len does not include the
2050 terminating \0. If the buffer is too small, len includes
2051 the space needed for the terminator. */
2052 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2053 wbuf2 = malloc(len * sizeof(wchar_t));
2054 if (wbuf2)
2055 len = GetCurrentDirectoryW(len, wbuf2);
2056 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002057 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002058 if (!wbuf2) {
2059 PyErr_NoMemory();
2060 return NULL;
2061 }
2062 if (!len) {
2063 if (wbuf2 != wbuf) free(wbuf2);
2064 return win32_error("getcwdu", NULL);
2065 }
2066 resobj = PyUnicode_FromWideChar(wbuf2, len);
2067 if (wbuf2 != wbuf) free(wbuf2);
2068 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002069 }
2070#endif
2071
2072 Py_BEGIN_ALLOW_THREADS
2073#if defined(PYOS_OS2) && defined(PYCC_GCC)
2074 res = _getcwd2(buf, sizeof buf);
2075#else
2076 res = getcwd(buf, sizeof buf);
2077#endif
2078 Py_END_ALLOW_THREADS
2079 if (res == NULL)
2080 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002081 if (use_bytes)
2082 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002083 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002084}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002085
2086PyDoc_STRVAR(posix_getcwd__doc__,
2087"getcwd() -> path\n\n\
2088Return a unicode string representing the current working directory.");
2089
2090static PyObject *
2091posix_getcwd_unicode(PyObject *self)
2092{
2093 return posix_getcwd(0);
2094}
2095
2096PyDoc_STRVAR(posix_getcwdb__doc__,
2097"getcwdb() -> path\n\n\
2098Return a bytes string representing the current working directory.");
2099
2100static PyObject *
2101posix_getcwd_bytes(PyObject *self)
2102{
2103 return posix_getcwd(1);
2104}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002105#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002106
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002107
Guido van Rossumb6775db1994-08-01 11:34:53 +00002108#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002109PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002110"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002111Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002112
Barry Warsaw53699e91996-12-10 23:23:01 +00002113static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002114posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002115{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002116 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002117}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002118#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002119
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002121PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002122"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002123Return a list containing the names of the entries in the directory.\n\
2124\n\
2125 path: path of directory to list\n\
2126\n\
2127The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002128entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002129
Barry Warsaw53699e91996-12-10 23:23:01 +00002130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002131posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002132{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002133 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002134 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002135#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002136
Barry Warsaw53699e91996-12-10 23:23:01 +00002137 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002138 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002139 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002140 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002141 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002142 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002143 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002144 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002145
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002146 PyObject *po;
2147 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2148 WIN32_FIND_DATAW wFileData;
2149 Py_UNICODE *wnamebuf;
2150 /* Overallocate for \\*.*\0 */
2151 len = PyUnicode_GET_SIZE(po);
2152 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2153 if (!wnamebuf) {
2154 PyErr_NoMemory();
2155 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002156 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002157 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2158 if (len > 0) {
2159 Py_UNICODE wch = wnamebuf[len-1];
2160 if (wch != L'/' && wch != L'\\' && wch != L':')
2161 wnamebuf[len++] = L'\\';
2162 wcscpy(wnamebuf + len, L"*.*");
2163 }
2164 if ((d = PyList_New(0)) == NULL) {
2165 free(wnamebuf);
2166 return NULL;
2167 }
2168 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2169 if (hFindFile == INVALID_HANDLE_VALUE) {
2170 int error = GetLastError();
2171 if (error == ERROR_FILE_NOT_FOUND) {
2172 free(wnamebuf);
2173 return d;
2174 }
2175 Py_DECREF(d);
2176 win32_error_unicode("FindFirstFileW", wnamebuf);
2177 free(wnamebuf);
2178 return NULL;
2179 }
2180 do {
2181 /* Skip over . and .. */
2182 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2183 wcscmp(wFileData.cFileName, L"..") != 0) {
2184 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2185 if (v == NULL) {
2186 Py_DECREF(d);
2187 d = NULL;
2188 break;
2189 }
2190 if (PyList_Append(d, v) != 0) {
2191 Py_DECREF(v);
2192 Py_DECREF(d);
2193 d = NULL;
2194 break;
2195 }
2196 Py_DECREF(v);
2197 }
2198 Py_BEGIN_ALLOW_THREADS
2199 result = FindNextFileW(hFindFile, &wFileData);
2200 Py_END_ALLOW_THREADS
2201 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2202 it got to the end of the directory. */
2203 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2204 Py_DECREF(d);
2205 win32_error_unicode("FindNextFileW", wnamebuf);
2206 FindClose(hFindFile);
2207 free(wnamebuf);
2208 return NULL;
2209 }
2210 } while (result == TRUE);
2211
2212 if (FindClose(hFindFile) == FALSE) {
2213 Py_DECREF(d);
2214 win32_error_unicode("FindClose", wnamebuf);
2215 free(wnamebuf);
2216 return NULL;
2217 }
2218 free(wnamebuf);
2219 return d;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002220 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002221 /* Drop the argument parsing error as narrow strings
2222 are also valid. */
2223 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002224
Martin v. Löwis011e8422009-05-05 04:43:17 +00002225 if (!PyArg_ParseTuple(args, "O&:listdir",
2226 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002227 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002228 if (PyObject_Size(opath)+1 > MAX_PATH) {
2229 PyErr_SetString(PyExc_ValueError, "path too long");
2230 Py_DECREF(opath);
2231 return NULL;
2232 }
2233 strcpy(namebuf, bytes2str(opath, 0));
2234 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002235 if (len > 0) {
2236 char ch = namebuf[len-1];
2237 if (ch != SEP && ch != ALTSEP && ch != ':')
2238 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002239 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002240 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002241
Barry Warsaw53699e91996-12-10 23:23:01 +00002242 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002243 return NULL;
2244
2245 hFindFile = FindFirstFile(namebuf, &FileData);
2246 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002247 int error = GetLastError();
2248 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002249 return d;
2250 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002251 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002252 }
2253 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002254 /* Skip over . and .. */
2255 if (strcmp(FileData.cFileName, ".") != 0 &&
2256 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002257 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002258 if (v == NULL) {
2259 Py_DECREF(d);
2260 d = NULL;
2261 break;
2262 }
2263 if (PyList_Append(d, v) != 0) {
2264 Py_DECREF(v);
2265 Py_DECREF(d);
2266 d = NULL;
2267 break;
2268 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002269 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002270 }
Georg Brandl622927b2006-03-07 12:48:03 +00002271 Py_BEGIN_ALLOW_THREADS
2272 result = FindNextFile(hFindFile, &FileData);
2273 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002274 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2275 it got to the end of the directory. */
2276 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2277 Py_DECREF(d);
2278 win32_error("FindNextFile", namebuf);
2279 FindClose(hFindFile);
2280 return NULL;
2281 }
Georg Brandl622927b2006-03-07 12:48:03 +00002282 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002283
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002284 if (FindClose(hFindFile) == FALSE) {
2285 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002286 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002287 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002288
2289 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002290
Tim Peters0bb44a42000-09-15 07:44:49 +00002291#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002292
2293#ifndef MAX_PATH
2294#define MAX_PATH CCHMAXPATH
2295#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002296 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002297 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002298 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002299 PyObject *d, *v;
2300 char namebuf[MAX_PATH+5];
2301 HDIR hdir = 1;
2302 ULONG srchcnt = 1;
2303 FILEFINDBUF3 ep;
2304 APIRET rc;
2305
Martin v. Löwis011e8422009-05-05 04:43:17 +00002306 if (!PyArg_ParseTuple(args, "O&:listdir",
2307 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002308 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002309 name = bytes2str(oname);
2310 len = PyObject_Size(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002311 if (len >= MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002312 release_bytes(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002313 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002314 return NULL;
2315 }
2316 strcpy(namebuf, name);
2317 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002318 if (*pt == ALTSEP)
2319 *pt = SEP;
2320 if (namebuf[len-1] != SEP)
2321 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002322 strcpy(namebuf + len, "*.*");
2323
Neal Norwitz6c913782007-10-14 03:23:09 +00002324 if ((d = PyList_New(0)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002325 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002326 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002327 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002328
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002329 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2330 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002331 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002332 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2333 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2334 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002335
2336 if (rc != NO_ERROR) {
2337 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002338 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002339 }
2340
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002341 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002342 do {
2343 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002344 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002345 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002346
2347 strcpy(namebuf, ep.achName);
2348
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002349 /* Leave Case of Name Alone -- In Native Form */
2350 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002351
Christian Heimes72b710a2008-05-26 13:28:38 +00002352 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002353 if (v == NULL) {
2354 Py_DECREF(d);
2355 d = NULL;
2356 break;
2357 }
2358 if (PyList_Append(d, v) != 0) {
2359 Py_DECREF(v);
2360 Py_DECREF(d);
2361 d = NULL;
2362 break;
2363 }
2364 Py_DECREF(v);
2365 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2366 }
2367
Martin v. Löwis011e8422009-05-05 04:43:17 +00002368 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002369 return d;
2370#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002371 PyObject *oname;
2372 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002373 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002374 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002375 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002376 int arg_is_unicode = 1;
2377
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002378 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002379 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2380 arg_is_unicode = 0;
2381 PyErr_Clear();
2382 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002383 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002384 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002385 name = bytes2str(oname, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002386 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002387 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002388 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002389 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002390 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002391 release_bytes(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002392 return NULL;
2393 }
Georg Brandl622927b2006-03-07 12:48:03 +00002394 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002395 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002396 Py_BEGIN_ALLOW_THREADS
2397 ep = readdir(dirp);
2398 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002399 if (ep == NULL) {
2400 if (errno == 0) {
2401 break;
2402 } else {
2403 closedir(dirp);
2404 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002405 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002406 }
2407 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002408 if (ep->d_name[0] == '.' &&
2409 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002410 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002411 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002412 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002413 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002414 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002415 d = NULL;
2416 break;
2417 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002418 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002419 PyObject *w;
2420
2421 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002422 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002423 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002424 Py_DECREF(v);
2425 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002426 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002427 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002428 /* Encoding failed to decode ASCII bytes.
2429 Raise exception. */
2430 Py_DECREF(d);
2431 d = NULL;
2432 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002433 }
Just van Rossum46c97842003-02-25 21:42:15 +00002434 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002435 if (PyList_Append(d, v) != 0) {
2436 Py_DECREF(v);
2437 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002438 d = NULL;
2439 break;
2440 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002441 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002442 }
2443 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002444 release_bytes(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002445
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002446 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002447
Tim Peters0bb44a42000-09-15 07:44:49 +00002448#endif /* which OS */
2449} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002450
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002451#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002452/* A helper function for abspath on win32 */
2453static PyObject *
2454posix__getfullpathname(PyObject *self, PyObject *args)
2455{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002456 PyObject *opath;
2457 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002458 char outbuf[MAX_PATH*2];
2459 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002460#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002461 PyUnicodeObject *po;
2462 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2463 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2464 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2465 Py_UNICODE *wtemp;
2466 DWORD result;
2467 PyObject *v;
2468 result = GetFullPathNameW(wpath,
2469 sizeof(woutbuf)/sizeof(woutbuf[0]),
2470 woutbuf, &wtemp);
2471 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2472 woutbufp = malloc(result * sizeof(Py_UNICODE));
2473 if (!woutbufp)
2474 return PyErr_NoMemory();
2475 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002476 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002477 if (result)
2478 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2479 else
2480 v = win32_error_unicode("GetFullPathNameW", wpath);
2481 if (woutbufp != woutbuf)
2482 free(woutbufp);
2483 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002484 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002485 /* Drop the argument parsing error as narrow strings
2486 are also valid. */
2487 PyErr_Clear();
2488
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002489#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002490 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2491 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002492 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002493 path = bytes2str(opath, 1);
2494 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2495 outbuf, &temp)) {
2496 win32_error("GetFullPathName", path);
2497 release_bytes(opath);
2498 return NULL;
2499 }
2500 release_bytes(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002501 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2502 return PyUnicode_Decode(outbuf, strlen(outbuf),
2503 Py_FileSystemDefaultEncoding, NULL);
2504 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002505 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002506} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002507#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002508
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002509PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002510"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002511Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002512
Barry Warsaw53699e91996-12-10 23:23:01 +00002513static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002514posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002515{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002516 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002517 PyObject *opath;
2518 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002519 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002520
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002521#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002522 PyUnicodeObject *po;
2523 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2524 Py_BEGIN_ALLOW_THREADS
2525 /* PyUnicode_AS_UNICODE OK without thread lock as
2526 it is a simple dereference. */
2527 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2528 Py_END_ALLOW_THREADS
2529 if (!res)
2530 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2531 Py_INCREF(Py_None);
2532 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002533 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002534 /* Drop the argument parsing error as narrow strings
2535 are also valid. */
2536 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00002537 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2538 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002539 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002540 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002541 Py_BEGIN_ALLOW_THREADS
2542 /* PyUnicode_AS_UNICODE OK without thread lock as
2543 it is a simple dereference. */
2544 res = CreateDirectoryA(path, NULL);
2545 Py_END_ALLOW_THREADS
2546 if (!res) {
2547 win32_error("mkdir", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002548 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002549 return NULL;
2550 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002551 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002552 Py_INCREF(Py_None);
2553 return Py_None;
2554#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002555
Martin v. Löwis011e8422009-05-05 04:43:17 +00002556 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2557 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002558 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002559 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00002560 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002561#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002562 res = mkdir(path);
2563#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002564 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002565#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002566 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002567 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002568 return posix_error_with_allocated_filename(opath);
2569 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002570 Py_INCREF(Py_None);
2571 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002572#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002573}
2574
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002575
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002576/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2577#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002578#include <sys/resource.h>
2579#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002580
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002581
2582#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002583PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002584"nice(inc) -> new_priority\n\n\
2585Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002586
Barry Warsaw53699e91996-12-10 23:23:01 +00002587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002588posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002589{
2590 int increment, value;
2591
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002592 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002593 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002594
2595 /* There are two flavours of 'nice': one that returns the new
2596 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002597 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2598 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002599
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002600 If we are of the nice family that returns the new priority, we
2601 need to clear errno before the call, and check if errno is filled
2602 before calling posix_error() on a returnvalue of -1, because the
2603 -1 may be the actual new priority! */
2604
2605 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002606 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002607#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002608 if (value == 0)
2609 value = getpriority(PRIO_PROCESS, 0);
2610#endif
2611 if (value == -1 && errno != 0)
2612 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002613 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002614 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002615}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002616#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002618PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002619"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002620Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002621
Barry Warsaw53699e91996-12-10 23:23:01 +00002622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002623posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002624{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002625#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002626 PyObject *o1, *o2;
2627 char *p1, *p2;
2628 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002629 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002630 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002631 if (!convert_to_unicode(&o1))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002632 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002633 if (!convert_to_unicode(&o2)) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002634 Py_DECREF(o1);
2635 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002636 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002637 Py_BEGIN_ALLOW_THREADS
2638 result = MoveFileW(PyUnicode_AsUnicode(o1),
2639 PyUnicode_AsUnicode(o2));
2640 Py_END_ALLOW_THREADS
2641 Py_DECREF(o1);
2642 Py_DECREF(o2);
2643 if (!result)
2644 return win32_error("rename", NULL);
2645 Py_INCREF(Py_None);
2646 return Py_None;
2647error:
2648 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002649 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2650 return NULL;
2651 Py_BEGIN_ALLOW_THREADS
2652 result = MoveFileA(p1, p2);
2653 Py_END_ALLOW_THREADS
2654 if (!result)
2655 return win32_error("rename", NULL);
2656 Py_INCREF(Py_None);
2657 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002658#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002659 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002660#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002661}
2662
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002664PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002665"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002666Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002667
Barry Warsaw53699e91996-12-10 23:23:01 +00002668static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002669posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002670{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002671#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002672 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002673#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002674 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002675#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002676}
2677
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002680"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002682
Barry Warsaw53699e91996-12-10 23:23:01 +00002683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002684posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002685{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002686#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002687 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002688#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002689 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002690#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002691}
2692
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002693
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002694#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002695PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002696"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002697Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002698
Barry Warsaw53699e91996-12-10 23:23:01 +00002699static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002700posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002701{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002702 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002703#ifdef MS_WINDOWS
2704 wchar_t *command;
2705 if (!PyArg_ParseTuple(args, "u:system", &command))
2706 return NULL;
2707#else
2708 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002709 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002710 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002711#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002712 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002713#ifdef MS_WINDOWS
2714 sts = _wsystem(command);
2715#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002716 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002717#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002718 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002719 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002720}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002721#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002722
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002724PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002725"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002726Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002727
Barry Warsaw53699e91996-12-10 23:23:01 +00002728static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002729posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002730{
2731 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002732 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002733 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002734 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002735 if (i < 0)
2736 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002737 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002738}
2739
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002741PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002742"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002743Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002745PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002746"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002747Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002748
Barry Warsaw53699e91996-12-10 23:23:01 +00002749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002750posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002751{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002752#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002753 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002754#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002755 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002756#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002757}
2758
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002759
Guido van Rossumb6775db1994-08-01 11:34:53 +00002760#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002761PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002762"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002763Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002764
Barry Warsaw53699e91996-12-10 23:23:01 +00002765static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002766posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002767{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002768 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002769 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002770
Barry Warsaw53699e91996-12-10 23:23:01 +00002771 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002772 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002773 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002774 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002775 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002776 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002777 u.sysname,
2778 u.nodename,
2779 u.release,
2780 u.version,
2781 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002782}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002783#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002784
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002785static int
2786extract_time(PyObject *t, long* sec, long* usec)
2787{
2788 long intval;
2789 if (PyFloat_Check(t)) {
2790 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002791 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002792 if (!intobj)
2793 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002794 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002795 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002796 if (intval == -1 && PyErr_Occurred())
2797 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002798 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002799 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002800 if (*usec < 0)
2801 /* If rounding gave us a negative number,
2802 truncate. */
2803 *usec = 0;
2804 return 0;
2805 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002806 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002807 if (intval == -1 && PyErr_Occurred())
2808 return -1;
2809 *sec = intval;
2810 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002811 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002812}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002813
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002814PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002815"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002816utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002817Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002818second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002819
Barry Warsaw53699e91996-12-10 23:23:01 +00002820static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002821posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002822{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002823#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002824 PyObject *arg;
2825 PyUnicodeObject *obwpath;
2826 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002827 PyObject *oapath;
2828 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002829 HANDLE hFile;
2830 long atimesec, mtimesec, ausec, musec;
2831 FILETIME atime, mtime;
2832 PyObject *result = NULL;
2833
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002834 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2835 wpath = PyUnicode_AS_UNICODE(obwpath);
2836 Py_BEGIN_ALLOW_THREADS
2837 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2838 NULL, OPEN_EXISTING,
2839 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2840 Py_END_ALLOW_THREADS
2841 if (hFile == INVALID_HANDLE_VALUE)
2842 return win32_error_unicode("utime", wpath);
2843 } else
2844 /* Drop the argument parsing error as narrow strings
2845 are also valid. */
2846 PyErr_Clear();
2847
Thomas Wouters477c8d52006-05-27 19:21:47 +00002848 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002849 if (!PyArg_ParseTuple(args, "O&O:utime",
2850 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002851 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002852 apath = bytes2str(oapath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002853 Py_BEGIN_ALLOW_THREADS
2854 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 NULL, OPEN_EXISTING,
2856 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002857 Py_END_ALLOW_THREADS
2858 if (hFile == INVALID_HANDLE_VALUE) {
2859 win32_error("utime", apath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002860 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002861 return NULL;
2862 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002863 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002864 }
2865
2866 if (arg == Py_None) {
2867 SYSTEMTIME now;
2868 GetSystemTime(&now);
2869 if (!SystemTimeToFileTime(&now, &mtime) ||
2870 !SystemTimeToFileTime(&now, &atime)) {
2871 win32_error("utime", NULL);
2872 goto done;
2873 }
2874 }
2875 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2876 PyErr_SetString(PyExc_TypeError,
2877 "utime() arg 2 must be a tuple (atime, mtime)");
2878 goto done;
2879 }
2880 else {
2881 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2882 &atimesec, &ausec) == -1)
2883 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002885 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2886 &mtimesec, &musec) == -1)
2887 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002888 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002889 }
2890 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2891 /* Avoid putting the file name into the error here,
2892 as that may confuse the user into believing that
2893 something is wrong with the file, when it also
2894 could be the time stamp that gives a problem. */
2895 win32_error("utime", NULL);
2896 }
2897 Py_INCREF(Py_None);
2898 result = Py_None;
2899done:
2900 CloseHandle(hFile);
2901 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002902#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002903
Martin v. Löwis011e8422009-05-05 04:43:17 +00002904 PyObject *opath;
2905 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002906 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002907 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002908 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002909
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002910#if defined(HAVE_UTIMES)
2911 struct timeval buf[2];
2912#define ATIME buf[0].tv_sec
2913#define MTIME buf[1].tv_sec
2914#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002915/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002916 struct utimbuf buf;
2917#define ATIME buf.actime
2918#define MTIME buf.modtime
2919#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002920#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002921 time_t buf[2];
2922#define ATIME buf[0]
2923#define MTIME buf[1]
2924#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002925#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002926
Mark Hammond817c9292003-12-03 01:22:38 +00002927
Martin v. Löwis011e8422009-05-05 04:43:17 +00002928 if (!PyArg_ParseTuple(args, "O&O:utime",
2929 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002930 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002931 path = bytes2str(opath, 1);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002932 if (arg == Py_None) {
2933 /* optional time values not given */
2934 Py_BEGIN_ALLOW_THREADS
2935 res = utime(path, NULL);
2936 Py_END_ALLOW_THREADS
2937 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002938 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002939 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002940 "utime() arg 2 must be a tuple (atime, mtime)");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002941 release_bytes(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002942 return NULL;
2943 }
2944 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002945 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002946 &atime, &ausec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002947 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002948 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002949 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002950 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002951 &mtime, &musec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002952 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002953 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002954 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002955 ATIME = atime;
2956 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002957#ifdef HAVE_UTIMES
2958 buf[0].tv_usec = ausec;
2959 buf[1].tv_usec = musec;
2960 Py_BEGIN_ALLOW_THREADS
2961 res = utimes(path, buf);
2962 Py_END_ALLOW_THREADS
2963#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002964 Py_BEGIN_ALLOW_THREADS
2965 res = utime(path, UTIME_ARG);
2966 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002967#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002968 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002969 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002970 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002971 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002972 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002973 Py_INCREF(Py_None);
2974 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002975#undef UTIME_ARG
2976#undef ATIME
2977#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002978#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002979}
2980
Guido van Rossum85e3b011991-06-03 12:42:10 +00002981
Guido van Rossum3b066191991-06-04 19:40:25 +00002982/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002984PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002985"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002986Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002987
Barry Warsaw53699e91996-12-10 23:23:01 +00002988static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002989posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002990{
2991 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002992 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002993 return NULL;
2994 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002995 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002996}
2997
Martin v. Löwis114619e2002-10-07 06:44:21 +00002998#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2999static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003000free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003001{
Martin v. Löwis725507b2006-03-07 12:08:51 +00003002 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00003003 for (i = 0; i < count; i++)
3004 PyMem_Free(array[i]);
3005 PyMem_DEL(array);
3006}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003007
Antoine Pitrou69f71142009-05-24 21:25:49 +00003008static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003009int fsconvert_strdup(PyObject *o, char**out)
3010{
3011 PyObject *bytes;
3012 Py_ssize_t size;
3013 if (!PyUnicode_FSConverter(o, &bytes))
3014 return 0;
3015 size = PyObject_Size(bytes);
3016 *out = PyMem_Malloc(size+1);
3017 if (!*out)
3018 return 0;
3019 /* Don't lock bytes, as we hold the GIL */
3020 memcpy(*out, bytes2str(bytes, 0), size+1);
3021 Py_DECREF(bytes);
3022 return 1;
3023}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003024#endif
3025
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003026
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003027#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003028PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003029"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003030Execute an executable path with arguments, replacing current process.\n\
3031\n\
3032 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003033 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003034
Barry Warsaw53699e91996-12-10 23:23:01 +00003035static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003036posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003037{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003038 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003039 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003040 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003041 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003042 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003043 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003044
Guido van Rossum89b33251993-10-22 14:26:06 +00003045 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003046 argv is a list or tuple of strings. */
3047
Martin v. Löwis011e8422009-05-05 04:43:17 +00003048 if (!PyArg_ParseTuple(args, "O&O:execv",
3049 PyUnicode_FSConverter,
3050 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003051 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003052 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003053 if (PyList_Check(argv)) {
3054 argc = PyList_Size(argv);
3055 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003056 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003057 else if (PyTuple_Check(argv)) {
3058 argc = PyTuple_Size(argv);
3059 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003060 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003061 else {
Fred Drake661ea262000-10-24 19:57:45 +00003062 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003063 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003064 return NULL;
3065 }
Thomas Heller6790d602007-08-30 17:15:14 +00003066 if (argc < 1) {
3067 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003068 release_bytes(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003069 return NULL;
3070 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003071
Barry Warsaw53699e91996-12-10 23:23:01 +00003072 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003073 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003074 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003075 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003076 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003077 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003078 if (!fsconvert_strdup((*getitem)(argv, i),
3079 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003080 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003081 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003082 "execv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003083 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003084 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003085
Guido van Rossum85e3b011991-06-03 12:42:10 +00003086 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003087 }
3088 argvlist[argc] = NULL;
3089
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003090 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003091
Guido van Rossum85e3b011991-06-03 12:42:10 +00003092 /* If we get here it's definitely an error */
3093
Martin v. Löwis114619e2002-10-07 06:44:21 +00003094 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003095 release_bytes(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003096 return posix_error();
3097}
3098
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003101"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003102Execute a path with arguments and environment, replacing current process.\n\
3103\n\
3104 path: path of executable file\n\
3105 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003106 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003107
Barry Warsaw53699e91996-12-10 23:23:01 +00003108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003109posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003110{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003111 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003112 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003113 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003114 char **argvlist;
3115 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003116 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003117 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003118 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003119 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003120
3121 /* execve has three arguments: (path, argv, env), where
3122 argv is a list or tuple of strings and env is a dictionary
3123 like posix.environ. */
3124
Martin v. Löwis011e8422009-05-05 04:43:17 +00003125 if (!PyArg_ParseTuple(args, "O&OO:execve",
3126 PyUnicode_FSConverter,
3127 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003128 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003129 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003130 if (PyList_Check(argv)) {
3131 argc = PyList_Size(argv);
3132 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003133 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003134 else if (PyTuple_Check(argv)) {
3135 argc = PyTuple_Size(argv);
3136 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003137 }
3138 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003139 PyErr_SetString(PyExc_TypeError,
3140 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003141 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003142 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003143 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 PyErr_SetString(PyExc_TypeError,
3145 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003146 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003147 }
3148
Barry Warsaw53699e91996-12-10 23:23:01 +00003149 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003150 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003151 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003152 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003153 }
3154 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003155 if (!fsconvert_strdup((*getitem)(argv, i),
3156 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003157 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003158 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003159 goto fail_1;
3160 }
3161 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003162 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003163 argvlist[argc] = NULL;
3164
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003165 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003166 if (i < 0)
3167 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003168 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003169 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003170 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003171 goto fail_1;
3172 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003173 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003174 keys = PyMapping_Keys(env);
3175 vals = PyMapping_Values(env);
3176 if (!keys || !vals)
3177 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003178 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3179 PyErr_SetString(PyExc_TypeError,
3180 "execve(): env.keys() or env.values() is not a list");
3181 goto fail_2;
3182 }
Tim Peters5aa91602002-01-30 05:46:57 +00003183
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003184 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003185 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003186 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003187
3188 key = PyList_GetItem(keys, pos);
3189 val = PyList_GetItem(vals, pos);
3190 if (!key || !val)
3191 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003192
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003193 if (!PyArg_Parse(
3194 key,
3195 "s;execve() arg 3 contains a non-string key",
3196 &k) ||
3197 !PyArg_Parse(
3198 val,
3199 "s;execve() arg 3 contains a non-string value",
3200 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003201 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003202 goto fail_2;
3203 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003204
3205#if defined(PYOS_OS2)
3206 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3207 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3208#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003209 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003210 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003211 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003212 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003213 goto fail_2;
3214 }
Tim Petersc8996f52001-12-03 20:41:00 +00003215 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003216 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003217#if defined(PYOS_OS2)
3218 }
3219#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003220 }
3221 envlist[envc] = 0;
3222
3223 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003224
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003225 /* If we get here it's definitely an error */
3226
3227 (void) posix_error();
3228
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003229 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003230 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003231 PyMem_DEL(envlist[envc]);
3232 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003233 fail_1:
3234 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003235 Py_XDECREF(vals);
3236 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003237 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003238 release_bytes(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003239 return NULL;
3240}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003241#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003242
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003243
Guido van Rossuma1065681999-01-25 23:20:23 +00003244#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003245PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003246"spawnv(mode, path, args)\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\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003249 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003250 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003251 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003252
3253static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003254posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003255{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003256 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003257 char *path;
3258 PyObject *argv;
3259 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003260 int mode, i;
3261 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003262 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003263 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003264
3265 /* spawnv has three arguments: (mode, path, argv), where
3266 argv is a list or tuple of strings. */
3267
Martin v. Löwis011e8422009-05-05 04:43:17 +00003268 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3269 PyUnicode_FSConverter,
3270 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003271 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003272 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003273 if (PyList_Check(argv)) {
3274 argc = PyList_Size(argv);
3275 getitem = PyList_GetItem;
3276 }
3277 else if (PyTuple_Check(argv)) {
3278 argc = PyTuple_Size(argv);
3279 getitem = PyTuple_GetItem;
3280 }
3281 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003282 PyErr_SetString(PyExc_TypeError,
3283 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003284 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003285 return NULL;
3286 }
3287
3288 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003289 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003290 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003291 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003292 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003293 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003294 if (!fsconvert_strdup((*getitem)(argv, i),
3295 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003296 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003297 PyErr_SetString(
3298 PyExc_TypeError,
3299 "spawnv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003300 release_bytes(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003301 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003302 }
3303 }
3304 argvlist[argc] = NULL;
3305
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003306#if defined(PYOS_OS2) && defined(PYCC_GCC)
3307 Py_BEGIN_ALLOW_THREADS
3308 spawnval = spawnv(mode, path, argvlist);
3309 Py_END_ALLOW_THREADS
3310#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003311 if (mode == _OLD_P_OVERLAY)
3312 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003313
Tim Peters25059d32001-12-07 20:35:43 +00003314 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003315 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003316 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003317#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003318
Martin v. Löwis114619e2002-10-07 06:44:21 +00003319 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003320 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003321
Fred Drake699f3522000-06-29 21:12:41 +00003322 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003323 return posix_error();
3324 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003325#if SIZEOF_LONG == SIZEOF_VOID_P
3326 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003327#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003328 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003329#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003330}
3331
3332
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003333PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003334"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003335Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003336\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003337 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003338 path: path of executable file\n\
3339 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003340 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003341
3342static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003343posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003344{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003345 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003346 char *path;
3347 PyObject *argv, *env;
3348 char **argvlist;
3349 char **envlist;
3350 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003351 int mode, pos, envc;
3352 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003353 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003354 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003355 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003356
3357 /* spawnve has four arguments: (mode, path, argv, env), where
3358 argv is a list or tuple of strings and env is a dictionary
3359 like posix.environ. */
3360
Martin v. Löwis011e8422009-05-05 04:43:17 +00003361 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3362 PyUnicode_FSConverter,
3363 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003364 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003365 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003366 if (PyList_Check(argv)) {
3367 argc = PyList_Size(argv);
3368 getitem = PyList_GetItem;
3369 }
3370 else if (PyTuple_Check(argv)) {
3371 argc = PyTuple_Size(argv);
3372 getitem = PyTuple_GetItem;
3373 }
3374 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003375 PyErr_SetString(PyExc_TypeError,
3376 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003377 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003378 }
3379 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003380 PyErr_SetString(PyExc_TypeError,
3381 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003382 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003383 }
3384
3385 argvlist = PyMem_NEW(char *, argc+1);
3386 if (argvlist == NULL) {
3387 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003388 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003389 }
3390 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003391 if (!fsconvert_strdup((*getitem)(argv, i),
3392 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003393 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003394 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003395 goto fail_1;
3396 }
3397 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003398 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003399 argvlist[argc] = NULL;
3400
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003401 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003402 if (i < 0)
3403 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003404 envlist = PyMem_NEW(char *, i + 1);
3405 if (envlist == NULL) {
3406 PyErr_NoMemory();
3407 goto fail_1;
3408 }
3409 envc = 0;
3410 keys = PyMapping_Keys(env);
3411 vals = PyMapping_Values(env);
3412 if (!keys || !vals)
3413 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003414 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3415 PyErr_SetString(PyExc_TypeError,
3416 "spawnve(): env.keys() or env.values() is not a list");
3417 goto fail_2;
3418 }
Tim Peters5aa91602002-01-30 05:46:57 +00003419
Guido van Rossuma1065681999-01-25 23:20:23 +00003420 for (pos = 0; pos < i; pos++) {
3421 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003422 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003423
3424 key = PyList_GetItem(keys, pos);
3425 val = PyList_GetItem(vals, pos);
3426 if (!key || !val)
3427 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003428
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003429 if (!PyArg_Parse(
3430 key,
3431 "s;spawnve() arg 3 contains a non-string key",
3432 &k) ||
3433 !PyArg_Parse(
3434 val,
3435 "s;spawnve() arg 3 contains a non-string value",
3436 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003437 {
3438 goto fail_2;
3439 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003440 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003441 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003442 if (p == NULL) {
3443 PyErr_NoMemory();
3444 goto fail_2;
3445 }
Tim Petersc8996f52001-12-03 20:41:00 +00003446 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003447 envlist[envc++] = p;
3448 }
3449 envlist[envc] = 0;
3450
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003451#if defined(PYOS_OS2) && defined(PYCC_GCC)
3452 Py_BEGIN_ALLOW_THREADS
3453 spawnval = spawnve(mode, path, argvlist, envlist);
3454 Py_END_ALLOW_THREADS
3455#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003456 if (mode == _OLD_P_OVERLAY)
3457 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003458
3459 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003460 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003461 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003462#endif
Tim Peters25059d32001-12-07 20:35:43 +00003463
Fred Drake699f3522000-06-29 21:12:41 +00003464 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003465 (void) posix_error();
3466 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003467#if SIZEOF_LONG == SIZEOF_VOID_P
3468 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003469#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003470 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003471#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003472
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003473 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003474 while (--envc >= 0)
3475 PyMem_DEL(envlist[envc]);
3476 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003477 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003478 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003479 Py_XDECREF(vals);
3480 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003481 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003482 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003483 return res;
3484}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003485
3486/* OS/2 supports spawnvp & spawnvpe natively */
3487#if defined(PYOS_OS2)
3488PyDoc_STRVAR(posix_spawnvp__doc__,
3489"spawnvp(mode, file, args)\n\n\
3490Execute the program 'file' in a new process, using the environment\n\
3491search path to find the file.\n\
3492\n\
3493 mode: mode of process creation\n\
3494 file: executable file name\n\
3495 args: tuple or list of strings");
3496
3497static PyObject *
3498posix_spawnvp(PyObject *self, PyObject *args)
3499{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003500 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003501 char *path;
3502 PyObject *argv;
3503 char **argvlist;
3504 int mode, i, argc;
3505 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003506 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003507
3508 /* spawnvp has three arguments: (mode, path, argv), where
3509 argv is a list or tuple of strings. */
3510
Martin v. Löwis011e8422009-05-05 04:43:17 +00003511 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3512 PyUnicode_FSConverter,
3513 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003514 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003515 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003516 if (PyList_Check(argv)) {
3517 argc = PyList_Size(argv);
3518 getitem = PyList_GetItem;
3519 }
3520 else if (PyTuple_Check(argv)) {
3521 argc = PyTuple_Size(argv);
3522 getitem = PyTuple_GetItem;
3523 }
3524 else {
3525 PyErr_SetString(PyExc_TypeError,
3526 "spawnvp() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003527 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003528 return NULL;
3529 }
3530
3531 argvlist = PyMem_NEW(char *, argc+1);
3532 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003533 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003534 return PyErr_NoMemory();
3535 }
3536 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003537 if (!fsconvert_strdup((*getitem)(argv, i),
3538 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003539 free_string_array(argvlist, i);
3540 PyErr_SetString(
3541 PyExc_TypeError,
3542 "spawnvp() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003543 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003544 return NULL;
3545 }
3546 }
3547 argvlist[argc] = NULL;
3548
3549 Py_BEGIN_ALLOW_THREADS
3550#if defined(PYCC_GCC)
3551 spawnval = spawnvp(mode, path, argvlist);
3552#else
3553 spawnval = _spawnvp(mode, path, argvlist);
3554#endif
3555 Py_END_ALLOW_THREADS
3556
3557 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003558 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003559
3560 if (spawnval == -1)
3561 return posix_error();
3562 else
3563 return Py_BuildValue("l", (long) spawnval);
3564}
3565
3566
3567PyDoc_STRVAR(posix_spawnvpe__doc__,
3568"spawnvpe(mode, file, args, env)\n\n\
3569Execute the program 'file' in a new process, using the environment\n\
3570search path to find the file.\n\
3571\n\
3572 mode: mode of process creation\n\
3573 file: executable file name\n\
3574 args: tuple or list of arguments\n\
3575 env: dictionary of strings mapping to strings");
3576
3577static PyObject *
3578posix_spawnvpe(PyObject *self, PyObject *args)
3579{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003580 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003581 char *path;
3582 PyObject *argv, *env;
3583 char **argvlist;
3584 char **envlist;
3585 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3586 int mode, i, pos, argc, envc;
3587 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003588 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003589 int lastarg = 0;
3590
3591 /* spawnvpe has four arguments: (mode, path, argv, env), where
3592 argv is a list or tuple of strings and env is a dictionary
3593 like posix.environ. */
3594
3595 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003596 PyUnicode_FSConverter,
3597 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003598 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003599 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003600 if (PyList_Check(argv)) {
3601 argc = PyList_Size(argv);
3602 getitem = PyList_GetItem;
3603 }
3604 else if (PyTuple_Check(argv)) {
3605 argc = PyTuple_Size(argv);
3606 getitem = PyTuple_GetItem;
3607 }
3608 else {
3609 PyErr_SetString(PyExc_TypeError,
3610 "spawnvpe() arg 2 must be a tuple or list");
3611 goto fail_0;
3612 }
3613 if (!PyMapping_Check(env)) {
3614 PyErr_SetString(PyExc_TypeError,
3615 "spawnvpe() arg 3 must be a mapping object");
3616 goto fail_0;
3617 }
3618
3619 argvlist = PyMem_NEW(char *, argc+1);
3620 if (argvlist == NULL) {
3621 PyErr_NoMemory();
3622 goto fail_0;
3623 }
3624 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003625 if (!fsconvert_strdup((*getitem)(argv, i),
3626 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003627 {
3628 lastarg = i;
3629 goto fail_1;
3630 }
3631 }
3632 lastarg = argc;
3633 argvlist[argc] = NULL;
3634
3635 i = PyMapping_Size(env);
3636 if (i < 0)
3637 goto fail_1;
3638 envlist = PyMem_NEW(char *, i + 1);
3639 if (envlist == NULL) {
3640 PyErr_NoMemory();
3641 goto fail_1;
3642 }
3643 envc = 0;
3644 keys = PyMapping_Keys(env);
3645 vals = PyMapping_Values(env);
3646 if (!keys || !vals)
3647 goto fail_2;
3648 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3649 PyErr_SetString(PyExc_TypeError,
3650 "spawnvpe(): env.keys() or env.values() is not a list");
3651 goto fail_2;
3652 }
3653
3654 for (pos = 0; pos < i; pos++) {
3655 char *p, *k, *v;
3656 size_t len;
3657
3658 key = PyList_GetItem(keys, pos);
3659 val = PyList_GetItem(vals, pos);
3660 if (!key || !val)
3661 goto fail_2;
3662
3663 if (!PyArg_Parse(
3664 key,
3665 "s;spawnvpe() arg 3 contains a non-string key",
3666 &k) ||
3667 !PyArg_Parse(
3668 val,
3669 "s;spawnvpe() arg 3 contains a non-string value",
3670 &v))
3671 {
3672 goto fail_2;
3673 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003674 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003675 p = PyMem_NEW(char, len);
3676 if (p == NULL) {
3677 PyErr_NoMemory();
3678 goto fail_2;
3679 }
3680 PyOS_snprintf(p, len, "%s=%s", k, v);
3681 envlist[envc++] = p;
3682 }
3683 envlist[envc] = 0;
3684
3685 Py_BEGIN_ALLOW_THREADS
3686#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003687 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003688#else
Christian Heimes292d3512008-02-03 16:51:08 +00003689 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003690#endif
3691 Py_END_ALLOW_THREADS
3692
3693 if (spawnval == -1)
3694 (void) posix_error();
3695 else
3696 res = Py_BuildValue("l", (long) spawnval);
3697
3698 fail_2:
3699 while (--envc >= 0)
3700 PyMem_DEL(envlist[envc]);
3701 PyMem_DEL(envlist);
3702 fail_1:
3703 free_string_array(argvlist, lastarg);
3704 Py_XDECREF(vals);
3705 Py_XDECREF(keys);
3706 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003707 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003708 return res;
3709}
3710#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003711#endif /* HAVE_SPAWNV */
3712
3713
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003714#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003715PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003716"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003717Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3718\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003719Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003720
3721static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003722posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003723{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003724 pid_t pid;
3725 int result;
3726 _PyImport_AcquireLock();
3727 pid = fork1();
3728 result = _PyImport_ReleaseLock();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003729 if (pid == -1)
3730 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003731 if (pid == 0)
3732 PyOS_AfterFork();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003733 if (result < 0) {
3734 /* Don't clobber the OSError if the fork failed. */
3735 PyErr_SetString(PyExc_RuntimeError,
3736 "not holding the import lock");
3737 return NULL;
3738 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003739 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003740}
3741#endif
3742
3743
Guido van Rossumad0ee831995-03-01 10:34:45 +00003744#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003745PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003746"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003747Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003748Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003749
Barry Warsaw53699e91996-12-10 23:23:01 +00003750static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003751posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003752{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003753 pid_t pid;
3754 int result;
3755 _PyImport_AcquireLock();
3756 pid = fork();
3757 result = _PyImport_ReleaseLock();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003758 if (pid == -1)
3759 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003760 if (pid == 0)
3761 PyOS_AfterFork();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003762 if (result < 0) {
3763 /* Don't clobber the OSError if the fork failed. */
3764 PyErr_SetString(PyExc_RuntimeError,
3765 "not holding the import lock");
3766 return NULL;
3767 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003768 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003769}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003770#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003771
Neal Norwitzb59798b2003-03-21 01:43:31 +00003772/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003773/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3774#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003775#define DEV_PTY_FILE "/dev/ptc"
3776#define HAVE_DEV_PTMX
3777#else
3778#define DEV_PTY_FILE "/dev/ptmx"
3779#endif
3780
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003781#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003782#ifdef HAVE_PTY_H
3783#include <pty.h>
3784#else
3785#ifdef HAVE_LIBUTIL_H
3786#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003787#endif /* HAVE_LIBUTIL_H */
3788#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003789#ifdef HAVE_STROPTS_H
3790#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003791#endif
3792#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003793
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003794#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003795PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003796"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003797Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003798
3799static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003800posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003801{
3802 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003803#ifndef HAVE_OPENPTY
3804 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003805#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003806#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003807 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003808#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003809 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003810#endif
3811#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003812
Thomas Wouters70c21a12000-07-14 14:28:33 +00003813#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003814 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3815 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003816#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003817 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3818 if (slave_name == NULL)
3819 return posix_error();
3820
3821 slave_fd = open(slave_name, O_RDWR);
3822 if (slave_fd < 0)
3823 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003824#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003825 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003826 if (master_fd < 0)
3827 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003828 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003829 /* change permission of slave */
3830 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003831 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003832 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003833 }
3834 /* unlock slave */
3835 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003836 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003837 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003838 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003839 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003840 slave_name = ptsname(master_fd); /* get name of slave */
3841 if (slave_name == NULL)
3842 return posix_error();
3843 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3844 if (slave_fd < 0)
3845 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003846#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003847 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3848 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003849#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003850 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003851#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003852#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003853#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003854
Fred Drake8cef4cf2000-06-28 16:40:38 +00003855 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003856
Fred Drake8cef4cf2000-06-28 16:40:38 +00003857}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003858#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003859
3860#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003861PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003862"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003863Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3864Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003865To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003866
3867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003868posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003869{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003870 int master_fd = -1, result;
Christian Heimes400adb02008-02-01 08:12:03 +00003871 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003872
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003873 _PyImport_AcquireLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003874 pid = forkpty(&master_fd, NULL, NULL, NULL);
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003875 result = _PyImport_ReleaseLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003876 if (pid == -1)
3877 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003878 if (pid == 0)
3879 PyOS_AfterFork();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003880 if (result < 0) {
3881 /* Don't clobber the OSError if the fork failed. */
3882 PyErr_SetString(PyExc_RuntimeError,
3883 "not holding the import lock");
3884 return NULL;
3885 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003886 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003887}
3888#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003889
Guido van Rossumad0ee831995-03-01 10:34:45 +00003890#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003891PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003892"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003893Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003894
Barry Warsaw53699e91996-12-10 23:23:01 +00003895static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003896posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003897{
Christian Heimes217cfd12007-12-02 14:31:20 +00003898 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003899}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003900#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003901
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003902
Guido van Rossumad0ee831995-03-01 10:34:45 +00003903#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003904PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003905"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003906Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003907
Barry Warsaw53699e91996-12-10 23:23:01 +00003908static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003909posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003910{
Christian Heimes217cfd12007-12-02 14:31:20 +00003911 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003912}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003913#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003915
Guido van Rossumad0ee831995-03-01 10:34:45 +00003916#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003917PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003918"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003919Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003920
Barry Warsaw53699e91996-12-10 23:23:01 +00003921static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003922posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003923{
Christian Heimes217cfd12007-12-02 14:31:20 +00003924 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003925}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003926#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003927
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003928
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003929PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003930"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003931Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003932
Barry Warsaw53699e91996-12-10 23:23:01 +00003933static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003934posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003935{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003936 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003937}
3938
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003939
Fred Drakec9680921999-12-13 16:37:25 +00003940#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003941PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003942"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003943Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003944
3945static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003946posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003947{
3948 PyObject *result = NULL;
3949
Fred Drakec9680921999-12-13 16:37:25 +00003950#ifdef NGROUPS_MAX
3951#define MAX_GROUPS NGROUPS_MAX
3952#else
3953 /* defined to be 16 on Solaris7, so this should be a small number */
3954#define MAX_GROUPS 64
3955#endif
3956 gid_t grouplist[MAX_GROUPS];
3957 int n;
3958
3959 n = getgroups(MAX_GROUPS, grouplist);
3960 if (n < 0)
3961 posix_error();
3962 else {
3963 result = PyList_New(n);
3964 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003965 int i;
3966 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003967 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003968 if (o == NULL) {
3969 Py_DECREF(result);
3970 result = NULL;
3971 break;
3972 }
3973 PyList_SET_ITEM(result, i, o);
3974 }
3975 }
3976 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003977
Fred Drakec9680921999-12-13 16:37:25 +00003978 return result;
3979}
3980#endif
3981
Martin v. Löwis606edc12002-06-13 21:09:11 +00003982#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003983PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003984"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003985Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003986
3987static PyObject *
3988posix_getpgid(PyObject *self, PyObject *args)
3989{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003990 pid_t pid, pgid;
3991 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00003992 return NULL;
3993 pgid = getpgid(pid);
3994 if (pgid < 0)
3995 return posix_error();
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003996 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003997}
3998#endif /* HAVE_GETPGID */
3999
4000
Guido van Rossumb6775db1994-08-01 11:34:53 +00004001#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004002PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004003"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004004Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004005
Barry Warsaw53699e91996-12-10 23:23:01 +00004006static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004007posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004008{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004009#ifdef GETPGRP_HAVE_ARG
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004010 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004011#else /* GETPGRP_HAVE_ARG */
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004012 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004013#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004014}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004015#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004016
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004017
Guido van Rossumb6775db1994-08-01 11:34:53 +00004018#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004019PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004020"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004021Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004022
Barry Warsaw53699e91996-12-10 23:23:01 +00004023static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004024posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004025{
Guido van Rossum64933891994-10-20 21:56:42 +00004026#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004027 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004028#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004029 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004030#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004031 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004032 Py_INCREF(Py_None);
4033 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004034}
4035
Guido van Rossumb6775db1994-08-01 11:34:53 +00004036#endif /* HAVE_SETPGRP */
4037
Guido van Rossumad0ee831995-03-01 10:34:45 +00004038#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004039PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004040"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004041Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004042
Barry Warsaw53699e91996-12-10 23:23:01 +00004043static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004044posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004045{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004046 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004047}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004048#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004049
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004050
Fred Drake12c6e2d1999-12-14 21:25:03 +00004051#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004052PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004053"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004054Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004055
4056static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004057posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004058{
Neal Norwitze241ce82003-02-17 18:17:05 +00004059 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004060 char *name;
4061 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004062
Fred Drakea30680b2000-12-06 21:24:28 +00004063 errno = 0;
4064 name = getlogin();
4065 if (name == NULL) {
4066 if (errno)
4067 posix_error();
4068 else
4069 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004070 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004071 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004072 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004073 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004074 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004075
Fred Drake12c6e2d1999-12-14 21:25:03 +00004076 return result;
4077}
4078#endif
4079
Guido van Rossumad0ee831995-03-01 10:34:45 +00004080#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004081PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004082"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004083Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004084
Barry Warsaw53699e91996-12-10 23:23:01 +00004085static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004086posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004087{
Christian Heimes217cfd12007-12-02 14:31:20 +00004088 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004089}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004090#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004091
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004092
Guido van Rossumad0ee831995-03-01 10:34:45 +00004093#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004094PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004095"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004096Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004097
Barry Warsaw53699e91996-12-10 23:23:01 +00004098static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004099posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004100{
Christian Heimes292d3512008-02-03 16:51:08 +00004101 pid_t pid;
4102 int sig;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004103 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004104 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004105#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004106 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4107 APIRET rc;
4108 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004109 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004110
4111 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4112 APIRET rc;
4113 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004114 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004115
4116 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004117 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004118#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004119 if (kill(pid, sig) == -1)
4120 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004121#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004122 Py_INCREF(Py_None);
4123 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004124}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004125#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004126
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004127#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004128PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004129"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004130Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004131
4132static PyObject *
4133posix_killpg(PyObject *self, PyObject *args)
4134{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004135 int sig;
4136 pid_t pgid;
4137 /* XXX some man pages make the `pgid` parameter an int, others
4138 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4139 take the same type. Moreover, pid_t is always at least as wide as
4140 int (else compilation of this module fails), which is safe. */
4141 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004142 return NULL;
4143 if (killpg(pgid, sig) == -1)
4144 return posix_error();
4145 Py_INCREF(Py_None);
4146 return Py_None;
4147}
4148#endif
4149
Guido van Rossumc0125471996-06-28 18:55:32 +00004150#ifdef HAVE_PLOCK
4151
4152#ifdef HAVE_SYS_LOCK_H
4153#include <sys/lock.h>
4154#endif
4155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004156PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004157"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004158Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004159
Barry Warsaw53699e91996-12-10 23:23:01 +00004160static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004161posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004162{
4163 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004164 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004165 return NULL;
4166 if (plock(op) == -1)
4167 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004168 Py_INCREF(Py_None);
4169 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004170}
4171#endif
4172
Guido van Rossumb6775db1994-08-01 11:34:53 +00004173#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004174PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004175"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004176Set the current process's user id.");
4177
Barry Warsaw53699e91996-12-10 23:23:01 +00004178static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004179posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004180{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004181 long uid_arg;
4182 uid_t uid;
4183 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004184 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004185 uid = uid_arg;
4186 if (uid != uid_arg) {
4187 PyErr_SetString(PyExc_OverflowError, "user id too big");
4188 return NULL;
4189 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004190 if (setuid(uid) < 0)
4191 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004192 Py_INCREF(Py_None);
4193 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004194}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004195#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004196
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004197
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004198#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004199PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004200"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004201Set the current process's effective user id.");
4202
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004203static PyObject *
4204posix_seteuid (PyObject *self, PyObject *args)
4205{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004206 long euid_arg;
4207 uid_t euid;
4208 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004209 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004210 euid = euid_arg;
4211 if (euid != euid_arg) {
4212 PyErr_SetString(PyExc_OverflowError, "user id too big");
4213 return NULL;
4214 }
4215 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004216 return posix_error();
4217 } else {
4218 Py_INCREF(Py_None);
4219 return Py_None;
4220 }
4221}
4222#endif /* HAVE_SETEUID */
4223
4224#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004225PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004226"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004227Set the current process's effective group id.");
4228
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004229static PyObject *
4230posix_setegid (PyObject *self, PyObject *args)
4231{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004232 long egid_arg;
4233 gid_t egid;
4234 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004235 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004236 egid = egid_arg;
4237 if (egid != egid_arg) {
4238 PyErr_SetString(PyExc_OverflowError, "group id too big");
4239 return NULL;
4240 }
4241 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004242 return posix_error();
4243 } else {
4244 Py_INCREF(Py_None);
4245 return Py_None;
4246 }
4247}
4248#endif /* HAVE_SETEGID */
4249
4250#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004251PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004252"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004253Set the current process's real and effective user ids.");
4254
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004255static PyObject *
4256posix_setreuid (PyObject *self, PyObject *args)
4257{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004258 long ruid_arg, euid_arg;
4259 uid_t ruid, euid;
4260 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004261 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004262 ruid = ruid_arg;
4263 euid = euid_arg;
4264 if (euid != euid_arg || ruid != ruid_arg) {
4265 PyErr_SetString(PyExc_OverflowError, "user id too big");
4266 return NULL;
4267 }
4268 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004269 return posix_error();
4270 } else {
4271 Py_INCREF(Py_None);
4272 return Py_None;
4273 }
4274}
4275#endif /* HAVE_SETREUID */
4276
4277#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004278PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004279"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004280Set the current process's real and effective group ids.");
4281
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004282static PyObject *
4283posix_setregid (PyObject *self, PyObject *args)
4284{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004285 long rgid_arg, egid_arg;
4286 gid_t rgid, egid;
4287 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004288 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004289 rgid = rgid_arg;
4290 egid = egid_arg;
4291 if (egid != egid_arg || rgid != rgid_arg) {
4292 PyErr_SetString(PyExc_OverflowError, "group id too big");
4293 return NULL;
4294 }
4295 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004296 return posix_error();
4297 } else {
4298 Py_INCREF(Py_None);
4299 return Py_None;
4300 }
4301}
4302#endif /* HAVE_SETREGID */
4303
Guido van Rossumb6775db1994-08-01 11:34:53 +00004304#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004305PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004306"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004307Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004308
Barry Warsaw53699e91996-12-10 23:23:01 +00004309static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004310posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004311{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004312 long gid_arg;
4313 gid_t gid;
4314 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004315 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004316 gid = gid_arg;
4317 if (gid != gid_arg) {
4318 PyErr_SetString(PyExc_OverflowError, "group id too big");
4319 return NULL;
4320 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004321 if (setgid(gid) < 0)
4322 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004323 Py_INCREF(Py_None);
4324 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004325}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004326#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004327
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004328#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004329PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004330"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004331Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004332
4333static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004334posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004335{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004336 int i, len;
4337 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004338
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004339 if (!PySequence_Check(groups)) {
4340 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4341 return NULL;
4342 }
4343 len = PySequence_Size(groups);
4344 if (len > MAX_GROUPS) {
4345 PyErr_SetString(PyExc_ValueError, "too many groups");
4346 return NULL;
4347 }
4348 for(i = 0; i < len; i++) {
4349 PyObject *elem;
4350 elem = PySequence_GetItem(groups, i);
4351 if (!elem)
4352 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004353 if (!PyLong_Check(elem)) {
4354 PyErr_SetString(PyExc_TypeError,
4355 "groups must be integers");
4356 Py_DECREF(elem);
4357 return NULL;
4358 } else {
4359 unsigned long x = PyLong_AsUnsignedLong(elem);
4360 if (PyErr_Occurred()) {
4361 PyErr_SetString(PyExc_TypeError,
4362 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004363 Py_DECREF(elem);
4364 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004365 }
Georg Brandla13c2442005-11-22 19:30:31 +00004366 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004367 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004368 if (grouplist[i] != x) {
4369 PyErr_SetString(PyExc_TypeError,
4370 "group id too big");
4371 Py_DECREF(elem);
4372 return NULL;
4373 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004374 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004375 Py_DECREF(elem);
4376 }
4377
4378 if (setgroups(len, grouplist) < 0)
4379 return posix_error();
4380 Py_INCREF(Py_None);
4381 return Py_None;
4382}
4383#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004384
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004385#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4386static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004387wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004388{
4389 PyObject *result;
4390 static PyObject *struct_rusage;
4391
4392 if (pid == -1)
4393 return posix_error();
4394
4395 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004396 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004397 if (m == NULL)
4398 return NULL;
4399 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4400 Py_DECREF(m);
4401 if (struct_rusage == NULL)
4402 return NULL;
4403 }
4404
4405 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4406 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4407 if (!result)
4408 return NULL;
4409
4410#ifndef doubletime
4411#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4412#endif
4413
4414 PyStructSequence_SET_ITEM(result, 0,
4415 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4416 PyStructSequence_SET_ITEM(result, 1,
4417 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4418#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004419 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004420 SET_INT(result, 2, ru->ru_maxrss);
4421 SET_INT(result, 3, ru->ru_ixrss);
4422 SET_INT(result, 4, ru->ru_idrss);
4423 SET_INT(result, 5, ru->ru_isrss);
4424 SET_INT(result, 6, ru->ru_minflt);
4425 SET_INT(result, 7, ru->ru_majflt);
4426 SET_INT(result, 8, ru->ru_nswap);
4427 SET_INT(result, 9, ru->ru_inblock);
4428 SET_INT(result, 10, ru->ru_oublock);
4429 SET_INT(result, 11, ru->ru_msgsnd);
4430 SET_INT(result, 12, ru->ru_msgrcv);
4431 SET_INT(result, 13, ru->ru_nsignals);
4432 SET_INT(result, 14, ru->ru_nvcsw);
4433 SET_INT(result, 15, ru->ru_nivcsw);
4434#undef SET_INT
4435
4436 if (PyErr_Occurred()) {
4437 Py_DECREF(result);
4438 return NULL;
4439 }
4440
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004441 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004442}
4443#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4444
4445#ifdef HAVE_WAIT3
4446PyDoc_STRVAR(posix_wait3__doc__,
4447"wait3(options) -> (pid, status, rusage)\n\n\
4448Wait for completion of a child process.");
4449
4450static PyObject *
4451posix_wait3(PyObject *self, PyObject *args)
4452{
Christian Heimes292d3512008-02-03 16:51:08 +00004453 pid_t pid;
4454 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004455 struct rusage ru;
4456 WAIT_TYPE status;
4457 WAIT_STATUS_INT(status) = 0;
4458
4459 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4460 return NULL;
4461
4462 Py_BEGIN_ALLOW_THREADS
4463 pid = wait3(&status, options, &ru);
4464 Py_END_ALLOW_THREADS
4465
4466 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4467}
4468#endif /* HAVE_WAIT3 */
4469
4470#ifdef HAVE_WAIT4
4471PyDoc_STRVAR(posix_wait4__doc__,
4472"wait4(pid, options) -> (pid, status, rusage)\n\n\
4473Wait for completion of a given child process.");
4474
4475static PyObject *
4476posix_wait4(PyObject *self, PyObject *args)
4477{
Christian Heimes292d3512008-02-03 16:51:08 +00004478 pid_t pid;
4479 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004480 struct rusage ru;
4481 WAIT_TYPE status;
4482 WAIT_STATUS_INT(status) = 0;
4483
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004484 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004485 return NULL;
4486
4487 Py_BEGIN_ALLOW_THREADS
4488 pid = wait4(pid, &status, options, &ru);
4489 Py_END_ALLOW_THREADS
4490
4491 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4492}
4493#endif /* HAVE_WAIT4 */
4494
Guido van Rossumb6775db1994-08-01 11:34:53 +00004495#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004496PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004497"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004498Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004499
Barry Warsaw53699e91996-12-10 23:23:01 +00004500static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004501posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004502{
Christian Heimes292d3512008-02-03 16:51:08 +00004503 pid_t pid;
4504 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004505 WAIT_TYPE status;
4506 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004507
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004508 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004509 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004510 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004511 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004512 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004513 if (pid == -1)
4514 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004515
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004516 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004517}
4518
Tim Petersab034fa2002-02-01 11:27:43 +00004519#elif defined(HAVE_CWAIT)
4520
4521/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004522PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004523"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004524"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004525
4526static PyObject *
4527posix_waitpid(PyObject *self, PyObject *args)
4528{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004529 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004530 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004531
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004532 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004533 return NULL;
4534 Py_BEGIN_ALLOW_THREADS
4535 pid = _cwait(&status, pid, options);
4536 Py_END_ALLOW_THREADS
4537 if (pid == -1)
4538 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004539
4540 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004541 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004542}
4543#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004544
Guido van Rossumad0ee831995-03-01 10:34:45 +00004545#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004546PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004547"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004548Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004549
Barry Warsaw53699e91996-12-10 23:23:01 +00004550static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004551posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004552{
Christian Heimes292d3512008-02-03 16:51:08 +00004553 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004554 WAIT_TYPE status;
4555 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004556
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004557 Py_BEGIN_ALLOW_THREADS
4558 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004559 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004560 if (pid == -1)
4561 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004562
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004563 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004564}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004565#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004566
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004568PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004569"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004570Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004571
Barry Warsaw53699e91996-12-10 23:23:01 +00004572static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004573posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004574{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004575#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004576 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004577#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004578#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004579 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004580#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004581 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004582#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004583#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004584}
4585
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004586
Guido van Rossumb6775db1994-08-01 11:34:53 +00004587#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004588PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004589"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004590Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004591
Barry Warsaw53699e91996-12-10 23:23:01 +00004592static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004593posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004594{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004595 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004596 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004597 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004598 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004599 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004600 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004601
Martin v. Löwis011e8422009-05-05 04:43:17 +00004602 if (!PyArg_ParseTuple(args, "O&:readlink",
4603 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004604 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004605 path = bytes2str(opath, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004606 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004607 if (v == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00004608 release_bytes(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004609 return NULL;
4610 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004611
4612 if (PyUnicode_Check(v)) {
4613 arg_is_unicode = 1;
4614 }
4615 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004616
Barry Warsaw53699e91996-12-10 23:23:01 +00004617 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004618 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004619 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004620 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004621 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004622
Martin v. Löwis011e8422009-05-05 04:43:17 +00004623 release_bytes(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004624 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004625 if (arg_is_unicode) {
4626 PyObject *w;
4627
4628 w = PyUnicode_FromEncodedObject(v,
4629 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004630 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004631 if (w != NULL) {
4632 Py_DECREF(v);
4633 v = w;
4634 }
4635 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004636 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004637 }
4638 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004639 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004640}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004641#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004642
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004643
Guido van Rossumb6775db1994-08-01 11:34:53 +00004644#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004645PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004646"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004647Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004648
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004649static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004650posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004651{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004652 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004653}
4654#endif /* HAVE_SYMLINK */
4655
4656
4657#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004658#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4659static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004660system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004661{
4662 ULONG value = 0;
4663
4664 Py_BEGIN_ALLOW_THREADS
4665 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4666 Py_END_ALLOW_THREADS
4667
4668 return value;
4669}
4670
4671static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004672posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004673{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004674 /* Currently Only Uptime is Provided -- Others Later */
4675 return Py_BuildValue("ddddd",
4676 (double)0 /* t.tms_utime / HZ */,
4677 (double)0 /* t.tms_stime / HZ */,
4678 (double)0 /* t.tms_cutime / HZ */,
4679 (double)0 /* t.tms_cstime / HZ */,
4680 (double)system_uptime() / 1000);
4681}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004682#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004683#define NEED_TICKS_PER_SECOND
4684static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004685static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004686posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004687{
4688 struct tms t;
4689 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004690 errno = 0;
4691 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004692 if (c == (clock_t) -1)
4693 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004694 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004695 (double)t.tms_utime / ticks_per_second,
4696 (double)t.tms_stime / ticks_per_second,
4697 (double)t.tms_cutime / ticks_per_second,
4698 (double)t.tms_cstime / ticks_per_second,
4699 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004700}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004701#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004702#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004703
4704
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004705#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004706#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004707static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004708posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004709{
4710 FILETIME create, exit, kernel, user;
4711 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004712 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004713 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4714 /* The fields of a FILETIME structure are the hi and lo part
4715 of a 64-bit value expressed in 100 nanosecond units.
4716 1e7 is one second in such units; 1e-7 the inverse.
4717 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4718 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004719 return Py_BuildValue(
4720 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004721 (double)(user.dwHighDateTime*429.4967296 +
4722 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004723 (double)(kernel.dwHighDateTime*429.4967296 +
4724 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004725 (double)0,
4726 (double)0,
4727 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004728}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004729#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004730
4731#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004732PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004733"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004734Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004735#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004736
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004737
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004738#ifdef HAVE_GETSID
4739PyDoc_STRVAR(posix_getsid__doc__,
4740"getsid(pid) -> sid\n\n\
4741Call the system call getsid().");
4742
4743static PyObject *
4744posix_getsid(PyObject *self, PyObject *args)
4745{
Christian Heimes292d3512008-02-03 16:51:08 +00004746 pid_t pid;
4747 int sid;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004748 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004749 return NULL;
4750 sid = getsid(pid);
4751 if (sid < 0)
4752 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004753 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004754}
4755#endif /* HAVE_GETSID */
4756
4757
Guido van Rossumb6775db1994-08-01 11:34:53 +00004758#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004759PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004760"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004761Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004762
Barry Warsaw53699e91996-12-10 23:23:01 +00004763static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004764posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004765{
Guido van Rossum687dd131993-05-17 08:34:16 +00004766 if (setsid() < 0)
4767 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004768 Py_INCREF(Py_None);
4769 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004770}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004771#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004772
Guido van Rossumb6775db1994-08-01 11:34:53 +00004773#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004774PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004775"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004776Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004777
Barry Warsaw53699e91996-12-10 23:23:01 +00004778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004779posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004780{
Christian Heimes292d3512008-02-03 16:51:08 +00004781 pid_t pid;
4782 int pgrp;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004783 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004784 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004785 if (setpgid(pid, pgrp) < 0)
4786 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004787 Py_INCREF(Py_None);
4788 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004789}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004790#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004791
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004792
Guido van Rossumb6775db1994-08-01 11:34:53 +00004793#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004794PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004795"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004796Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004797
Barry Warsaw53699e91996-12-10 23:23:01 +00004798static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004799posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004800{
Christian Heimes15ebc882008-02-04 18:48:49 +00004801 int fd;
4802 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004803 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004804 return NULL;
4805 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004806 if (pgid < 0)
4807 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004808 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004809}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004810#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004811
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004812
Guido van Rossumb6775db1994-08-01 11:34:53 +00004813#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004814PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004815"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004816Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004817
Barry Warsaw53699e91996-12-10 23:23:01 +00004818static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004819posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004820{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004821 int fd;
4822 pid_t pgid;
4823 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004824 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004825 if (tcsetpgrp(fd, pgid) < 0)
4826 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004827 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004828 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004829}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004830#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004831
Guido van Rossum687dd131993-05-17 08:34:16 +00004832/* Functions acting on file descriptors */
4833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004834PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004835"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004836Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004837
Barry Warsaw53699e91996-12-10 23:23:01 +00004838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004839posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004840{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004841 PyObject *ofile;
4842 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004843 int flag;
4844 int mode = 0777;
4845 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004846
4847#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004848 PyUnicodeObject *po;
4849 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4850 Py_BEGIN_ALLOW_THREADS
4851 /* PyUnicode_AS_UNICODE OK without thread
4852 lock as it is a simple dereference. */
4853 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4854 Py_END_ALLOW_THREADS
4855 if (fd < 0)
4856 return posix_error();
4857 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004858 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004859 /* Drop the argument parsing error as narrow strings
4860 are also valid. */
4861 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004862#endif
4863
Martin v. Löwis011e8422009-05-05 04:43:17 +00004864 if (!PyArg_ParseTuple(args, "O&i|i",
4865 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004866 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004867 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004868 file = bytes2str(ofile, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00004869 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004870 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004871 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004872 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004873 return posix_error_with_allocated_filename(ofile);
4874 release_bytes(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004875 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004876}
4877
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004879PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004880"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004881Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004882
Barry Warsaw53699e91996-12-10 23:23:01 +00004883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004884posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004885{
4886 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004887 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004888 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004889 if (!_PyVerify_fd(fd))
4890 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004891 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004892 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004893 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004894 if (res < 0)
4895 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004896 Py_INCREF(Py_None);
4897 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004898}
4899
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004900
Christian Heimesfdab48e2008-01-20 09:06:41 +00004901PyDoc_STRVAR(posix_closerange__doc__,
4902"closerange(fd_low, fd_high)\n\n\
4903Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4904
4905static PyObject *
4906posix_closerange(PyObject *self, PyObject *args)
4907{
4908 int fd_from, fd_to, i;
4909 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4910 return NULL;
4911 Py_BEGIN_ALLOW_THREADS
4912 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004913 if (_PyVerify_fd(i))
4914 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00004915 Py_END_ALLOW_THREADS
4916 Py_RETURN_NONE;
4917}
4918
4919
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004920PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004921"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004922Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004923
Barry Warsaw53699e91996-12-10 23:23:01 +00004924static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004925posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004926{
4927 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004928 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004929 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004930 if (!_PyVerify_fd(fd))
4931 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004932 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004933 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004934 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004935 if (fd < 0)
4936 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004937 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004938}
4939
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004940
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004941PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004942"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004943Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004944
Barry Warsaw53699e91996-12-10 23:23:01 +00004945static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004946posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004947{
4948 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004949 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004950 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004951 if (!_PyVerify_fd_dup2(fd, fd2))
4952 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004953 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004954 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004955 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004956 if (res < 0)
4957 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004958 Py_INCREF(Py_None);
4959 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004960}
4961
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004962
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004963PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004964"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004965Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004966
Barry Warsaw53699e91996-12-10 23:23:01 +00004967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004968posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004969{
4970 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004971#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004972 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004973#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004974 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004975#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004976 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004977 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004978 return NULL;
4979#ifdef SEEK_SET
4980 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4981 switch (how) {
4982 case 0: how = SEEK_SET; break;
4983 case 1: how = SEEK_CUR; break;
4984 case 2: how = SEEK_END; break;
4985 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004986#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004987
4988#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004989 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004990#else
4991 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00004992 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004993#endif
4994 if (PyErr_Occurred())
4995 return NULL;
4996
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004997 if (!_PyVerify_fd(fd))
4998 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004999 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005000#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005001 res = _lseeki64(fd, pos, how);
5002#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005003 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005004#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005005 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005006 if (res < 0)
5007 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005008
5009#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005010 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005011#else
5012 return PyLong_FromLongLong(res);
5013#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005014}
5015
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005017PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005018"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005019Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005020
Barry Warsaw53699e91996-12-10 23:23:01 +00005021static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005022posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005023{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005024 int fd, size;
5025 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005026 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005027 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005028 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005029 if (size < 0) {
5030 errno = EINVAL;
5031 return posix_error();
5032 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005033 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005034 if (buffer == NULL)
5035 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005036 if (!_PyVerify_fd(fd))
5037 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005038 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005039 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005040 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005041 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005042 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005043 return posix_error();
5044 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005045 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005046 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005047 return buffer;
5048}
5049
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005051PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005052"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005053Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005054
Barry Warsaw53699e91996-12-10 23:23:01 +00005055static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005056posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005057{
Martin v. Löwis423be952008-08-13 15:53:07 +00005058 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005059 int fd;
5060 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005061
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005062 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005063 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005064 if (!_PyVerify_fd(fd))
5065 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005066 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005067 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005068 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005069 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005070 if (size < 0)
5071 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005072 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005073}
5074
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005076PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005077"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005078Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005079
Barry Warsaw53699e91996-12-10 23:23:01 +00005080static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005081posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005082{
5083 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005084 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005085 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005086 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005087 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005088#ifdef __VMS
5089 /* on OpenVMS we must ensure that all bytes are written to the file */
5090 fsync(fd);
5091#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005092 if (!_PyVerify_fd(fd))
5093 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005094 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005095 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005096 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005097 if (res != 0) {
5098#ifdef MS_WINDOWS
5099 return win32_error("fstat", NULL);
5100#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005101 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005102#endif
5103 }
Tim Peters5aa91602002-01-30 05:46:57 +00005104
Martin v. Löwis14694662006-02-03 12:54:16 +00005105 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005106}
5107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005108PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005109"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005110Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005111connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005112
5113static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005114posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005115{
5116 int fd;
5117 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5118 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005119 if (!_PyVerify_fd(fd))
5120 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005121 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005122}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005123
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005124#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005125PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005126"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005127Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005128
Barry Warsaw53699e91996-12-10 23:23:01 +00005129static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005130posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005131{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005132#if defined(PYOS_OS2)
5133 HFILE read, write;
5134 APIRET rc;
5135
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005136 Py_BEGIN_ALLOW_THREADS
5137 rc = DosCreatePipe( &read, &write, 4096);
5138 Py_END_ALLOW_THREADS
5139 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005140 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005141
5142 return Py_BuildValue("(ii)", read, write);
5143#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005144#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005145 int fds[2];
5146 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005147 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005148 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005149 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005150 if (res != 0)
5151 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005152 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005153#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005154 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005155 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005156 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005157 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005158 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005159 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005160 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005161 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005162 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5163 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005164 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005165#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005166#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005167}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005168#endif /* HAVE_PIPE */
5169
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005170
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005171#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005172PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005173"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005174Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005175
Barry Warsaw53699e91996-12-10 23:23:01 +00005176static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005177posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005178{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005179 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005180 int mode = 0666;
5181 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005182 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005183 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005184 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005185 res = mkfifo(filename, mode);
5186 Py_END_ALLOW_THREADS
5187 if (res < 0)
5188 return posix_error();
5189 Py_INCREF(Py_None);
5190 return Py_None;
5191}
5192#endif
5193
5194
Neal Norwitz11690112002-07-30 01:08:28 +00005195#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005196PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005197"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005198Create a filesystem node (file, device special file or named pipe)\n\
5199named filename. mode specifies both the permissions to use and the\n\
5200type of node to be created, being combined (bitwise OR) with one of\n\
5201S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005202device defines the newly created device special file (probably using\n\
5203os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005204
5205
5206static PyObject *
5207posix_mknod(PyObject *self, PyObject *args)
5208{
5209 char *filename;
5210 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005211 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005212 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005213 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005214 return NULL;
5215 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005216 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005217 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005218 if (res < 0)
5219 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005220 Py_INCREF(Py_None);
5221 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005222}
5223#endif
5224
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005225#ifdef HAVE_DEVICE_MACROS
5226PyDoc_STRVAR(posix_major__doc__,
5227"major(device) -> major number\n\
5228Extracts a device major number from a raw device number.");
5229
5230static PyObject *
5231posix_major(PyObject *self, PyObject *args)
5232{
5233 int device;
5234 if (!PyArg_ParseTuple(args, "i:major", &device))
5235 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005236 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005237}
5238
5239PyDoc_STRVAR(posix_minor__doc__,
5240"minor(device) -> minor number\n\
5241Extracts a device minor number from a raw device number.");
5242
5243static PyObject *
5244posix_minor(PyObject *self, PyObject *args)
5245{
5246 int device;
5247 if (!PyArg_ParseTuple(args, "i:minor", &device))
5248 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005249 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005250}
5251
5252PyDoc_STRVAR(posix_makedev__doc__,
5253"makedev(major, minor) -> device number\n\
5254Composes a raw device number from the major and minor device numbers.");
5255
5256static PyObject *
5257posix_makedev(PyObject *self, PyObject *args)
5258{
5259 int major, minor;
5260 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5261 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005262 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005263}
5264#endif /* device macros */
5265
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005266
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005267#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005268PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005269"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005270Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005271
Barry Warsaw53699e91996-12-10 23:23:01 +00005272static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005273posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005274{
5275 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005276 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005277 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005278 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005279
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005280 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005281 return NULL;
5282
5283#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005284 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005285#else
5286 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005287 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005288#endif
5289 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005290 return NULL;
5291
Barry Warsaw53699e91996-12-10 23:23:01 +00005292 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005293 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005294 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005295 if (res < 0)
5296 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005297 Py_INCREF(Py_None);
5298 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005299}
5300#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005301
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005302#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005303PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005304"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005305Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005306
Fred Drake762e2061999-08-26 17:23:54 +00005307/* Save putenv() parameters as values here, so we can collect them when they
5308 * get re-set with another call for the same key. */
5309static PyObject *posix_putenv_garbage;
5310
Tim Peters5aa91602002-01-30 05:46:57 +00005311static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005312posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005313{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005314#ifdef MS_WINDOWS
5315 wchar_t *s1, *s2;
5316 wchar_t *newenv;
5317#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005318 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005319 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005320 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005321#endif
Fred Drake762e2061999-08-26 17:23:54 +00005322 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005323 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005324
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005325#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005326 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005327 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005328 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005329 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005330#else
5331 if (!PyArg_ParseTuple(args,
5332 "O&O&:putenv",
5333 PyUnicode_FSConverter, &os1,
5334 PyUnicode_FSConverter, &os2))
5335 return NULL;
5336 s1 = bytes2str(os1, 1);
5337 s2 = bytes2str(os2, 1);
5338#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005339
5340#if defined(PYOS_OS2)
5341 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5342 APIRET rc;
5343
Guido van Rossumd48f2521997-12-05 22:19:34 +00005344 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5345 if (rc != NO_ERROR)
5346 return os2_error(rc);
5347
5348 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5349 APIRET rc;
5350
Guido van Rossumd48f2521997-12-05 22:19:34 +00005351 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5352 if (rc != NO_ERROR)
5353 return os2_error(rc);
5354 } else {
5355#endif
Fred Drake762e2061999-08-26 17:23:54 +00005356 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005357 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005358 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005359#ifdef MS_WINDOWS
5360 len = wcslen(s1) + wcslen(s2) + 2;
5361 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5362#else
5363 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005364 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005365#endif
Fred Drake762e2061999-08-26 17:23:54 +00005366 if (newstr == NULL)
5367 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005368#ifdef MS_WINDOWS
5369 newenv = PyUnicode_AsUnicode(newstr);
5370 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5371 if (_wputenv(newenv)) {
5372 Py_DECREF(newstr);
5373 posix_error();
5374 return NULL;
5375 }
5376#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005377 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005378 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5379 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005380 Py_DECREF(newstr);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005381 release_bytes(os1);
5382 release_bytes(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005383 posix_error();
5384 return NULL;
5385 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005386#endif
Fred Drake762e2061999-08-26 17:23:54 +00005387 /* Install the first arg and newstr in posix_putenv_garbage;
5388 * this will cause previous value to be collected. This has to
5389 * happen after the real putenv() call because the old value
5390 * was still accessible until then. */
5391 if (PyDict_SetItem(posix_putenv_garbage,
5392 PyTuple_GET_ITEM(args, 0), newstr)) {
5393 /* really not much we can do; just leak */
5394 PyErr_Clear();
5395 }
5396 else {
5397 Py_DECREF(newstr);
5398 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005399
5400#if defined(PYOS_OS2)
5401 }
5402#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005403#ifndef MS_WINDOWS
5404 release_bytes(os1);
5405 release_bytes(os2);
5406#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005407 Py_INCREF(Py_None);
5408 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005409}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005410#endif /* putenv */
5411
Guido van Rossumc524d952001-10-19 01:31:59 +00005412#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005413PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005414"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005415Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005416
5417static PyObject *
5418posix_unsetenv(PyObject *self, PyObject *args)
5419{
5420 char *s1;
5421
5422 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5423 return NULL;
5424
5425 unsetenv(s1);
5426
5427 /* Remove the key from posix_putenv_garbage;
5428 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005429 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005430 * old value was still accessible until then.
5431 */
5432 if (PyDict_DelItem(posix_putenv_garbage,
5433 PyTuple_GET_ITEM(args, 0))) {
5434 /* really not much we can do; just leak */
5435 PyErr_Clear();
5436 }
5437
5438 Py_INCREF(Py_None);
5439 return Py_None;
5440}
5441#endif /* unsetenv */
5442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005443PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005444"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005445Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005446
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005447static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005448posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005449{
5450 int code;
5451 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005452 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005453 return NULL;
5454 message = strerror(code);
5455 if (message == NULL) {
5456 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005457 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005458 return NULL;
5459 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005460 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005461}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005462
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005463
Guido van Rossumc9641791998-08-04 15:26:23 +00005464#ifdef HAVE_SYS_WAIT_H
5465
Fred Drake106c1a02002-04-23 15:58:02 +00005466#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005467PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005468"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005469Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005470
5471static PyObject *
5472posix_WCOREDUMP(PyObject *self, PyObject *args)
5473{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005474 WAIT_TYPE status;
5475 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005476
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005477 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005478 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005479
5480 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005481}
5482#endif /* WCOREDUMP */
5483
5484#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005486"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005487Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005488job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005489
5490static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005491posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005492{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005493 WAIT_TYPE status;
5494 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005495
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005496 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005497 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005498
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005499 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005500}
5501#endif /* WIFCONTINUED */
5502
Guido van Rossumc9641791998-08-04 15:26:23 +00005503#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005505"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005506Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005507
5508static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005509posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005510{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005511 WAIT_TYPE status;
5512 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005513
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005514 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005515 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005516
Fred Drake106c1a02002-04-23 15:58:02 +00005517 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005518}
5519#endif /* WIFSTOPPED */
5520
5521#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005522PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005523"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005525
5526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005527posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005528{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005529 WAIT_TYPE status;
5530 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005531
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005532 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005533 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005534
Fred Drake106c1a02002-04-23 15:58:02 +00005535 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005536}
5537#endif /* WIFSIGNALED */
5538
5539#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005540PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005541"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005542Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005543system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005544
5545static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005546posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005547{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005548 WAIT_TYPE status;
5549 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005550
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005551 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005552 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005553
Fred Drake106c1a02002-04-23 15:58:02 +00005554 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005555}
5556#endif /* WIFEXITED */
5557
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005558#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005560"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005561Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005562
5563static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005564posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005565{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005566 WAIT_TYPE status;
5567 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005568
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005569 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005570 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005571
Guido van Rossumc9641791998-08-04 15:26:23 +00005572 return Py_BuildValue("i", WEXITSTATUS(status));
5573}
5574#endif /* WEXITSTATUS */
5575
5576#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005577PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005578"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005579Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005581
5582static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005583posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005584{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005585 WAIT_TYPE status;
5586 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005587
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005588 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005589 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005590
Guido van Rossumc9641791998-08-04 15:26:23 +00005591 return Py_BuildValue("i", WTERMSIG(status));
5592}
5593#endif /* WTERMSIG */
5594
5595#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005596PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005597"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598Return the signal that stopped the process that provided\n\
5599the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005600
5601static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005602posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005603{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005604 WAIT_TYPE status;
5605 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005606
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005607 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005608 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005609
Guido van Rossumc9641791998-08-04 15:26:23 +00005610 return Py_BuildValue("i", WSTOPSIG(status));
5611}
5612#endif /* WSTOPSIG */
5613
5614#endif /* HAVE_SYS_WAIT_H */
5615
5616
Thomas Wouters477c8d52006-05-27 19:21:47 +00005617#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005618#ifdef _SCO_DS
5619/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5620 needed definitions in sys/statvfs.h */
5621#define _SVID3
5622#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005623#include <sys/statvfs.h>
5624
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005625static PyObject*
5626_pystatvfs_fromstructstatvfs(struct statvfs st) {
5627 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5628 if (v == NULL)
5629 return NULL;
5630
5631#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005632 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5633 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5634 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5635 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5636 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5637 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5638 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5639 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5640 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5641 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005642#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005643 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5644 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005645 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005646 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005647 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005648 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005649 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005650 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005651 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005652 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005653 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005654 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005655 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005656 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005657 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5658 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005659#endif
5660
5661 return v;
5662}
5663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005664PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005665"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005666Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005667
5668static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005669posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005670{
5671 int fd, res;
5672 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005673
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005674 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005675 return NULL;
5676 Py_BEGIN_ALLOW_THREADS
5677 res = fstatvfs(fd, &st);
5678 Py_END_ALLOW_THREADS
5679 if (res != 0)
5680 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005681
5682 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005683}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005684#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005685
5686
Thomas Wouters477c8d52006-05-27 19:21:47 +00005687#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005688#include <sys/statvfs.h>
5689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005690PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005691"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005692Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005693
5694static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005695posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005696{
5697 char *path;
5698 int res;
5699 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005700 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005701 return NULL;
5702 Py_BEGIN_ALLOW_THREADS
5703 res = statvfs(path, &st);
5704 Py_END_ALLOW_THREADS
5705 if (res != 0)
5706 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005707
5708 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005709}
5710#endif /* HAVE_STATVFS */
5711
Fred Drakec9680921999-12-13 16:37:25 +00005712/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5713 * It maps strings representing configuration variable names to
5714 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005715 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005716 * rarely-used constants. There are three separate tables that use
5717 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005718 *
5719 * This code is always included, even if none of the interfaces that
5720 * need it are included. The #if hackery needed to avoid it would be
5721 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005722 */
5723struct constdef {
5724 char *name;
5725 long value;
5726};
5727
Fred Drake12c6e2d1999-12-14 21:25:03 +00005728static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005729conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005730 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005731{
Christian Heimes217cfd12007-12-02 14:31:20 +00005732 if (PyLong_Check(arg)) {
5733 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005734 return 1;
5735 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005736 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005737 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005738 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005739 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005740 size_t hi = tablesize;
5741 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005742 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005743 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005744 PyErr_SetString(PyExc_TypeError,
5745 "configuration names must be strings or integers");
5746 return 0;
5747 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005748 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005749 if (confname == NULL)
5750 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005751 while (lo < hi) {
5752 mid = (lo + hi) / 2;
5753 cmp = strcmp(confname, table[mid].name);
5754 if (cmp < 0)
5755 hi = mid;
5756 else if (cmp > 0)
5757 lo = mid + 1;
5758 else {
5759 *valuep = table[mid].value;
5760 return 1;
5761 }
5762 }
5763 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005764 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005765 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005766}
5767
5768
5769#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5770static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005771#ifdef _PC_ABI_AIO_XFER_MAX
5772 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5773#endif
5774#ifdef _PC_ABI_ASYNC_IO
5775 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5776#endif
Fred Drakec9680921999-12-13 16:37:25 +00005777#ifdef _PC_ASYNC_IO
5778 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5779#endif
5780#ifdef _PC_CHOWN_RESTRICTED
5781 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5782#endif
5783#ifdef _PC_FILESIZEBITS
5784 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5785#endif
5786#ifdef _PC_LAST
5787 {"PC_LAST", _PC_LAST},
5788#endif
5789#ifdef _PC_LINK_MAX
5790 {"PC_LINK_MAX", _PC_LINK_MAX},
5791#endif
5792#ifdef _PC_MAX_CANON
5793 {"PC_MAX_CANON", _PC_MAX_CANON},
5794#endif
5795#ifdef _PC_MAX_INPUT
5796 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5797#endif
5798#ifdef _PC_NAME_MAX
5799 {"PC_NAME_MAX", _PC_NAME_MAX},
5800#endif
5801#ifdef _PC_NO_TRUNC
5802 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5803#endif
5804#ifdef _PC_PATH_MAX
5805 {"PC_PATH_MAX", _PC_PATH_MAX},
5806#endif
5807#ifdef _PC_PIPE_BUF
5808 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5809#endif
5810#ifdef _PC_PRIO_IO
5811 {"PC_PRIO_IO", _PC_PRIO_IO},
5812#endif
5813#ifdef _PC_SOCK_MAXBUF
5814 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5815#endif
5816#ifdef _PC_SYNC_IO
5817 {"PC_SYNC_IO", _PC_SYNC_IO},
5818#endif
5819#ifdef _PC_VDISABLE
5820 {"PC_VDISABLE", _PC_VDISABLE},
5821#endif
5822};
5823
Fred Drakec9680921999-12-13 16:37:25 +00005824static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005825conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005826{
5827 return conv_confname(arg, valuep, posix_constants_pathconf,
5828 sizeof(posix_constants_pathconf)
5829 / sizeof(struct constdef));
5830}
5831#endif
5832
5833#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005834PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005835"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005836Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005837If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005838
5839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005840posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005841{
5842 PyObject *result = NULL;
5843 int name, fd;
5844
Fred Drake12c6e2d1999-12-14 21:25:03 +00005845 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5846 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005847 long limit;
5848
5849 errno = 0;
5850 limit = fpathconf(fd, name);
5851 if (limit == -1 && errno != 0)
5852 posix_error();
5853 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005854 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005855 }
5856 return result;
5857}
5858#endif
5859
5860
5861#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005862PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005863"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005864Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005865If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005866
5867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005868posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005869{
5870 PyObject *result = NULL;
5871 int name;
5872 char *path;
5873
5874 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5875 conv_path_confname, &name)) {
5876 long limit;
5877
5878 errno = 0;
5879 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005880 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005881 if (errno == EINVAL)
5882 /* could be a path or name problem */
5883 posix_error();
5884 else
5885 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005886 }
Fred Drakec9680921999-12-13 16:37:25 +00005887 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005888 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005889 }
5890 return result;
5891}
5892#endif
5893
5894#ifdef HAVE_CONFSTR
5895static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005896#ifdef _CS_ARCHITECTURE
5897 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5898#endif
5899#ifdef _CS_HOSTNAME
5900 {"CS_HOSTNAME", _CS_HOSTNAME},
5901#endif
5902#ifdef _CS_HW_PROVIDER
5903 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5904#endif
5905#ifdef _CS_HW_SERIAL
5906 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5907#endif
5908#ifdef _CS_INITTAB_NAME
5909 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5910#endif
Fred Drakec9680921999-12-13 16:37:25 +00005911#ifdef _CS_LFS64_CFLAGS
5912 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5913#endif
5914#ifdef _CS_LFS64_LDFLAGS
5915 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5916#endif
5917#ifdef _CS_LFS64_LIBS
5918 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5919#endif
5920#ifdef _CS_LFS64_LINTFLAGS
5921 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5922#endif
5923#ifdef _CS_LFS_CFLAGS
5924 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5925#endif
5926#ifdef _CS_LFS_LDFLAGS
5927 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5928#endif
5929#ifdef _CS_LFS_LIBS
5930 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5931#endif
5932#ifdef _CS_LFS_LINTFLAGS
5933 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5934#endif
Fred Draked86ed291999-12-15 15:34:33 +00005935#ifdef _CS_MACHINE
5936 {"CS_MACHINE", _CS_MACHINE},
5937#endif
Fred Drakec9680921999-12-13 16:37:25 +00005938#ifdef _CS_PATH
5939 {"CS_PATH", _CS_PATH},
5940#endif
Fred Draked86ed291999-12-15 15:34:33 +00005941#ifdef _CS_RELEASE
5942 {"CS_RELEASE", _CS_RELEASE},
5943#endif
5944#ifdef _CS_SRPC_DOMAIN
5945 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5946#endif
5947#ifdef _CS_SYSNAME
5948 {"CS_SYSNAME", _CS_SYSNAME},
5949#endif
5950#ifdef _CS_VERSION
5951 {"CS_VERSION", _CS_VERSION},
5952#endif
Fred Drakec9680921999-12-13 16:37:25 +00005953#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5954 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5955#endif
5956#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5957 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5958#endif
5959#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5960 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5961#endif
5962#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5963 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5964#endif
5965#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5966 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5967#endif
5968#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5969 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5970#endif
5971#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5972 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5973#endif
5974#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5975 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5976#endif
5977#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5978 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5979#endif
5980#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5981 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5982#endif
5983#ifdef _CS_XBS5_LP64_OFF64_LIBS
5984 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5985#endif
5986#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5987 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5988#endif
5989#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5990 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5991#endif
5992#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5993 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5994#endif
5995#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5996 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5997#endif
5998#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5999 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6000#endif
Fred Draked86ed291999-12-15 15:34:33 +00006001#ifdef _MIPS_CS_AVAIL_PROCESSORS
6002 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6003#endif
6004#ifdef _MIPS_CS_BASE
6005 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6006#endif
6007#ifdef _MIPS_CS_HOSTID
6008 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6009#endif
6010#ifdef _MIPS_CS_HW_NAME
6011 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6012#endif
6013#ifdef _MIPS_CS_NUM_PROCESSORS
6014 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6015#endif
6016#ifdef _MIPS_CS_OSREL_MAJ
6017 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6018#endif
6019#ifdef _MIPS_CS_OSREL_MIN
6020 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6021#endif
6022#ifdef _MIPS_CS_OSREL_PATCH
6023 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6024#endif
6025#ifdef _MIPS_CS_OS_NAME
6026 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6027#endif
6028#ifdef _MIPS_CS_OS_PROVIDER
6029 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6030#endif
6031#ifdef _MIPS_CS_PROCESSORS
6032 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6033#endif
6034#ifdef _MIPS_CS_SERIAL
6035 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6036#endif
6037#ifdef _MIPS_CS_VENDOR
6038 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6039#endif
Fred Drakec9680921999-12-13 16:37:25 +00006040};
6041
6042static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006043conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006044{
6045 return conv_confname(arg, valuep, posix_constants_confstr,
6046 sizeof(posix_constants_confstr)
6047 / sizeof(struct constdef));
6048}
6049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006050PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006051"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006052Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006053
6054static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006055posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006056{
6057 PyObject *result = NULL;
6058 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006059 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006060
6061 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006062 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006063
Fred Drakec9680921999-12-13 16:37:25 +00006064 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006065 len = confstr(name, buffer, sizeof(buffer));
6066 if (len == 0) {
6067 if (errno) {
6068 posix_error();
6069 }
6070 else {
6071 result = Py_None;
6072 Py_INCREF(Py_None);
6073 }
Fred Drakec9680921999-12-13 16:37:25 +00006074 }
6075 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006076 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006077 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006078 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006079 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006080 }
6081 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006082 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006083 }
6084 }
6085 return result;
6086}
6087#endif
6088
6089
6090#ifdef HAVE_SYSCONF
6091static struct constdef posix_constants_sysconf[] = {
6092#ifdef _SC_2_CHAR_TERM
6093 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6094#endif
6095#ifdef _SC_2_C_BIND
6096 {"SC_2_C_BIND", _SC_2_C_BIND},
6097#endif
6098#ifdef _SC_2_C_DEV
6099 {"SC_2_C_DEV", _SC_2_C_DEV},
6100#endif
6101#ifdef _SC_2_C_VERSION
6102 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6103#endif
6104#ifdef _SC_2_FORT_DEV
6105 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6106#endif
6107#ifdef _SC_2_FORT_RUN
6108 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6109#endif
6110#ifdef _SC_2_LOCALEDEF
6111 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6112#endif
6113#ifdef _SC_2_SW_DEV
6114 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6115#endif
6116#ifdef _SC_2_UPE
6117 {"SC_2_UPE", _SC_2_UPE},
6118#endif
6119#ifdef _SC_2_VERSION
6120 {"SC_2_VERSION", _SC_2_VERSION},
6121#endif
Fred Draked86ed291999-12-15 15:34:33 +00006122#ifdef _SC_ABI_ASYNCHRONOUS_IO
6123 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6124#endif
6125#ifdef _SC_ACL
6126 {"SC_ACL", _SC_ACL},
6127#endif
Fred Drakec9680921999-12-13 16:37:25 +00006128#ifdef _SC_AIO_LISTIO_MAX
6129 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6130#endif
Fred Drakec9680921999-12-13 16:37:25 +00006131#ifdef _SC_AIO_MAX
6132 {"SC_AIO_MAX", _SC_AIO_MAX},
6133#endif
6134#ifdef _SC_AIO_PRIO_DELTA_MAX
6135 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6136#endif
6137#ifdef _SC_ARG_MAX
6138 {"SC_ARG_MAX", _SC_ARG_MAX},
6139#endif
6140#ifdef _SC_ASYNCHRONOUS_IO
6141 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6142#endif
6143#ifdef _SC_ATEXIT_MAX
6144 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6145#endif
Fred Draked86ed291999-12-15 15:34:33 +00006146#ifdef _SC_AUDIT
6147 {"SC_AUDIT", _SC_AUDIT},
6148#endif
Fred Drakec9680921999-12-13 16:37:25 +00006149#ifdef _SC_AVPHYS_PAGES
6150 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6151#endif
6152#ifdef _SC_BC_BASE_MAX
6153 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6154#endif
6155#ifdef _SC_BC_DIM_MAX
6156 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6157#endif
6158#ifdef _SC_BC_SCALE_MAX
6159 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6160#endif
6161#ifdef _SC_BC_STRING_MAX
6162 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6163#endif
Fred Draked86ed291999-12-15 15:34:33 +00006164#ifdef _SC_CAP
6165 {"SC_CAP", _SC_CAP},
6166#endif
Fred Drakec9680921999-12-13 16:37:25 +00006167#ifdef _SC_CHARCLASS_NAME_MAX
6168 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6169#endif
6170#ifdef _SC_CHAR_BIT
6171 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6172#endif
6173#ifdef _SC_CHAR_MAX
6174 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6175#endif
6176#ifdef _SC_CHAR_MIN
6177 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6178#endif
6179#ifdef _SC_CHILD_MAX
6180 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6181#endif
6182#ifdef _SC_CLK_TCK
6183 {"SC_CLK_TCK", _SC_CLK_TCK},
6184#endif
6185#ifdef _SC_COHER_BLKSZ
6186 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6187#endif
6188#ifdef _SC_COLL_WEIGHTS_MAX
6189 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6190#endif
6191#ifdef _SC_DCACHE_ASSOC
6192 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6193#endif
6194#ifdef _SC_DCACHE_BLKSZ
6195 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6196#endif
6197#ifdef _SC_DCACHE_LINESZ
6198 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6199#endif
6200#ifdef _SC_DCACHE_SZ
6201 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6202#endif
6203#ifdef _SC_DCACHE_TBLKSZ
6204 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6205#endif
6206#ifdef _SC_DELAYTIMER_MAX
6207 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6208#endif
6209#ifdef _SC_EQUIV_CLASS_MAX
6210 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6211#endif
6212#ifdef _SC_EXPR_NEST_MAX
6213 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6214#endif
6215#ifdef _SC_FSYNC
6216 {"SC_FSYNC", _SC_FSYNC},
6217#endif
6218#ifdef _SC_GETGR_R_SIZE_MAX
6219 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6220#endif
6221#ifdef _SC_GETPW_R_SIZE_MAX
6222 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6223#endif
6224#ifdef _SC_ICACHE_ASSOC
6225 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6226#endif
6227#ifdef _SC_ICACHE_BLKSZ
6228 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6229#endif
6230#ifdef _SC_ICACHE_LINESZ
6231 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6232#endif
6233#ifdef _SC_ICACHE_SZ
6234 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6235#endif
Fred Draked86ed291999-12-15 15:34:33 +00006236#ifdef _SC_INF
6237 {"SC_INF", _SC_INF},
6238#endif
Fred Drakec9680921999-12-13 16:37:25 +00006239#ifdef _SC_INT_MAX
6240 {"SC_INT_MAX", _SC_INT_MAX},
6241#endif
6242#ifdef _SC_INT_MIN
6243 {"SC_INT_MIN", _SC_INT_MIN},
6244#endif
6245#ifdef _SC_IOV_MAX
6246 {"SC_IOV_MAX", _SC_IOV_MAX},
6247#endif
Fred Draked86ed291999-12-15 15:34:33 +00006248#ifdef _SC_IP_SECOPTS
6249 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6250#endif
Fred Drakec9680921999-12-13 16:37:25 +00006251#ifdef _SC_JOB_CONTROL
6252 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6253#endif
Fred Draked86ed291999-12-15 15:34:33 +00006254#ifdef _SC_KERN_POINTERS
6255 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6256#endif
6257#ifdef _SC_KERN_SIM
6258 {"SC_KERN_SIM", _SC_KERN_SIM},
6259#endif
Fred Drakec9680921999-12-13 16:37:25 +00006260#ifdef _SC_LINE_MAX
6261 {"SC_LINE_MAX", _SC_LINE_MAX},
6262#endif
6263#ifdef _SC_LOGIN_NAME_MAX
6264 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6265#endif
6266#ifdef _SC_LOGNAME_MAX
6267 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6268#endif
6269#ifdef _SC_LONG_BIT
6270 {"SC_LONG_BIT", _SC_LONG_BIT},
6271#endif
Fred Draked86ed291999-12-15 15:34:33 +00006272#ifdef _SC_MAC
6273 {"SC_MAC", _SC_MAC},
6274#endif
Fred Drakec9680921999-12-13 16:37:25 +00006275#ifdef _SC_MAPPED_FILES
6276 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6277#endif
6278#ifdef _SC_MAXPID
6279 {"SC_MAXPID", _SC_MAXPID},
6280#endif
6281#ifdef _SC_MB_LEN_MAX
6282 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6283#endif
6284#ifdef _SC_MEMLOCK
6285 {"SC_MEMLOCK", _SC_MEMLOCK},
6286#endif
6287#ifdef _SC_MEMLOCK_RANGE
6288 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6289#endif
6290#ifdef _SC_MEMORY_PROTECTION
6291 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6292#endif
6293#ifdef _SC_MESSAGE_PASSING
6294 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6295#endif
Fred Draked86ed291999-12-15 15:34:33 +00006296#ifdef _SC_MMAP_FIXED_ALIGNMENT
6297 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6298#endif
Fred Drakec9680921999-12-13 16:37:25 +00006299#ifdef _SC_MQ_OPEN_MAX
6300 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6301#endif
6302#ifdef _SC_MQ_PRIO_MAX
6303 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6304#endif
Fred Draked86ed291999-12-15 15:34:33 +00006305#ifdef _SC_NACLS_MAX
6306 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6307#endif
Fred Drakec9680921999-12-13 16:37:25 +00006308#ifdef _SC_NGROUPS_MAX
6309 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6310#endif
6311#ifdef _SC_NL_ARGMAX
6312 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6313#endif
6314#ifdef _SC_NL_LANGMAX
6315 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6316#endif
6317#ifdef _SC_NL_MSGMAX
6318 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6319#endif
6320#ifdef _SC_NL_NMAX
6321 {"SC_NL_NMAX", _SC_NL_NMAX},
6322#endif
6323#ifdef _SC_NL_SETMAX
6324 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6325#endif
6326#ifdef _SC_NL_TEXTMAX
6327 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6328#endif
6329#ifdef _SC_NPROCESSORS_CONF
6330 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6331#endif
6332#ifdef _SC_NPROCESSORS_ONLN
6333 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6334#endif
Fred Draked86ed291999-12-15 15:34:33 +00006335#ifdef _SC_NPROC_CONF
6336 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6337#endif
6338#ifdef _SC_NPROC_ONLN
6339 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6340#endif
Fred Drakec9680921999-12-13 16:37:25 +00006341#ifdef _SC_NZERO
6342 {"SC_NZERO", _SC_NZERO},
6343#endif
6344#ifdef _SC_OPEN_MAX
6345 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6346#endif
6347#ifdef _SC_PAGESIZE
6348 {"SC_PAGESIZE", _SC_PAGESIZE},
6349#endif
6350#ifdef _SC_PAGE_SIZE
6351 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6352#endif
6353#ifdef _SC_PASS_MAX
6354 {"SC_PASS_MAX", _SC_PASS_MAX},
6355#endif
6356#ifdef _SC_PHYS_PAGES
6357 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6358#endif
6359#ifdef _SC_PII
6360 {"SC_PII", _SC_PII},
6361#endif
6362#ifdef _SC_PII_INTERNET
6363 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6364#endif
6365#ifdef _SC_PII_INTERNET_DGRAM
6366 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6367#endif
6368#ifdef _SC_PII_INTERNET_STREAM
6369 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6370#endif
6371#ifdef _SC_PII_OSI
6372 {"SC_PII_OSI", _SC_PII_OSI},
6373#endif
6374#ifdef _SC_PII_OSI_CLTS
6375 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6376#endif
6377#ifdef _SC_PII_OSI_COTS
6378 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6379#endif
6380#ifdef _SC_PII_OSI_M
6381 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6382#endif
6383#ifdef _SC_PII_SOCKET
6384 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6385#endif
6386#ifdef _SC_PII_XTI
6387 {"SC_PII_XTI", _SC_PII_XTI},
6388#endif
6389#ifdef _SC_POLL
6390 {"SC_POLL", _SC_POLL},
6391#endif
6392#ifdef _SC_PRIORITIZED_IO
6393 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6394#endif
6395#ifdef _SC_PRIORITY_SCHEDULING
6396 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6397#endif
6398#ifdef _SC_REALTIME_SIGNALS
6399 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6400#endif
6401#ifdef _SC_RE_DUP_MAX
6402 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6403#endif
6404#ifdef _SC_RTSIG_MAX
6405 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6406#endif
6407#ifdef _SC_SAVED_IDS
6408 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6409#endif
6410#ifdef _SC_SCHAR_MAX
6411 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6412#endif
6413#ifdef _SC_SCHAR_MIN
6414 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6415#endif
6416#ifdef _SC_SELECT
6417 {"SC_SELECT", _SC_SELECT},
6418#endif
6419#ifdef _SC_SEMAPHORES
6420 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6421#endif
6422#ifdef _SC_SEM_NSEMS_MAX
6423 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6424#endif
6425#ifdef _SC_SEM_VALUE_MAX
6426 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6427#endif
6428#ifdef _SC_SHARED_MEMORY_OBJECTS
6429 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6430#endif
6431#ifdef _SC_SHRT_MAX
6432 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6433#endif
6434#ifdef _SC_SHRT_MIN
6435 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6436#endif
6437#ifdef _SC_SIGQUEUE_MAX
6438 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6439#endif
6440#ifdef _SC_SIGRT_MAX
6441 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6442#endif
6443#ifdef _SC_SIGRT_MIN
6444 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6445#endif
Fred Draked86ed291999-12-15 15:34:33 +00006446#ifdef _SC_SOFTPOWER
6447 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6448#endif
Fred Drakec9680921999-12-13 16:37:25 +00006449#ifdef _SC_SPLIT_CACHE
6450 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6451#endif
6452#ifdef _SC_SSIZE_MAX
6453 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6454#endif
6455#ifdef _SC_STACK_PROT
6456 {"SC_STACK_PROT", _SC_STACK_PROT},
6457#endif
6458#ifdef _SC_STREAM_MAX
6459 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6460#endif
6461#ifdef _SC_SYNCHRONIZED_IO
6462 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6463#endif
6464#ifdef _SC_THREADS
6465 {"SC_THREADS", _SC_THREADS},
6466#endif
6467#ifdef _SC_THREAD_ATTR_STACKADDR
6468 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6469#endif
6470#ifdef _SC_THREAD_ATTR_STACKSIZE
6471 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6472#endif
6473#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6474 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6475#endif
6476#ifdef _SC_THREAD_KEYS_MAX
6477 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6478#endif
6479#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6480 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6481#endif
6482#ifdef _SC_THREAD_PRIO_INHERIT
6483 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6484#endif
6485#ifdef _SC_THREAD_PRIO_PROTECT
6486 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6487#endif
6488#ifdef _SC_THREAD_PROCESS_SHARED
6489 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6490#endif
6491#ifdef _SC_THREAD_SAFE_FUNCTIONS
6492 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6493#endif
6494#ifdef _SC_THREAD_STACK_MIN
6495 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6496#endif
6497#ifdef _SC_THREAD_THREADS_MAX
6498 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6499#endif
6500#ifdef _SC_TIMERS
6501 {"SC_TIMERS", _SC_TIMERS},
6502#endif
6503#ifdef _SC_TIMER_MAX
6504 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6505#endif
6506#ifdef _SC_TTY_NAME_MAX
6507 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6508#endif
6509#ifdef _SC_TZNAME_MAX
6510 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6511#endif
6512#ifdef _SC_T_IOV_MAX
6513 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6514#endif
6515#ifdef _SC_UCHAR_MAX
6516 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6517#endif
6518#ifdef _SC_UINT_MAX
6519 {"SC_UINT_MAX", _SC_UINT_MAX},
6520#endif
6521#ifdef _SC_UIO_MAXIOV
6522 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6523#endif
6524#ifdef _SC_ULONG_MAX
6525 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6526#endif
6527#ifdef _SC_USHRT_MAX
6528 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6529#endif
6530#ifdef _SC_VERSION
6531 {"SC_VERSION", _SC_VERSION},
6532#endif
6533#ifdef _SC_WORD_BIT
6534 {"SC_WORD_BIT", _SC_WORD_BIT},
6535#endif
6536#ifdef _SC_XBS5_ILP32_OFF32
6537 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6538#endif
6539#ifdef _SC_XBS5_ILP32_OFFBIG
6540 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6541#endif
6542#ifdef _SC_XBS5_LP64_OFF64
6543 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6544#endif
6545#ifdef _SC_XBS5_LPBIG_OFFBIG
6546 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6547#endif
6548#ifdef _SC_XOPEN_CRYPT
6549 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6550#endif
6551#ifdef _SC_XOPEN_ENH_I18N
6552 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6553#endif
6554#ifdef _SC_XOPEN_LEGACY
6555 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6556#endif
6557#ifdef _SC_XOPEN_REALTIME
6558 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6559#endif
6560#ifdef _SC_XOPEN_REALTIME_THREADS
6561 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6562#endif
6563#ifdef _SC_XOPEN_SHM
6564 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6565#endif
6566#ifdef _SC_XOPEN_UNIX
6567 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6568#endif
6569#ifdef _SC_XOPEN_VERSION
6570 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6571#endif
6572#ifdef _SC_XOPEN_XCU_VERSION
6573 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6574#endif
6575#ifdef _SC_XOPEN_XPG2
6576 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6577#endif
6578#ifdef _SC_XOPEN_XPG3
6579 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6580#endif
6581#ifdef _SC_XOPEN_XPG4
6582 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6583#endif
6584};
6585
6586static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006587conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006588{
6589 return conv_confname(arg, valuep, posix_constants_sysconf,
6590 sizeof(posix_constants_sysconf)
6591 / sizeof(struct constdef));
6592}
6593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006594PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006595"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006596Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006597
6598static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006599posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006600{
6601 PyObject *result = NULL;
6602 int name;
6603
6604 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6605 int value;
6606
6607 errno = 0;
6608 value = sysconf(name);
6609 if (value == -1 && errno != 0)
6610 posix_error();
6611 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006612 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006613 }
6614 return result;
6615}
6616#endif
6617
6618
Fred Drakebec628d1999-12-15 18:31:10 +00006619/* This code is used to ensure that the tables of configuration value names
6620 * are in sorted order as required by conv_confname(), and also to build the
6621 * the exported dictionaries that are used to publish information about the
6622 * names available on the host platform.
6623 *
6624 * Sorting the table at runtime ensures that the table is properly ordered
6625 * when used, even for platforms we're not able to test on. It also makes
6626 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006627 */
Fred Drakebec628d1999-12-15 18:31:10 +00006628
6629static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006630cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006631{
6632 const struct constdef *c1 =
6633 (const struct constdef *) v1;
6634 const struct constdef *c2 =
6635 (const struct constdef *) v2;
6636
6637 return strcmp(c1->name, c2->name);
6638}
6639
6640static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006641setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006642 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006643{
Fred Drakebec628d1999-12-15 18:31:10 +00006644 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006645 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006646
6647 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6648 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006649 if (d == NULL)
6650 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006651
Barry Warsaw3155db32000-04-13 15:20:40 +00006652 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006653 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006654 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6655 Py_XDECREF(o);
6656 Py_DECREF(d);
6657 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006658 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006659 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006660 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006661 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006662}
6663
Fred Drakebec628d1999-12-15 18:31:10 +00006664/* Return -1 on failure, 0 on success. */
6665static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006666setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006667{
6668#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006669 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006670 sizeof(posix_constants_pathconf)
6671 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006672 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006673 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006674#endif
6675#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006676 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006677 sizeof(posix_constants_confstr)
6678 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006679 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006680 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006681#endif
6682#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006683 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006684 sizeof(posix_constants_sysconf)
6685 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006686 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006687 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006688#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006689 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006690}
Fred Draked86ed291999-12-15 15:34:33 +00006691
6692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006693PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006694"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006695Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006696in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006697
6698static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006699posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006700{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006701 abort();
6702 /*NOTREACHED*/
6703 Py_FatalError("abort() called from Python code didn't abort!");
6704 return NULL;
6705}
Fred Drakebec628d1999-12-15 18:31:10 +00006706
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006707#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006708PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006709"startfile(filepath [, operation]) - Start a file with its associated\n\
6710application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006711\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006712When \"operation\" is not specified or \"open\", this acts like\n\
6713double-clicking the file in Explorer, or giving the file name as an\n\
6714argument to the DOS \"start\" command: the file is opened with whatever\n\
6715application (if any) its extension is associated.\n\
6716When another \"operation\" is given, it specifies what should be done with\n\
6717the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006718\n\
6719startfile returns as soon as the associated application is launched.\n\
6720There is no option to wait for the application to close, and no way\n\
6721to retrieve the application's exit status.\n\
6722\n\
6723The filepath is relative to the current directory. If you want to use\n\
6724an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006725the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006726
6727static PyObject *
6728win32_startfile(PyObject *self, PyObject *args)
6729{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006730 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006731 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006732 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006733 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006734
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006735 PyObject *unipath, *woperation = NULL;
6736 if (!PyArg_ParseTuple(args, "U|s:startfile",
6737 &unipath, &operation)) {
6738 PyErr_Clear();
6739 goto normal;
6740 }
6741
6742 if (operation) {
6743 woperation = PyUnicode_DecodeASCII(operation,
6744 strlen(operation), NULL);
6745 if (!woperation) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006746 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006747 operation = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006748 goto normal;
6749 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006750 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006751
6752 Py_BEGIN_ALLOW_THREADS
6753 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6754 PyUnicode_AS_UNICODE(unipath),
6755 NULL, NULL, SW_SHOWNORMAL);
6756 Py_END_ALLOW_THREADS
6757
6758 Py_XDECREF(woperation);
6759 if (rc <= (HINSTANCE)32) {
6760 PyObject *errval = win32_error_unicode("startfile",
6761 PyUnicode_AS_UNICODE(unipath));
6762 return errval;
6763 }
6764 Py_INCREF(Py_None);
6765 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006766
6767normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006768 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6769 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006770 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006771 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006772 filepath = bytes2str(ofilepath, 1);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006773 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006774 rc = ShellExecute((HWND)0, operation, filepath,
6775 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006776 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006777 if (rc <= (HINSTANCE)32) {
6778 PyObject *errval = win32_error("startfile", filepath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006779 release_bytes(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006780 return errval;
6781 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00006782 release_bytes(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006783 Py_INCREF(Py_None);
6784 return Py_None;
6785}
6786#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006787
Martin v. Löwis438b5342002-12-27 10:16:42 +00006788#ifdef HAVE_GETLOADAVG
6789PyDoc_STRVAR(posix_getloadavg__doc__,
6790"getloadavg() -> (float, float, float)\n\n\
6791Return the number of processes in the system run queue averaged over\n\
6792the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6793was unobtainable");
6794
6795static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006796posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006797{
6798 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006799 if (getloadavg(loadavg, 3)!=3) {
6800 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6801 return NULL;
6802 } else
6803 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6804}
6805#endif
6806
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006807#ifdef MS_WINDOWS
6808
6809PyDoc_STRVAR(win32_urandom__doc__,
6810"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006811Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006812
6813typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6814 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6815 DWORD dwFlags );
6816typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6817 BYTE *pbBuffer );
6818
6819static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006820/* This handle is never explicitly released. Instead, the operating
6821 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006822static HCRYPTPROV hCryptProv = 0;
6823
Tim Peters4ad82172004-08-30 17:02:04 +00006824static PyObject*
6825win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006826{
Tim Petersd3115382004-08-30 17:36:46 +00006827 int howMany;
6828 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006829
Tim Peters4ad82172004-08-30 17:02:04 +00006830 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006831 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006832 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006833 if (howMany < 0)
6834 return PyErr_Format(PyExc_ValueError,
6835 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006836
Tim Peters4ad82172004-08-30 17:02:04 +00006837 if (hCryptProv == 0) {
6838 HINSTANCE hAdvAPI32 = NULL;
6839 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006840
Tim Peters4ad82172004-08-30 17:02:04 +00006841 /* Obtain handle to the DLL containing CryptoAPI
6842 This should not fail */
6843 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6844 if(hAdvAPI32 == NULL)
6845 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006846
Tim Peters4ad82172004-08-30 17:02:04 +00006847 /* Obtain pointers to the CryptoAPI functions
6848 This will fail on some early versions of Win95 */
6849 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6850 hAdvAPI32,
6851 "CryptAcquireContextA");
6852 if (pCryptAcquireContext == NULL)
6853 return PyErr_Format(PyExc_NotImplementedError,
6854 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006855
Tim Peters4ad82172004-08-30 17:02:04 +00006856 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6857 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006858 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006859 return PyErr_Format(PyExc_NotImplementedError,
6860 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006861
Tim Peters4ad82172004-08-30 17:02:04 +00006862 /* Acquire context */
6863 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6864 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6865 return win32_error("CryptAcquireContext", NULL);
6866 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006867
Tim Peters4ad82172004-08-30 17:02:04 +00006868 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006869 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006870 if (result != NULL) {
6871 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006872 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006873 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006874 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006875 Py_DECREF(result);
6876 return win32_error("CryptGenRandom", NULL);
6877 }
Tim Peters4ad82172004-08-30 17:02:04 +00006878 }
Tim Petersd3115382004-08-30 17:36:46 +00006879 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006880}
6881#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006882
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006883PyDoc_STRVAR(device_encoding__doc__,
6884"device_encoding(fd) -> str\n\n\
6885Return a string describing the encoding of the device\n\
6886if the output is a terminal; else return None.");
6887
6888static PyObject *
6889device_encoding(PyObject *self, PyObject *args)
6890{
6891 int fd;
6892 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6893 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006894 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006895 Py_INCREF(Py_None);
6896 return Py_None;
6897 }
6898#if defined(MS_WINDOWS) || defined(MS_WIN64)
6899 if (fd == 0) {
6900 char buf[100];
6901 sprintf(buf, "cp%d", GetConsoleCP());
6902 return PyUnicode_FromString(buf);
6903 }
6904 if (fd == 1 || fd == 2) {
6905 char buf[100];
6906 sprintf(buf, "cp%d", GetConsoleOutputCP());
6907 return PyUnicode_FromString(buf);
6908 }
6909#elif defined(CODESET)
6910 {
6911 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006912 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006913 return PyUnicode_FromString(codeset);
6914 }
6915#endif
6916 Py_INCREF(Py_None);
6917 return Py_None;
6918}
6919
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006920#ifdef __VMS
6921/* Use openssl random routine */
6922#include <openssl/rand.h>
6923PyDoc_STRVAR(vms_urandom__doc__,
6924"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006925Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006926
6927static PyObject*
6928vms_urandom(PyObject *self, PyObject *args)
6929{
6930 int howMany;
6931 PyObject* result;
6932
6933 /* Read arguments */
6934 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6935 return NULL;
6936 if (howMany < 0)
6937 return PyErr_Format(PyExc_ValueError,
6938 "negative argument not allowed");
6939
6940 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006941 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006942 if (result != NULL) {
6943 /* Get random data */
6944 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006945 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006946 howMany) < 0) {
6947 Py_DECREF(result);
6948 return PyErr_Format(PyExc_ValueError,
6949 "RAND_pseudo_bytes");
6950 }
6951 }
6952 return result;
6953}
6954#endif
6955
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006956static PyMethodDef posix_methods[] = {
6957 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6958#ifdef HAVE_TTYNAME
6959 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6960#endif
6961 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006962#ifdef HAVE_CHFLAGS
6963 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6964#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006965 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00006966#ifdef HAVE_FCHMOD
6967 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
6968#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006969#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006970 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006971#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00006972#ifdef HAVE_LCHMOD
6973 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
6974#endif /* HAVE_LCHMOD */
6975#ifdef HAVE_FCHOWN
6976 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
6977#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006978#ifdef HAVE_LCHFLAGS
6979 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6980#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006981#ifdef HAVE_LCHOWN
6982 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6983#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006984#ifdef HAVE_CHROOT
6985 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6986#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006987#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006988 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006989#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006990#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00006991 {"getcwd", (PyCFunction)posix_getcwd_unicode,
6992 METH_NOARGS, posix_getcwd__doc__},
6993 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
6994 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006995#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006996#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006997 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006998#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006999 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7000 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7001 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007002#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007003 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007004#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007005#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007006 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007007#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007008 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7009 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7010 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007011 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007012#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007013 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007014#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007015#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007016 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007017#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007018 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007019#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007020 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007021#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007022 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7023 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7024 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007025#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007026 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007027#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007028 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007029#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007030 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7031 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007032#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007033#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007034 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7035 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007036#if defined(PYOS_OS2)
7037 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7038 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7039#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007040#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007041#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007042 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007043#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007044#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007045 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007046#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007047#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007048 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007049#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007050#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007051 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007052#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007053#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007054 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007055#endif /* HAVE_GETEGID */
7056#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007057 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007058#endif /* HAVE_GETEUID */
7059#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007060 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007061#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007062#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007063 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007065 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007066#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007067 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007068#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007069#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007070 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007071#endif /* HAVE_GETPPID */
7072#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007073 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007074#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007075#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007076 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007077#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007078#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007079 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007080#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007081#ifdef HAVE_KILLPG
7082 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7083#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007084#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007085 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007086#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007087#ifdef MS_WINDOWS
7088 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7089#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007090#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007091 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007092#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007093#ifdef HAVE_SETEUID
7094 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7095#endif /* HAVE_SETEUID */
7096#ifdef HAVE_SETEGID
7097 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7098#endif /* HAVE_SETEGID */
7099#ifdef HAVE_SETREUID
7100 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7101#endif /* HAVE_SETREUID */
7102#ifdef HAVE_SETREGID
7103 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7104#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007105#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007106 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007107#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007108#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007109 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007110#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007111#ifdef HAVE_GETPGID
7112 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7113#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007114#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007115 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007116#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007117#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007118 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007119#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007120#ifdef HAVE_WAIT3
7121 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7122#endif /* HAVE_WAIT3 */
7123#ifdef HAVE_WAIT4
7124 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7125#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007126#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007127 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007128#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007129#ifdef HAVE_GETSID
7130 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7131#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007132#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007133 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007134#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007135#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007136 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007137#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007138#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007139 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007140#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007141#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007142 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007143#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007144 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7145 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007146 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007147 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007148 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7149 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7150 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7151 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7152 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7153 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007154 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007155#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007156 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007157#endif
7158#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007159 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007160#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007161#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007162 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7163#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007164#ifdef HAVE_DEVICE_MACROS
7165 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7166 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7167 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7168#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007169#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007170 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007171#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007172#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007173 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007174#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007175#ifdef HAVE_UNSETENV
7176 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7177#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007178 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007179#ifdef HAVE_FCHDIR
7180 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7181#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007182#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007183 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007184#endif
7185#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007186 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007187#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007188#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007189#ifdef WCOREDUMP
7190 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7191#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007192#ifdef WIFCONTINUED
7193 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7194#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007195#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007196 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007197#endif /* WIFSTOPPED */
7198#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007199 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007200#endif /* WIFSIGNALED */
7201#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007202 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007203#endif /* WIFEXITED */
7204#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007205 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007206#endif /* WEXITSTATUS */
7207#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007208 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007209#endif /* WTERMSIG */
7210#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007211 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007212#endif /* WSTOPSIG */
7213#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007214#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007215 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007216#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007217#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007218 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007219#endif
Fred Drakec9680921999-12-13 16:37:25 +00007220#ifdef HAVE_CONFSTR
7221 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7222#endif
7223#ifdef HAVE_SYSCONF
7224 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7225#endif
7226#ifdef HAVE_FPATHCONF
7227 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7228#endif
7229#ifdef HAVE_PATHCONF
7230 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7231#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007232 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007233#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007234 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7235#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007236#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007237 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007238#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007239 #ifdef MS_WINDOWS
7240 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7241 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007242 #ifdef __VMS
7243 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7244 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007245 {NULL, NULL} /* Sentinel */
7246};
7247
7248
Barry Warsaw4a342091996-12-19 23:50:02 +00007249static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007250ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007251{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007252 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007253}
7254
Guido van Rossumd48f2521997-12-05 22:19:34 +00007255#if defined(PYOS_OS2)
7256/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007257static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007258{
7259 APIRET rc;
7260 ULONG values[QSV_MAX+1];
7261 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007262 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007263
7264 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007265 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007266 Py_END_ALLOW_THREADS
7267
7268 if (rc != NO_ERROR) {
7269 os2_error(rc);
7270 return -1;
7271 }
7272
Fred Drake4d1e64b2002-04-15 19:40:07 +00007273 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7274 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7275 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7276 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7277 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7278 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7279 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007280
7281 switch (values[QSV_VERSION_MINOR]) {
7282 case 0: ver = "2.00"; break;
7283 case 10: ver = "2.10"; break;
7284 case 11: ver = "2.11"; break;
7285 case 30: ver = "3.00"; break;
7286 case 40: ver = "4.00"; break;
7287 case 50: ver = "5.00"; break;
7288 default:
Tim Peters885d4572001-11-28 20:27:42 +00007289 PyOS_snprintf(tmp, sizeof(tmp),
7290 "%d-%d", values[QSV_VERSION_MAJOR],
7291 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007292 ver = &tmp[0];
7293 }
7294
7295 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007296 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007297 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007298
7299 /* Add Indicator of Which Drive was Used to Boot the System */
7300 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7301 tmp[1] = ':';
7302 tmp[2] = '\0';
7303
Fred Drake4d1e64b2002-04-15 19:40:07 +00007304 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007305}
7306#endif
7307
Barry Warsaw4a342091996-12-19 23:50:02 +00007308static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007309all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007310{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007311#ifdef F_OK
7312 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007313#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007314#ifdef R_OK
7315 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007316#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007317#ifdef W_OK
7318 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007319#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007320#ifdef X_OK
7321 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007322#endif
Fred Drakec9680921999-12-13 16:37:25 +00007323#ifdef NGROUPS_MAX
7324 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7325#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007326#ifdef TMP_MAX
7327 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7328#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007329#ifdef WCONTINUED
7330 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7331#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007332#ifdef WNOHANG
7333 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007334#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007335#ifdef WUNTRACED
7336 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7337#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007338#ifdef O_RDONLY
7339 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7340#endif
7341#ifdef O_WRONLY
7342 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7343#endif
7344#ifdef O_RDWR
7345 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7346#endif
7347#ifdef O_NDELAY
7348 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7349#endif
7350#ifdef O_NONBLOCK
7351 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7352#endif
7353#ifdef O_APPEND
7354 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7355#endif
7356#ifdef O_DSYNC
7357 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7358#endif
7359#ifdef O_RSYNC
7360 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7361#endif
7362#ifdef O_SYNC
7363 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7364#endif
7365#ifdef O_NOCTTY
7366 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7367#endif
7368#ifdef O_CREAT
7369 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7370#endif
7371#ifdef O_EXCL
7372 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7373#endif
7374#ifdef O_TRUNC
7375 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7376#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007377#ifdef O_BINARY
7378 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7379#endif
7380#ifdef O_TEXT
7381 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7382#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007383#ifdef O_LARGEFILE
7384 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7385#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007386#ifdef O_SHLOCK
7387 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7388#endif
7389#ifdef O_EXLOCK
7390 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7391#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007392
Tim Peters5aa91602002-01-30 05:46:57 +00007393/* MS Windows */
7394#ifdef O_NOINHERIT
7395 /* Don't inherit in child processes. */
7396 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7397#endif
7398#ifdef _O_SHORT_LIVED
7399 /* Optimize for short life (keep in memory). */
7400 /* MS forgot to define this one with a non-underscore form too. */
7401 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7402#endif
7403#ifdef O_TEMPORARY
7404 /* Automatically delete when last handle is closed. */
7405 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7406#endif
7407#ifdef O_RANDOM
7408 /* Optimize for random access. */
7409 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7410#endif
7411#ifdef O_SEQUENTIAL
7412 /* Optimize for sequential access. */
7413 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7414#endif
7415
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007416/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007417#ifdef O_ASYNC
7418 /* Send a SIGIO signal whenever input or output
7419 becomes available on file descriptor */
7420 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7421#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007422#ifdef O_DIRECT
7423 /* Direct disk access. */
7424 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7425#endif
7426#ifdef O_DIRECTORY
7427 /* Must be a directory. */
7428 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7429#endif
7430#ifdef O_NOFOLLOW
7431 /* Do not follow links. */
7432 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7433#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007434#ifdef O_NOATIME
7435 /* Do not update the access time. */
7436 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7437#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007438
Barry Warsaw5676bd12003-01-07 20:57:09 +00007439 /* These come from sysexits.h */
7440#ifdef EX_OK
7441 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007442#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007443#ifdef EX_USAGE
7444 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007445#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007446#ifdef EX_DATAERR
7447 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007448#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007449#ifdef EX_NOINPUT
7450 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007451#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007452#ifdef EX_NOUSER
7453 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007454#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007455#ifdef EX_NOHOST
7456 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007457#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007458#ifdef EX_UNAVAILABLE
7459 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007460#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007461#ifdef EX_SOFTWARE
7462 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007463#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007464#ifdef EX_OSERR
7465 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007466#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007467#ifdef EX_OSFILE
7468 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007469#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007470#ifdef EX_CANTCREAT
7471 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007472#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007473#ifdef EX_IOERR
7474 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007475#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007476#ifdef EX_TEMPFAIL
7477 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007478#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007479#ifdef EX_PROTOCOL
7480 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007481#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007482#ifdef EX_NOPERM
7483 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007484#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007485#ifdef EX_CONFIG
7486 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007487#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007488#ifdef EX_NOTFOUND
7489 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007490#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007491
Guido van Rossum246bc171999-02-01 23:54:31 +00007492#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007493#if defined(PYOS_OS2) && defined(PYCC_GCC)
7494 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7495 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7496 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7497 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7498 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7499 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7500 if (ins(d, "P_PM", (long)P_PM)) return -1;
7501 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7502 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7503 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7504 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7505 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7506 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7507 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7508 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7509 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7510 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7511 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7512 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7513 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7514#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007515 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7516 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7517 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7518 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7519 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007520#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007521#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007522
Guido van Rossumd48f2521997-12-05 22:19:34 +00007523#if defined(PYOS_OS2)
7524 if (insertvalues(d)) return -1;
7525#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007526 return 0;
7527}
7528
7529
Tim Peters5aa91602002-01-30 05:46:57 +00007530#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007531#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007532#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007533
7534#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007535#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007536#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007537
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007538#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007539#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007540#define MODNAME "posix"
7541#endif
7542
Martin v. Löwis1a214512008-06-11 05:26:20 +00007543static struct PyModuleDef posixmodule = {
7544 PyModuleDef_HEAD_INIT,
7545 MODNAME,
7546 posix__doc__,
7547 -1,
7548 posix_methods,
7549 NULL,
7550 NULL,
7551 NULL,
7552 NULL
7553};
7554
7555
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007556PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007557INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007558{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007559 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007560
Martin v. Löwis1a214512008-06-11 05:26:20 +00007561 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007562 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007563 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007564
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007565 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007566 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007567 Py_XINCREF(v);
7568 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007569 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007570 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007571
Fred Drake4d1e64b2002-04-15 19:40:07 +00007572 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007573 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007574
Fred Drake4d1e64b2002-04-15 19:40:07 +00007575 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007576 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007577
Fred Drake4d1e64b2002-04-15 19:40:07 +00007578 Py_INCREF(PyExc_OSError);
7579 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007580
Guido van Rossumb3d39562000-01-31 18:41:26 +00007581#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007582 if (posix_putenv_garbage == NULL)
7583 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007584#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007585
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007586 if (!initialized) {
7587 stat_result_desc.name = MODNAME ".stat_result";
7588 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7589 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7590 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7591 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7592 structseq_new = StatResultType.tp_new;
7593 StatResultType.tp_new = statresult_new;
7594
7595 statvfs_result_desc.name = MODNAME ".statvfs_result";
7596 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007597#ifdef NEED_TICKS_PER_SECOND
7598# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7599 ticks_per_second = sysconf(_SC_CLK_TCK);
7600# elif defined(HZ)
7601 ticks_per_second = HZ;
7602# else
7603 ticks_per_second = 60; /* magic fallback value; may be bogus */
7604# endif
7605#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007606 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007607 Py_INCREF((PyObject*) &StatResultType);
7608 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007609 Py_INCREF((PyObject*) &StatVFSResultType);
7610 PyModule_AddObject(m, "statvfs_result",
7611 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007612 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007613
7614#ifdef __APPLE__
7615 /*
7616 * Step 2 of weak-linking support on Mac OS X.
7617 *
7618 * The code below removes functions that are not available on the
7619 * currently active platform.
7620 *
7621 * This block allow one to use a python binary that was build on
7622 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7623 * OSX 10.4.
7624 */
7625#ifdef HAVE_FSTATVFS
7626 if (fstatvfs == NULL) {
7627 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007628 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007629 }
7630 }
7631#endif /* HAVE_FSTATVFS */
7632
7633#ifdef HAVE_STATVFS
7634 if (statvfs == NULL) {
7635 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007636 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007637 }
7638 }
7639#endif /* HAVE_STATVFS */
7640
7641# ifdef HAVE_LCHOWN
7642 if (lchown == NULL) {
7643 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007644 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007645 }
7646 }
7647#endif /* HAVE_LCHOWN */
7648
7649
7650#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007651 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007652
Guido van Rossumb6775db1994-08-01 11:34:53 +00007653}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007654
7655#ifdef __cplusplus
7656}
7657#endif