blob: abe618919518b30c50723a573f63d390d2d28938 [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))
Antoine Pitrou1119a642010-01-17 12:16:23 +0000583 o->ob_type->tp_as_buffer->bf_releasebuffer(o, 0);
Martin v. Löwis011e8422009-05-05 04:43:17 +0000584 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{
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001991 int fd;
1992 long uid, gid;
Christian Heimes4e30a842007-11-30 22:12:06 +00001993 int res;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001994 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
Christian Heimes4e30a842007-11-30 22:12:06 +00001995 return NULL;
1996 Py_BEGIN_ALLOW_THREADS
1997 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1998 Py_END_ALLOW_THREADS
1999 if (res < 0)
2000 return posix_error();
2001 Py_RETURN_NONE;
2002}
2003#endif /* HAVE_FCHOWN */
2004
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002005#ifdef HAVE_LCHOWN
2006PyDoc_STRVAR(posix_lchown__doc__,
2007"lchown(path, uid, gid)\n\n\
2008Change the owner and group id of path to the numeric uid and gid.\n\
2009This function will not follow symbolic links.");
2010
2011static PyObject *
2012posix_lchown(PyObject *self, PyObject *args)
2013{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002014 PyObject *opath;
2015 char *path;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00002016 long uid, gid;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002017 int res;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00002018 if (!PyArg_ParseTuple(args, "O&ll:lchown",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002019 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002020 &uid, &gid))
2021 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002022 path = bytes2str(opath, 1);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002023 Py_BEGIN_ALLOW_THREADS
2024 res = lchown(path, (uid_t) uid, (gid_t) gid);
2025 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00002026 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002027 return posix_error_with_allocated_filename(opath);
2028 release_bytes(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002029 Py_INCREF(Py_None);
2030 return Py_None;
2031}
2032#endif /* HAVE_LCHOWN */
2033
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002034
Guido van Rossum36bc6801995-06-14 22:54:23 +00002035#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002036static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002037posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002038{
2039 char buf[1026];
2040 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002041
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002042#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002043 if (!use_bytes) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002044 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002045 wchar_t *wbuf2 = wbuf;
2046 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002047 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002048 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002049 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2050 /* If the buffer is large enough, len does not include the
2051 terminating \0. If the buffer is too small, len includes
2052 the space needed for the terminator. */
2053 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2054 wbuf2 = malloc(len * sizeof(wchar_t));
2055 if (wbuf2)
2056 len = GetCurrentDirectoryW(len, wbuf2);
2057 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002058 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002059 if (!wbuf2) {
2060 PyErr_NoMemory();
2061 return NULL;
2062 }
2063 if (!len) {
2064 if (wbuf2 != wbuf) free(wbuf2);
2065 return win32_error("getcwdu", NULL);
2066 }
2067 resobj = PyUnicode_FromWideChar(wbuf2, len);
2068 if (wbuf2 != wbuf) free(wbuf2);
2069 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002070 }
2071#endif
2072
2073 Py_BEGIN_ALLOW_THREADS
2074#if defined(PYOS_OS2) && defined(PYCC_GCC)
2075 res = _getcwd2(buf, sizeof buf);
2076#else
2077 res = getcwd(buf, sizeof buf);
2078#endif
2079 Py_END_ALLOW_THREADS
2080 if (res == NULL)
2081 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002082 if (use_bytes)
2083 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002084 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002085}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002086
2087PyDoc_STRVAR(posix_getcwd__doc__,
2088"getcwd() -> path\n\n\
2089Return a unicode string representing the current working directory.");
2090
2091static PyObject *
2092posix_getcwd_unicode(PyObject *self)
2093{
2094 return posix_getcwd(0);
2095}
2096
2097PyDoc_STRVAR(posix_getcwdb__doc__,
2098"getcwdb() -> path\n\n\
2099Return a bytes string representing the current working directory.");
2100
2101static PyObject *
2102posix_getcwd_bytes(PyObject *self)
2103{
2104 return posix_getcwd(1);
2105}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002106#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002107
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002108
Guido van Rossumb6775db1994-08-01 11:34:53 +00002109#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002110PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002111"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002112Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002113
Barry Warsaw53699e91996-12-10 23:23:01 +00002114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002115posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002116{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002117 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002118}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002119#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002120
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002122PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002123"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002124Return a list containing the names of the entries in the directory.\n\
2125\n\
2126 path: path of directory to list\n\
2127\n\
2128The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002129entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002130
Barry Warsaw53699e91996-12-10 23:23:01 +00002131static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002132posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002133{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002134 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002135 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002136#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002137
Barry Warsaw53699e91996-12-10 23:23:01 +00002138 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002139 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002140 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002141 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002142 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002143 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002144 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002145 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002146
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002147 PyObject *po;
2148 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2149 WIN32_FIND_DATAW wFileData;
2150 Py_UNICODE *wnamebuf;
2151 /* Overallocate for \\*.*\0 */
2152 len = PyUnicode_GET_SIZE(po);
2153 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2154 if (!wnamebuf) {
2155 PyErr_NoMemory();
2156 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002157 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002158 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2159 if (len > 0) {
2160 Py_UNICODE wch = wnamebuf[len-1];
2161 if (wch != L'/' && wch != L'\\' && wch != L':')
2162 wnamebuf[len++] = L'\\';
2163 wcscpy(wnamebuf + len, L"*.*");
2164 }
2165 if ((d = PyList_New(0)) == NULL) {
2166 free(wnamebuf);
2167 return NULL;
2168 }
2169 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2170 if (hFindFile == INVALID_HANDLE_VALUE) {
2171 int error = GetLastError();
2172 if (error == ERROR_FILE_NOT_FOUND) {
2173 free(wnamebuf);
2174 return d;
2175 }
2176 Py_DECREF(d);
2177 win32_error_unicode("FindFirstFileW", wnamebuf);
2178 free(wnamebuf);
2179 return NULL;
2180 }
2181 do {
2182 /* Skip over . and .. */
2183 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2184 wcscmp(wFileData.cFileName, L"..") != 0) {
2185 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2186 if (v == NULL) {
2187 Py_DECREF(d);
2188 d = NULL;
2189 break;
2190 }
2191 if (PyList_Append(d, v) != 0) {
2192 Py_DECREF(v);
2193 Py_DECREF(d);
2194 d = NULL;
2195 break;
2196 }
2197 Py_DECREF(v);
2198 }
2199 Py_BEGIN_ALLOW_THREADS
2200 result = FindNextFileW(hFindFile, &wFileData);
2201 Py_END_ALLOW_THREADS
2202 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2203 it got to the end of the directory. */
2204 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2205 Py_DECREF(d);
2206 win32_error_unicode("FindNextFileW", wnamebuf);
2207 FindClose(hFindFile);
2208 free(wnamebuf);
2209 return NULL;
2210 }
2211 } while (result == TRUE);
2212
2213 if (FindClose(hFindFile) == FALSE) {
2214 Py_DECREF(d);
2215 win32_error_unicode("FindClose", wnamebuf);
2216 free(wnamebuf);
2217 return NULL;
2218 }
2219 free(wnamebuf);
2220 return d;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002221 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002222 /* Drop the argument parsing error as narrow strings
2223 are also valid. */
2224 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002225
Martin v. Löwis011e8422009-05-05 04:43:17 +00002226 if (!PyArg_ParseTuple(args, "O&:listdir",
2227 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002228 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002229 if (PyObject_Size(opath)+1 > MAX_PATH) {
2230 PyErr_SetString(PyExc_ValueError, "path too long");
2231 Py_DECREF(opath);
2232 return NULL;
2233 }
2234 strcpy(namebuf, bytes2str(opath, 0));
2235 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002236 if (len > 0) {
2237 char ch = namebuf[len-1];
2238 if (ch != SEP && ch != ALTSEP && ch != ':')
2239 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002240 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002241 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002242
Barry Warsaw53699e91996-12-10 23:23:01 +00002243 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002244 return NULL;
2245
2246 hFindFile = FindFirstFile(namebuf, &FileData);
2247 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002248 int error = GetLastError();
2249 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002250 return d;
2251 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002252 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002253 }
2254 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002255 /* Skip over . and .. */
2256 if (strcmp(FileData.cFileName, ".") != 0 &&
2257 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002258 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002259 if (v == NULL) {
2260 Py_DECREF(d);
2261 d = NULL;
2262 break;
2263 }
2264 if (PyList_Append(d, v) != 0) {
2265 Py_DECREF(v);
2266 Py_DECREF(d);
2267 d = NULL;
2268 break;
2269 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002270 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002271 }
Georg Brandl622927b2006-03-07 12:48:03 +00002272 Py_BEGIN_ALLOW_THREADS
2273 result = FindNextFile(hFindFile, &FileData);
2274 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002275 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2276 it got to the end of the directory. */
2277 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2278 Py_DECREF(d);
2279 win32_error("FindNextFile", namebuf);
2280 FindClose(hFindFile);
2281 return NULL;
2282 }
Georg Brandl622927b2006-03-07 12:48:03 +00002283 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002284
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002285 if (FindClose(hFindFile) == FALSE) {
2286 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002287 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002288 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002289
2290 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002291
Tim Peters0bb44a42000-09-15 07:44:49 +00002292#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002293
2294#ifndef MAX_PATH
2295#define MAX_PATH CCHMAXPATH
2296#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002297 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002298 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002299 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002300 PyObject *d, *v;
2301 char namebuf[MAX_PATH+5];
2302 HDIR hdir = 1;
2303 ULONG srchcnt = 1;
2304 FILEFINDBUF3 ep;
2305 APIRET rc;
2306
Martin v. Löwis011e8422009-05-05 04:43:17 +00002307 if (!PyArg_ParseTuple(args, "O&:listdir",
2308 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002309 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002310 name = bytes2str(oname);
2311 len = PyObject_Size(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002312 if (len >= MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002313 release_bytes(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002314 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002315 return NULL;
2316 }
2317 strcpy(namebuf, name);
2318 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002319 if (*pt == ALTSEP)
2320 *pt = SEP;
2321 if (namebuf[len-1] != SEP)
2322 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002323 strcpy(namebuf + len, "*.*");
2324
Neal Norwitz6c913782007-10-14 03:23:09 +00002325 if ((d = PyList_New(0)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002326 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002327 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002328 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002329
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002330 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2331 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002332 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002333 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2334 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2335 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002336
2337 if (rc != NO_ERROR) {
2338 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002339 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002340 }
2341
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002342 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002343 do {
2344 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002345 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002346 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002347
2348 strcpy(namebuf, ep.achName);
2349
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002350 /* Leave Case of Name Alone -- In Native Form */
2351 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002352
Christian Heimes72b710a2008-05-26 13:28:38 +00002353 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002354 if (v == NULL) {
2355 Py_DECREF(d);
2356 d = NULL;
2357 break;
2358 }
2359 if (PyList_Append(d, v) != 0) {
2360 Py_DECREF(v);
2361 Py_DECREF(d);
2362 d = NULL;
2363 break;
2364 }
2365 Py_DECREF(v);
2366 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2367 }
2368
Martin v. Löwis011e8422009-05-05 04:43:17 +00002369 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002370 return d;
2371#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002372 PyObject *oname;
2373 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002374 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002375 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002376 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002377 int arg_is_unicode = 1;
2378
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002379 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002380 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2381 arg_is_unicode = 0;
2382 PyErr_Clear();
2383 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002384 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002385 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002386 name = bytes2str(oname, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002387 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002388 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002389 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002390 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002391 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002392 release_bytes(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002393 return NULL;
2394 }
Georg Brandl622927b2006-03-07 12:48:03 +00002395 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002396 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002397 Py_BEGIN_ALLOW_THREADS
2398 ep = readdir(dirp);
2399 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002400 if (ep == NULL) {
2401 if (errno == 0) {
2402 break;
2403 } else {
2404 closedir(dirp);
2405 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002406 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002407 }
2408 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002409 if (ep->d_name[0] == '.' &&
2410 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002411 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002412 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002413 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002414 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002415 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002416 d = NULL;
2417 break;
2418 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002419 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002420 PyObject *w;
2421
2422 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002423 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002424 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002425 Py_DECREF(v);
2426 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002427 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002428 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002429 /* Encoding failed to decode ASCII bytes.
2430 Raise exception. */
2431 Py_DECREF(d);
2432 d = NULL;
2433 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002434 }
Just van Rossum46c97842003-02-25 21:42:15 +00002435 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002436 if (PyList_Append(d, v) != 0) {
2437 Py_DECREF(v);
2438 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002439 d = NULL;
2440 break;
2441 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002442 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002443 }
2444 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002445 release_bytes(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002446
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002447 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002448
Tim Peters0bb44a42000-09-15 07:44:49 +00002449#endif /* which OS */
2450} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002451
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002452#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002453/* A helper function for abspath on win32 */
2454static PyObject *
2455posix__getfullpathname(PyObject *self, PyObject *args)
2456{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002457 PyObject *opath;
2458 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002459 char outbuf[MAX_PATH*2];
2460 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002461#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002462 PyUnicodeObject *po;
2463 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2464 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2465 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2466 Py_UNICODE *wtemp;
2467 DWORD result;
2468 PyObject *v;
2469 result = GetFullPathNameW(wpath,
2470 sizeof(woutbuf)/sizeof(woutbuf[0]),
2471 woutbuf, &wtemp);
2472 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2473 woutbufp = malloc(result * sizeof(Py_UNICODE));
2474 if (!woutbufp)
2475 return PyErr_NoMemory();
2476 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002477 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002478 if (result)
2479 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2480 else
2481 v = win32_error_unicode("GetFullPathNameW", wpath);
2482 if (woutbufp != woutbuf)
2483 free(woutbufp);
2484 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002485 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002486 /* Drop the argument parsing error as narrow strings
2487 are also valid. */
2488 PyErr_Clear();
2489
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002490#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002491 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2492 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002493 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002494 path = bytes2str(opath, 1);
2495 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2496 outbuf, &temp)) {
2497 win32_error("GetFullPathName", path);
2498 release_bytes(opath);
2499 return NULL;
2500 }
2501 release_bytes(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002502 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2503 return PyUnicode_Decode(outbuf, strlen(outbuf),
2504 Py_FileSystemDefaultEncoding, NULL);
2505 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002506 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002507} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002508#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002510PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002511"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002512Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002513
Barry Warsaw53699e91996-12-10 23:23:01 +00002514static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002515posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002516{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002517 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002518 PyObject *opath;
2519 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002520 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002521
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002522#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002523 PyUnicodeObject *po;
2524 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2525 Py_BEGIN_ALLOW_THREADS
2526 /* PyUnicode_AS_UNICODE OK without thread lock as
2527 it is a simple dereference. */
2528 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2529 Py_END_ALLOW_THREADS
2530 if (!res)
2531 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2532 Py_INCREF(Py_None);
2533 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002534 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002535 /* Drop the argument parsing error as narrow strings
2536 are also valid. */
2537 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00002538 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2539 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002540 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002541 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002542 Py_BEGIN_ALLOW_THREADS
2543 /* PyUnicode_AS_UNICODE OK without thread lock as
2544 it is a simple dereference. */
2545 res = CreateDirectoryA(path, NULL);
2546 Py_END_ALLOW_THREADS
2547 if (!res) {
2548 win32_error("mkdir", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002549 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002550 return NULL;
2551 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002552 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002553 Py_INCREF(Py_None);
2554 return Py_None;
2555#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002556
Martin v. Löwis011e8422009-05-05 04:43:17 +00002557 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2558 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002559 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002560 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00002561 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002562#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002563 res = mkdir(path);
2564#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002565 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002566#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002567 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002568 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002569 return posix_error_with_allocated_filename(opath);
2570 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002571 Py_INCREF(Py_None);
2572 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002573#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002574}
2575
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002576
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002577/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2578#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002579#include <sys/resource.h>
2580#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002581
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002582
2583#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002584PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002585"nice(inc) -> new_priority\n\n\
2586Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002587
Barry Warsaw53699e91996-12-10 23:23:01 +00002588static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002589posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002590{
2591 int increment, value;
2592
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002593 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002594 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002595
2596 /* There are two flavours of 'nice': one that returns the new
2597 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002598 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2599 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002600
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002601 If we are of the nice family that returns the new priority, we
2602 need to clear errno before the call, and check if errno is filled
2603 before calling posix_error() on a returnvalue of -1, because the
2604 -1 may be the actual new priority! */
2605
2606 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002607 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002608#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002609 if (value == 0)
2610 value = getpriority(PRIO_PROCESS, 0);
2611#endif
2612 if (value == -1 && errno != 0)
2613 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002614 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002615 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002616}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002617#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002619PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002620"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002621Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002622
Barry Warsaw53699e91996-12-10 23:23:01 +00002623static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002624posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002625{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002626#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002627 PyObject *o1, *o2;
2628 char *p1, *p2;
2629 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002630 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002631 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002632 if (!convert_to_unicode(&o1))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002633 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002634 if (!convert_to_unicode(&o2)) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002635 Py_DECREF(o1);
2636 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002637 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002638 Py_BEGIN_ALLOW_THREADS
2639 result = MoveFileW(PyUnicode_AsUnicode(o1),
2640 PyUnicode_AsUnicode(o2));
2641 Py_END_ALLOW_THREADS
2642 Py_DECREF(o1);
2643 Py_DECREF(o2);
2644 if (!result)
2645 return win32_error("rename", NULL);
2646 Py_INCREF(Py_None);
2647 return Py_None;
2648error:
2649 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002650 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2651 return NULL;
2652 Py_BEGIN_ALLOW_THREADS
2653 result = MoveFileA(p1, p2);
2654 Py_END_ALLOW_THREADS
2655 if (!result)
2656 return win32_error("rename", NULL);
2657 Py_INCREF(Py_None);
2658 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002659#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002660 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002661#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002662}
2663
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002664
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002665PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002666"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002667Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002668
Barry Warsaw53699e91996-12-10 23:23:01 +00002669static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002670posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002671{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002672#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002673 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002674#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002675 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002676#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002677}
2678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002681"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002683
Barry Warsaw53699e91996-12-10 23:23:01 +00002684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002685posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002686{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002687#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002688 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002689#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002690 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002691#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002692}
2693
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002694
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002695#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002696PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002697"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002699
Barry Warsaw53699e91996-12-10 23:23:01 +00002700static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002701posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002702{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002703 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002704#ifdef MS_WINDOWS
2705 wchar_t *command;
2706 if (!PyArg_ParseTuple(args, "u:system", &command))
2707 return NULL;
2708#else
2709 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002710 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002711 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002712#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002713 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002714#ifdef MS_WINDOWS
2715 sts = _wsystem(command);
2716#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002717 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002718#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002719 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002720 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002721}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002722#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002723
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002725PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002726"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002727Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002728
Barry Warsaw53699e91996-12-10 23:23:01 +00002729static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002730posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002731{
2732 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002733 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002734 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002735 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002736 if (i < 0)
2737 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002738 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002739}
2740
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002742PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002743"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002744Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002747"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002748Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002749
Barry Warsaw53699e91996-12-10 23:23:01 +00002750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002751posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002752{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002753#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002754 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002755#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002756 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002757#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002758}
2759
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002760
Guido van Rossumb6775db1994-08-01 11:34:53 +00002761#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002762PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002763"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002764Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002765
Barry Warsaw53699e91996-12-10 23:23:01 +00002766static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002767posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002768{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002769 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002770 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002771
Barry Warsaw53699e91996-12-10 23:23:01 +00002772 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002773 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002774 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002775 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002776 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002777 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002778 u.sysname,
2779 u.nodename,
2780 u.release,
2781 u.version,
2782 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002783}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002784#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002785
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002786static int
2787extract_time(PyObject *t, long* sec, long* usec)
2788{
2789 long intval;
2790 if (PyFloat_Check(t)) {
2791 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002792 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002793 if (!intobj)
2794 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002795 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002796 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002797 if (intval == -1 && PyErr_Occurred())
2798 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002799 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002800 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002801 if (*usec < 0)
2802 /* If rounding gave us a negative number,
2803 truncate. */
2804 *usec = 0;
2805 return 0;
2806 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002807 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002808 if (intval == -1 && PyErr_Occurred())
2809 return -1;
2810 *sec = intval;
2811 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002812 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002813}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002815PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002816"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002817utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002818Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002819second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002820
Barry Warsaw53699e91996-12-10 23:23:01 +00002821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002822posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002823{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002824#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002825 PyObject *arg;
2826 PyUnicodeObject *obwpath;
2827 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002828 PyObject *oapath;
2829 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002830 HANDLE hFile;
2831 long atimesec, mtimesec, ausec, musec;
2832 FILETIME atime, mtime;
2833 PyObject *result = NULL;
2834
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002835 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2836 wpath = PyUnicode_AS_UNICODE(obwpath);
2837 Py_BEGIN_ALLOW_THREADS
2838 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2839 NULL, OPEN_EXISTING,
2840 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2841 Py_END_ALLOW_THREADS
2842 if (hFile == INVALID_HANDLE_VALUE)
2843 return win32_error_unicode("utime", wpath);
2844 } else
2845 /* Drop the argument parsing error as narrow strings
2846 are also valid. */
2847 PyErr_Clear();
2848
Thomas Wouters477c8d52006-05-27 19:21:47 +00002849 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002850 if (!PyArg_ParseTuple(args, "O&O:utime",
2851 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002852 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002853 apath = bytes2str(oapath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002854 Py_BEGIN_ALLOW_THREADS
2855 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002856 NULL, OPEN_EXISTING,
2857 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002858 Py_END_ALLOW_THREADS
2859 if (hFile == INVALID_HANDLE_VALUE) {
2860 win32_error("utime", apath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002861 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002862 return NULL;
2863 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002864 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002865 }
2866
2867 if (arg == Py_None) {
2868 SYSTEMTIME now;
2869 GetSystemTime(&now);
2870 if (!SystemTimeToFileTime(&now, &mtime) ||
2871 !SystemTimeToFileTime(&now, &atime)) {
2872 win32_error("utime", NULL);
2873 goto done;
2874 }
2875 }
2876 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2877 PyErr_SetString(PyExc_TypeError,
2878 "utime() arg 2 must be a tuple (atime, mtime)");
2879 goto done;
2880 }
2881 else {
2882 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2883 &atimesec, &ausec) == -1)
2884 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002885 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002886 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2887 &mtimesec, &musec) == -1)
2888 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002889 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002890 }
2891 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2892 /* Avoid putting the file name into the error here,
2893 as that may confuse the user into believing that
2894 something is wrong with the file, when it also
2895 could be the time stamp that gives a problem. */
2896 win32_error("utime", NULL);
2897 }
2898 Py_INCREF(Py_None);
2899 result = Py_None;
2900done:
2901 CloseHandle(hFile);
2902 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002903#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002904
Martin v. Löwis011e8422009-05-05 04:43:17 +00002905 PyObject *opath;
2906 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002907 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002908 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002909 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002910
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002911#if defined(HAVE_UTIMES)
2912 struct timeval buf[2];
2913#define ATIME buf[0].tv_sec
2914#define MTIME buf[1].tv_sec
2915#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002916/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002917 struct utimbuf buf;
2918#define ATIME buf.actime
2919#define MTIME buf.modtime
2920#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002921#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002922 time_t buf[2];
2923#define ATIME buf[0]
2924#define MTIME buf[1]
2925#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002926#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002927
Mark Hammond817c9292003-12-03 01:22:38 +00002928
Martin v. Löwis011e8422009-05-05 04:43:17 +00002929 if (!PyArg_ParseTuple(args, "O&O:utime",
2930 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002931 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002932 path = bytes2str(opath, 1);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002933 if (arg == Py_None) {
2934 /* optional time values not given */
2935 Py_BEGIN_ALLOW_THREADS
2936 res = utime(path, NULL);
2937 Py_END_ALLOW_THREADS
2938 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002939 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002940 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002941 "utime() arg 2 must be a tuple (atime, mtime)");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002942 release_bytes(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002943 return NULL;
2944 }
2945 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002946 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002947 &atime, &ausec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002948 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002949 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002950 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002951 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002952 &mtime, &musec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002953 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002954 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002955 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002956 ATIME = atime;
2957 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002958#ifdef HAVE_UTIMES
2959 buf[0].tv_usec = ausec;
2960 buf[1].tv_usec = musec;
2961 Py_BEGIN_ALLOW_THREADS
2962 res = utimes(path, buf);
2963 Py_END_ALLOW_THREADS
2964#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002965 Py_BEGIN_ALLOW_THREADS
2966 res = utime(path, UTIME_ARG);
2967 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002968#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002969 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002970 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002971 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002972 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002973 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002974 Py_INCREF(Py_None);
2975 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002976#undef UTIME_ARG
2977#undef ATIME
2978#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002979#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002980}
2981
Guido van Rossum85e3b011991-06-03 12:42:10 +00002982
Guido van Rossum3b066191991-06-04 19:40:25 +00002983/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002984
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002985PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002986"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002987Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002988
Barry Warsaw53699e91996-12-10 23:23:01 +00002989static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002990posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002991{
2992 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002993 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002994 return NULL;
2995 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002996 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002997}
2998
Martin v. Löwis114619e2002-10-07 06:44:21 +00002999#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3000static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003001free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003002{
Martin v. Löwis725507b2006-03-07 12:08:51 +00003003 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00003004 for (i = 0; i < count; i++)
3005 PyMem_Free(array[i]);
3006 PyMem_DEL(array);
3007}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003008
Antoine Pitrou69f71142009-05-24 21:25:49 +00003009static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003010int fsconvert_strdup(PyObject *o, char**out)
3011{
3012 PyObject *bytes;
3013 Py_ssize_t size;
3014 if (!PyUnicode_FSConverter(o, &bytes))
3015 return 0;
3016 size = PyObject_Size(bytes);
3017 *out = PyMem_Malloc(size+1);
3018 if (!*out)
3019 return 0;
3020 /* Don't lock bytes, as we hold the GIL */
3021 memcpy(*out, bytes2str(bytes, 0), size+1);
3022 Py_DECREF(bytes);
3023 return 1;
3024}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003025#endif
3026
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003027
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003028#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003029PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003030"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003031Execute an executable path with arguments, replacing current process.\n\
3032\n\
3033 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003034 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003035
Barry Warsaw53699e91996-12-10 23:23:01 +00003036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003037posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003038{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003039 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003040 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003041 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003042 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003043 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003044 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003045
Guido van Rossum89b33251993-10-22 14:26:06 +00003046 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003047 argv is a list or tuple of strings. */
3048
Martin v. Löwis011e8422009-05-05 04:43:17 +00003049 if (!PyArg_ParseTuple(args, "O&O:execv",
3050 PyUnicode_FSConverter,
3051 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003052 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003053 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003054 if (PyList_Check(argv)) {
3055 argc = PyList_Size(argv);
3056 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003057 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003058 else if (PyTuple_Check(argv)) {
3059 argc = PyTuple_Size(argv);
3060 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003061 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003062 else {
Fred Drake661ea262000-10-24 19:57:45 +00003063 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003064 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003065 return NULL;
3066 }
Thomas Heller6790d602007-08-30 17:15:14 +00003067 if (argc < 1) {
3068 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003069 release_bytes(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003070 return NULL;
3071 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003072
Barry Warsaw53699e91996-12-10 23:23:01 +00003073 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003074 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003075 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003076 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003077 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003078 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003079 if (!fsconvert_strdup((*getitem)(argv, i),
3080 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003081 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003082 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003083 "execv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003084 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003085 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003086
Guido van Rossum85e3b011991-06-03 12:42:10 +00003087 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003088 }
3089 argvlist[argc] = NULL;
3090
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003091 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003092
Guido van Rossum85e3b011991-06-03 12:42:10 +00003093 /* If we get here it's definitely an error */
3094
Martin v. Löwis114619e2002-10-07 06:44:21 +00003095 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003096 release_bytes(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003097 return posix_error();
3098}
3099
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003101PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003102"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003103Execute a path with arguments and environment, replacing current process.\n\
3104\n\
3105 path: path of executable file\n\
3106 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003107 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003108
Barry Warsaw53699e91996-12-10 23:23:01 +00003109static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003110posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003111{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003112 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003113 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003114 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003115 char **argvlist;
3116 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003117 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003118 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003119 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003121
3122 /* execve has three arguments: (path, argv, env), where
3123 argv is a list or tuple of strings and env is a dictionary
3124 like posix.environ. */
3125
Martin v. Löwis011e8422009-05-05 04:43:17 +00003126 if (!PyArg_ParseTuple(args, "O&OO:execve",
3127 PyUnicode_FSConverter,
3128 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003129 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003130 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003131 if (PyList_Check(argv)) {
3132 argc = PyList_Size(argv);
3133 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003134 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003135 else if (PyTuple_Check(argv)) {
3136 argc = PyTuple_Size(argv);
3137 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003138 }
3139 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003140 PyErr_SetString(PyExc_TypeError,
3141 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003142 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003143 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003144 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003145 PyErr_SetString(PyExc_TypeError,
3146 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003147 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003148 }
3149
Barry Warsaw53699e91996-12-10 23:23:01 +00003150 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003151 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003152 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003153 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003154 }
3155 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003156 if (!fsconvert_strdup((*getitem)(argv, i),
3157 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003158 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003159 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003160 goto fail_1;
3161 }
3162 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003163 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003164 argvlist[argc] = NULL;
3165
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003166 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003167 if (i < 0)
3168 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003169 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003170 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003171 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003172 goto fail_1;
3173 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003174 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003175 keys = PyMapping_Keys(env);
3176 vals = PyMapping_Values(env);
3177 if (!keys || !vals)
3178 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003179 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3180 PyErr_SetString(PyExc_TypeError,
3181 "execve(): env.keys() or env.values() is not a list");
3182 goto fail_2;
3183 }
Tim Peters5aa91602002-01-30 05:46:57 +00003184
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003185 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003186 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003187 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003188
3189 key = PyList_GetItem(keys, pos);
3190 val = PyList_GetItem(vals, pos);
3191 if (!key || !val)
3192 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003193
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003194 if (!PyArg_Parse(
3195 key,
3196 "s;execve() arg 3 contains a non-string key",
3197 &k) ||
3198 !PyArg_Parse(
3199 val,
3200 "s;execve() arg 3 contains a non-string value",
3201 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003202 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003203 goto fail_2;
3204 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003205
3206#if defined(PYOS_OS2)
3207 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3208 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3209#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003210 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003211 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003212 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003213 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003214 goto fail_2;
3215 }
Tim Petersc8996f52001-12-03 20:41:00 +00003216 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003217 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003218#if defined(PYOS_OS2)
3219 }
3220#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003221 }
3222 envlist[envc] = 0;
3223
3224 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003225
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003226 /* If we get here it's definitely an error */
3227
3228 (void) posix_error();
3229
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003230 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003231 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003232 PyMem_DEL(envlist[envc]);
3233 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003234 fail_1:
3235 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003236 Py_XDECREF(vals);
3237 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003238 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003239 release_bytes(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003240 return NULL;
3241}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003242#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003243
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003244
Guido van Rossuma1065681999-01-25 23:20:23 +00003245#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003246PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003247"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003248Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003249\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003250 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003251 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003252 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003253
3254static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003255posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003256{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003257 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003258 char *path;
3259 PyObject *argv;
3260 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003261 int mode, i;
3262 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003263 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003264 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003265
3266 /* spawnv has three arguments: (mode, path, argv), where
3267 argv is a list or tuple of strings. */
3268
Martin v. Löwis011e8422009-05-05 04:43:17 +00003269 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3270 PyUnicode_FSConverter,
3271 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003272 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003273 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003274 if (PyList_Check(argv)) {
3275 argc = PyList_Size(argv);
3276 getitem = PyList_GetItem;
3277 }
3278 else if (PyTuple_Check(argv)) {
3279 argc = PyTuple_Size(argv);
3280 getitem = PyTuple_GetItem;
3281 }
3282 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003283 PyErr_SetString(PyExc_TypeError,
3284 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003285 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003286 return NULL;
3287 }
3288
3289 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003290 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003291 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003292 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003293 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003294 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003295 if (!fsconvert_strdup((*getitem)(argv, i),
3296 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003297 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003298 PyErr_SetString(
3299 PyExc_TypeError,
3300 "spawnv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003301 release_bytes(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003302 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003303 }
3304 }
3305 argvlist[argc] = NULL;
3306
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003307#if defined(PYOS_OS2) && defined(PYCC_GCC)
3308 Py_BEGIN_ALLOW_THREADS
3309 spawnval = spawnv(mode, path, argvlist);
3310 Py_END_ALLOW_THREADS
3311#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003312 if (mode == _OLD_P_OVERLAY)
3313 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003314
Tim Peters25059d32001-12-07 20:35:43 +00003315 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003316 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003317 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003318#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003319
Martin v. Löwis114619e2002-10-07 06:44:21 +00003320 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003321 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003322
Fred Drake699f3522000-06-29 21:12:41 +00003323 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003324 return posix_error();
3325 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003326#if SIZEOF_LONG == SIZEOF_VOID_P
3327 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003328#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003329 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003330#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003331}
3332
3333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003334PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003335"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003336Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003337\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003338 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003339 path: path of executable file\n\
3340 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003341 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003342
3343static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003344posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003345{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003346 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003347 char *path;
3348 PyObject *argv, *env;
3349 char **argvlist;
3350 char **envlist;
3351 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003352 int mode, pos, envc;
3353 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003354 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003355 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003356 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003357
3358 /* spawnve has four arguments: (mode, path, argv, env), where
3359 argv is a list or tuple of strings and env is a dictionary
3360 like posix.environ. */
3361
Martin v. Löwis011e8422009-05-05 04:43:17 +00003362 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3363 PyUnicode_FSConverter,
3364 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003365 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003366 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003367 if (PyList_Check(argv)) {
3368 argc = PyList_Size(argv);
3369 getitem = PyList_GetItem;
3370 }
3371 else if (PyTuple_Check(argv)) {
3372 argc = PyTuple_Size(argv);
3373 getitem = PyTuple_GetItem;
3374 }
3375 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003376 PyErr_SetString(PyExc_TypeError,
3377 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003378 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003379 }
3380 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003381 PyErr_SetString(PyExc_TypeError,
3382 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003383 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003384 }
3385
3386 argvlist = PyMem_NEW(char *, argc+1);
3387 if (argvlist == NULL) {
3388 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003389 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003390 }
3391 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003392 if (!fsconvert_strdup((*getitem)(argv, i),
3393 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003394 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003395 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003396 goto fail_1;
3397 }
3398 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003399 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003400 argvlist[argc] = NULL;
3401
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003402 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003403 if (i < 0)
3404 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003405 envlist = PyMem_NEW(char *, i + 1);
3406 if (envlist == NULL) {
3407 PyErr_NoMemory();
3408 goto fail_1;
3409 }
3410 envc = 0;
3411 keys = PyMapping_Keys(env);
3412 vals = PyMapping_Values(env);
3413 if (!keys || !vals)
3414 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003415 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3416 PyErr_SetString(PyExc_TypeError,
3417 "spawnve(): env.keys() or env.values() is not a list");
3418 goto fail_2;
3419 }
Tim Peters5aa91602002-01-30 05:46:57 +00003420
Guido van Rossuma1065681999-01-25 23:20:23 +00003421 for (pos = 0; pos < i; pos++) {
3422 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003423 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003424
3425 key = PyList_GetItem(keys, pos);
3426 val = PyList_GetItem(vals, pos);
3427 if (!key || !val)
3428 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003429
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003430 if (!PyArg_Parse(
3431 key,
3432 "s;spawnve() arg 3 contains a non-string key",
3433 &k) ||
3434 !PyArg_Parse(
3435 val,
3436 "s;spawnve() arg 3 contains a non-string value",
3437 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003438 {
3439 goto fail_2;
3440 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003441 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003442 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003443 if (p == NULL) {
3444 PyErr_NoMemory();
3445 goto fail_2;
3446 }
Tim Petersc8996f52001-12-03 20:41:00 +00003447 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003448 envlist[envc++] = p;
3449 }
3450 envlist[envc] = 0;
3451
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003452#if defined(PYOS_OS2) && defined(PYCC_GCC)
3453 Py_BEGIN_ALLOW_THREADS
3454 spawnval = spawnve(mode, path, argvlist, envlist);
3455 Py_END_ALLOW_THREADS
3456#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003457 if (mode == _OLD_P_OVERLAY)
3458 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003459
3460 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003461 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003462 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003463#endif
Tim Peters25059d32001-12-07 20:35:43 +00003464
Fred Drake699f3522000-06-29 21:12:41 +00003465 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003466 (void) posix_error();
3467 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003468#if SIZEOF_LONG == SIZEOF_VOID_P
3469 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003470#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003471 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003472#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003473
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003474 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003475 while (--envc >= 0)
3476 PyMem_DEL(envlist[envc]);
3477 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003478 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003479 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003480 Py_XDECREF(vals);
3481 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003482 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003483 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003484 return res;
3485}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003486
3487/* OS/2 supports spawnvp & spawnvpe natively */
3488#if defined(PYOS_OS2)
3489PyDoc_STRVAR(posix_spawnvp__doc__,
3490"spawnvp(mode, file, args)\n\n\
3491Execute the program 'file' in a new process, using the environment\n\
3492search path to find the file.\n\
3493\n\
3494 mode: mode of process creation\n\
3495 file: executable file name\n\
3496 args: tuple or list of strings");
3497
3498static PyObject *
3499posix_spawnvp(PyObject *self, PyObject *args)
3500{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003501 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003502 char *path;
3503 PyObject *argv;
3504 char **argvlist;
3505 int mode, i, argc;
3506 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003507 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003508
3509 /* spawnvp has three arguments: (mode, path, argv), where
3510 argv is a list or tuple of strings. */
3511
Martin v. Löwis011e8422009-05-05 04:43:17 +00003512 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3513 PyUnicode_FSConverter,
3514 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003515 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003516 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003517 if (PyList_Check(argv)) {
3518 argc = PyList_Size(argv);
3519 getitem = PyList_GetItem;
3520 }
3521 else if (PyTuple_Check(argv)) {
3522 argc = PyTuple_Size(argv);
3523 getitem = PyTuple_GetItem;
3524 }
3525 else {
3526 PyErr_SetString(PyExc_TypeError,
3527 "spawnvp() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003528 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003529 return NULL;
3530 }
3531
3532 argvlist = PyMem_NEW(char *, argc+1);
3533 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003534 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003535 return PyErr_NoMemory();
3536 }
3537 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003538 if (!fsconvert_strdup((*getitem)(argv, i),
3539 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003540 free_string_array(argvlist, i);
3541 PyErr_SetString(
3542 PyExc_TypeError,
3543 "spawnvp() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003544 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003545 return NULL;
3546 }
3547 }
3548 argvlist[argc] = NULL;
3549
3550 Py_BEGIN_ALLOW_THREADS
3551#if defined(PYCC_GCC)
3552 spawnval = spawnvp(mode, path, argvlist);
3553#else
3554 spawnval = _spawnvp(mode, path, argvlist);
3555#endif
3556 Py_END_ALLOW_THREADS
3557
3558 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003559 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003560
3561 if (spawnval == -1)
3562 return posix_error();
3563 else
3564 return Py_BuildValue("l", (long) spawnval);
3565}
3566
3567
3568PyDoc_STRVAR(posix_spawnvpe__doc__,
3569"spawnvpe(mode, file, args, env)\n\n\
3570Execute the program 'file' in a new process, using the environment\n\
3571search path to find the file.\n\
3572\n\
3573 mode: mode of process creation\n\
3574 file: executable file name\n\
3575 args: tuple or list of arguments\n\
3576 env: dictionary of strings mapping to strings");
3577
3578static PyObject *
3579posix_spawnvpe(PyObject *self, PyObject *args)
3580{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003581 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003582 char *path;
3583 PyObject *argv, *env;
3584 char **argvlist;
3585 char **envlist;
3586 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3587 int mode, i, pos, argc, envc;
3588 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003589 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003590 int lastarg = 0;
3591
3592 /* spawnvpe has four arguments: (mode, path, argv, env), where
3593 argv is a list or tuple of strings and env is a dictionary
3594 like posix.environ. */
3595
3596 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003597 PyUnicode_FSConverter,
3598 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003599 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003600 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003601 if (PyList_Check(argv)) {
3602 argc = PyList_Size(argv);
3603 getitem = PyList_GetItem;
3604 }
3605 else if (PyTuple_Check(argv)) {
3606 argc = PyTuple_Size(argv);
3607 getitem = PyTuple_GetItem;
3608 }
3609 else {
3610 PyErr_SetString(PyExc_TypeError,
3611 "spawnvpe() arg 2 must be a tuple or list");
3612 goto fail_0;
3613 }
3614 if (!PyMapping_Check(env)) {
3615 PyErr_SetString(PyExc_TypeError,
3616 "spawnvpe() arg 3 must be a mapping object");
3617 goto fail_0;
3618 }
3619
3620 argvlist = PyMem_NEW(char *, argc+1);
3621 if (argvlist == NULL) {
3622 PyErr_NoMemory();
3623 goto fail_0;
3624 }
3625 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003626 if (!fsconvert_strdup((*getitem)(argv, i),
3627 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003628 {
3629 lastarg = i;
3630 goto fail_1;
3631 }
3632 }
3633 lastarg = argc;
3634 argvlist[argc] = NULL;
3635
3636 i = PyMapping_Size(env);
3637 if (i < 0)
3638 goto fail_1;
3639 envlist = PyMem_NEW(char *, i + 1);
3640 if (envlist == NULL) {
3641 PyErr_NoMemory();
3642 goto fail_1;
3643 }
3644 envc = 0;
3645 keys = PyMapping_Keys(env);
3646 vals = PyMapping_Values(env);
3647 if (!keys || !vals)
3648 goto fail_2;
3649 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3650 PyErr_SetString(PyExc_TypeError,
3651 "spawnvpe(): env.keys() or env.values() is not a list");
3652 goto fail_2;
3653 }
3654
3655 for (pos = 0; pos < i; pos++) {
3656 char *p, *k, *v;
3657 size_t len;
3658
3659 key = PyList_GetItem(keys, pos);
3660 val = PyList_GetItem(vals, pos);
3661 if (!key || !val)
3662 goto fail_2;
3663
3664 if (!PyArg_Parse(
3665 key,
3666 "s;spawnvpe() arg 3 contains a non-string key",
3667 &k) ||
3668 !PyArg_Parse(
3669 val,
3670 "s;spawnvpe() arg 3 contains a non-string value",
3671 &v))
3672 {
3673 goto fail_2;
3674 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003675 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003676 p = PyMem_NEW(char, len);
3677 if (p == NULL) {
3678 PyErr_NoMemory();
3679 goto fail_2;
3680 }
3681 PyOS_snprintf(p, len, "%s=%s", k, v);
3682 envlist[envc++] = p;
3683 }
3684 envlist[envc] = 0;
3685
3686 Py_BEGIN_ALLOW_THREADS
3687#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003688 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003689#else
Christian Heimes292d3512008-02-03 16:51:08 +00003690 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003691#endif
3692 Py_END_ALLOW_THREADS
3693
3694 if (spawnval == -1)
3695 (void) posix_error();
3696 else
3697 res = Py_BuildValue("l", (long) spawnval);
3698
3699 fail_2:
3700 while (--envc >= 0)
3701 PyMem_DEL(envlist[envc]);
3702 PyMem_DEL(envlist);
3703 fail_1:
3704 free_string_array(argvlist, lastarg);
3705 Py_XDECREF(vals);
3706 Py_XDECREF(keys);
3707 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003708 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003709 return res;
3710}
3711#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003712#endif /* HAVE_SPAWNV */
3713
3714
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003715#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003716PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003717"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003718Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3719\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003720Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003721
3722static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003723posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003724{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003725 pid_t pid;
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003726 int result = 0;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003727 _PyImport_AcquireLock();
3728 pid = fork1();
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003729 if (pid == 0) {
3730 /* child: this clobbers and resets the import lock. */
3731 PyOS_AfterFork();
3732 } else {
3733 /* parent: release the import lock. */
3734 result = _PyImport_ReleaseLock();
3735 }
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003736 if (pid == -1)
3737 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003738 if (result < 0) {
3739 /* Don't clobber the OSError if the fork failed. */
3740 PyErr_SetString(PyExc_RuntimeError,
3741 "not holding the import lock");
3742 return NULL;
3743 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003744 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003745}
3746#endif
3747
3748
Guido van Rossumad0ee831995-03-01 10:34:45 +00003749#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003750PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003751"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003752Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003753Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003754
Barry Warsaw53699e91996-12-10 23:23:01 +00003755static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003756posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003757{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003758 pid_t pid;
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003759 int result = 0;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003760 _PyImport_AcquireLock();
3761 pid = fork();
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003762 if (pid == 0) {
3763 /* child: this clobbers and resets the import lock. */
3764 PyOS_AfterFork();
3765 } else {
3766 /* parent: release the import lock. */
3767 result = _PyImport_ReleaseLock();
3768 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003769 if (pid == -1)
3770 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003771 if (result < 0) {
3772 /* Don't clobber the OSError if the fork failed. */
3773 PyErr_SetString(PyExc_RuntimeError,
3774 "not holding the import lock");
3775 return NULL;
3776 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003777 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003778}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003779#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003780
Neal Norwitzb59798b2003-03-21 01:43:31 +00003781/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003782/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3783#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003784#define DEV_PTY_FILE "/dev/ptc"
3785#define HAVE_DEV_PTMX
3786#else
3787#define DEV_PTY_FILE "/dev/ptmx"
3788#endif
3789
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003790#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003791#ifdef HAVE_PTY_H
3792#include <pty.h>
3793#else
3794#ifdef HAVE_LIBUTIL_H
3795#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003796#else
3797#ifdef HAVE_UTIL_H
3798#include <util.h>
3799#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003800#endif /* HAVE_LIBUTIL_H */
3801#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003802#ifdef HAVE_STROPTS_H
3803#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003804#endif
3805#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003806
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003807#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003808PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003809"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003810Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003811
3812static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003813posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003814{
3815 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003816#ifndef HAVE_OPENPTY
3817 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003818#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003819#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003820 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003821#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003822 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003823#endif
3824#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003825
Thomas Wouters70c21a12000-07-14 14:28:33 +00003826#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003827 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3828 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003829#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003830 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3831 if (slave_name == NULL)
3832 return posix_error();
3833
3834 slave_fd = open(slave_name, O_RDWR);
3835 if (slave_fd < 0)
3836 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003837#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003838 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003839 if (master_fd < 0)
3840 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003841 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003842 /* change permission of slave */
3843 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003844 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003845 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003846 }
3847 /* unlock slave */
3848 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003849 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003850 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003851 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003852 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003853 slave_name = ptsname(master_fd); /* get name of slave */
3854 if (slave_name == NULL)
3855 return posix_error();
3856 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3857 if (slave_fd < 0)
3858 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003859#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003860 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3861 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003862#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003863 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003864#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003865#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003866#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003867
Fred Drake8cef4cf2000-06-28 16:40:38 +00003868 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003869
Fred Drake8cef4cf2000-06-28 16:40:38 +00003870}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003871#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003872
3873#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003874PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003875"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003876Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3877Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003878To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003879
3880static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003881posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003882{
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003883 int master_fd = -1, result = 0;
Christian Heimes400adb02008-02-01 08:12:03 +00003884 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003885
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003886 _PyImport_AcquireLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003887 pid = forkpty(&master_fd, NULL, NULL, NULL);
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003888 if (pid == 0) {
3889 /* child: this clobbers and resets the import lock. */
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003890 PyOS_AfterFork();
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003891 } else {
3892 /* parent: release the import lock. */
3893 result = _PyImport_ReleaseLock();
3894 }
Fred Drake8cef4cf2000-06-28 16:40:38 +00003895 if (pid == -1)
3896 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003897 if (result < 0) {
3898 /* Don't clobber the OSError if the fork failed. */
3899 PyErr_SetString(PyExc_RuntimeError,
3900 "not holding the import lock");
3901 return NULL;
3902 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003903 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003904}
3905#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003906
Guido van Rossumad0ee831995-03-01 10:34:45 +00003907#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003908PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003909"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003910Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003911
Barry Warsaw53699e91996-12-10 23:23:01 +00003912static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003913posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003914{
Christian Heimes217cfd12007-12-02 14:31:20 +00003915 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003916}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003917#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003918
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003919
Guido van Rossumad0ee831995-03-01 10:34:45 +00003920#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003921PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003922"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003923Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003924
Barry Warsaw53699e91996-12-10 23:23:01 +00003925static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003926posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003927{
Christian Heimes217cfd12007-12-02 14:31:20 +00003928 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003929}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003930#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003931
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003932
Guido van Rossumad0ee831995-03-01 10:34:45 +00003933#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003934PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003935"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003936Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003937
Barry Warsaw53699e91996-12-10 23:23:01 +00003938static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003939posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003940{
Christian Heimes217cfd12007-12-02 14:31:20 +00003941 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003942}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003943#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003944
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003945
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003946PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003947"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003948Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003949
Barry Warsaw53699e91996-12-10 23:23:01 +00003950static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003951posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003952{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003953 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003954}
3955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003956
Fred Drakec9680921999-12-13 16:37:25 +00003957#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003958PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003959"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003960Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003961
3962static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003963posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003964{
3965 PyObject *result = NULL;
3966
Fred Drakec9680921999-12-13 16:37:25 +00003967#ifdef NGROUPS_MAX
3968#define MAX_GROUPS NGROUPS_MAX
3969#else
3970 /* defined to be 16 on Solaris7, so this should be a small number */
3971#define MAX_GROUPS 64
3972#endif
3973 gid_t grouplist[MAX_GROUPS];
3974 int n;
3975
3976 n = getgroups(MAX_GROUPS, grouplist);
3977 if (n < 0)
3978 posix_error();
3979 else {
3980 result = PyList_New(n);
3981 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003982 int i;
3983 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003984 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003985 if (o == NULL) {
3986 Py_DECREF(result);
3987 result = NULL;
3988 break;
3989 }
3990 PyList_SET_ITEM(result, i, o);
3991 }
3992 }
3993 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003994
Fred Drakec9680921999-12-13 16:37:25 +00003995 return result;
3996}
3997#endif
3998
Antoine Pitroub7572f02009-12-02 20:46:48 +00003999#ifdef HAVE_INITGROUPS
4000PyDoc_STRVAR(posix_initgroups__doc__,
4001"initgroups(username, gid) -> None\n\n\
4002Call the system initgroups() to initialize the group access list with all of\n\
4003the groups of which the specified username is a member, plus the specified\n\
4004group id.");
4005
4006static PyObject *
4007posix_initgroups(PyObject *self, PyObject *args)
4008{
4009 char *username;
4010 long gid;
4011
4012 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
4013 return NULL;
4014
4015 if (initgroups(username, (gid_t) gid) == -1)
4016 return PyErr_SetFromErrno(PyExc_OSError);
4017
4018 Py_INCREF(Py_None);
4019 return Py_None;
4020}
4021#endif
4022
Martin v. Löwis606edc12002-06-13 21:09:11 +00004023#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004024PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004025"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004026Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004027
4028static PyObject *
4029posix_getpgid(PyObject *self, PyObject *args)
4030{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004031 pid_t pid, pgid;
4032 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00004033 return NULL;
4034 pgid = getpgid(pid);
4035 if (pgid < 0)
4036 return posix_error();
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004037 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004038}
4039#endif /* HAVE_GETPGID */
4040
4041
Guido van Rossumb6775db1994-08-01 11:34:53 +00004042#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004043PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004044"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004045Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004046
Barry Warsaw53699e91996-12-10 23:23:01 +00004047static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004048posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004049{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004050#ifdef GETPGRP_HAVE_ARG
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004051 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004052#else /* GETPGRP_HAVE_ARG */
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004053 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004054#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004055}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004056#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004057
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004058
Guido van Rossumb6775db1994-08-01 11:34:53 +00004059#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004060PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004061"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004062Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004063
Barry Warsaw53699e91996-12-10 23:23:01 +00004064static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004065posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004066{
Guido van Rossum64933891994-10-20 21:56:42 +00004067#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004068 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004069#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004070 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004071#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004072 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004073 Py_INCREF(Py_None);
4074 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004075}
4076
Guido van Rossumb6775db1994-08-01 11:34:53 +00004077#endif /* HAVE_SETPGRP */
4078
Guido van Rossumad0ee831995-03-01 10:34:45 +00004079#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004080PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004081"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004082Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004083
Barry Warsaw53699e91996-12-10 23:23:01 +00004084static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004085posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004086{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004087 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004088}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004089#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004090
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004091
Fred Drake12c6e2d1999-12-14 21:25:03 +00004092#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004093PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004094"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004095Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004096
4097static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004098posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004099{
Neal Norwitze241ce82003-02-17 18:17:05 +00004100 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004101 char *name;
4102 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004103
Fred Drakea30680b2000-12-06 21:24:28 +00004104 errno = 0;
4105 name = getlogin();
4106 if (name == NULL) {
4107 if (errno)
4108 posix_error();
4109 else
4110 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004111 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004112 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004113 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004114 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004115 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004116
Fred Drake12c6e2d1999-12-14 21:25:03 +00004117 return result;
4118}
4119#endif
4120
Guido van Rossumad0ee831995-03-01 10:34:45 +00004121#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004122PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004123"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004124Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004125
Barry Warsaw53699e91996-12-10 23:23:01 +00004126static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004127posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004128{
Christian Heimes217cfd12007-12-02 14:31:20 +00004129 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004130}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004131#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004132
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004133
Guido van Rossumad0ee831995-03-01 10:34:45 +00004134#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004135PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004136"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004137Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004138
Barry Warsaw53699e91996-12-10 23:23:01 +00004139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004140posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004141{
Christian Heimes292d3512008-02-03 16:51:08 +00004142 pid_t pid;
4143 int sig;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004144 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004145 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004146#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004147 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4148 APIRET rc;
4149 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004150 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004151
4152 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4153 APIRET rc;
4154 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004155 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004156
4157 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004158 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004159#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004160 if (kill(pid, sig) == -1)
4161 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004162#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004163 Py_INCREF(Py_None);
4164 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004165}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004166#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004167
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004168#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004169PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004170"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004171Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004172
4173static PyObject *
4174posix_killpg(PyObject *self, PyObject *args)
4175{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004176 int sig;
4177 pid_t pgid;
4178 /* XXX some man pages make the `pgid` parameter an int, others
4179 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4180 take the same type. Moreover, pid_t is always at least as wide as
4181 int (else compilation of this module fails), which is safe. */
4182 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004183 return NULL;
4184 if (killpg(pgid, sig) == -1)
4185 return posix_error();
4186 Py_INCREF(Py_None);
4187 return Py_None;
4188}
4189#endif
4190
Guido van Rossumc0125471996-06-28 18:55:32 +00004191#ifdef HAVE_PLOCK
4192
4193#ifdef HAVE_SYS_LOCK_H
4194#include <sys/lock.h>
4195#endif
4196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004197PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004198"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004199Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004200
Barry Warsaw53699e91996-12-10 23:23:01 +00004201static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004202posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004203{
4204 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004205 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004206 return NULL;
4207 if (plock(op) == -1)
4208 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004209 Py_INCREF(Py_None);
4210 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004211}
4212#endif
4213
Guido van Rossumb6775db1994-08-01 11:34:53 +00004214#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004215PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004216"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004217Set the current process's user id.");
4218
Barry Warsaw53699e91996-12-10 23:23:01 +00004219static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004220posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004221{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004222 long uid_arg;
4223 uid_t uid;
4224 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004225 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004226 uid = uid_arg;
4227 if (uid != uid_arg) {
4228 PyErr_SetString(PyExc_OverflowError, "user id too big");
4229 return NULL;
4230 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004231 if (setuid(uid) < 0)
4232 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004233 Py_INCREF(Py_None);
4234 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004235}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004236#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004237
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004238
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004239#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004240PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004241"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004242Set the current process's effective user id.");
4243
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004244static PyObject *
4245posix_seteuid (PyObject *self, PyObject *args)
4246{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004247 long euid_arg;
4248 uid_t euid;
4249 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004250 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004251 euid = euid_arg;
4252 if (euid != euid_arg) {
4253 PyErr_SetString(PyExc_OverflowError, "user id too big");
4254 return NULL;
4255 }
4256 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004257 return posix_error();
4258 } else {
4259 Py_INCREF(Py_None);
4260 return Py_None;
4261 }
4262}
4263#endif /* HAVE_SETEUID */
4264
4265#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004266PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004267"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004268Set the current process's effective group id.");
4269
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004270static PyObject *
4271posix_setegid (PyObject *self, PyObject *args)
4272{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004273 long egid_arg;
4274 gid_t egid;
4275 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004276 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004277 egid = egid_arg;
4278 if (egid != egid_arg) {
4279 PyErr_SetString(PyExc_OverflowError, "group id too big");
4280 return NULL;
4281 }
4282 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004283 return posix_error();
4284 } else {
4285 Py_INCREF(Py_None);
4286 return Py_None;
4287 }
4288}
4289#endif /* HAVE_SETEGID */
4290
4291#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004292PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004293"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004294Set the current process's real and effective user ids.");
4295
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004296static PyObject *
4297posix_setreuid (PyObject *self, PyObject *args)
4298{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004299 long ruid_arg, euid_arg;
4300 uid_t ruid, euid;
4301 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004302 return NULL;
Gregory P. Smithc78d79c2010-03-01 05:54:14 +00004303 if (ruid_arg == -1)
4304 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4305 else
4306 ruid = ruid_arg; /* otherwise, assign from our long */
4307 if (euid_arg == -1)
4308 euid = (uid_t)-1;
4309 else
4310 euid = euid_arg;
4311 if ((euid_arg != -1 && euid != euid_arg) ||
4312 (ruid_arg != -1 && ruid != ruid_arg)) {
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004313 PyErr_SetString(PyExc_OverflowError, "user id too big");
4314 return NULL;
4315 }
4316 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004317 return posix_error();
4318 } else {
4319 Py_INCREF(Py_None);
4320 return Py_None;
4321 }
4322}
4323#endif /* HAVE_SETREUID */
4324
4325#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004326PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004327"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004328Set the current process's real and effective group ids.");
4329
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004330static PyObject *
4331posix_setregid (PyObject *self, PyObject *args)
4332{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004333 long rgid_arg, egid_arg;
4334 gid_t rgid, egid;
4335 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004336 return NULL;
Gregory P. Smithc78d79c2010-03-01 05:54:14 +00004337 if (rgid_arg == -1)
4338 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4339 else
4340 rgid = rgid_arg; /* otherwise, assign from our long */
4341 if (egid_arg == -1)
4342 egid = (gid_t)-1;
4343 else
4344 egid = egid_arg;
4345 if ((egid_arg != -1 && egid != egid_arg) ||
4346 (rgid_arg != -1 && rgid != rgid_arg)) {
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004347 PyErr_SetString(PyExc_OverflowError, "group id too big");
4348 return NULL;
4349 }
4350 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004351 return posix_error();
4352 } else {
4353 Py_INCREF(Py_None);
4354 return Py_None;
4355 }
4356}
4357#endif /* HAVE_SETREGID */
4358
Guido van Rossumb6775db1994-08-01 11:34:53 +00004359#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004360PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004361"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004362Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004363
Barry Warsaw53699e91996-12-10 23:23:01 +00004364static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004365posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004366{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004367 long gid_arg;
4368 gid_t gid;
4369 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004370 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004371 gid = gid_arg;
4372 if (gid != gid_arg) {
4373 PyErr_SetString(PyExc_OverflowError, "group id too big");
4374 return NULL;
4375 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004376 if (setgid(gid) < 0)
4377 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004378 Py_INCREF(Py_None);
4379 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004380}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004381#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004382
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004383#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004384PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004385"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004386Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004387
4388static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004389posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004390{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004391 int i, len;
4392 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004393
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004394 if (!PySequence_Check(groups)) {
4395 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4396 return NULL;
4397 }
4398 len = PySequence_Size(groups);
4399 if (len > MAX_GROUPS) {
4400 PyErr_SetString(PyExc_ValueError, "too many groups");
4401 return NULL;
4402 }
4403 for(i = 0; i < len; i++) {
4404 PyObject *elem;
4405 elem = PySequence_GetItem(groups, i);
4406 if (!elem)
4407 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004408 if (!PyLong_Check(elem)) {
4409 PyErr_SetString(PyExc_TypeError,
4410 "groups must be integers");
4411 Py_DECREF(elem);
4412 return NULL;
4413 } else {
4414 unsigned long x = PyLong_AsUnsignedLong(elem);
4415 if (PyErr_Occurred()) {
4416 PyErr_SetString(PyExc_TypeError,
4417 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004418 Py_DECREF(elem);
4419 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004420 }
Georg Brandla13c2442005-11-22 19:30:31 +00004421 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004422 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004423 if (grouplist[i] != x) {
4424 PyErr_SetString(PyExc_TypeError,
4425 "group id too big");
4426 Py_DECREF(elem);
4427 return NULL;
4428 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004429 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004430 Py_DECREF(elem);
4431 }
4432
4433 if (setgroups(len, grouplist) < 0)
4434 return posix_error();
4435 Py_INCREF(Py_None);
4436 return Py_None;
4437}
4438#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004439
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004440#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4441static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004442wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004443{
4444 PyObject *result;
4445 static PyObject *struct_rusage;
4446
4447 if (pid == -1)
4448 return posix_error();
4449
4450 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004451 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004452 if (m == NULL)
4453 return NULL;
4454 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4455 Py_DECREF(m);
4456 if (struct_rusage == NULL)
4457 return NULL;
4458 }
4459
4460 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4461 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4462 if (!result)
4463 return NULL;
4464
4465#ifndef doubletime
4466#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4467#endif
4468
4469 PyStructSequence_SET_ITEM(result, 0,
4470 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4471 PyStructSequence_SET_ITEM(result, 1,
4472 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4473#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004474 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004475 SET_INT(result, 2, ru->ru_maxrss);
4476 SET_INT(result, 3, ru->ru_ixrss);
4477 SET_INT(result, 4, ru->ru_idrss);
4478 SET_INT(result, 5, ru->ru_isrss);
4479 SET_INT(result, 6, ru->ru_minflt);
4480 SET_INT(result, 7, ru->ru_majflt);
4481 SET_INT(result, 8, ru->ru_nswap);
4482 SET_INT(result, 9, ru->ru_inblock);
4483 SET_INT(result, 10, ru->ru_oublock);
4484 SET_INT(result, 11, ru->ru_msgsnd);
4485 SET_INT(result, 12, ru->ru_msgrcv);
4486 SET_INT(result, 13, ru->ru_nsignals);
4487 SET_INT(result, 14, ru->ru_nvcsw);
4488 SET_INT(result, 15, ru->ru_nivcsw);
4489#undef SET_INT
4490
4491 if (PyErr_Occurred()) {
4492 Py_DECREF(result);
4493 return NULL;
4494 }
4495
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004496 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004497}
4498#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4499
4500#ifdef HAVE_WAIT3
4501PyDoc_STRVAR(posix_wait3__doc__,
4502"wait3(options) -> (pid, status, rusage)\n\n\
4503Wait for completion of a child process.");
4504
4505static PyObject *
4506posix_wait3(PyObject *self, PyObject *args)
4507{
Christian Heimes292d3512008-02-03 16:51:08 +00004508 pid_t pid;
4509 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004510 struct rusage ru;
4511 WAIT_TYPE status;
4512 WAIT_STATUS_INT(status) = 0;
4513
4514 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4515 return NULL;
4516
4517 Py_BEGIN_ALLOW_THREADS
4518 pid = wait3(&status, options, &ru);
4519 Py_END_ALLOW_THREADS
4520
4521 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4522}
4523#endif /* HAVE_WAIT3 */
4524
4525#ifdef HAVE_WAIT4
4526PyDoc_STRVAR(posix_wait4__doc__,
4527"wait4(pid, options) -> (pid, status, rusage)\n\n\
4528Wait for completion of a given child process.");
4529
4530static PyObject *
4531posix_wait4(PyObject *self, PyObject *args)
4532{
Christian Heimes292d3512008-02-03 16:51:08 +00004533 pid_t pid;
4534 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004535 struct rusage ru;
4536 WAIT_TYPE status;
4537 WAIT_STATUS_INT(status) = 0;
4538
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004539 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004540 return NULL;
4541
4542 Py_BEGIN_ALLOW_THREADS
4543 pid = wait4(pid, &status, options, &ru);
4544 Py_END_ALLOW_THREADS
4545
4546 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4547}
4548#endif /* HAVE_WAIT4 */
4549
Guido van Rossumb6775db1994-08-01 11:34:53 +00004550#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004551PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004552"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004553Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004554
Barry Warsaw53699e91996-12-10 23:23:01 +00004555static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004556posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004557{
Christian Heimes292d3512008-02-03 16:51:08 +00004558 pid_t pid;
4559 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004560 WAIT_TYPE status;
4561 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004562
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004563 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004564 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004565 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004566 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004567 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004568 if (pid == -1)
4569 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004570
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004571 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004572}
4573
Tim Petersab034fa2002-02-01 11:27:43 +00004574#elif defined(HAVE_CWAIT)
4575
4576/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004577PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004578"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004579"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004580
4581static PyObject *
4582posix_waitpid(PyObject *self, PyObject *args)
4583{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004584 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004585 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004586
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004587 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004588 return NULL;
4589 Py_BEGIN_ALLOW_THREADS
4590 pid = _cwait(&status, pid, options);
4591 Py_END_ALLOW_THREADS
4592 if (pid == -1)
4593 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004594
4595 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004596 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004597}
4598#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004599
Guido van Rossumad0ee831995-03-01 10:34:45 +00004600#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004601PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004602"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004603Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004604
Barry Warsaw53699e91996-12-10 23:23:01 +00004605static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004606posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004607{
Christian Heimes292d3512008-02-03 16:51:08 +00004608 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004609 WAIT_TYPE status;
4610 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004611
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004612 Py_BEGIN_ALLOW_THREADS
4613 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004614 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004615 if (pid == -1)
4616 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004617
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004618 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004619}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004620#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004621
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004622
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004623PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004624"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004625Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004626
Barry Warsaw53699e91996-12-10 23:23:01 +00004627static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004628posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004629{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004630#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004631 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004632#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004633#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004634 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004635#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004636 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004637#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004638#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004639}
4640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004641
Guido van Rossumb6775db1994-08-01 11:34:53 +00004642#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004643PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004644"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004645Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004646
Barry Warsaw53699e91996-12-10 23:23:01 +00004647static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004648posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004649{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004650 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004651 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004652 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004653 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004654 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004655 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004656
Martin v. Löwis011e8422009-05-05 04:43:17 +00004657 if (!PyArg_ParseTuple(args, "O&:readlink",
4658 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004659 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004660 path = bytes2str(opath, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004661 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004662 if (v == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00004663 release_bytes(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004664 return NULL;
4665 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004666
4667 if (PyUnicode_Check(v)) {
4668 arg_is_unicode = 1;
4669 }
4670 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004671
Barry Warsaw53699e91996-12-10 23:23:01 +00004672 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004673 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004674 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004675 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004676 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004677
Martin v. Löwis011e8422009-05-05 04:43:17 +00004678 release_bytes(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004679 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004680 if (arg_is_unicode) {
4681 PyObject *w;
4682
4683 w = PyUnicode_FromEncodedObject(v,
4684 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004685 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004686 if (w != NULL) {
4687 Py_DECREF(v);
4688 v = w;
4689 }
4690 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004691 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004692 }
4693 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004694 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004695}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004696#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004697
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004698
Guido van Rossumb6775db1994-08-01 11:34:53 +00004699#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004700PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004701"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004702Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004703
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004704static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004705posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004706{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004707 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004708}
4709#endif /* HAVE_SYMLINK */
4710
4711
4712#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004713#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4714static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004715system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004716{
4717 ULONG value = 0;
4718
4719 Py_BEGIN_ALLOW_THREADS
4720 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4721 Py_END_ALLOW_THREADS
4722
4723 return value;
4724}
4725
4726static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004727posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004728{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004729 /* Currently Only Uptime is Provided -- Others Later */
4730 return Py_BuildValue("ddddd",
4731 (double)0 /* t.tms_utime / HZ */,
4732 (double)0 /* t.tms_stime / HZ */,
4733 (double)0 /* t.tms_cutime / HZ */,
4734 (double)0 /* t.tms_cstime / HZ */,
4735 (double)system_uptime() / 1000);
4736}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004737#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004738#define NEED_TICKS_PER_SECOND
4739static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004740static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004741posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004742{
4743 struct tms t;
4744 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004745 errno = 0;
4746 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004747 if (c == (clock_t) -1)
4748 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004749 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004750 (double)t.tms_utime / ticks_per_second,
4751 (double)t.tms_stime / ticks_per_second,
4752 (double)t.tms_cutime / ticks_per_second,
4753 (double)t.tms_cstime / ticks_per_second,
4754 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004755}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004756#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004757#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004758
4759
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004760#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004761#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004762static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004763posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004764{
4765 FILETIME create, exit, kernel, user;
4766 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004767 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004768 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4769 /* The fields of a FILETIME structure are the hi and lo part
4770 of a 64-bit value expressed in 100 nanosecond units.
4771 1e7 is one second in such units; 1e-7 the inverse.
4772 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4773 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004774 return Py_BuildValue(
4775 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004776 (double)(user.dwHighDateTime*429.4967296 +
4777 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004778 (double)(kernel.dwHighDateTime*429.4967296 +
4779 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004780 (double)0,
4781 (double)0,
4782 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004783}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004784#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004785
4786#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004787PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004788"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004789Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004790#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004791
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004792
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004793#ifdef HAVE_GETSID
4794PyDoc_STRVAR(posix_getsid__doc__,
4795"getsid(pid) -> sid\n\n\
4796Call the system call getsid().");
4797
4798static PyObject *
4799posix_getsid(PyObject *self, PyObject *args)
4800{
Christian Heimes292d3512008-02-03 16:51:08 +00004801 pid_t pid;
4802 int sid;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004803 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004804 return NULL;
4805 sid = getsid(pid);
4806 if (sid < 0)
4807 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004808 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004809}
4810#endif /* HAVE_GETSID */
4811
4812
Guido van Rossumb6775db1994-08-01 11:34:53 +00004813#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004814PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004815"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004816Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004817
Barry Warsaw53699e91996-12-10 23:23:01 +00004818static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004819posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004820{
Guido van Rossum687dd131993-05-17 08:34:16 +00004821 if (setsid() < 0)
4822 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004823 Py_INCREF(Py_None);
4824 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004825}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004826#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004827
Guido van Rossumb6775db1994-08-01 11:34:53 +00004828#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004829PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004830"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004831Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004832
Barry Warsaw53699e91996-12-10 23:23:01 +00004833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004834posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004835{
Christian Heimes292d3512008-02-03 16:51:08 +00004836 pid_t pid;
4837 int pgrp;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004838 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004839 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004840 if (setpgid(pid, pgrp) < 0)
4841 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004842 Py_INCREF(Py_None);
4843 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004844}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004845#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004846
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004847
Guido van Rossumb6775db1994-08-01 11:34:53 +00004848#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004849PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004850"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004851Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004852
Barry Warsaw53699e91996-12-10 23:23:01 +00004853static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004854posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004855{
Christian Heimes15ebc882008-02-04 18:48:49 +00004856 int fd;
4857 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004858 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004859 return NULL;
4860 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004861 if (pgid < 0)
4862 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004863 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004864}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004865#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004866
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004867
Guido van Rossumb6775db1994-08-01 11:34:53 +00004868#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004869PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004870"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004871Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004872
Barry Warsaw53699e91996-12-10 23:23:01 +00004873static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004874posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004875{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004876 int fd;
4877 pid_t pgid;
4878 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004879 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004880 if (tcsetpgrp(fd, pgid) < 0)
4881 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004882 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004883 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004884}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004885#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004886
Guido van Rossum687dd131993-05-17 08:34:16 +00004887/* Functions acting on file descriptors */
4888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004889PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004890"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004891Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004892
Barry Warsaw53699e91996-12-10 23:23:01 +00004893static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004894posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004895{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004896 PyObject *ofile;
4897 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004898 int flag;
4899 int mode = 0777;
4900 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004901
4902#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004903 PyUnicodeObject *po;
4904 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4905 Py_BEGIN_ALLOW_THREADS
4906 /* PyUnicode_AS_UNICODE OK without thread
4907 lock as it is a simple dereference. */
4908 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4909 Py_END_ALLOW_THREADS
4910 if (fd < 0)
4911 return posix_error();
4912 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004913 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004914 /* Drop the argument parsing error as narrow strings
4915 are also valid. */
4916 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004917#endif
4918
Martin v. Löwis011e8422009-05-05 04:43:17 +00004919 if (!PyArg_ParseTuple(args, "O&i|i",
4920 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004921 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004922 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004923 file = bytes2str(ofile, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00004924 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004925 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004926 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004927 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004928 return posix_error_with_allocated_filename(ofile);
4929 release_bytes(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004930 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004931}
4932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004933
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004934PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004935"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004936Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004937
Barry Warsaw53699e91996-12-10 23:23:01 +00004938static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004939posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004940{
4941 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004942 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004943 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004944 if (!_PyVerify_fd(fd))
4945 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004946 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004947 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004948 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004949 if (res < 0)
4950 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004951 Py_INCREF(Py_None);
4952 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004953}
4954
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004955
Christian Heimesfdab48e2008-01-20 09:06:41 +00004956PyDoc_STRVAR(posix_closerange__doc__,
4957"closerange(fd_low, fd_high)\n\n\
4958Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4959
4960static PyObject *
4961posix_closerange(PyObject *self, PyObject *args)
4962{
4963 int fd_from, fd_to, i;
4964 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4965 return NULL;
4966 Py_BEGIN_ALLOW_THREADS
4967 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004968 if (_PyVerify_fd(i))
4969 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00004970 Py_END_ALLOW_THREADS
4971 Py_RETURN_NONE;
4972}
4973
4974
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004975PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004976"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004977Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004978
Barry Warsaw53699e91996-12-10 23:23:01 +00004979static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004980posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004981{
4982 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004983 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004984 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004985 if (!_PyVerify_fd(fd))
4986 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004987 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004988 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004989 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004990 if (fd < 0)
4991 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004992 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004993}
4994
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004995
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004996PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004997"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004998Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004999
Barry Warsaw53699e91996-12-10 23:23:01 +00005000static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005001posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005002{
5003 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005004 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005005 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005006 if (!_PyVerify_fd_dup2(fd, fd2))
5007 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005008 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005009 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005010 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005011 if (res < 0)
5012 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005013 Py_INCREF(Py_None);
5014 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005015}
5016
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005017
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005018PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005019"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005020Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005021
Barry Warsaw53699e91996-12-10 23:23:01 +00005022static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005023posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005024{
5025 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005026#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005027 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005028#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005029 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005030#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005031 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005032 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005033 return NULL;
5034#ifdef SEEK_SET
5035 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5036 switch (how) {
5037 case 0: how = SEEK_SET; break;
5038 case 1: how = SEEK_CUR; break;
5039 case 2: how = SEEK_END; break;
5040 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005041#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005042
5043#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005044 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005045#else
5046 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005047 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005048#endif
5049 if (PyErr_Occurred())
5050 return NULL;
5051
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005052 if (!_PyVerify_fd(fd))
5053 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005054 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005055#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005056 res = _lseeki64(fd, pos, how);
5057#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005058 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005059#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005060 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005061 if (res < 0)
5062 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005063
5064#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005065 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005066#else
5067 return PyLong_FromLongLong(res);
5068#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005069}
5070
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005072PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005073"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005074Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005075
Barry Warsaw53699e91996-12-10 23:23:01 +00005076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005077posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005078{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005079 int fd, size;
5080 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005081 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005082 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005083 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005084 if (size < 0) {
5085 errno = EINVAL;
5086 return posix_error();
5087 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005088 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005089 if (buffer == NULL)
5090 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005091 if (!_PyVerify_fd(fd))
5092 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005093 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005094 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005095 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005096 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005097 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005098 return posix_error();
5099 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005100 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005101 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005102 return buffer;
5103}
5104
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005106PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005107"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005108Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005109
Barry Warsaw53699e91996-12-10 23:23:01 +00005110static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005111posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005112{
Martin v. Löwis423be952008-08-13 15:53:07 +00005113 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005114 int fd;
5115 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005116
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005117 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005118 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005119 if (!_PyVerify_fd(fd))
5120 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005121 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005122 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005123 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005124 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005125 if (size < 0)
5126 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005127 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005128}
5129
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005131PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005132"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005133Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005134
Barry Warsaw53699e91996-12-10 23:23:01 +00005135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005136posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005137{
5138 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005139 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005140 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005141 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005142 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005143#ifdef __VMS
5144 /* on OpenVMS we must ensure that all bytes are written to the file */
5145 fsync(fd);
5146#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005147 if (!_PyVerify_fd(fd))
5148 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005149 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005150 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005151 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005152 if (res != 0) {
5153#ifdef MS_WINDOWS
5154 return win32_error("fstat", NULL);
5155#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005156 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005157#endif
5158 }
Tim Peters5aa91602002-01-30 05:46:57 +00005159
Martin v. Löwis14694662006-02-03 12:54:16 +00005160 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005161}
5162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005163PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005164"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005165Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005166connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005167
5168static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005169posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005170{
5171 int fd;
5172 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5173 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005174 if (!_PyVerify_fd(fd))
5175 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005176 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005177}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005178
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005179#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005180PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005181"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005182Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005183
Barry Warsaw53699e91996-12-10 23:23:01 +00005184static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005185posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005186{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005187#if defined(PYOS_OS2)
5188 HFILE read, write;
5189 APIRET rc;
5190
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005191 Py_BEGIN_ALLOW_THREADS
5192 rc = DosCreatePipe( &read, &write, 4096);
5193 Py_END_ALLOW_THREADS
5194 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005195 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005196
5197 return Py_BuildValue("(ii)", read, write);
5198#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005199#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005200 int fds[2];
5201 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005202 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005203 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005204 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005205 if (res != 0)
5206 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005207 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005208#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005209 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005210 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005211 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005212 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005213 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005214 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005215 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005216 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005217 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5218 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005219 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005220#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005221#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005222}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005223#endif /* HAVE_PIPE */
5224
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005225
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005226#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005227PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005228"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005229Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005230
Barry Warsaw53699e91996-12-10 23:23:01 +00005231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005232posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005233{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005234 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005235 int mode = 0666;
5236 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005237 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005238 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005239 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005240 res = mkfifo(filename, mode);
5241 Py_END_ALLOW_THREADS
5242 if (res < 0)
5243 return posix_error();
5244 Py_INCREF(Py_None);
5245 return Py_None;
5246}
5247#endif
5248
5249
Neal Norwitz11690112002-07-30 01:08:28 +00005250#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005251PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005252"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005253Create a filesystem node (file, device special file or named pipe)\n\
5254named filename. mode specifies both the permissions to use and the\n\
5255type of node to be created, being combined (bitwise OR) with one of\n\
5256S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005257device defines the newly created device special file (probably using\n\
5258os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005259
5260
5261static PyObject *
5262posix_mknod(PyObject *self, PyObject *args)
5263{
5264 char *filename;
5265 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005266 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005267 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005268 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005269 return NULL;
5270 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005271 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005272 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005273 if (res < 0)
5274 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005275 Py_INCREF(Py_None);
5276 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005277}
5278#endif
5279
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005280#ifdef HAVE_DEVICE_MACROS
5281PyDoc_STRVAR(posix_major__doc__,
5282"major(device) -> major number\n\
5283Extracts a device major number from a raw device number.");
5284
5285static PyObject *
5286posix_major(PyObject *self, PyObject *args)
5287{
5288 int device;
5289 if (!PyArg_ParseTuple(args, "i:major", &device))
5290 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005291 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005292}
5293
5294PyDoc_STRVAR(posix_minor__doc__,
5295"minor(device) -> minor number\n\
5296Extracts a device minor number from a raw device number.");
5297
5298static PyObject *
5299posix_minor(PyObject *self, PyObject *args)
5300{
5301 int device;
5302 if (!PyArg_ParseTuple(args, "i:minor", &device))
5303 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005304 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005305}
5306
5307PyDoc_STRVAR(posix_makedev__doc__,
5308"makedev(major, minor) -> device number\n\
5309Composes a raw device number from the major and minor device numbers.");
5310
5311static PyObject *
5312posix_makedev(PyObject *self, PyObject *args)
5313{
5314 int major, minor;
5315 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5316 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005317 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005318}
5319#endif /* device macros */
5320
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005321
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005322#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005323PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005324"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005325Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005326
Barry Warsaw53699e91996-12-10 23:23:01 +00005327static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005328posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005329{
5330 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005331 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005332 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005333 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005334
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005335 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005336 return NULL;
5337
5338#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005339 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005340#else
5341 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005342 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005343#endif
5344 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005345 return NULL;
5346
Barry Warsaw53699e91996-12-10 23:23:01 +00005347 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005348 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005349 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005350 if (res < 0)
5351 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005352 Py_INCREF(Py_None);
5353 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005354}
5355#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005356
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005357#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005358PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005359"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005360Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005361
Fred Drake762e2061999-08-26 17:23:54 +00005362/* Save putenv() parameters as values here, so we can collect them when they
5363 * get re-set with another call for the same key. */
5364static PyObject *posix_putenv_garbage;
5365
Tim Peters5aa91602002-01-30 05:46:57 +00005366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005367posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005368{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005369#ifdef MS_WINDOWS
5370 wchar_t *s1, *s2;
5371 wchar_t *newenv;
5372#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005373 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005374 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005375 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005376#endif
Fred Drake762e2061999-08-26 17:23:54 +00005377 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005378 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005379
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005380#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005381 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005382 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005383 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005384 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005385#else
5386 if (!PyArg_ParseTuple(args,
5387 "O&O&:putenv",
5388 PyUnicode_FSConverter, &os1,
5389 PyUnicode_FSConverter, &os2))
5390 return NULL;
5391 s1 = bytes2str(os1, 1);
5392 s2 = bytes2str(os2, 1);
5393#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005394
5395#if defined(PYOS_OS2)
5396 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5397 APIRET rc;
5398
Guido van Rossumd48f2521997-12-05 22:19:34 +00005399 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5400 if (rc != NO_ERROR)
5401 return os2_error(rc);
5402
5403 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5404 APIRET rc;
5405
Guido van Rossumd48f2521997-12-05 22:19:34 +00005406 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5407 if (rc != NO_ERROR)
5408 return os2_error(rc);
5409 } else {
5410#endif
Fred Drake762e2061999-08-26 17:23:54 +00005411 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005412 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005413 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005414#ifdef MS_WINDOWS
5415 len = wcslen(s1) + wcslen(s2) + 2;
5416 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5417#else
5418 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005419 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005420#endif
Fred Drake762e2061999-08-26 17:23:54 +00005421 if (newstr == NULL)
5422 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005423#ifdef MS_WINDOWS
5424 newenv = PyUnicode_AsUnicode(newstr);
5425 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5426 if (_wputenv(newenv)) {
5427 Py_DECREF(newstr);
5428 posix_error();
5429 return NULL;
5430 }
5431#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005432 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005433 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5434 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005435 Py_DECREF(newstr);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005436 release_bytes(os1);
5437 release_bytes(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005438 posix_error();
5439 return NULL;
5440 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005441#endif
Fred Drake762e2061999-08-26 17:23:54 +00005442 /* Install the first arg and newstr in posix_putenv_garbage;
5443 * this will cause previous value to be collected. This has to
5444 * happen after the real putenv() call because the old value
5445 * was still accessible until then. */
5446 if (PyDict_SetItem(posix_putenv_garbage,
5447 PyTuple_GET_ITEM(args, 0), newstr)) {
5448 /* really not much we can do; just leak */
5449 PyErr_Clear();
5450 }
5451 else {
5452 Py_DECREF(newstr);
5453 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005454
5455#if defined(PYOS_OS2)
5456 }
5457#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005458#ifndef MS_WINDOWS
5459 release_bytes(os1);
5460 release_bytes(os2);
5461#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005462 Py_INCREF(Py_None);
5463 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005464}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005465#endif /* putenv */
5466
Guido van Rossumc524d952001-10-19 01:31:59 +00005467#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005468PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005469"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005470Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005471
5472static PyObject *
5473posix_unsetenv(PyObject *self, PyObject *args)
5474{
5475 char *s1;
5476
5477 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5478 return NULL;
5479
5480 unsetenv(s1);
5481
5482 /* Remove the key from posix_putenv_garbage;
5483 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005484 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005485 * old value was still accessible until then.
5486 */
5487 if (PyDict_DelItem(posix_putenv_garbage,
5488 PyTuple_GET_ITEM(args, 0))) {
5489 /* really not much we can do; just leak */
5490 PyErr_Clear();
5491 }
5492
5493 Py_INCREF(Py_None);
5494 return Py_None;
5495}
5496#endif /* unsetenv */
5497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005498PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005499"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005501
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005502static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005503posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005504{
5505 int code;
5506 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005507 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005508 return NULL;
5509 message = strerror(code);
5510 if (message == NULL) {
5511 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005512 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005513 return NULL;
5514 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005515 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005516}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005517
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005518
Guido van Rossumc9641791998-08-04 15:26:23 +00005519#ifdef HAVE_SYS_WAIT_H
5520
Fred Drake106c1a02002-04-23 15:58:02 +00005521#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005522PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005523"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005525
5526static PyObject *
5527posix_WCOREDUMP(PyObject *self, PyObject *args)
5528{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005529 WAIT_TYPE status;
5530 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005531
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005532 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005533 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005534
5535 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005536}
5537#endif /* WCOREDUMP */
5538
5539#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005540PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005541"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005542Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005543job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005544
5545static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005546posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005547{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005548 WAIT_TYPE status;
5549 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005550
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005551 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005552 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005553
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005554 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005555}
5556#endif /* WIFCONTINUED */
5557
Guido van Rossumc9641791998-08-04 15:26:23 +00005558#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005560"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005561Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005562
5563static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005564posix_WIFSTOPPED(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:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005570 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005571
Fred Drake106c1a02002-04-23 15:58:02 +00005572 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005573}
5574#endif /* WIFSTOPPED */
5575
5576#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005577PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005578"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005579Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005580
5581static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005582posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005583{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005584 WAIT_TYPE status;
5585 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005586
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005587 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005588 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005589
Fred Drake106c1a02002-04-23 15:58:02 +00005590 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005591}
5592#endif /* WIFSIGNALED */
5593
5594#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005595PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005596"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005597Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005599
5600static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005601posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005602{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005603 WAIT_TYPE status;
5604 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005605
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005606 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005607 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005608
Fred Drake106c1a02002-04-23 15:58:02 +00005609 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005610}
5611#endif /* WIFEXITED */
5612
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005613#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005614PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005615"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005616Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005617
5618static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005619posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005620{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005621 WAIT_TYPE status;
5622 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005623
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005624 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005625 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005626
Guido van Rossumc9641791998-08-04 15:26:23 +00005627 return Py_BuildValue("i", WEXITSTATUS(status));
5628}
5629#endif /* WEXITSTATUS */
5630
5631#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005632PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005633"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005634Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005635value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005636
5637static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005638posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005639{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005640 WAIT_TYPE status;
5641 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005642
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005643 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005644 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005645
Guido van Rossumc9641791998-08-04 15:26:23 +00005646 return Py_BuildValue("i", WTERMSIG(status));
5647}
5648#endif /* WTERMSIG */
5649
5650#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005651PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005652"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005653Return the signal that stopped the process that provided\n\
5654the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005655
5656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005657posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005658{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005659 WAIT_TYPE status;
5660 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005661
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005662 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005663 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005664
Guido van Rossumc9641791998-08-04 15:26:23 +00005665 return Py_BuildValue("i", WSTOPSIG(status));
5666}
5667#endif /* WSTOPSIG */
5668
5669#endif /* HAVE_SYS_WAIT_H */
5670
5671
Thomas Wouters477c8d52006-05-27 19:21:47 +00005672#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005673#ifdef _SCO_DS
5674/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5675 needed definitions in sys/statvfs.h */
5676#define _SVID3
5677#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005678#include <sys/statvfs.h>
5679
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005680static PyObject*
5681_pystatvfs_fromstructstatvfs(struct statvfs st) {
5682 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5683 if (v == NULL)
5684 return NULL;
5685
5686#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005687 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5688 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5689 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5690 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5691 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5692 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5693 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5694 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5695 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5696 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005697#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005698 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5699 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005700 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005701 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005702 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005703 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005704 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005705 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005706 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005707 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005708 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005709 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005710 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005711 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005712 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5713 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005714#endif
5715
5716 return v;
5717}
5718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005719PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005720"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005721Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005722
5723static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005724posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005725{
5726 int fd, res;
5727 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005728
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005729 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005730 return NULL;
5731 Py_BEGIN_ALLOW_THREADS
5732 res = fstatvfs(fd, &st);
5733 Py_END_ALLOW_THREADS
5734 if (res != 0)
5735 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005736
5737 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005738}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005739#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005740
5741
Thomas Wouters477c8d52006-05-27 19:21:47 +00005742#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005743#include <sys/statvfs.h>
5744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005745PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005746"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005747Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005748
5749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005750posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005751{
5752 char *path;
5753 int res;
5754 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005755 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005756 return NULL;
5757 Py_BEGIN_ALLOW_THREADS
5758 res = statvfs(path, &st);
5759 Py_END_ALLOW_THREADS
5760 if (res != 0)
5761 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005762
5763 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005764}
5765#endif /* HAVE_STATVFS */
5766
Fred Drakec9680921999-12-13 16:37:25 +00005767/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5768 * It maps strings representing configuration variable names to
5769 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005770 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005771 * rarely-used constants. There are three separate tables that use
5772 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005773 *
5774 * This code is always included, even if none of the interfaces that
5775 * need it are included. The #if hackery needed to avoid it would be
5776 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005777 */
5778struct constdef {
5779 char *name;
5780 long value;
5781};
5782
Fred Drake12c6e2d1999-12-14 21:25:03 +00005783static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005784conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005785 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005786{
Christian Heimes217cfd12007-12-02 14:31:20 +00005787 if (PyLong_Check(arg)) {
5788 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005789 return 1;
5790 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005791 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005792 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005793 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005794 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005795 size_t hi = tablesize;
5796 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005797 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005798 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005799 PyErr_SetString(PyExc_TypeError,
5800 "configuration names must be strings or integers");
5801 return 0;
5802 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005803 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005804 if (confname == NULL)
5805 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005806 while (lo < hi) {
5807 mid = (lo + hi) / 2;
5808 cmp = strcmp(confname, table[mid].name);
5809 if (cmp < 0)
5810 hi = mid;
5811 else if (cmp > 0)
5812 lo = mid + 1;
5813 else {
5814 *valuep = table[mid].value;
5815 return 1;
5816 }
5817 }
5818 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005819 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005820 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005821}
5822
5823
5824#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5825static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005826#ifdef _PC_ABI_AIO_XFER_MAX
5827 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5828#endif
5829#ifdef _PC_ABI_ASYNC_IO
5830 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5831#endif
Fred Drakec9680921999-12-13 16:37:25 +00005832#ifdef _PC_ASYNC_IO
5833 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5834#endif
5835#ifdef _PC_CHOWN_RESTRICTED
5836 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5837#endif
5838#ifdef _PC_FILESIZEBITS
5839 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5840#endif
5841#ifdef _PC_LAST
5842 {"PC_LAST", _PC_LAST},
5843#endif
5844#ifdef _PC_LINK_MAX
5845 {"PC_LINK_MAX", _PC_LINK_MAX},
5846#endif
5847#ifdef _PC_MAX_CANON
5848 {"PC_MAX_CANON", _PC_MAX_CANON},
5849#endif
5850#ifdef _PC_MAX_INPUT
5851 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5852#endif
5853#ifdef _PC_NAME_MAX
5854 {"PC_NAME_MAX", _PC_NAME_MAX},
5855#endif
5856#ifdef _PC_NO_TRUNC
5857 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5858#endif
5859#ifdef _PC_PATH_MAX
5860 {"PC_PATH_MAX", _PC_PATH_MAX},
5861#endif
5862#ifdef _PC_PIPE_BUF
5863 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5864#endif
5865#ifdef _PC_PRIO_IO
5866 {"PC_PRIO_IO", _PC_PRIO_IO},
5867#endif
5868#ifdef _PC_SOCK_MAXBUF
5869 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5870#endif
5871#ifdef _PC_SYNC_IO
5872 {"PC_SYNC_IO", _PC_SYNC_IO},
5873#endif
5874#ifdef _PC_VDISABLE
5875 {"PC_VDISABLE", _PC_VDISABLE},
5876#endif
5877};
5878
Fred Drakec9680921999-12-13 16:37:25 +00005879static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005880conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005881{
5882 return conv_confname(arg, valuep, posix_constants_pathconf,
5883 sizeof(posix_constants_pathconf)
5884 / sizeof(struct constdef));
5885}
5886#endif
5887
5888#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005889PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005890"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005891Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005892If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005893
5894static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005895posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005896{
5897 PyObject *result = NULL;
5898 int name, fd;
5899
Fred Drake12c6e2d1999-12-14 21:25:03 +00005900 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5901 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005902 long limit;
5903
5904 errno = 0;
5905 limit = fpathconf(fd, name);
5906 if (limit == -1 && errno != 0)
5907 posix_error();
5908 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005909 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005910 }
5911 return result;
5912}
5913#endif
5914
5915
5916#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005917PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005918"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005919Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005920If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005921
5922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005923posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005924{
5925 PyObject *result = NULL;
5926 int name;
5927 char *path;
5928
5929 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5930 conv_path_confname, &name)) {
5931 long limit;
5932
5933 errno = 0;
5934 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005935 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005936 if (errno == EINVAL)
5937 /* could be a path or name problem */
5938 posix_error();
5939 else
5940 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005941 }
Fred Drakec9680921999-12-13 16:37:25 +00005942 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005943 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005944 }
5945 return result;
5946}
5947#endif
5948
5949#ifdef HAVE_CONFSTR
5950static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005951#ifdef _CS_ARCHITECTURE
5952 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5953#endif
5954#ifdef _CS_HOSTNAME
5955 {"CS_HOSTNAME", _CS_HOSTNAME},
5956#endif
5957#ifdef _CS_HW_PROVIDER
5958 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5959#endif
5960#ifdef _CS_HW_SERIAL
5961 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5962#endif
5963#ifdef _CS_INITTAB_NAME
5964 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5965#endif
Fred Drakec9680921999-12-13 16:37:25 +00005966#ifdef _CS_LFS64_CFLAGS
5967 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5968#endif
5969#ifdef _CS_LFS64_LDFLAGS
5970 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5971#endif
5972#ifdef _CS_LFS64_LIBS
5973 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5974#endif
5975#ifdef _CS_LFS64_LINTFLAGS
5976 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5977#endif
5978#ifdef _CS_LFS_CFLAGS
5979 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5980#endif
5981#ifdef _CS_LFS_LDFLAGS
5982 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5983#endif
5984#ifdef _CS_LFS_LIBS
5985 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5986#endif
5987#ifdef _CS_LFS_LINTFLAGS
5988 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5989#endif
Fred Draked86ed291999-12-15 15:34:33 +00005990#ifdef _CS_MACHINE
5991 {"CS_MACHINE", _CS_MACHINE},
5992#endif
Fred Drakec9680921999-12-13 16:37:25 +00005993#ifdef _CS_PATH
5994 {"CS_PATH", _CS_PATH},
5995#endif
Fred Draked86ed291999-12-15 15:34:33 +00005996#ifdef _CS_RELEASE
5997 {"CS_RELEASE", _CS_RELEASE},
5998#endif
5999#ifdef _CS_SRPC_DOMAIN
6000 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6001#endif
6002#ifdef _CS_SYSNAME
6003 {"CS_SYSNAME", _CS_SYSNAME},
6004#endif
6005#ifdef _CS_VERSION
6006 {"CS_VERSION", _CS_VERSION},
6007#endif
Fred Drakec9680921999-12-13 16:37:25 +00006008#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6009 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6010#endif
6011#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6012 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6013#endif
6014#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6015 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6016#endif
6017#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6018 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6019#endif
6020#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6021 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6022#endif
6023#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6024 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6025#endif
6026#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6027 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6028#endif
6029#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6030 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6031#endif
6032#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6033 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6034#endif
6035#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6036 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6037#endif
6038#ifdef _CS_XBS5_LP64_OFF64_LIBS
6039 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6040#endif
6041#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6042 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6043#endif
6044#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6045 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6046#endif
6047#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6048 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6049#endif
6050#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6051 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6052#endif
6053#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6054 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6055#endif
Fred Draked86ed291999-12-15 15:34:33 +00006056#ifdef _MIPS_CS_AVAIL_PROCESSORS
6057 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6058#endif
6059#ifdef _MIPS_CS_BASE
6060 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6061#endif
6062#ifdef _MIPS_CS_HOSTID
6063 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6064#endif
6065#ifdef _MIPS_CS_HW_NAME
6066 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6067#endif
6068#ifdef _MIPS_CS_NUM_PROCESSORS
6069 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6070#endif
6071#ifdef _MIPS_CS_OSREL_MAJ
6072 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6073#endif
6074#ifdef _MIPS_CS_OSREL_MIN
6075 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6076#endif
6077#ifdef _MIPS_CS_OSREL_PATCH
6078 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6079#endif
6080#ifdef _MIPS_CS_OS_NAME
6081 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6082#endif
6083#ifdef _MIPS_CS_OS_PROVIDER
6084 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6085#endif
6086#ifdef _MIPS_CS_PROCESSORS
6087 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6088#endif
6089#ifdef _MIPS_CS_SERIAL
6090 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6091#endif
6092#ifdef _MIPS_CS_VENDOR
6093 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6094#endif
Fred Drakec9680921999-12-13 16:37:25 +00006095};
6096
6097static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006098conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006099{
6100 return conv_confname(arg, valuep, posix_constants_confstr,
6101 sizeof(posix_constants_confstr)
6102 / sizeof(struct constdef));
6103}
6104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006105PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006106"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006107Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006108
6109static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006110posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006111{
6112 PyObject *result = NULL;
6113 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006114 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006115
6116 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006117 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006118
Fred Drakec9680921999-12-13 16:37:25 +00006119 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006120 len = confstr(name, buffer, sizeof(buffer));
6121 if (len == 0) {
6122 if (errno) {
6123 posix_error();
6124 }
6125 else {
6126 result = Py_None;
6127 Py_INCREF(Py_None);
6128 }
Fred Drakec9680921999-12-13 16:37:25 +00006129 }
6130 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006131 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006132 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006133 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006134 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006135 }
6136 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006137 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006138 }
6139 }
6140 return result;
6141}
6142#endif
6143
6144
6145#ifdef HAVE_SYSCONF
6146static struct constdef posix_constants_sysconf[] = {
6147#ifdef _SC_2_CHAR_TERM
6148 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6149#endif
6150#ifdef _SC_2_C_BIND
6151 {"SC_2_C_BIND", _SC_2_C_BIND},
6152#endif
6153#ifdef _SC_2_C_DEV
6154 {"SC_2_C_DEV", _SC_2_C_DEV},
6155#endif
6156#ifdef _SC_2_C_VERSION
6157 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6158#endif
6159#ifdef _SC_2_FORT_DEV
6160 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6161#endif
6162#ifdef _SC_2_FORT_RUN
6163 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6164#endif
6165#ifdef _SC_2_LOCALEDEF
6166 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6167#endif
6168#ifdef _SC_2_SW_DEV
6169 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6170#endif
6171#ifdef _SC_2_UPE
6172 {"SC_2_UPE", _SC_2_UPE},
6173#endif
6174#ifdef _SC_2_VERSION
6175 {"SC_2_VERSION", _SC_2_VERSION},
6176#endif
Fred Draked86ed291999-12-15 15:34:33 +00006177#ifdef _SC_ABI_ASYNCHRONOUS_IO
6178 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6179#endif
6180#ifdef _SC_ACL
6181 {"SC_ACL", _SC_ACL},
6182#endif
Fred Drakec9680921999-12-13 16:37:25 +00006183#ifdef _SC_AIO_LISTIO_MAX
6184 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6185#endif
Fred Drakec9680921999-12-13 16:37:25 +00006186#ifdef _SC_AIO_MAX
6187 {"SC_AIO_MAX", _SC_AIO_MAX},
6188#endif
6189#ifdef _SC_AIO_PRIO_DELTA_MAX
6190 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6191#endif
6192#ifdef _SC_ARG_MAX
6193 {"SC_ARG_MAX", _SC_ARG_MAX},
6194#endif
6195#ifdef _SC_ASYNCHRONOUS_IO
6196 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6197#endif
6198#ifdef _SC_ATEXIT_MAX
6199 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6200#endif
Fred Draked86ed291999-12-15 15:34:33 +00006201#ifdef _SC_AUDIT
6202 {"SC_AUDIT", _SC_AUDIT},
6203#endif
Fred Drakec9680921999-12-13 16:37:25 +00006204#ifdef _SC_AVPHYS_PAGES
6205 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6206#endif
6207#ifdef _SC_BC_BASE_MAX
6208 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6209#endif
6210#ifdef _SC_BC_DIM_MAX
6211 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6212#endif
6213#ifdef _SC_BC_SCALE_MAX
6214 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6215#endif
6216#ifdef _SC_BC_STRING_MAX
6217 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6218#endif
Fred Draked86ed291999-12-15 15:34:33 +00006219#ifdef _SC_CAP
6220 {"SC_CAP", _SC_CAP},
6221#endif
Fred Drakec9680921999-12-13 16:37:25 +00006222#ifdef _SC_CHARCLASS_NAME_MAX
6223 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6224#endif
6225#ifdef _SC_CHAR_BIT
6226 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6227#endif
6228#ifdef _SC_CHAR_MAX
6229 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6230#endif
6231#ifdef _SC_CHAR_MIN
6232 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6233#endif
6234#ifdef _SC_CHILD_MAX
6235 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6236#endif
6237#ifdef _SC_CLK_TCK
6238 {"SC_CLK_TCK", _SC_CLK_TCK},
6239#endif
6240#ifdef _SC_COHER_BLKSZ
6241 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6242#endif
6243#ifdef _SC_COLL_WEIGHTS_MAX
6244 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6245#endif
6246#ifdef _SC_DCACHE_ASSOC
6247 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6248#endif
6249#ifdef _SC_DCACHE_BLKSZ
6250 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6251#endif
6252#ifdef _SC_DCACHE_LINESZ
6253 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6254#endif
6255#ifdef _SC_DCACHE_SZ
6256 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6257#endif
6258#ifdef _SC_DCACHE_TBLKSZ
6259 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6260#endif
6261#ifdef _SC_DELAYTIMER_MAX
6262 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6263#endif
6264#ifdef _SC_EQUIV_CLASS_MAX
6265 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6266#endif
6267#ifdef _SC_EXPR_NEST_MAX
6268 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6269#endif
6270#ifdef _SC_FSYNC
6271 {"SC_FSYNC", _SC_FSYNC},
6272#endif
6273#ifdef _SC_GETGR_R_SIZE_MAX
6274 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6275#endif
6276#ifdef _SC_GETPW_R_SIZE_MAX
6277 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6278#endif
6279#ifdef _SC_ICACHE_ASSOC
6280 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6281#endif
6282#ifdef _SC_ICACHE_BLKSZ
6283 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6284#endif
6285#ifdef _SC_ICACHE_LINESZ
6286 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6287#endif
6288#ifdef _SC_ICACHE_SZ
6289 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6290#endif
Fred Draked86ed291999-12-15 15:34:33 +00006291#ifdef _SC_INF
6292 {"SC_INF", _SC_INF},
6293#endif
Fred Drakec9680921999-12-13 16:37:25 +00006294#ifdef _SC_INT_MAX
6295 {"SC_INT_MAX", _SC_INT_MAX},
6296#endif
6297#ifdef _SC_INT_MIN
6298 {"SC_INT_MIN", _SC_INT_MIN},
6299#endif
6300#ifdef _SC_IOV_MAX
6301 {"SC_IOV_MAX", _SC_IOV_MAX},
6302#endif
Fred Draked86ed291999-12-15 15:34:33 +00006303#ifdef _SC_IP_SECOPTS
6304 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6305#endif
Fred Drakec9680921999-12-13 16:37:25 +00006306#ifdef _SC_JOB_CONTROL
6307 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6308#endif
Fred Draked86ed291999-12-15 15:34:33 +00006309#ifdef _SC_KERN_POINTERS
6310 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6311#endif
6312#ifdef _SC_KERN_SIM
6313 {"SC_KERN_SIM", _SC_KERN_SIM},
6314#endif
Fred Drakec9680921999-12-13 16:37:25 +00006315#ifdef _SC_LINE_MAX
6316 {"SC_LINE_MAX", _SC_LINE_MAX},
6317#endif
6318#ifdef _SC_LOGIN_NAME_MAX
6319 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6320#endif
6321#ifdef _SC_LOGNAME_MAX
6322 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6323#endif
6324#ifdef _SC_LONG_BIT
6325 {"SC_LONG_BIT", _SC_LONG_BIT},
6326#endif
Fred Draked86ed291999-12-15 15:34:33 +00006327#ifdef _SC_MAC
6328 {"SC_MAC", _SC_MAC},
6329#endif
Fred Drakec9680921999-12-13 16:37:25 +00006330#ifdef _SC_MAPPED_FILES
6331 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6332#endif
6333#ifdef _SC_MAXPID
6334 {"SC_MAXPID", _SC_MAXPID},
6335#endif
6336#ifdef _SC_MB_LEN_MAX
6337 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6338#endif
6339#ifdef _SC_MEMLOCK
6340 {"SC_MEMLOCK", _SC_MEMLOCK},
6341#endif
6342#ifdef _SC_MEMLOCK_RANGE
6343 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6344#endif
6345#ifdef _SC_MEMORY_PROTECTION
6346 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6347#endif
6348#ifdef _SC_MESSAGE_PASSING
6349 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6350#endif
Fred Draked86ed291999-12-15 15:34:33 +00006351#ifdef _SC_MMAP_FIXED_ALIGNMENT
6352 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6353#endif
Fred Drakec9680921999-12-13 16:37:25 +00006354#ifdef _SC_MQ_OPEN_MAX
6355 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6356#endif
6357#ifdef _SC_MQ_PRIO_MAX
6358 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6359#endif
Fred Draked86ed291999-12-15 15:34:33 +00006360#ifdef _SC_NACLS_MAX
6361 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6362#endif
Fred Drakec9680921999-12-13 16:37:25 +00006363#ifdef _SC_NGROUPS_MAX
6364 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6365#endif
6366#ifdef _SC_NL_ARGMAX
6367 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6368#endif
6369#ifdef _SC_NL_LANGMAX
6370 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6371#endif
6372#ifdef _SC_NL_MSGMAX
6373 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6374#endif
6375#ifdef _SC_NL_NMAX
6376 {"SC_NL_NMAX", _SC_NL_NMAX},
6377#endif
6378#ifdef _SC_NL_SETMAX
6379 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6380#endif
6381#ifdef _SC_NL_TEXTMAX
6382 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6383#endif
6384#ifdef _SC_NPROCESSORS_CONF
6385 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6386#endif
6387#ifdef _SC_NPROCESSORS_ONLN
6388 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6389#endif
Fred Draked86ed291999-12-15 15:34:33 +00006390#ifdef _SC_NPROC_CONF
6391 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6392#endif
6393#ifdef _SC_NPROC_ONLN
6394 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6395#endif
Fred Drakec9680921999-12-13 16:37:25 +00006396#ifdef _SC_NZERO
6397 {"SC_NZERO", _SC_NZERO},
6398#endif
6399#ifdef _SC_OPEN_MAX
6400 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6401#endif
6402#ifdef _SC_PAGESIZE
6403 {"SC_PAGESIZE", _SC_PAGESIZE},
6404#endif
6405#ifdef _SC_PAGE_SIZE
6406 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6407#endif
6408#ifdef _SC_PASS_MAX
6409 {"SC_PASS_MAX", _SC_PASS_MAX},
6410#endif
6411#ifdef _SC_PHYS_PAGES
6412 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6413#endif
6414#ifdef _SC_PII
6415 {"SC_PII", _SC_PII},
6416#endif
6417#ifdef _SC_PII_INTERNET
6418 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6419#endif
6420#ifdef _SC_PII_INTERNET_DGRAM
6421 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6422#endif
6423#ifdef _SC_PII_INTERNET_STREAM
6424 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6425#endif
6426#ifdef _SC_PII_OSI
6427 {"SC_PII_OSI", _SC_PII_OSI},
6428#endif
6429#ifdef _SC_PII_OSI_CLTS
6430 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6431#endif
6432#ifdef _SC_PII_OSI_COTS
6433 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6434#endif
6435#ifdef _SC_PII_OSI_M
6436 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6437#endif
6438#ifdef _SC_PII_SOCKET
6439 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6440#endif
6441#ifdef _SC_PII_XTI
6442 {"SC_PII_XTI", _SC_PII_XTI},
6443#endif
6444#ifdef _SC_POLL
6445 {"SC_POLL", _SC_POLL},
6446#endif
6447#ifdef _SC_PRIORITIZED_IO
6448 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6449#endif
6450#ifdef _SC_PRIORITY_SCHEDULING
6451 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6452#endif
6453#ifdef _SC_REALTIME_SIGNALS
6454 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6455#endif
6456#ifdef _SC_RE_DUP_MAX
6457 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6458#endif
6459#ifdef _SC_RTSIG_MAX
6460 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6461#endif
6462#ifdef _SC_SAVED_IDS
6463 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6464#endif
6465#ifdef _SC_SCHAR_MAX
6466 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6467#endif
6468#ifdef _SC_SCHAR_MIN
6469 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6470#endif
6471#ifdef _SC_SELECT
6472 {"SC_SELECT", _SC_SELECT},
6473#endif
6474#ifdef _SC_SEMAPHORES
6475 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6476#endif
6477#ifdef _SC_SEM_NSEMS_MAX
6478 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6479#endif
6480#ifdef _SC_SEM_VALUE_MAX
6481 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6482#endif
6483#ifdef _SC_SHARED_MEMORY_OBJECTS
6484 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6485#endif
6486#ifdef _SC_SHRT_MAX
6487 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6488#endif
6489#ifdef _SC_SHRT_MIN
6490 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6491#endif
6492#ifdef _SC_SIGQUEUE_MAX
6493 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6494#endif
6495#ifdef _SC_SIGRT_MAX
6496 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6497#endif
6498#ifdef _SC_SIGRT_MIN
6499 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6500#endif
Fred Draked86ed291999-12-15 15:34:33 +00006501#ifdef _SC_SOFTPOWER
6502 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6503#endif
Fred Drakec9680921999-12-13 16:37:25 +00006504#ifdef _SC_SPLIT_CACHE
6505 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6506#endif
6507#ifdef _SC_SSIZE_MAX
6508 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6509#endif
6510#ifdef _SC_STACK_PROT
6511 {"SC_STACK_PROT", _SC_STACK_PROT},
6512#endif
6513#ifdef _SC_STREAM_MAX
6514 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6515#endif
6516#ifdef _SC_SYNCHRONIZED_IO
6517 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6518#endif
6519#ifdef _SC_THREADS
6520 {"SC_THREADS", _SC_THREADS},
6521#endif
6522#ifdef _SC_THREAD_ATTR_STACKADDR
6523 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6524#endif
6525#ifdef _SC_THREAD_ATTR_STACKSIZE
6526 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6527#endif
6528#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6529 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6530#endif
6531#ifdef _SC_THREAD_KEYS_MAX
6532 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6533#endif
6534#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6535 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6536#endif
6537#ifdef _SC_THREAD_PRIO_INHERIT
6538 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6539#endif
6540#ifdef _SC_THREAD_PRIO_PROTECT
6541 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6542#endif
6543#ifdef _SC_THREAD_PROCESS_SHARED
6544 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6545#endif
6546#ifdef _SC_THREAD_SAFE_FUNCTIONS
6547 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6548#endif
6549#ifdef _SC_THREAD_STACK_MIN
6550 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6551#endif
6552#ifdef _SC_THREAD_THREADS_MAX
6553 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6554#endif
6555#ifdef _SC_TIMERS
6556 {"SC_TIMERS", _SC_TIMERS},
6557#endif
6558#ifdef _SC_TIMER_MAX
6559 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6560#endif
6561#ifdef _SC_TTY_NAME_MAX
6562 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6563#endif
6564#ifdef _SC_TZNAME_MAX
6565 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6566#endif
6567#ifdef _SC_T_IOV_MAX
6568 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6569#endif
6570#ifdef _SC_UCHAR_MAX
6571 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6572#endif
6573#ifdef _SC_UINT_MAX
6574 {"SC_UINT_MAX", _SC_UINT_MAX},
6575#endif
6576#ifdef _SC_UIO_MAXIOV
6577 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6578#endif
6579#ifdef _SC_ULONG_MAX
6580 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6581#endif
6582#ifdef _SC_USHRT_MAX
6583 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6584#endif
6585#ifdef _SC_VERSION
6586 {"SC_VERSION", _SC_VERSION},
6587#endif
6588#ifdef _SC_WORD_BIT
6589 {"SC_WORD_BIT", _SC_WORD_BIT},
6590#endif
6591#ifdef _SC_XBS5_ILP32_OFF32
6592 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6593#endif
6594#ifdef _SC_XBS5_ILP32_OFFBIG
6595 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6596#endif
6597#ifdef _SC_XBS5_LP64_OFF64
6598 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6599#endif
6600#ifdef _SC_XBS5_LPBIG_OFFBIG
6601 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6602#endif
6603#ifdef _SC_XOPEN_CRYPT
6604 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6605#endif
6606#ifdef _SC_XOPEN_ENH_I18N
6607 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6608#endif
6609#ifdef _SC_XOPEN_LEGACY
6610 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6611#endif
6612#ifdef _SC_XOPEN_REALTIME
6613 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6614#endif
6615#ifdef _SC_XOPEN_REALTIME_THREADS
6616 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6617#endif
6618#ifdef _SC_XOPEN_SHM
6619 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6620#endif
6621#ifdef _SC_XOPEN_UNIX
6622 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6623#endif
6624#ifdef _SC_XOPEN_VERSION
6625 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6626#endif
6627#ifdef _SC_XOPEN_XCU_VERSION
6628 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6629#endif
6630#ifdef _SC_XOPEN_XPG2
6631 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6632#endif
6633#ifdef _SC_XOPEN_XPG3
6634 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6635#endif
6636#ifdef _SC_XOPEN_XPG4
6637 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6638#endif
6639};
6640
6641static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006642conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006643{
6644 return conv_confname(arg, valuep, posix_constants_sysconf,
6645 sizeof(posix_constants_sysconf)
6646 / sizeof(struct constdef));
6647}
6648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006649PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006650"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006651Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006652
6653static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006654posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006655{
6656 PyObject *result = NULL;
6657 int name;
6658
6659 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6660 int value;
6661
6662 errno = 0;
6663 value = sysconf(name);
6664 if (value == -1 && errno != 0)
6665 posix_error();
6666 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006667 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006668 }
6669 return result;
6670}
6671#endif
6672
6673
Fred Drakebec628d1999-12-15 18:31:10 +00006674/* This code is used to ensure that the tables of configuration value names
6675 * are in sorted order as required by conv_confname(), and also to build the
6676 * the exported dictionaries that are used to publish information about the
6677 * names available on the host platform.
6678 *
6679 * Sorting the table at runtime ensures that the table is properly ordered
6680 * when used, even for platforms we're not able to test on. It also makes
6681 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006682 */
Fred Drakebec628d1999-12-15 18:31:10 +00006683
6684static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006685cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006686{
6687 const struct constdef *c1 =
6688 (const struct constdef *) v1;
6689 const struct constdef *c2 =
6690 (const struct constdef *) v2;
6691
6692 return strcmp(c1->name, c2->name);
6693}
6694
6695static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006696setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006697 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006698{
Fred Drakebec628d1999-12-15 18:31:10 +00006699 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006700 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006701
6702 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6703 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006704 if (d == NULL)
6705 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006706
Barry Warsaw3155db32000-04-13 15:20:40 +00006707 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006708 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006709 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6710 Py_XDECREF(o);
6711 Py_DECREF(d);
6712 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006713 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006714 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006715 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006716 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006717}
6718
Fred Drakebec628d1999-12-15 18:31:10 +00006719/* Return -1 on failure, 0 on success. */
6720static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006721setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006722{
6723#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006724 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006725 sizeof(posix_constants_pathconf)
6726 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006727 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006728 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006729#endif
6730#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006731 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006732 sizeof(posix_constants_confstr)
6733 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006734 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006735 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006736#endif
6737#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006738 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006739 sizeof(posix_constants_sysconf)
6740 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006741 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006742 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006743#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006744 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006745}
Fred Draked86ed291999-12-15 15:34:33 +00006746
6747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006748PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006749"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006750Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006751in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006752
6753static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006754posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006755{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006756 abort();
6757 /*NOTREACHED*/
6758 Py_FatalError("abort() called from Python code didn't abort!");
6759 return NULL;
6760}
Fred Drakebec628d1999-12-15 18:31:10 +00006761
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006762#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006763PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006764"startfile(filepath [, operation]) - Start a file with its associated\n\
6765application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006766\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006767When \"operation\" is not specified or \"open\", this acts like\n\
6768double-clicking the file in Explorer, or giving the file name as an\n\
6769argument to the DOS \"start\" command: the file is opened with whatever\n\
6770application (if any) its extension is associated.\n\
6771When another \"operation\" is given, it specifies what should be done with\n\
6772the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006773\n\
6774startfile returns as soon as the associated application is launched.\n\
6775There is no option to wait for the application to close, and no way\n\
6776to retrieve the application's exit status.\n\
6777\n\
6778The filepath is relative to the current directory. If you want to use\n\
6779an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006780the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006781
6782static PyObject *
6783win32_startfile(PyObject *self, PyObject *args)
6784{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006785 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006786 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006787 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006788 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006789
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006790 PyObject *unipath, *woperation = NULL;
6791 if (!PyArg_ParseTuple(args, "U|s:startfile",
6792 &unipath, &operation)) {
6793 PyErr_Clear();
6794 goto normal;
6795 }
6796
6797 if (operation) {
6798 woperation = PyUnicode_DecodeASCII(operation,
6799 strlen(operation), NULL);
6800 if (!woperation) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006801 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006802 operation = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006803 goto normal;
6804 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006805 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006806
6807 Py_BEGIN_ALLOW_THREADS
6808 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6809 PyUnicode_AS_UNICODE(unipath),
6810 NULL, NULL, SW_SHOWNORMAL);
6811 Py_END_ALLOW_THREADS
6812
6813 Py_XDECREF(woperation);
6814 if (rc <= (HINSTANCE)32) {
6815 PyObject *errval = win32_error_unicode("startfile",
6816 PyUnicode_AS_UNICODE(unipath));
6817 return errval;
6818 }
6819 Py_INCREF(Py_None);
6820 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006821
6822normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006823 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6824 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006825 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006826 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006827 filepath = bytes2str(ofilepath, 1);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006828 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006829 rc = ShellExecute((HWND)0, operation, filepath,
6830 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006831 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006832 if (rc <= (HINSTANCE)32) {
6833 PyObject *errval = win32_error("startfile", filepath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006834 release_bytes(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006835 return errval;
6836 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00006837 release_bytes(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006838 Py_INCREF(Py_None);
6839 return Py_None;
6840}
6841#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006842
Martin v. Löwis438b5342002-12-27 10:16:42 +00006843#ifdef HAVE_GETLOADAVG
6844PyDoc_STRVAR(posix_getloadavg__doc__,
6845"getloadavg() -> (float, float, float)\n\n\
6846Return the number of processes in the system run queue averaged over\n\
6847the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6848was unobtainable");
6849
6850static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006851posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006852{
6853 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006854 if (getloadavg(loadavg, 3)!=3) {
6855 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6856 return NULL;
6857 } else
6858 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6859}
6860#endif
6861
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006862#ifdef MS_WINDOWS
6863
6864PyDoc_STRVAR(win32_urandom__doc__,
6865"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006866Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006867
6868typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6869 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6870 DWORD dwFlags );
6871typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6872 BYTE *pbBuffer );
6873
6874static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006875/* This handle is never explicitly released. Instead, the operating
6876 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006877static HCRYPTPROV hCryptProv = 0;
6878
Tim Peters4ad82172004-08-30 17:02:04 +00006879static PyObject*
6880win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006881{
Tim Petersd3115382004-08-30 17:36:46 +00006882 int howMany;
6883 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006884
Tim Peters4ad82172004-08-30 17:02:04 +00006885 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006886 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006887 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006888 if (howMany < 0)
6889 return PyErr_Format(PyExc_ValueError,
6890 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006891
Tim Peters4ad82172004-08-30 17:02:04 +00006892 if (hCryptProv == 0) {
6893 HINSTANCE hAdvAPI32 = NULL;
6894 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006895
Tim Peters4ad82172004-08-30 17:02:04 +00006896 /* Obtain handle to the DLL containing CryptoAPI
6897 This should not fail */
6898 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6899 if(hAdvAPI32 == NULL)
6900 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006901
Tim Peters4ad82172004-08-30 17:02:04 +00006902 /* Obtain pointers to the CryptoAPI functions
6903 This will fail on some early versions of Win95 */
6904 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6905 hAdvAPI32,
6906 "CryptAcquireContextA");
6907 if (pCryptAcquireContext == NULL)
6908 return PyErr_Format(PyExc_NotImplementedError,
6909 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006910
Tim Peters4ad82172004-08-30 17:02:04 +00006911 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6912 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006913 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006914 return PyErr_Format(PyExc_NotImplementedError,
6915 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006916
Tim Peters4ad82172004-08-30 17:02:04 +00006917 /* Acquire context */
6918 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6919 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6920 return win32_error("CryptAcquireContext", NULL);
6921 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006922
Tim Peters4ad82172004-08-30 17:02:04 +00006923 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006924 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006925 if (result != NULL) {
6926 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006927 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006928 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006929 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006930 Py_DECREF(result);
6931 return win32_error("CryptGenRandom", NULL);
6932 }
Tim Peters4ad82172004-08-30 17:02:04 +00006933 }
Tim Petersd3115382004-08-30 17:36:46 +00006934 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006935}
6936#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006937
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006938PyDoc_STRVAR(device_encoding__doc__,
6939"device_encoding(fd) -> str\n\n\
6940Return a string describing the encoding of the device\n\
6941if the output is a terminal; else return None.");
6942
6943static PyObject *
6944device_encoding(PyObject *self, PyObject *args)
6945{
6946 int fd;
6947 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6948 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006949 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006950 Py_INCREF(Py_None);
6951 return Py_None;
6952 }
6953#if defined(MS_WINDOWS) || defined(MS_WIN64)
6954 if (fd == 0) {
6955 char buf[100];
6956 sprintf(buf, "cp%d", GetConsoleCP());
6957 return PyUnicode_FromString(buf);
6958 }
6959 if (fd == 1 || fd == 2) {
6960 char buf[100];
6961 sprintf(buf, "cp%d", GetConsoleOutputCP());
6962 return PyUnicode_FromString(buf);
6963 }
6964#elif defined(CODESET)
6965 {
6966 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006967 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006968 return PyUnicode_FromString(codeset);
6969 }
6970#endif
6971 Py_INCREF(Py_None);
6972 return Py_None;
6973}
6974
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006975#ifdef __VMS
6976/* Use openssl random routine */
6977#include <openssl/rand.h>
6978PyDoc_STRVAR(vms_urandom__doc__,
6979"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006980Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006981
6982static PyObject*
6983vms_urandom(PyObject *self, PyObject *args)
6984{
6985 int howMany;
6986 PyObject* result;
6987
6988 /* Read arguments */
6989 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6990 return NULL;
6991 if (howMany < 0)
6992 return PyErr_Format(PyExc_ValueError,
6993 "negative argument not allowed");
6994
6995 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006996 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006997 if (result != NULL) {
6998 /* Get random data */
6999 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00007000 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007001 howMany) < 0) {
7002 Py_DECREF(result);
7003 return PyErr_Format(PyExc_ValueError,
7004 "RAND_pseudo_bytes");
7005 }
7006 }
7007 return result;
7008}
7009#endif
7010
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007011#ifdef HAVE_SETRESUID
7012PyDoc_STRVAR(posix_setresuid__doc__,
7013"setresuid(ruid, euid, suid)\n\n\
7014Set the current process's real, effective, and saved user ids.");
7015
7016static PyObject*
7017posix_setresuid (PyObject *self, PyObject *args)
7018{
7019 /* We assume uid_t is no larger than a long. */
7020 long ruid, euid, suid;
7021 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7022 return NULL;
7023 if (setresuid(ruid, euid, suid) < 0)
7024 return posix_error();
7025 Py_RETURN_NONE;
7026}
7027#endif
7028
7029#ifdef HAVE_SETRESGID
7030PyDoc_STRVAR(posix_setresgid__doc__,
7031"setresgid(rgid, egid, sgid)\n\n\
7032Set the current process's real, effective, and saved group ids.");
7033
7034static PyObject*
7035posix_setresgid (PyObject *self, PyObject *args)
7036{
7037 /* We assume uid_t is no larger than a long. */
7038 long rgid, egid, sgid;
7039 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7040 return NULL;
7041 if (setresgid(rgid, egid, sgid) < 0)
7042 return posix_error();
7043 Py_RETURN_NONE;
7044}
7045#endif
7046
7047#ifdef HAVE_GETRESUID
7048PyDoc_STRVAR(posix_getresuid__doc__,
7049"getresuid() -> (ruid, euid, suid)\n\n\
7050Get tuple of the current process's real, effective, and saved user ids.");
7051
7052static PyObject*
7053posix_getresuid (PyObject *self, PyObject *noargs)
7054{
7055 uid_t ruid, euid, suid;
7056 long l_ruid, l_euid, l_suid;
7057 if (getresuid(&ruid, &euid, &suid) < 0)
7058 return posix_error();
7059 /* Force the values into long's as we don't know the size of uid_t. */
7060 l_ruid = ruid;
7061 l_euid = euid;
7062 l_suid = suid;
7063 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
7064}
7065#endif
7066
7067#ifdef HAVE_GETRESGID
7068PyDoc_STRVAR(posix_getresgid__doc__,
7069"getresgid() -> (rgid, egid, sgid)\n\n\
7070Get tuple of the current process's real, effective, and saved user ids.");
7071
7072static PyObject*
7073posix_getresgid (PyObject *self, PyObject *noargs)
7074{
7075 uid_t rgid, egid, sgid;
7076 long l_rgid, l_egid, l_sgid;
7077 if (getresgid(&rgid, &egid, &sgid) < 0)
7078 return posix_error();
7079 /* Force the values into long's as we don't know the size of uid_t. */
7080 l_rgid = rgid;
7081 l_egid = egid;
7082 l_sgid = sgid;
7083 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
7084}
7085#endif
7086
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007087static PyMethodDef posix_methods[] = {
7088 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7089#ifdef HAVE_TTYNAME
7090 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7091#endif
7092 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007093#ifdef HAVE_CHFLAGS
7094 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7095#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007096 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007097#ifdef HAVE_FCHMOD
7098 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
7099#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007100#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007101 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007102#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007103#ifdef HAVE_LCHMOD
7104 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
7105#endif /* HAVE_LCHMOD */
7106#ifdef HAVE_FCHOWN
7107 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
7108#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007109#ifdef HAVE_LCHFLAGS
7110 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
7111#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007112#ifdef HAVE_LCHOWN
7113 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7114#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007115#ifdef HAVE_CHROOT
7116 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7117#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007118#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007119 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007120#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007121#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00007122 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7123 METH_NOARGS, posix_getcwd__doc__},
7124 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7125 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007126#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007127#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007128 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007129#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007130 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7131 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7132 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007133#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007134 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007135#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007136#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007137 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007138#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007139 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7140 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7141 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007142 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007143#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007144 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007145#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007146#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007147 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007148#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007149 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007150#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007151 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007152#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007153 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7154 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7155 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007156#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007157 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007158#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007159 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007160#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007161 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7162 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007163#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007164#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007165 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7166 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007167#if defined(PYOS_OS2)
7168 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7169 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7170#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007171#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007172#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007173 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007174#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007175#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007176 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007177#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007178#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007179 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007180#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007181#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007182 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007183#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007184#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007185 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007186#endif /* HAVE_GETEGID */
7187#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007188 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007189#endif /* HAVE_GETEUID */
7190#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007191 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007192#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007193#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007194 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007195#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007196 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007197#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007198 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007199#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007200#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007201 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007202#endif /* HAVE_GETPPID */
7203#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007204 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007205#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007206#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007207 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007208#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007209#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007210 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007211#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007212#ifdef HAVE_KILLPG
7213 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7214#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007215#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007216 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007217#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007218#ifdef MS_WINDOWS
7219 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7220#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007221#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007222 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007223#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007224#ifdef HAVE_SETEUID
7225 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7226#endif /* HAVE_SETEUID */
7227#ifdef HAVE_SETEGID
7228 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7229#endif /* HAVE_SETEGID */
7230#ifdef HAVE_SETREUID
7231 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7232#endif /* HAVE_SETREUID */
7233#ifdef HAVE_SETREGID
7234 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7235#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007236#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007237 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007238#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007239#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007240 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007241#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007242#ifdef HAVE_INITGROUPS
7243 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
7244#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007245#ifdef HAVE_GETPGID
7246 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7247#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007248#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007249 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007250#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007251#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007252 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007253#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007254#ifdef HAVE_WAIT3
7255 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7256#endif /* HAVE_WAIT3 */
7257#ifdef HAVE_WAIT4
7258 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7259#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007260#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007261 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007262#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007263#ifdef HAVE_GETSID
7264 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7265#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007266#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007267 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007268#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007269#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007270 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007271#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007272#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007273 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007274#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007275#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007276 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007277#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007278 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7279 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007280 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007281 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007282 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7283 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7284 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7285 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7286 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7287 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007288 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007289#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007290 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007291#endif
7292#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007293 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007294#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007295#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007296 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7297#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007298#ifdef HAVE_DEVICE_MACROS
7299 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7300 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7301 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7302#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007303#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007304 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007305#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007306#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007307 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007308#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007309#ifdef HAVE_UNSETENV
7310 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7311#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007312 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007313#ifdef HAVE_FCHDIR
7314 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7315#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007316#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007317 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007318#endif
7319#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007320 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007321#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007322#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007323#ifdef WCOREDUMP
7324 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7325#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007326#ifdef WIFCONTINUED
7327 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7328#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007329#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007330 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007331#endif /* WIFSTOPPED */
7332#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007333 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007334#endif /* WIFSIGNALED */
7335#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007336 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007337#endif /* WIFEXITED */
7338#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007339 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007340#endif /* WEXITSTATUS */
7341#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007342 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007343#endif /* WTERMSIG */
7344#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007345 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007346#endif /* WSTOPSIG */
7347#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007348#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007349 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007350#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007351#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007352 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007353#endif
Fred Drakec9680921999-12-13 16:37:25 +00007354#ifdef HAVE_CONFSTR
7355 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7356#endif
7357#ifdef HAVE_SYSCONF
7358 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7359#endif
7360#ifdef HAVE_FPATHCONF
7361 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7362#endif
7363#ifdef HAVE_PATHCONF
7364 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7365#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007366 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007367#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007368 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7369#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007370#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007371 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007372#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007373 #ifdef MS_WINDOWS
7374 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7375 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007376 #ifdef __VMS
7377 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7378 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007379#ifdef HAVE_SETRESUID
7380 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
7381#endif
7382#ifdef HAVE_SETRESGID
7383 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
7384#endif
7385#ifdef HAVE_GETRESUID
7386 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
7387#endif
7388#ifdef HAVE_GETRESGID
7389 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
7390#endif
7391
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007392 {NULL, NULL} /* Sentinel */
7393};
7394
7395
Barry Warsaw4a342091996-12-19 23:50:02 +00007396static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007397ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007398{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007399 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007400}
7401
Guido van Rossumd48f2521997-12-05 22:19:34 +00007402#if defined(PYOS_OS2)
7403/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007404static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007405{
7406 APIRET rc;
7407 ULONG values[QSV_MAX+1];
7408 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007409 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007410
7411 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007412 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007413 Py_END_ALLOW_THREADS
7414
7415 if (rc != NO_ERROR) {
7416 os2_error(rc);
7417 return -1;
7418 }
7419
Fred Drake4d1e64b2002-04-15 19:40:07 +00007420 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7421 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7422 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7423 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7424 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7425 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7426 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007427
7428 switch (values[QSV_VERSION_MINOR]) {
7429 case 0: ver = "2.00"; break;
7430 case 10: ver = "2.10"; break;
7431 case 11: ver = "2.11"; break;
7432 case 30: ver = "3.00"; break;
7433 case 40: ver = "4.00"; break;
7434 case 50: ver = "5.00"; break;
7435 default:
Tim Peters885d4572001-11-28 20:27:42 +00007436 PyOS_snprintf(tmp, sizeof(tmp),
7437 "%d-%d", values[QSV_VERSION_MAJOR],
7438 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007439 ver = &tmp[0];
7440 }
7441
7442 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007443 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007444 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007445
7446 /* Add Indicator of Which Drive was Used to Boot the System */
7447 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7448 tmp[1] = ':';
7449 tmp[2] = '\0';
7450
Fred Drake4d1e64b2002-04-15 19:40:07 +00007451 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007452}
7453#endif
7454
Barry Warsaw4a342091996-12-19 23:50:02 +00007455static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007456all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007457{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007458#ifdef F_OK
7459 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007460#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007461#ifdef R_OK
7462 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007463#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007464#ifdef W_OK
7465 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007466#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007467#ifdef X_OK
7468 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007469#endif
Fred Drakec9680921999-12-13 16:37:25 +00007470#ifdef NGROUPS_MAX
7471 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7472#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007473#ifdef TMP_MAX
7474 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7475#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007476#ifdef WCONTINUED
7477 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7478#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007479#ifdef WNOHANG
7480 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007481#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007482#ifdef WUNTRACED
7483 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7484#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007485#ifdef O_RDONLY
7486 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7487#endif
7488#ifdef O_WRONLY
7489 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7490#endif
7491#ifdef O_RDWR
7492 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7493#endif
7494#ifdef O_NDELAY
7495 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7496#endif
7497#ifdef O_NONBLOCK
7498 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7499#endif
7500#ifdef O_APPEND
7501 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7502#endif
7503#ifdef O_DSYNC
7504 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7505#endif
7506#ifdef O_RSYNC
7507 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7508#endif
7509#ifdef O_SYNC
7510 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7511#endif
7512#ifdef O_NOCTTY
7513 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7514#endif
7515#ifdef O_CREAT
7516 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7517#endif
7518#ifdef O_EXCL
7519 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7520#endif
7521#ifdef O_TRUNC
7522 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7523#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007524#ifdef O_BINARY
7525 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7526#endif
7527#ifdef O_TEXT
7528 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7529#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007530#ifdef O_LARGEFILE
7531 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7532#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007533#ifdef O_SHLOCK
7534 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7535#endif
7536#ifdef O_EXLOCK
7537 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7538#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007539
Tim Peters5aa91602002-01-30 05:46:57 +00007540/* MS Windows */
7541#ifdef O_NOINHERIT
7542 /* Don't inherit in child processes. */
7543 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7544#endif
7545#ifdef _O_SHORT_LIVED
7546 /* Optimize for short life (keep in memory). */
7547 /* MS forgot to define this one with a non-underscore form too. */
7548 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7549#endif
7550#ifdef O_TEMPORARY
7551 /* Automatically delete when last handle is closed. */
7552 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7553#endif
7554#ifdef O_RANDOM
7555 /* Optimize for random access. */
7556 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7557#endif
7558#ifdef O_SEQUENTIAL
7559 /* Optimize for sequential access. */
7560 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7561#endif
7562
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007563/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007564#ifdef O_ASYNC
7565 /* Send a SIGIO signal whenever input or output
7566 becomes available on file descriptor */
7567 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7568#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007569#ifdef O_DIRECT
7570 /* Direct disk access. */
7571 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7572#endif
7573#ifdef O_DIRECTORY
7574 /* Must be a directory. */
7575 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7576#endif
7577#ifdef O_NOFOLLOW
7578 /* Do not follow links. */
7579 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7580#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007581#ifdef O_NOATIME
7582 /* Do not update the access time. */
7583 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7584#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007585
Barry Warsaw5676bd12003-01-07 20:57:09 +00007586 /* These come from sysexits.h */
7587#ifdef EX_OK
7588 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007589#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007590#ifdef EX_USAGE
7591 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007592#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007593#ifdef EX_DATAERR
7594 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007595#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007596#ifdef EX_NOINPUT
7597 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007598#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007599#ifdef EX_NOUSER
7600 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007601#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007602#ifdef EX_NOHOST
7603 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007604#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007605#ifdef EX_UNAVAILABLE
7606 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007607#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007608#ifdef EX_SOFTWARE
7609 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007610#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007611#ifdef EX_OSERR
7612 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007613#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007614#ifdef EX_OSFILE
7615 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007616#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007617#ifdef EX_CANTCREAT
7618 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007619#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007620#ifdef EX_IOERR
7621 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007622#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007623#ifdef EX_TEMPFAIL
7624 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007625#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007626#ifdef EX_PROTOCOL
7627 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007628#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007629#ifdef EX_NOPERM
7630 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007631#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007632#ifdef EX_CONFIG
7633 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007634#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007635#ifdef EX_NOTFOUND
7636 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007637#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007638
Guido van Rossum246bc171999-02-01 23:54:31 +00007639#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007640#if defined(PYOS_OS2) && defined(PYCC_GCC)
7641 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7642 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7643 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7644 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7645 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7646 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7647 if (ins(d, "P_PM", (long)P_PM)) return -1;
7648 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7649 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7650 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7651 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7652 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7653 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7654 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7655 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7656 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7657 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7658 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7659 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7660 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7661#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007662 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7663 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7664 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7665 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7666 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007667#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007668#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007669
Guido van Rossumd48f2521997-12-05 22:19:34 +00007670#if defined(PYOS_OS2)
7671 if (insertvalues(d)) return -1;
7672#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007673 return 0;
7674}
7675
7676
Tim Peters5aa91602002-01-30 05:46:57 +00007677#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007678#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007679#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007680
7681#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007682#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007683#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007684
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007685#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007686#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007687#define MODNAME "posix"
7688#endif
7689
Martin v. Löwis1a214512008-06-11 05:26:20 +00007690static struct PyModuleDef posixmodule = {
7691 PyModuleDef_HEAD_INIT,
7692 MODNAME,
7693 posix__doc__,
7694 -1,
7695 posix_methods,
7696 NULL,
7697 NULL,
7698 NULL,
7699 NULL
7700};
7701
7702
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007703PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007704INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007705{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007706 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007707
Martin v. Löwis1a214512008-06-11 05:26:20 +00007708 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007709 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007710 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007711
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007712 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007713 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007714 Py_XINCREF(v);
7715 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007716 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007717 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007718
Fred Drake4d1e64b2002-04-15 19:40:07 +00007719 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007720 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007721
Fred Drake4d1e64b2002-04-15 19:40:07 +00007722 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007723 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007724
Fred Drake4d1e64b2002-04-15 19:40:07 +00007725 Py_INCREF(PyExc_OSError);
7726 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007727
Guido van Rossumb3d39562000-01-31 18:41:26 +00007728#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007729 if (posix_putenv_garbage == NULL)
7730 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007731#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007732
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007733 if (!initialized) {
7734 stat_result_desc.name = MODNAME ".stat_result";
7735 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7736 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7737 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7738 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7739 structseq_new = StatResultType.tp_new;
7740 StatResultType.tp_new = statresult_new;
7741
7742 statvfs_result_desc.name = MODNAME ".statvfs_result";
7743 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007744#ifdef NEED_TICKS_PER_SECOND
7745# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7746 ticks_per_second = sysconf(_SC_CLK_TCK);
7747# elif defined(HZ)
7748 ticks_per_second = HZ;
7749# else
7750 ticks_per_second = 60; /* magic fallback value; may be bogus */
7751# endif
7752#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007753 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007754 Py_INCREF((PyObject*) &StatResultType);
7755 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007756 Py_INCREF((PyObject*) &StatVFSResultType);
7757 PyModule_AddObject(m, "statvfs_result",
7758 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007759 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007760
7761#ifdef __APPLE__
7762 /*
7763 * Step 2 of weak-linking support on Mac OS X.
7764 *
7765 * The code below removes functions that are not available on the
7766 * currently active platform.
7767 *
7768 * This block allow one to use a python binary that was build on
7769 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7770 * OSX 10.4.
7771 */
7772#ifdef HAVE_FSTATVFS
7773 if (fstatvfs == NULL) {
7774 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007775 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007776 }
7777 }
7778#endif /* HAVE_FSTATVFS */
7779
7780#ifdef HAVE_STATVFS
7781 if (statvfs == NULL) {
7782 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007783 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007784 }
7785 }
7786#endif /* HAVE_STATVFS */
7787
7788# ifdef HAVE_LCHOWN
7789 if (lchown == NULL) {
7790 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007791 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007792 }
7793 }
7794#endif /* HAVE_LCHOWN */
7795
7796
7797#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007798 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007799
Guido van Rossumb6775db1994-08-01 11:34:53 +00007800}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007801
7802#ifdef __cplusplus
7803}
7804#endif