blob: d1951f9dbc7fee1e630cd476b886076a6034a997 [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 */
307#ifdef SIZEOF_PID_T
308#if SIZEOF_PID_T == SIZEOF_INT
309#define PARSE_PID "i"
310#define PyLong_FromPid PyLong_FromLong
311#define PyLong_AsPid PyLong_AsLong
312#elif SIZEOF_PID_T == SIZEOF_LONG
313#define PARSE_PID "l"
314#define PyLong_FromPid PyLong_FromLong
315#define PyLong_AsPid PyLong_AsLong
316#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
317#define PARSE_PID "L"
318#define PyLong_FromPid PyLong_FromLongLong
319#define PyLong_AsPid PyLong_AsLongLong
320#else
321#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
322#endif
323#endif /* SIZEOF_PID_T */
324
Greg Wardb48bc172000-03-01 21:51:56 +0000325/* Don't use the "_r" form if we don't need it (also, won't have a
326 prototype for it, at least on Solaris -- maybe others as well?). */
327#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
328#define USE_CTERMID_R
329#endif
330
Fred Drake699f3522000-06-29 21:12:41 +0000331/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000332#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000333#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000334# define STAT win32_stat
335# define FSTAT win32_fstat
336# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000337#else
338# define STAT stat
339# define FSTAT fstat
340# define STRUCT_STAT struct stat
341#endif
342
Tim Peters11b23062003-04-23 02:39:17 +0000343#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000344#include <sys/mkdev.h>
345#else
346#if defined(MAJOR_IN_SYSMACROS)
347#include <sys/sysmacros.h>
348#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000349#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
350#include <sys/mkdev.h>
351#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000352#endif
Fred Drake699f3522000-06-29 21:12:41 +0000353
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000354#if defined _MSC_VER && _MSC_VER >= 1400
355/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
356 * valid and throw an assertion if it isn't.
357 * Normally, an invalid fd is likely to be a C program error and therefore
358 * an assertion can be useful, but it does contradict the POSIX standard
359 * which for write(2) states:
360 * "Otherwise, -1 shall be returned and errno set to indicate the error."
361 * "[EBADF] The fildes argument is not a valid file descriptor open for
362 * writing."
363 * Furthermore, python allows the user to enter any old integer
364 * as a fd and should merely raise a python exception on error.
365 * The Microsoft CRT doesn't provide an official way to check for the
366 * validity of a file descriptor, but we can emulate its internal behaviour
367 * by using the exported __pinfo data member and knowledge of the
368 * internal structures involved.
369 * The structures below must be updated for each version of visual studio
370 * according to the file internal.h in the CRT source, until MS comes
371 * up with a less hacky way to do this.
372 * (all of this is to avoid globally modifying the CRT behaviour using
373 * _set_invalid_parameter_handler() and _CrtSetReportMode())
374 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000375/* The actual size of the structure is determined at runtime.
376 * Only the first items must be present.
377 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000378typedef struct {
379 intptr_t osfhnd;
380 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000381} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000382
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000383extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000384#define IOINFO_L2E 5
385#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
386#define IOINFO_ARRAYS 64
387#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
388#define FOPEN 0x01
389#define _NO_CONSOLE_FILENO (intptr_t)-2
390
391/* This function emulates what the windows CRT does to validate file handles */
392int
393_PyVerify_fd(int fd)
394{
395 const int i1 = fd >> IOINFO_L2E;
396 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000397
398 static int sizeof_ioinfo = 0;
399
400 /* Determine the actual size of the ioinfo structure,
401 * as used by the CRT loaded in memory
402 */
403 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
404 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
405 }
406 if (sizeof_ioinfo == 0) {
407 /* This should not happen... */
408 goto fail;
409 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000410
411 /* See that it isn't a special CLEAR fileno */
412 if (fd != _NO_CONSOLE_FILENO) {
413 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
414 * we check pointer validity and other info
415 */
416 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
417 /* finally, check that the file is open */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000418 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
419 if (info->osfile & FOPEN) {
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000420 return 1;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000421 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422 }
423 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000424 fail:
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000425 errno = EBADF;
426 return 0;
427}
428
429/* the special case of checking dup2. The target fd must be in a sensible range */
430static int
431_PyVerify_fd_dup2(int fd1, int fd2)
432{
433 if (!_PyVerify_fd(fd1))
434 return 0;
435 if (fd2 == _NO_CONSOLE_FILENO)
436 return 0;
437 if ((unsigned)fd2 < _NHANDLE_)
438 return 1;
439 else
440 return 0;
441}
442#else
443/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
444#define _PyVerify_fd_dup2(A, B) (1)
445#endif
446
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000448#ifdef WITH_NEXT_FRAMEWORK
449/* On Darwin/MacOSX a shared library or framework has no access to
450** environ directly, we must obtain it with _NSGetEnviron().
451*/
452#include <crt_externs.h>
453static char **environ;
454#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000456#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457
Barry Warsaw53699e91996-12-10 23:23:01 +0000458static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000459convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460{
Barry Warsaw53699e91996-12-10 23:23:01 +0000461 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000462#ifdef MS_WINDOWS
463 wchar_t **e;
464#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000466#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 if (d == NULL)
469 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000470#ifdef WITH_NEXT_FRAMEWORK
471 if (environ == NULL)
472 environ = *_NSGetEnviron();
473#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000474#ifdef MS_WINDOWS
475 /* _wenviron must be initialized in this way if the program is started
476 through main() instead of wmain(). */
477 _wgetenv(L"");
478 if (_wenviron == NULL)
479 return d;
480 /* This part ignores errors */
481 for (e = _wenviron; *e != NULL; e++) {
482 PyObject *k;
483 PyObject *v;
484 wchar_t *p = wcschr(*e, L'=');
485 if (p == NULL)
486 continue;
487 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
488 if (k == NULL) {
489 PyErr_Clear();
490 continue;
491 }
492 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
493 if (v == NULL) {
494 PyErr_Clear();
495 Py_DECREF(k);
496 continue;
497 }
498 if (PyDict_GetItem(d, k) == NULL) {
499 if (PyDict_SetItem(d, k, v) != 0)
500 PyErr_Clear();
501 }
502 Py_DECREF(k);
503 Py_DECREF(v);
504 }
505#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506 if (environ == NULL)
507 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000508 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000510 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000511 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512 char *p = strchr(*e, '=');
513 if (p == NULL)
514 continue;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000515 k = PyUnicode_Decode(*e, (int)(p-*e),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000516 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000517 if (k == NULL) {
518 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000520 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000521 v = PyUnicode_Decode(p+1, strlen(p+1),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000522 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000523 if (v == NULL) {
524 PyErr_Clear();
525 Py_DECREF(k);
526 continue;
527 }
528 if (PyDict_GetItem(d, k) == NULL) {
529 if (PyDict_SetItem(d, k, v) != 0)
530 PyErr_Clear();
531 }
532 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000533 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000535#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000536#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000537 {
538 APIRET rc;
539 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
540
541 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000542 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000543 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000544 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000545 Py_DECREF(v);
546 }
547 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
548 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000549 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000550 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000551 Py_DECREF(v);
552 }
553 }
554#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555 return d;
556}
557
Martin v. Löwis011e8422009-05-05 04:43:17 +0000558/* Convert a bytes object to a char*. Optionally lock the buffer if it is a
559 bytes array. */
560
561static char*
562bytes2str(PyObject* o, int lock)
563{
564 if(PyBytes_Check(o))
565 return PyBytes_AsString(o);
566 else if(PyByteArray_Check(o)) {
567 if (lock && PyObject_GetBuffer(o, NULL, 0) < 0)
568 /* On a bytearray, this should not fail. */
569 PyErr_BadInternalCall();
570 return PyByteArray_AsString(o);
571 } else {
572 /* The FS converter should have verified that this
573 is either bytes or bytearray. */
574 Py_FatalError("bad object passed to bytes2str");
575 /* not reached. */
576 return "";
577 }
578}
579
580/* Release the lock, decref the object. */
581static void
582release_bytes(PyObject* o)
583{
584 if (PyByteArray_Check(o))
585 o->ob_type->tp_as_buffer->bf_releasebuffer(NULL, 0);
586 Py_DECREF(o);
587}
588
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590/* Set a POSIX-specific error from errno, and return NULL */
591
Barry Warsawd58d7641998-07-23 16:14:40 +0000592static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000593posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000594{
Barry Warsawca74da41999-02-09 19:31:45 +0000595 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596}
Barry Warsawd58d7641998-07-23 16:14:40 +0000597static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000598posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000599{
Barry Warsawca74da41999-02-09 19:31:45 +0000600 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000601}
602
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000603#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000604static PyObject *
605posix_error_with_unicode_filename(Py_UNICODE* name)
606{
607 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
608}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000609#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000610
611
Mark Hammondef8b6542001-05-13 08:04:26 +0000612static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000613posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000614{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000615 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
616 bytes2str(name, 0));
617 release_bytes(name);
Mark Hammondef8b6542001-05-13 08:04:26 +0000618 return rc;
619}
620
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000621#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000622static PyObject *
623win32_error(char* function, char* filename)
624{
Mark Hammond33a6da92000-08-15 00:46:38 +0000625 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000626 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000627 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000628 Windows error object, which is non-trivial.
629 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000630 errno = GetLastError();
631 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000632 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000633 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000634 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000635}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000636
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000637static PyObject *
638win32_error_unicode(char* function, Py_UNICODE* filename)
639{
640 /* XXX - see win32_error for comments on 'function' */
641 errno = GetLastError();
642 if (filename)
643 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
644 else
645 return PyErr_SetFromWindowsErr(errno);
646}
647
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000649convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000651 if (PyUnicode_CheckExact(*param))
652 Py_INCREF(*param);
653 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000654 /* For a Unicode subtype that's not a Unicode object,
655 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000656 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
657 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000659 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 Py_FileSystemDefaultEncoding,
661 "strict");
662 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000663}
664
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000665#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666
Guido van Rossumd48f2521997-12-05 22:19:34 +0000667#if defined(PYOS_OS2)
668/**********************************************************************
669 * Helper Function to Trim and Format OS/2 Messages
670 **********************************************************************/
671 static void
672os2_formatmsg(char *msgbuf, int msglen, char *reason)
673{
674 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
675
676 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
677 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
678
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000679 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000680 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
681 }
682
683 /* Add Optional Reason Text */
684 if (reason) {
685 strcat(msgbuf, " : ");
686 strcat(msgbuf, reason);
687 }
688}
689
690/**********************************************************************
691 * Decode an OS/2 Operating System Error Code
692 *
693 * A convenience function to lookup an OS/2 error code and return a
694 * text message we can use to raise a Python exception.
695 *
696 * Notes:
697 * The messages for errors returned from the OS/2 kernel reside in
698 * the file OSO001.MSG in the \OS2 directory hierarchy.
699 *
700 **********************************************************************/
701 static char *
702os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
703{
704 APIRET rc;
705 ULONG msglen;
706
707 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
708 Py_BEGIN_ALLOW_THREADS
709 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
710 errorcode, "oso001.msg", &msglen);
711 Py_END_ALLOW_THREADS
712
713 if (rc == NO_ERROR)
714 os2_formatmsg(msgbuf, msglen, reason);
715 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000716 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000717 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000718
719 return msgbuf;
720}
721
722/* Set an OS/2-specific error and return NULL. OS/2 kernel
723 errors are not in a global variable e.g. 'errno' nor are
724 they congruent with posix error numbers. */
725
726static PyObject * os2_error(int code)
727{
728 char text[1024];
729 PyObject *v;
730
731 os2_strerror(text, sizeof(text), code, "");
732
733 v = Py_BuildValue("(is)", code, text);
734 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000735 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000736 Py_DECREF(v);
737 }
738 return NULL; /* Signal to Python that an Exception is Pending */
739}
740
741#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742
743/* POSIX generic methods */
744
Barry Warsaw53699e91996-12-10 23:23:01 +0000745static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000746posix_fildes(PyObject *fdobj, int (*func)(int))
747{
748 int fd;
749 int res;
750 fd = PyObject_AsFileDescriptor(fdobj);
751 if (fd < 0)
752 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000753 if (!_PyVerify_fd(fd))
754 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000755 Py_BEGIN_ALLOW_THREADS
756 res = (*func)(fd);
757 Py_END_ALLOW_THREADS
758 if (res < 0)
759 return posix_error();
760 Py_INCREF(Py_None);
761 return Py_None;
762}
Guido van Rossum21142a01999-01-08 21:05:37 +0000763
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000764#ifdef MS_WINDOWS
Tim Peters11b23062003-04-23 02:39:17 +0000765static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000766unicode_file_names(void)
767{
768 static int canusewide = -1;
769 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000770 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000771 the Windows NT family. */
772 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
773 }
774 return canusewide;
775}
776#endif
Tim Peters11b23062003-04-23 02:39:17 +0000777
Guido van Rossum21142a01999-01-08 21:05:37 +0000778static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000781 PyObject *opath1 = NULL;
782 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000783 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000784 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000785 PyUnicode_FSConverter, &opath1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000786 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000787 path1 = bytes2str(opath1, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000788 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000789 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000790 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000791 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000792 return posix_error_with_allocated_filename(opath1);
793 release_bytes(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000794 Py_INCREF(Py_None);
795 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000796}
797
Barry Warsaw53699e91996-12-10 23:23:01 +0000798static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000799posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000800 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000801 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802{
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000803 PyObject *opath1 = NULL, *opath2 = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000804 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000805 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000806 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000807 PyUnicode_FSConverter, &opath1,
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000808 PyUnicode_FSConverter, &opath2)) {
809 Py_XDECREF(opath1);
810 Py_XDECREF(opath2);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811 return NULL;
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000812 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000813 path1 = bytes2str(opath1, 1);
814 path2 = bytes2str(opath2, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000815 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000816 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000817 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +0000818 release_bytes(opath1);
819 release_bytes(opath2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000820 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000821 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000823 Py_INCREF(Py_None);
824 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000825}
826
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000827#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828static PyObject*
829win32_1str(PyObject* args, char* func,
830 char* format, BOOL (__stdcall *funcA)(LPCSTR),
831 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
832{
833 PyObject *uni;
834 char *ansi;
835 BOOL result;
836 if (unicode_file_names()) {
837 if (!PyArg_ParseTuple(args, wformat, &uni))
838 PyErr_Clear();
839 else {
840 Py_BEGIN_ALLOW_THREADS
841 result = funcW(PyUnicode_AsUnicode(uni));
842 Py_END_ALLOW_THREADS
843 if (!result)
844 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
845 Py_INCREF(Py_None);
846 return Py_None;
847 }
848 }
849 if (!PyArg_ParseTuple(args, format, &ansi))
850 return NULL;
851 Py_BEGIN_ALLOW_THREADS
852 result = funcA(ansi);
853 Py_END_ALLOW_THREADS
854 if (!result)
855 return win32_error(func, ansi);
856 Py_INCREF(Py_None);
857 return Py_None;
858
859}
860
861/* This is a reimplementation of the C library's chdir function,
862 but one that produces Win32 errors instead of DOS error codes.
863 chdir is essentially a wrapper around SetCurrentDirectory; however,
864 it also needs to set "magic" environment variables indicating
865 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000866static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867win32_chdir(LPCSTR path)
868{
869 char new_path[MAX_PATH+1];
870 int result;
871 char env[4] = "=x:";
872
873 if(!SetCurrentDirectoryA(path))
874 return FALSE;
875 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
876 if (!result)
877 return FALSE;
878 /* In the ANSI API, there should not be any paths longer
879 than MAX_PATH. */
880 assert(result <= MAX_PATH+1);
881 if (strncmp(new_path, "\\\\", 2) == 0 ||
882 strncmp(new_path, "//", 2) == 0)
883 /* UNC path, nothing to do. */
884 return TRUE;
885 env[1] = new_path[0];
886 return SetEnvironmentVariableA(env, new_path);
887}
888
889/* The Unicode version differs from the ANSI version
890 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000891static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892win32_wchdir(LPCWSTR path)
893{
894 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
895 int result;
896 wchar_t env[4] = L"=x:";
897
898 if(!SetCurrentDirectoryW(path))
899 return FALSE;
900 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
901 if (!result)
902 return FALSE;
903 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000904 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905 if (!new_path) {
906 SetLastError(ERROR_OUTOFMEMORY);
907 return FALSE;
908 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000909 result = GetCurrentDirectoryW(result, new_path);
910 if (!result) {
911 free(new_path);
912 return FALSE;
913 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000914 }
915 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
916 wcsncmp(new_path, L"//", 2) == 0)
917 /* UNC path, nothing to do. */
918 return TRUE;
919 env[1] = new_path[0];
920 result = SetEnvironmentVariableW(env, new_path);
921 if (new_path != _new_path)
922 free(new_path);
923 return result;
924}
925#endif
926
Martin v. Löwis14694662006-02-03 12:54:16 +0000927#ifdef MS_WINDOWS
928/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
929 - time stamps are restricted to second resolution
930 - file modification times suffer from forth-and-back conversions between
931 UTC and local time
932 Therefore, we implement our own stat, based on the Win32 API directly.
933*/
934#define HAVE_STAT_NSEC 1
935
936struct win32_stat{
937 int st_dev;
938 __int64 st_ino;
939 unsigned short st_mode;
940 int st_nlink;
941 int st_uid;
942 int st_gid;
943 int st_rdev;
944 __int64 st_size;
945 int st_atime;
946 int st_atime_nsec;
947 int st_mtime;
948 int st_mtime_nsec;
949 int st_ctime;
950 int st_ctime_nsec;
951};
952
953static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
954
955static void
956FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
957{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
959 /* Cannot simply cast and dereference in_ptr,
960 since it might not be aligned properly */
961 __int64 in;
962 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000963 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
964 /* XXX Win32 supports time stamps past 2038; we currently don't */
965 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
966}
967
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968static void
969time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
970{
971 /* XXX endianness */
972 __int64 out;
973 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000974 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000975 memcpy(out_ptr, &out, sizeof(out));
976}
977
Martin v. Löwis14694662006-02-03 12:54:16 +0000978/* Below, we *know* that ugo+r is 0444 */
979#if _S_IREAD != 0400
980#error Unsupported C library
981#endif
982static int
983attributes_to_mode(DWORD attr)
984{
985 int m = 0;
986 if (attr & FILE_ATTRIBUTE_DIRECTORY)
987 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
988 else
989 m |= _S_IFREG;
990 if (attr & FILE_ATTRIBUTE_READONLY)
991 m |= 0444;
992 else
993 m |= 0666;
994 return m;
995}
996
997static int
998attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
999{
1000 memset(result, 0, sizeof(*result));
1001 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1002 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1003 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1004 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1005 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1006
1007 return 0;
1008}
1009
Thomas Wouters89f507f2006-12-13 04:49:30 +00001010/* Emulate GetFileAttributesEx[AW] on Windows 95 */
1011static int checked = 0;
1012static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1013static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1014static void
1015check_gfax()
1016{
1017 HINSTANCE hKernel32;
1018 if (checked)
1019 return;
1020 checked = 1;
1021 hKernel32 = GetModuleHandle("KERNEL32");
1022 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
1023 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
1024}
1025
Guido van Rossumd8faa362007-04-27 19:54:29 +00001026static BOOL
1027attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1028{
1029 HANDLE hFindFile;
1030 WIN32_FIND_DATAA FileData;
1031 hFindFile = FindFirstFileA(pszFile, &FileData);
1032 if (hFindFile == INVALID_HANDLE_VALUE)
1033 return FALSE;
1034 FindClose(hFindFile);
1035 pfad->dwFileAttributes = FileData.dwFileAttributes;
1036 pfad->ftCreationTime = FileData.ftCreationTime;
1037 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1038 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1039 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1040 pfad->nFileSizeLow = FileData.nFileSizeLow;
1041 return TRUE;
1042}
1043
1044static BOOL
1045attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1046{
1047 HANDLE hFindFile;
1048 WIN32_FIND_DATAW FileData;
1049 hFindFile = FindFirstFileW(pszFile, &FileData);
1050 if (hFindFile == INVALID_HANDLE_VALUE)
1051 return FALSE;
1052 FindClose(hFindFile);
1053 pfad->dwFileAttributes = FileData.dwFileAttributes;
1054 pfad->ftCreationTime = FileData.ftCreationTime;
1055 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1056 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1057 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1058 pfad->nFileSizeLow = FileData.nFileSizeLow;
1059 return TRUE;
1060}
1061
Thomas Wouters89f507f2006-12-13 04:49:30 +00001062static BOOL WINAPI
1063Py_GetFileAttributesExA(LPCSTR pszFile,
1064 GET_FILEEX_INFO_LEVELS level,
1065 LPVOID pv)
1066{
1067 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001068 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1069 /* First try to use the system's implementation, if that is
1070 available and either succeeds to gives an error other than
1071 that it isn't implemented. */
1072 check_gfax();
1073 if (gfaxa) {
1074 result = gfaxa(pszFile, level, pv);
1075 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1076 return result;
1077 }
1078 /* It's either not present, or not implemented.
1079 Emulate using FindFirstFile. */
1080 if (level != GetFileExInfoStandard) {
1081 SetLastError(ERROR_INVALID_PARAMETER);
1082 return FALSE;
1083 }
1084 /* Use GetFileAttributes to validate that the file name
1085 does not contain wildcards (which FindFirstFile would
1086 accept). */
1087 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
1088 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001089 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001090}
1091
1092static BOOL WINAPI
1093Py_GetFileAttributesExW(LPCWSTR pszFile,
1094 GET_FILEEX_INFO_LEVELS level,
1095 LPVOID pv)
1096{
1097 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001098 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1099 /* First try to use the system's implementation, if that is
1100 available and either succeeds to gives an error other than
1101 that it isn't implemented. */
1102 check_gfax();
1103 if (gfaxa) {
1104 result = gfaxw(pszFile, level, pv);
1105 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1106 return result;
1107 }
1108 /* It's either not present, or not implemented.
1109 Emulate using FindFirstFile. */
1110 if (level != GetFileExInfoStandard) {
1111 SetLastError(ERROR_INVALID_PARAMETER);
1112 return FALSE;
1113 }
1114 /* Use GetFileAttributes to validate that the file name
1115 does not contain wildcards (which FindFirstFile would
1116 accept). */
1117 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
1118 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001120}
1121
Martin v. Löwis14694662006-02-03 12:54:16 +00001122static int
1123win32_stat(const char* path, struct win32_stat *result)
1124{
1125 WIN32_FILE_ATTRIBUTE_DATA info;
1126 int code;
1127 char *dot;
1128 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001129 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001130 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1131 /* Protocol violation: we explicitly clear errno, instead of
1132 setting it to a POSIX error. Callers should use GetLastError. */
1133 errno = 0;
1134 return -1;
1135 } else {
1136 /* Could not get attributes on open file. Fall back to
1137 reading the directory. */
1138 if (!attributes_from_dir(path, &info)) {
1139 /* Very strange. This should not fail now */
1140 errno = 0;
1141 return -1;
1142 }
1143 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001144 }
1145 code = attribute_data_to_stat(&info, result);
1146 if (code != 0)
1147 return code;
1148 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1149 dot = strrchr(path, '.');
1150 if (dot) {
1151 if (stricmp(dot, ".bat") == 0 ||
1152 stricmp(dot, ".cmd") == 0 ||
1153 stricmp(dot, ".exe") == 0 ||
1154 stricmp(dot, ".com") == 0)
1155 result->st_mode |= 0111;
1156 }
1157 return code;
1158}
1159
1160static int
1161win32_wstat(const wchar_t* path, struct win32_stat *result)
1162{
1163 int code;
1164 const wchar_t *dot;
1165 WIN32_FILE_ATTRIBUTE_DATA info;
1166 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001167 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001168 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1169 /* Protocol violation: we explicitly clear errno, instead of
1170 setting it to a POSIX error. Callers should use GetLastError. */
1171 errno = 0;
1172 return -1;
1173 } else {
1174 /* Could not get attributes on open file. Fall back to
1175 reading the directory. */
1176 if (!attributes_from_dir_w(path, &info)) {
1177 /* Very strange. This should not fail now */
1178 errno = 0;
1179 return -1;
1180 }
1181 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001182 }
1183 code = attribute_data_to_stat(&info, result);
1184 if (code < 0)
1185 return code;
1186 /* Set IFEXEC if it is an .exe, .bat, ... */
1187 dot = wcsrchr(path, '.');
1188 if (dot) {
1189 if (_wcsicmp(dot, L".bat") == 0 ||
1190 _wcsicmp(dot, L".cmd") == 0 ||
1191 _wcsicmp(dot, L".exe") == 0 ||
1192 _wcsicmp(dot, L".com") == 0)
1193 result->st_mode |= 0111;
1194 }
1195 return code;
1196}
1197
1198static int
1199win32_fstat(int file_number, struct win32_stat *result)
1200{
1201 BY_HANDLE_FILE_INFORMATION info;
1202 HANDLE h;
1203 int type;
1204
1205 h = (HANDLE)_get_osfhandle(file_number);
1206
1207 /* Protocol violation: we explicitly clear errno, instead of
1208 setting it to a POSIX error. Callers should use GetLastError. */
1209 errno = 0;
1210
1211 if (h == INVALID_HANDLE_VALUE) {
1212 /* This is really a C library error (invalid file handle).
1213 We set the Win32 error to the closes one matching. */
1214 SetLastError(ERROR_INVALID_HANDLE);
1215 return -1;
1216 }
1217 memset(result, 0, sizeof(*result));
1218
1219 type = GetFileType(h);
1220 if (type == FILE_TYPE_UNKNOWN) {
1221 DWORD error = GetLastError();
1222 if (error != 0) {
1223 return -1;
1224 }
1225 /* else: valid but unknown file */
1226 }
1227
1228 if (type != FILE_TYPE_DISK) {
1229 if (type == FILE_TYPE_CHAR)
1230 result->st_mode = _S_IFCHR;
1231 else if (type == FILE_TYPE_PIPE)
1232 result->st_mode = _S_IFIFO;
1233 return 0;
1234 }
1235
1236 if (!GetFileInformationByHandle(h, &info)) {
1237 return -1;
1238 }
1239
1240 /* similar to stat() */
1241 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1242 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1243 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1244 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1245 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1246 /* specific to fstat() */
1247 result->st_nlink = info.nNumberOfLinks;
1248 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1249 return 0;
1250}
1251
1252#endif /* MS_WINDOWS */
1253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001254PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001255"stat_result: Result from stat or lstat.\n\n\
1256This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001257 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001258or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1259\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001260Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1261or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001262\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001263See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001264
1265static PyStructSequence_Field stat_result_fields[] = {
1266 {"st_mode", "protection bits"},
1267 {"st_ino", "inode"},
1268 {"st_dev", "device"},
1269 {"st_nlink", "number of hard links"},
1270 {"st_uid", "user ID of owner"},
1271 {"st_gid", "group ID of owner"},
1272 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001273 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1274 {NULL, "integer time of last access"},
1275 {NULL, "integer time of last modification"},
1276 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001277 {"st_atime", "time of last access"},
1278 {"st_mtime", "time of last modification"},
1279 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001280#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001281 {"st_blksize", "blocksize for filesystem I/O"},
1282#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001283#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001284 {"st_blocks", "number of blocks allocated"},
1285#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001286#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001287 {"st_rdev", "device type (if inode device)"},
1288#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001289#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1290 {"st_flags", "user defined flags for file"},
1291#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001292#ifdef HAVE_STRUCT_STAT_ST_GEN
1293 {"st_gen", "generation number"},
1294#endif
1295#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1296 {"st_birthtime", "time of creation"},
1297#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001298 {0}
1299};
1300
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001301#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001302#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001303#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001304#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001305#endif
1306
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001307#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001308#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1309#else
1310#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1311#endif
1312
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001313#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1315#else
1316#define ST_RDEV_IDX ST_BLOCKS_IDX
1317#endif
1318
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001319#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1320#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1321#else
1322#define ST_FLAGS_IDX ST_RDEV_IDX
1323#endif
1324
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001325#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001326#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001327#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001328#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001329#endif
1330
1331#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1332#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1333#else
1334#define ST_BIRTHTIME_IDX ST_GEN_IDX
1335#endif
1336
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001337static PyStructSequence_Desc stat_result_desc = {
1338 "stat_result", /* name */
1339 stat_result__doc__, /* doc */
1340 stat_result_fields,
1341 10
1342};
1343
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001344PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001345"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1346This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001347 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001348or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001349\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001350See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001351
1352static PyStructSequence_Field statvfs_result_fields[] = {
1353 {"f_bsize", },
1354 {"f_frsize", },
1355 {"f_blocks", },
1356 {"f_bfree", },
1357 {"f_bavail", },
1358 {"f_files", },
1359 {"f_ffree", },
1360 {"f_favail", },
1361 {"f_flag", },
1362 {"f_namemax",},
1363 {0}
1364};
1365
1366static PyStructSequence_Desc statvfs_result_desc = {
1367 "statvfs_result", /* name */
1368 statvfs_result__doc__, /* doc */
1369 statvfs_result_fields,
1370 10
1371};
1372
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001374static PyTypeObject StatResultType;
1375static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001376static newfunc structseq_new;
1377
1378static PyObject *
1379statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1380{
1381 PyStructSequence *result;
1382 int i;
1383
1384 result = (PyStructSequence*)structseq_new(type, args, kwds);
1385 if (!result)
1386 return NULL;
1387 /* If we have been initialized from a tuple,
1388 st_?time might be set to None. Initialize it
1389 from the int slots. */
1390 for (i = 7; i <= 9; i++) {
1391 if (result->ob_item[i+3] == Py_None) {
1392 Py_DECREF(Py_None);
1393 Py_INCREF(result->ob_item[i]);
1394 result->ob_item[i+3] = result->ob_item[i];
1395 }
1396 }
1397 return (PyObject*)result;
1398}
1399
1400
1401
1402/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001403static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001404
1405PyDoc_STRVAR(stat_float_times__doc__,
1406"stat_float_times([newval]) -> oldval\n\n\
1407Determine whether os.[lf]stat represents time stamps as float objects.\n\
1408If newval is True, future calls to stat() return floats, if it is False,\n\
1409future calls return ints. \n\
1410If newval is omitted, return the current setting.\n");
1411
1412static PyObject*
1413stat_float_times(PyObject* self, PyObject *args)
1414{
1415 int newval = -1;
1416 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1417 return NULL;
1418 if (newval == -1)
1419 /* Return old value */
1420 return PyBool_FromLong(_stat_float_times);
1421 _stat_float_times = newval;
1422 Py_INCREF(Py_None);
1423 return Py_None;
1424}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001425
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001426static void
1427fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1428{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001429 PyObject *fval,*ival;
1430#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001431 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001432#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001433 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001434#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001435 if (!ival)
1436 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001437 if (_stat_float_times) {
1438 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1439 } else {
1440 fval = ival;
1441 Py_INCREF(fval);
1442 }
1443 PyStructSequence_SET_ITEM(v, index, ival);
1444 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001445}
1446
Tim Peters5aa91602002-01-30 05:46:57 +00001447/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001448 (used by posix_stat() and posix_fstat()) */
1449static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001450_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001451{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001452 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001453 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001454 if (v == NULL)
1455 return NULL;
1456
Christian Heimes217cfd12007-12-02 14:31:20 +00001457 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001458#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001459 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001460 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001461#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001462 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001463#endif
1464#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001465 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001466 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001467#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001468 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001469#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001470 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1471 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1472 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001473#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001474 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001475 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001476#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001477 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001478#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001479
Martin v. Löwis14694662006-02-03 12:54:16 +00001480#if defined(HAVE_STAT_TV_NSEC)
1481 ansec = st->st_atim.tv_nsec;
1482 mnsec = st->st_mtim.tv_nsec;
1483 cnsec = st->st_ctim.tv_nsec;
1484#elif defined(HAVE_STAT_TV_NSEC2)
1485 ansec = st->st_atimespec.tv_nsec;
1486 mnsec = st->st_mtimespec.tv_nsec;
1487 cnsec = st->st_ctimespec.tv_nsec;
1488#elif defined(HAVE_STAT_NSEC)
1489 ansec = st->st_atime_nsec;
1490 mnsec = st->st_mtime_nsec;
1491 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001492#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001493 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001494#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001495 fill_time(v, 7, st->st_atime, ansec);
1496 fill_time(v, 8, st->st_mtime, mnsec);
1497 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001498
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001499#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001500 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001501 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001502#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001503#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001504 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001505 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001506#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001507#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001508 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001509 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001510#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001511#ifdef HAVE_STRUCT_STAT_ST_GEN
1512 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001513 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001514#endif
1515#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1516 {
1517 PyObject *val;
1518 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001519 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001520#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001521 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001522#else
1523 bnsec = 0;
1524#endif
1525 if (_stat_float_times) {
1526 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1527 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001528 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001529 }
1530 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1531 val);
1532 }
1533#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001534#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1535 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001536 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001537#endif
Fred Drake699f3522000-06-29 21:12:41 +00001538
1539 if (PyErr_Occurred()) {
1540 Py_DECREF(v);
1541 return NULL;
1542 }
1543
1544 return v;
1545}
1546
Martin v. Löwisd8948722004-06-02 09:57:56 +00001547#ifdef MS_WINDOWS
1548
1549/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1550 where / can be used in place of \ and the trailing slash is optional.
1551 Both SERVER and SHARE must have at least one character.
1552*/
1553
1554#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1555#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001556#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001557#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001558#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001559
Tim Peters4ad82172004-08-30 17:02:04 +00001560static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001561IsUNCRootA(char *path, int pathlen)
1562{
1563 #define ISSLASH ISSLASHA
1564
1565 int i, share;
1566
1567 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1568 /* minimum UNCRoot is \\x\y */
1569 return FALSE;
1570 for (i = 2; i < pathlen ; i++)
1571 if (ISSLASH(path[i])) break;
1572 if (i == 2 || i == pathlen)
1573 /* do not allow \\\SHARE or \\SERVER */
1574 return FALSE;
1575 share = i+1;
1576 for (i = share; i < pathlen; i++)
1577 if (ISSLASH(path[i])) break;
1578 return (i != share && (i == pathlen || i == pathlen-1));
1579
1580 #undef ISSLASH
1581}
1582
Tim Peters4ad82172004-08-30 17:02:04 +00001583static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001584IsUNCRootW(Py_UNICODE *path, int pathlen)
1585{
1586 #define ISSLASH ISSLASHW
1587
1588 int i, share;
1589
1590 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1591 /* minimum UNCRoot is \\x\y */
1592 return FALSE;
1593 for (i = 2; i < pathlen ; i++)
1594 if (ISSLASH(path[i])) break;
1595 if (i == 2 || i == pathlen)
1596 /* do not allow \\\SHARE or \\SERVER */
1597 return FALSE;
1598 share = i+1;
1599 for (i = share; i < pathlen; i++)
1600 if (ISSLASH(path[i])) break;
1601 return (i != share && (i == pathlen || i == pathlen-1));
1602
1603 #undef ISSLASH
1604}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001605#endif /* MS_WINDOWS */
1606
Barry Warsaw53699e91996-12-10 23:23:01 +00001607static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001608posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001609 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001610#ifdef __VMS
1611 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1612#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001613 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001614#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001615 char *wformat,
1616 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001617{
Fred Drake699f3522000-06-29 21:12:41 +00001618 STRUCT_STAT st;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001619 PyObject *opath;
1620 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001621 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001622 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001623
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001624#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001625 /* If on wide-character-capable OS see if argument
1626 is Unicode and if so use wide API. */
1627 if (unicode_file_names()) {
1628 PyUnicodeObject *po;
1629 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001630 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1631
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001632 Py_BEGIN_ALLOW_THREADS
1633 /* PyUnicode_AS_UNICODE result OK without
1634 thread lock as it is a simple dereference. */
1635 res = wstatfunc(wpath, &st);
1636 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001637
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001638 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001639 return win32_error_unicode("stat", wpath);
1640 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001641 }
1642 /* Drop the argument parsing error as narrow strings
1643 are also valid. */
1644 PyErr_Clear();
1645 }
1646#endif
1647
Tim Peters5aa91602002-01-30 05:46:57 +00001648 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +00001649 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001650 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001651 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00001652 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001653 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001654 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001655
1656 if (res != 0) {
1657#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001658 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001659#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001660 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001661#endif
1662 }
1663 else
1664 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001665
Martin v. Löwis011e8422009-05-05 04:43:17 +00001666 release_bytes(opath);
Martin v. Löwis14694662006-02-03 12:54:16 +00001667 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668}
1669
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001670/* POSIX methods */
1671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001672PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001673"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001674Use the real uid/gid to test for access to a path. Note that most\n\
1675operations will use the effective uid/gid, therefore this routine can\n\
1676be used in a suid/sgid environment to test if the invoking user has the\n\
1677specified access to the path. The mode argument can be F_OK to test\n\
1678existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001679
1680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001681posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001682{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001683 PyObject *opath;
Guido van Rossum015f22a1999-01-06 22:52:38 +00001684 char *path;
1685 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001686
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001687#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001688 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001689 if (unicode_file_names()) {
1690 PyUnicodeObject *po;
1691 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1692 Py_BEGIN_ALLOW_THREADS
1693 /* PyUnicode_AS_UNICODE OK without thread lock as
1694 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001696 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001697 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001698 }
1699 /* Drop the argument parsing error as narrow strings
1700 are also valid. */
1701 PyErr_Clear();
1702 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001703 if (!PyArg_ParseTuple(args, "O&i:access",
1704 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001705 return 0;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001706 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001707 Py_BEGIN_ALLOW_THREADS
1708 attr = GetFileAttributesA(path);
1709 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001710 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001711finish:
1712 if (attr == 0xFFFFFFFF)
1713 /* File does not exist, or cannot read attributes */
1714 return PyBool_FromLong(0);
1715 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001716 the file isn't read-only, or if it's a directory, as there are
1717 no read-only directories on Windows. */
1718 return PyBool_FromLong(!(mode & 2)
1719 || !(attr & FILE_ATTRIBUTE_READONLY)
1720 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001721#else
1722 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001723 if (!PyArg_ParseTuple(args, "O&i:access",
1724 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001725 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001726 path = bytes2str(opath, 1);
Guido van Rossum015f22a1999-01-06 22:52:38 +00001727 Py_BEGIN_ALLOW_THREADS
1728 res = access(path, mode);
1729 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001730 release_bytes(opath);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001731 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001732#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001733}
1734
Guido van Rossumd371ff11999-01-25 16:12:23 +00001735#ifndef F_OK
1736#define F_OK 0
1737#endif
1738#ifndef R_OK
1739#define R_OK 4
1740#endif
1741#ifndef W_OK
1742#define W_OK 2
1743#endif
1744#ifndef X_OK
1745#define X_OK 1
1746#endif
1747
1748#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001750"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001751Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001752
1753static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001754posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001755{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001756 int id;
1757 char *ret;
1758
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001759 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001760 return NULL;
1761
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001762#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001763 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001764 if (id == 0) {
1765 ret = ttyname();
1766 }
1767 else {
1768 ret = NULL;
1769 }
1770#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001771 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001772#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001773 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001774 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001775 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001776}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001777#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001778
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001779#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001780PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001781"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001783
1784static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001785posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001786{
1787 char *ret;
1788 char buffer[L_ctermid];
1789
Greg Wardb48bc172000-03-01 21:51:56 +00001790#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001791 ret = ctermid_r(buffer);
1792#else
1793 ret = ctermid(buffer);
1794#endif
1795 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001796 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001797 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001798}
1799#endif
1800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001802"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001804
Barry Warsaw53699e91996-12-10 23:23:01 +00001805static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001806posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001807{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001808#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001809 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001810#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001811 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001812#elif defined(__VMS)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001813 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001814#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001815 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001816#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001817}
1818
Fred Drake4d1e64b2002-04-15 19:40:07 +00001819#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001821"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001822Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001823opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001824
1825static PyObject *
1826posix_fchdir(PyObject *self, PyObject *fdobj)
1827{
1828 return posix_fildes(fdobj, fchdir);
1829}
1830#endif /* HAVE_FCHDIR */
1831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001833PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001834"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001836
Barry Warsaw53699e91996-12-10 23:23:01 +00001837static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001838posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001839{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001840 PyObject *opath = NULL;
Mark Hammondef8b6542001-05-13 08:04:26 +00001841 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001842 int i;
1843 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001844#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001845 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001846 if (unicode_file_names()) {
1847 PyUnicodeObject *po;
1848 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1849 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001850 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1851 if (attr != 0xFFFFFFFF) {
1852 if (i & _S_IWRITE)
1853 attr &= ~FILE_ATTRIBUTE_READONLY;
1854 else
1855 attr |= FILE_ATTRIBUTE_READONLY;
1856 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1857 }
1858 else
1859 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001860 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001861 if (!res)
1862 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001863 PyUnicode_AS_UNICODE(po));
1864 Py_INCREF(Py_None);
1865 return Py_None;
1866 }
1867 /* Drop the argument parsing error as narrow strings
1868 are also valid. */
1869 PyErr_Clear();
1870 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001871 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1872 &opath, &i))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001873 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001874 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001875 Py_BEGIN_ALLOW_THREADS
1876 attr = GetFileAttributesA(path);
1877 if (attr != 0xFFFFFFFF) {
1878 if (i & _S_IWRITE)
1879 attr &= ~FILE_ATTRIBUTE_READONLY;
1880 else
1881 attr |= FILE_ATTRIBUTE_READONLY;
1882 res = SetFileAttributesA(path, attr);
1883 }
1884 else
1885 res = 0;
1886 Py_END_ALLOW_THREADS
1887 if (!res) {
1888 win32_error("chmod", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00001889 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001890 return NULL;
1891 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001892 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001893 Py_INCREF(Py_None);
1894 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001895#else /* MS_WINDOWS */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001896 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1897 &opath, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001898 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001899 path = bytes2str(opath, 1);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001900 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001901 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001902 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);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001906 Py_INCREF(Py_None);
1907 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001908#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001909}
1910
Christian Heimes4e30a842007-11-30 22:12:06 +00001911#ifdef HAVE_FCHMOD
1912PyDoc_STRVAR(posix_fchmod__doc__,
1913"fchmod(fd, mode)\n\n\
1914Change the access permissions of the file given by file\n\
1915descriptor fd.");
1916
1917static PyObject *
1918posix_fchmod(PyObject *self, PyObject *args)
1919{
1920 int fd, mode, res;
1921 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1922 return NULL;
1923 Py_BEGIN_ALLOW_THREADS
1924 res = fchmod(fd, mode);
1925 Py_END_ALLOW_THREADS
1926 if (res < 0)
1927 return posix_error();
1928 Py_RETURN_NONE;
1929}
1930#endif /* HAVE_FCHMOD */
1931
1932#ifdef HAVE_LCHMOD
1933PyDoc_STRVAR(posix_lchmod__doc__,
1934"lchmod(path, mode)\n\n\
1935Change the access permissions of a file. If path is a symlink, this\n\
1936affects the link itself rather than the target.");
1937
1938static PyObject *
1939posix_lchmod(PyObject *self, PyObject *args)
1940{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001941 PyObject *opath;
1942 char *path;
Christian Heimes4e30a842007-11-30 22:12:06 +00001943 int i;
1944 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001945 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1946 &opath, &i))
Christian Heimes4e30a842007-11-30 22:12:06 +00001947 return NULL;
Eric Smith86a05ec2009-05-05 13:07:30 +00001948 path = bytes2str(opath, 1);
Christian Heimes4e30a842007-11-30 22:12:06 +00001949 Py_BEGIN_ALLOW_THREADS
1950 res = lchmod(path, i);
1951 Py_END_ALLOW_THREADS
1952 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001953 return posix_error_with_allocated_filename(opath);
1954 release_bytes(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001955 Py_RETURN_NONE;
1956}
1957#endif /* HAVE_LCHMOD */
1958
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001959
Thomas Wouterscf297e42007-02-23 15:07:44 +00001960#ifdef HAVE_CHFLAGS
1961PyDoc_STRVAR(posix_chflags__doc__,
1962"chflags(path, flags)\n\n\
1963Set file flags.");
1964
1965static PyObject *
1966posix_chflags(PyObject *self, PyObject *args)
1967{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001968 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001969 char *path;
1970 unsigned long flags;
1971 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001972 if (!PyArg_ParseTuple(args, "O&k:chflags",
1973 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001974 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001975 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001976 Py_BEGIN_ALLOW_THREADS
1977 res = chflags(path, flags);
1978 Py_END_ALLOW_THREADS
1979 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001980 return posix_error_with_allocated_filename(opath);
1981 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001982 Py_INCREF(Py_None);
1983 return Py_None;
1984}
1985#endif /* HAVE_CHFLAGS */
1986
1987#ifdef HAVE_LCHFLAGS
1988PyDoc_STRVAR(posix_lchflags__doc__,
1989"lchflags(path, flags)\n\n\
1990Set file flags.\n\
1991This function will not follow symbolic links.");
1992
1993static PyObject *
1994posix_lchflags(PyObject *self, PyObject *args)
1995{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001996 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001997 char *path;
1998 unsigned long flags;
1999 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002000 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Martin v. Löwis4adbc342009-05-05 17:17:55 +00002001 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00002002 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002003 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002004 Py_BEGIN_ALLOW_THREADS
2005 res = lchflags(path, flags);
2006 Py_END_ALLOW_THREADS
2007 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002008 return posix_error_with_allocated_filename(opath);
2009 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002010 Py_INCREF(Py_None);
2011 return Py_None;
2012}
2013#endif /* HAVE_LCHFLAGS */
2014
Martin v. Löwis244edc82001-10-04 22:44:26 +00002015#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002016PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002017"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002018Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002019
2020static PyObject *
2021posix_chroot(PyObject *self, PyObject *args)
2022{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002023 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002024}
2025#endif
2026
Guido van Rossum21142a01999-01-08 21:05:37 +00002027#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002028PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002029"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002030force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002031
2032static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002033posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002034{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002035 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002036}
2037#endif /* HAVE_FSYNC */
2038
2039#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002040
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002041#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002042extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2043#endif
2044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002045PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002046"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002047force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002049
2050static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002051posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002052{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002053 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002054}
2055#endif /* HAVE_FDATASYNC */
2056
2057
Fredrik Lundh10723342000-07-10 16:38:09 +00002058#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002060"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002061Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002062
Barry Warsaw53699e91996-12-10 23:23:01 +00002063static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002064posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002065{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002066 PyObject *opath;
2067 char *path;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002068 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00002069 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002070 if (!PyArg_ParseTuple(args, "O&ll:chown",
2071 PyUnicode_FSConverter, &opath,
Mark Hammondef8b6542001-05-13 08:04:26 +00002072 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00002073 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002074 path = bytes2str(opath, 1);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002075 Py_BEGIN_ALLOW_THREADS
2076 res = chown(path, (uid_t) uid, (gid_t) gid);
2077 Py_END_ALLOW_THREADS
2078 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002079 return posix_error_with_allocated_filename(opath);
2080 release_bytes(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002081 Py_INCREF(Py_None);
2082 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002083}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002084#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085
Christian Heimes4e30a842007-11-30 22:12:06 +00002086#ifdef HAVE_FCHOWN
2087PyDoc_STRVAR(posix_fchown__doc__,
2088"fchown(fd, uid, gid)\n\n\
2089Change the owner and group id of the file given by file descriptor\n\
2090fd to the numeric uid and gid.");
2091
2092static PyObject *
2093posix_fchown(PyObject *self, PyObject *args)
2094{
2095 int fd, uid, gid;
2096 int res;
2097 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
2098 return NULL;
2099 Py_BEGIN_ALLOW_THREADS
2100 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2101 Py_END_ALLOW_THREADS
2102 if (res < 0)
2103 return posix_error();
2104 Py_RETURN_NONE;
2105}
2106#endif /* HAVE_FCHOWN */
2107
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002108#ifdef HAVE_LCHOWN
2109PyDoc_STRVAR(posix_lchown__doc__,
2110"lchown(path, uid, gid)\n\n\
2111Change the owner and group id of path to the numeric uid and gid.\n\
2112This function will not follow symbolic links.");
2113
2114static PyObject *
2115posix_lchown(PyObject *self, PyObject *args)
2116{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002117 PyObject *opath;
2118 char *path;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002119 int uid, gid;
2120 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002121 if (!PyArg_ParseTuple(args, "O&ii:lchown",
2122 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002123 &uid, &gid))
2124 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002125 path = bytes2str(opath, 1);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002126 Py_BEGIN_ALLOW_THREADS
2127 res = lchown(path, (uid_t) uid, (gid_t) gid);
2128 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00002129 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002130 return posix_error_with_allocated_filename(opath);
2131 release_bytes(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002132 Py_INCREF(Py_None);
2133 return Py_None;
2134}
2135#endif /* HAVE_LCHOWN */
2136
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002137
Guido van Rossum36bc6801995-06-14 22:54:23 +00002138#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002139static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002140posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002141{
2142 char buf[1026];
2143 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002144
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002145#ifdef MS_WINDOWS
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002146 if (!use_bytes && unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002147 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002148 wchar_t *wbuf2 = wbuf;
2149 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002150 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002151 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002152 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2153 /* If the buffer is large enough, len does not include the
2154 terminating \0. If the buffer is too small, len includes
2155 the space needed for the terminator. */
2156 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2157 wbuf2 = malloc(len * sizeof(wchar_t));
2158 if (wbuf2)
2159 len = GetCurrentDirectoryW(len, wbuf2);
2160 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002161 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002162 if (!wbuf2) {
2163 PyErr_NoMemory();
2164 return NULL;
2165 }
2166 if (!len) {
2167 if (wbuf2 != wbuf) free(wbuf2);
2168 return win32_error("getcwdu", NULL);
2169 }
2170 resobj = PyUnicode_FromWideChar(wbuf2, len);
2171 if (wbuf2 != wbuf) free(wbuf2);
2172 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002173 }
2174#endif
2175
2176 Py_BEGIN_ALLOW_THREADS
2177#if defined(PYOS_OS2) && defined(PYCC_GCC)
2178 res = _getcwd2(buf, sizeof buf);
2179#else
2180 res = getcwd(buf, sizeof buf);
2181#endif
2182 Py_END_ALLOW_THREADS
2183 if (res == NULL)
2184 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002185 if (use_bytes)
2186 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002187 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002188}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002189
2190PyDoc_STRVAR(posix_getcwd__doc__,
2191"getcwd() -> path\n\n\
2192Return a unicode string representing the current working directory.");
2193
2194static PyObject *
2195posix_getcwd_unicode(PyObject *self)
2196{
2197 return posix_getcwd(0);
2198}
2199
2200PyDoc_STRVAR(posix_getcwdb__doc__,
2201"getcwdb() -> path\n\n\
2202Return a bytes string representing the current working directory.");
2203
2204static PyObject *
2205posix_getcwd_bytes(PyObject *self)
2206{
2207 return posix_getcwd(1);
2208}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002209#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002211
Guido van Rossumb6775db1994-08-01 11:34:53 +00002212#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002214"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002215Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002216
Barry Warsaw53699e91996-12-10 23:23:01 +00002217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002218posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002220 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002222#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002225PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002226"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002227Return a list containing the names of the entries in the directory.\n\
2228\n\
2229 path: path of directory to list\n\
2230\n\
2231The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002232entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002233
Barry Warsaw53699e91996-12-10 23:23:01 +00002234static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002235posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002236{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002237 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002238 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002239#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002240
Barry Warsaw53699e91996-12-10 23:23:01 +00002241 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002242 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002243 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002244 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002245 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002246 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002247 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002248 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002249
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002250 /* If on wide-character-capable OS see if argument
2251 is Unicode and if so use wide API. */
2252 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002253 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002254 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2255 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002256 Py_UNICODE *wnamebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002257 /* Overallocate for \\*.*\0 */
2258 len = PyUnicode_GET_SIZE(po);
2259 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2260 if (!wnamebuf) {
2261 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002262 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002263 }
2264 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002265 if (len > 0) {
2266 Py_UNICODE wch = wnamebuf[len-1];
2267 if (wch != L'/' && wch != L'\\' && wch != L':')
2268 wnamebuf[len++] = L'\\';
2269 wcscpy(wnamebuf + len, L"*.*");
2270 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002271 if ((d = PyList_New(0)) == NULL) {
2272 free(wnamebuf);
2273 return NULL;
2274 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002275 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2276 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002277 int error = GetLastError();
2278 if (error == ERROR_FILE_NOT_FOUND) {
2279 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002280 return d;
2281 }
2282 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002283 win32_error_unicode("FindFirstFileW", wnamebuf);
2284 free(wnamebuf);
2285 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002286 }
2287 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002288 /* Skip over . and .. */
2289 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2290 wcscmp(wFileData.cFileName, L"..") != 0) {
2291 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2292 if (v == NULL) {
2293 Py_DECREF(d);
2294 d = NULL;
2295 break;
2296 }
2297 if (PyList_Append(d, v) != 0) {
2298 Py_DECREF(v);
2299 Py_DECREF(d);
2300 d = NULL;
2301 break;
2302 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002303 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002304 }
Georg Brandl622927b2006-03-07 12:48:03 +00002305 Py_BEGIN_ALLOW_THREADS
2306 result = FindNextFileW(hFindFile, &wFileData);
2307 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002308 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2309 it got to the end of the directory. */
2310 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2311 Py_DECREF(d);
2312 win32_error_unicode("FindNextFileW", wnamebuf);
2313 FindClose(hFindFile);
2314 free(wnamebuf);
2315 return NULL;
2316 }
Georg Brandl622927b2006-03-07 12:48:03 +00002317 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002318
2319 if (FindClose(hFindFile) == FALSE) {
2320 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002321 win32_error_unicode("FindClose", wnamebuf);
2322 free(wnamebuf);
2323 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002324 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002325 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002326 return d;
2327 }
2328 /* Drop the argument parsing error as narrow strings
2329 are also valid. */
2330 PyErr_Clear();
2331 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002332
Martin v. Löwis011e8422009-05-05 04:43:17 +00002333 if (!PyArg_ParseTuple(args, "O&:listdir",
2334 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002335 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002336 if (PyObject_Size(opath)+1 > MAX_PATH) {
2337 PyErr_SetString(PyExc_ValueError, "path too long");
2338 Py_DECREF(opath);
2339 return NULL;
2340 }
2341 strcpy(namebuf, bytes2str(opath, 0));
2342 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002343 if (len > 0) {
2344 char ch = namebuf[len-1];
2345 if (ch != SEP && ch != ALTSEP && ch != ':')
2346 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002347 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002348 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002349
Barry Warsaw53699e91996-12-10 23:23:01 +00002350 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002351 return NULL;
2352
2353 hFindFile = FindFirstFile(namebuf, &FileData);
2354 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002355 int error = GetLastError();
2356 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002357 return d;
2358 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002359 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002360 }
2361 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002362 /* Skip over . and .. */
2363 if (strcmp(FileData.cFileName, ".") != 0 &&
2364 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002365 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002366 if (v == NULL) {
2367 Py_DECREF(d);
2368 d = NULL;
2369 break;
2370 }
2371 if (PyList_Append(d, v) != 0) {
2372 Py_DECREF(v);
2373 Py_DECREF(d);
2374 d = NULL;
2375 break;
2376 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002377 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002378 }
Georg Brandl622927b2006-03-07 12:48:03 +00002379 Py_BEGIN_ALLOW_THREADS
2380 result = FindNextFile(hFindFile, &FileData);
2381 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002382 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2383 it got to the end of the directory. */
2384 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2385 Py_DECREF(d);
2386 win32_error("FindNextFile", namebuf);
2387 FindClose(hFindFile);
2388 return NULL;
2389 }
Georg Brandl622927b2006-03-07 12:48:03 +00002390 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002391
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002392 if (FindClose(hFindFile) == FALSE) {
2393 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002394 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002395 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002396
2397 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002398
Tim Peters0bb44a42000-09-15 07:44:49 +00002399#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002400
2401#ifndef MAX_PATH
2402#define MAX_PATH CCHMAXPATH
2403#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002404 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002405 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002406 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002407 PyObject *d, *v;
2408 char namebuf[MAX_PATH+5];
2409 HDIR hdir = 1;
2410 ULONG srchcnt = 1;
2411 FILEFINDBUF3 ep;
2412 APIRET rc;
2413
Martin v. Löwis011e8422009-05-05 04:43:17 +00002414 if (!PyArg_ParseTuple(args, "O&:listdir",
2415 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002416 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002417 name = bytes2str(oname);
2418 len = PyObject_Size(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002419 if (len >= MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002420 release_bytes(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002421 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002422 return NULL;
2423 }
2424 strcpy(namebuf, name);
2425 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002426 if (*pt == ALTSEP)
2427 *pt = SEP;
2428 if (namebuf[len-1] != SEP)
2429 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002430 strcpy(namebuf + len, "*.*");
2431
Neal Norwitz6c913782007-10-14 03:23:09 +00002432 if ((d = PyList_New(0)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002433 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002434 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002435 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002436
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002437 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2438 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002439 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002440 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2441 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2442 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002443
2444 if (rc != NO_ERROR) {
2445 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002446 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002447 }
2448
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002449 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002450 do {
2451 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002452 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002453 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002454
2455 strcpy(namebuf, ep.achName);
2456
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002457 /* Leave Case of Name Alone -- In Native Form */
2458 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002459
Christian Heimes72b710a2008-05-26 13:28:38 +00002460 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002461 if (v == NULL) {
2462 Py_DECREF(d);
2463 d = NULL;
2464 break;
2465 }
2466 if (PyList_Append(d, v) != 0) {
2467 Py_DECREF(v);
2468 Py_DECREF(d);
2469 d = NULL;
2470 break;
2471 }
2472 Py_DECREF(v);
2473 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2474 }
2475
Martin v. Löwis011e8422009-05-05 04:43:17 +00002476 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002477 return d;
2478#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002479 PyObject *oname;
2480 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002481 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002482 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002483 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002484 int arg_is_unicode = 1;
2485
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002486 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002487 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2488 arg_is_unicode = 0;
2489 PyErr_Clear();
2490 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002491 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002492 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002493 name = bytes2str(oname, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002494 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002495 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002496 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002497 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002498 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002499 release_bytes(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002500 return NULL;
2501 }
Georg Brandl622927b2006-03-07 12:48:03 +00002502 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002503 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002504 Py_BEGIN_ALLOW_THREADS
2505 ep = readdir(dirp);
2506 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002507 if (ep == NULL) {
2508 if (errno == 0) {
2509 break;
2510 } else {
2511 closedir(dirp);
2512 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002513 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002514 }
2515 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002516 if (ep->d_name[0] == '.' &&
2517 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002518 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002519 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002520 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002521 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002522 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002523 d = NULL;
2524 break;
2525 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002526 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002527 PyObject *w;
2528
2529 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002530 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002531 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002532 Py_DECREF(v);
2533 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002534 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002535 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002536 /* Encoding failed to decode ASCII bytes.
2537 Raise exception. */
2538 Py_DECREF(d);
2539 d = NULL;
2540 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002541 }
Just van Rossum46c97842003-02-25 21:42:15 +00002542 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002543 if (PyList_Append(d, v) != 0) {
2544 Py_DECREF(v);
2545 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002546 d = NULL;
2547 break;
2548 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002549 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002550 }
2551 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002552 release_bytes(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002553
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002554 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002555
Tim Peters0bb44a42000-09-15 07:44:49 +00002556#endif /* which OS */
2557} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002558
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002559#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002560/* A helper function for abspath on win32 */
2561static PyObject *
2562posix__getfullpathname(PyObject *self, PyObject *args)
2563{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002564 PyObject *opath;
2565 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002566 char outbuf[MAX_PATH*2];
2567 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002568#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002569 if (unicode_file_names()) {
2570 PyUnicodeObject *po;
2571 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Benjamin Petersonf608c612008-11-16 18:33:53 +00002572 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2573 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002574 Py_UNICODE *wtemp;
Benjamin Petersonf608c612008-11-16 18:33:53 +00002575 DWORD result;
2576 PyObject *v;
2577 result = GetFullPathNameW(wpath,
2578 sizeof(woutbuf)/sizeof(woutbuf[0]),
2579 woutbuf, &wtemp);
2580 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2581 woutbufp = malloc(result * sizeof(Py_UNICODE));
2582 if (!woutbufp)
2583 return PyErr_NoMemory();
2584 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2585 }
2586 if (result)
2587 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2588 else
2589 v = win32_error_unicode("GetFullPathNameW", wpath);
2590 if (woutbufp != woutbuf)
2591 free(woutbufp);
2592 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002593 }
2594 /* Drop the argument parsing error as narrow strings
2595 are also valid. */
2596 PyErr_Clear();
2597 }
2598#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002599 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2600 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002601 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002602 path = bytes2str(opath, 1);
2603 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2604 outbuf, &temp)) {
2605 win32_error("GetFullPathName", path);
2606 release_bytes(opath);
2607 return NULL;
2608 }
2609 release_bytes(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002610 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2611 return PyUnicode_Decode(outbuf, strlen(outbuf),
2612 Py_FileSystemDefaultEncoding, NULL);
2613 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002614 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002615} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002616#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002618PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002619"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002620Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002621
Barry Warsaw53699e91996-12-10 23:23:01 +00002622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002623posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002624{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002625 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002626 PyObject *opath;
2627 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002628 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002629
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002630#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002631 if (unicode_file_names()) {
2632 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002633 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002634 Py_BEGIN_ALLOW_THREADS
2635 /* PyUnicode_AS_UNICODE OK without thread lock as
2636 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002637 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002638 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002639 if (!res)
2640 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002641 Py_INCREF(Py_None);
2642 return Py_None;
2643 }
2644 /* Drop the argument parsing error as narrow strings
2645 are also valid. */
2646 PyErr_Clear();
2647 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002648 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2649 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002650 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002651 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002652 Py_BEGIN_ALLOW_THREADS
2653 /* PyUnicode_AS_UNICODE OK without thread lock as
2654 it is a simple dereference. */
2655 res = CreateDirectoryA(path, NULL);
2656 Py_END_ALLOW_THREADS
2657 if (!res) {
2658 win32_error("mkdir", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002659 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002660 return NULL;
2661 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002662 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002663 Py_INCREF(Py_None);
2664 return Py_None;
2665#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002666
Martin v. Löwis011e8422009-05-05 04:43:17 +00002667 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2668 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002669 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002670 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00002671 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002672#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002673 res = mkdir(path);
2674#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002675 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002676#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002677 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002678 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002679 return posix_error_with_allocated_filename(opath);
2680 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002681 Py_INCREF(Py_None);
2682 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002683#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002684}
2685
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002686
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002687/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2688#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002689#include <sys/resource.h>
2690#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002691
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002692
2693#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002694PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002695"nice(inc) -> new_priority\n\n\
2696Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697
Barry Warsaw53699e91996-12-10 23:23:01 +00002698static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002699posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002700{
2701 int increment, value;
2702
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002703 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002704 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002705
2706 /* There are two flavours of 'nice': one that returns the new
2707 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002708 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2709 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002710
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002711 If we are of the nice family that returns the new priority, we
2712 need to clear errno before the call, and check if errno is filled
2713 before calling posix_error() on a returnvalue of -1, because the
2714 -1 may be the actual new priority! */
2715
2716 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002717 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002718#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002719 if (value == 0)
2720 value = getpriority(PRIO_PROCESS, 0);
2721#endif
2722 if (value == -1 && errno != 0)
2723 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002724 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002725 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002726}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002727#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002728
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002729PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002730"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002731Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002732
Barry Warsaw53699e91996-12-10 23:23:01 +00002733static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002734posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002735{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002736#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002737 PyObject *o1, *o2;
2738 char *p1, *p2;
2739 BOOL result;
2740 if (unicode_file_names()) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002741 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2742 goto error;
2743 if (!convert_to_unicode(&o1))
2744 goto error;
2745 if (!convert_to_unicode(&o2)) {
2746 Py_DECREF(o1);
2747 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002748 }
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002749 Py_BEGIN_ALLOW_THREADS
2750 result = MoveFileW(PyUnicode_AsUnicode(o1),
2751 PyUnicode_AsUnicode(o2));
2752 Py_END_ALLOW_THREADS
2753 Py_DECREF(o1);
2754 Py_DECREF(o2);
2755 if (!result)
2756 return win32_error("rename", NULL);
2757 Py_INCREF(Py_None);
2758 return Py_None;
2759error:
2760 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002761 }
2762 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2763 return NULL;
2764 Py_BEGIN_ALLOW_THREADS
2765 result = MoveFileA(p1, p2);
2766 Py_END_ALLOW_THREADS
2767 if (!result)
2768 return win32_error("rename", NULL);
2769 Py_INCREF(Py_None);
2770 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002771#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002772 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002773#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002774}
2775
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002777PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002778"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002779Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002780
Barry Warsaw53699e91996-12-10 23:23:01 +00002781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002782posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002783{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002784#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002785 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002786#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002787 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002788#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002789}
2790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002793"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002794Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002795
Barry Warsaw53699e91996-12-10 23:23:01 +00002796static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002797posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002798{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002799#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002800 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002801#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002802 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002803#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002804}
2805
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002806
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002807#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002808PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002809"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002810Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002811
Barry Warsaw53699e91996-12-10 23:23:01 +00002812static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002813posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002814{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002815 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002816#ifdef MS_WINDOWS
2817 wchar_t *command;
2818 if (!PyArg_ParseTuple(args, "u:system", &command))
2819 return NULL;
2820#else
2821 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002822 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002823 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002824#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002825 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002826#ifdef MS_WINDOWS
2827 sts = _wsystem(command);
2828#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002829 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002830#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002831 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002832 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002833}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002834#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002835
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002836
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002837PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002838"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002839Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002840
Barry Warsaw53699e91996-12-10 23:23:01 +00002841static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002842posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002843{
2844 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002845 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002846 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002847 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002848 if (i < 0)
2849 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002850 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002851}
2852
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002854PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002855"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002856Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002858PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002859"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002860Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002861
Barry Warsaw53699e91996-12-10 23:23:01 +00002862static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002863posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002864{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002865#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002866 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002867#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002868 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002869#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002870}
2871
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002872
Guido van Rossumb6775db1994-08-01 11:34:53 +00002873#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002874PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002875"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002876Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002877
Barry Warsaw53699e91996-12-10 23:23:01 +00002878static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002879posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002880{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002881 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002882 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002883
Barry Warsaw53699e91996-12-10 23:23:01 +00002884 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002885 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002886 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002887 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002888 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002889 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002890 u.sysname,
2891 u.nodename,
2892 u.release,
2893 u.version,
2894 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002895}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002896#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002897
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002898static int
2899extract_time(PyObject *t, long* sec, long* usec)
2900{
2901 long intval;
2902 if (PyFloat_Check(t)) {
2903 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002904 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002905 if (!intobj)
2906 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002907 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002908 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002909 if (intval == -1 && PyErr_Occurred())
2910 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002911 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002912 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002913 if (*usec < 0)
2914 /* If rounding gave us a negative number,
2915 truncate. */
2916 *usec = 0;
2917 return 0;
2918 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002919 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002920 if (intval == -1 && PyErr_Occurred())
2921 return -1;
2922 *sec = intval;
2923 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002924 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002925}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002927PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002928"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002929utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002930Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002931second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002932
Barry Warsaw53699e91996-12-10 23:23:01 +00002933static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002934posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002935{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002936#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002937 PyObject *arg;
2938 PyUnicodeObject *obwpath;
2939 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002940 PyObject *oapath;
2941 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002942 HANDLE hFile;
2943 long atimesec, mtimesec, ausec, musec;
2944 FILETIME atime, mtime;
2945 PyObject *result = NULL;
2946
2947 if (unicode_file_names()) {
2948 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2949 wpath = PyUnicode_AS_UNICODE(obwpath);
2950 Py_BEGIN_ALLOW_THREADS
2951 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002952 NULL, OPEN_EXISTING,
2953 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002954 Py_END_ALLOW_THREADS
2955 if (hFile == INVALID_HANDLE_VALUE)
2956 return win32_error_unicode("utime", wpath);
2957 } else
2958 /* Drop the argument parsing error as narrow strings
2959 are also valid. */
2960 PyErr_Clear();
2961 }
2962 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002963 if (!PyArg_ParseTuple(args, "O&O:utime",
2964 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002965 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002966 apath = bytes2str(oapath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002967 Py_BEGIN_ALLOW_THREADS
2968 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002969 NULL, OPEN_EXISTING,
2970 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002971 Py_END_ALLOW_THREADS
2972 if (hFile == INVALID_HANDLE_VALUE) {
2973 win32_error("utime", apath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002974 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002975 return NULL;
2976 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002977 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002978 }
2979
2980 if (arg == Py_None) {
2981 SYSTEMTIME now;
2982 GetSystemTime(&now);
2983 if (!SystemTimeToFileTime(&now, &mtime) ||
2984 !SystemTimeToFileTime(&now, &atime)) {
2985 win32_error("utime", NULL);
2986 goto done;
2987 }
2988 }
2989 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2990 PyErr_SetString(PyExc_TypeError,
2991 "utime() arg 2 must be a tuple (atime, mtime)");
2992 goto done;
2993 }
2994 else {
2995 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2996 &atimesec, &ausec) == -1)
2997 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002998 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002999 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3000 &mtimesec, &musec) == -1)
3001 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003002 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003003 }
3004 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3005 /* Avoid putting the file name into the error here,
3006 as that may confuse the user into believing that
3007 something is wrong with the file, when it also
3008 could be the time stamp that gives a problem. */
3009 win32_error("utime", NULL);
3010 }
3011 Py_INCREF(Py_None);
3012 result = Py_None;
3013done:
3014 CloseHandle(hFile);
3015 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003016#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003017
Martin v. Löwis011e8422009-05-05 04:43:17 +00003018 PyObject *opath;
3019 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003020 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00003021 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00003022 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003023
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003024#if defined(HAVE_UTIMES)
3025 struct timeval buf[2];
3026#define ATIME buf[0].tv_sec
3027#define MTIME buf[1].tv_sec
3028#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003029/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003030 struct utimbuf buf;
3031#define ATIME buf.actime
3032#define MTIME buf.modtime
3033#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003034#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003035 time_t buf[2];
3036#define ATIME buf[0]
3037#define MTIME buf[1]
3038#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003039#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003040
Mark Hammond817c9292003-12-03 01:22:38 +00003041
Martin v. Löwis011e8422009-05-05 04:43:17 +00003042 if (!PyArg_ParseTuple(args, "O&O:utime",
3043 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003044 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003045 path = bytes2str(opath, 1);
Barry Warsaw3cef8562000-05-01 16:17:24 +00003046 if (arg == Py_None) {
3047 /* optional time values not given */
3048 Py_BEGIN_ALLOW_THREADS
3049 res = utime(path, NULL);
3050 Py_END_ALLOW_THREADS
3051 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003052 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00003053 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003054 "utime() arg 2 must be a tuple (atime, mtime)");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003055 release_bytes(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00003056 return NULL;
3057 }
3058 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003059 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00003060 &atime, &ausec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003061 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003062 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00003063 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003064 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00003065 &mtime, &musec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003066 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003067 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00003068 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00003069 ATIME = atime;
3070 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003071#ifdef HAVE_UTIMES
3072 buf[0].tv_usec = ausec;
3073 buf[1].tv_usec = musec;
3074 Py_BEGIN_ALLOW_THREADS
3075 res = utimes(path, buf);
3076 Py_END_ALLOW_THREADS
3077#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00003078 Py_BEGIN_ALLOW_THREADS
3079 res = utime(path, UTIME_ARG);
3080 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003081#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00003082 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00003083 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003084 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00003085 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00003086 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00003087 Py_INCREF(Py_None);
3088 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003089#undef UTIME_ARG
3090#undef ATIME
3091#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003092#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003093}
3094
Guido van Rossum85e3b011991-06-03 12:42:10 +00003095
Guido van Rossum3b066191991-06-04 19:40:25 +00003096/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003099"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003101
Barry Warsaw53699e91996-12-10 23:23:01 +00003102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003103posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003104{
3105 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003106 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003107 return NULL;
3108 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00003109 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003110}
3111
Martin v. Löwis114619e2002-10-07 06:44:21 +00003112#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3113static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003114free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003115{
Martin v. Löwis725507b2006-03-07 12:08:51 +00003116 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00003117 for (i = 0; i < count; i++)
3118 PyMem_Free(array[i]);
3119 PyMem_DEL(array);
3120}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003121
3122int fsconvert_strdup(PyObject *o, char**out)
3123{
3124 PyObject *bytes;
3125 Py_ssize_t size;
3126 if (!PyUnicode_FSConverter(o, &bytes))
3127 return 0;
3128 size = PyObject_Size(bytes);
3129 *out = PyMem_Malloc(size+1);
3130 if (!*out)
3131 return 0;
3132 /* Don't lock bytes, as we hold the GIL */
3133 memcpy(*out, bytes2str(bytes, 0), size+1);
3134 Py_DECREF(bytes);
3135 return 1;
3136}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003137#endif
3138
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003140#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003141PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003142"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003143Execute an executable path with arguments, replacing current process.\n\
3144\n\
3145 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003146 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003147
Barry Warsaw53699e91996-12-10 23:23:01 +00003148static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003149posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003150{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003151 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003152 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003153 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003154 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003155 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003156 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003157
Guido van Rossum89b33251993-10-22 14:26:06 +00003158 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003159 argv is a list or tuple of strings. */
3160
Martin v. Löwis011e8422009-05-05 04:43:17 +00003161 if (!PyArg_ParseTuple(args, "O&O:execv",
3162 PyUnicode_FSConverter,
3163 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003164 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003165 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003166 if (PyList_Check(argv)) {
3167 argc = PyList_Size(argv);
3168 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003169 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003170 else if (PyTuple_Check(argv)) {
3171 argc = PyTuple_Size(argv);
3172 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003173 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003174 else {
Fred Drake661ea262000-10-24 19:57:45 +00003175 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003176 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003177 return NULL;
3178 }
Thomas Heller6790d602007-08-30 17:15:14 +00003179 if (argc < 1) {
3180 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003181 release_bytes(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003182 return NULL;
3183 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003184
Barry Warsaw53699e91996-12-10 23:23:01 +00003185 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003186 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003187 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003188 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003189 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003190 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003191 if (!fsconvert_strdup((*getitem)(argv, i),
3192 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003193 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003194 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003195 "execv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003196 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003197 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003198
Guido van Rossum85e3b011991-06-03 12:42:10 +00003199 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003200 }
3201 argvlist[argc] = NULL;
3202
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003203 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003204
Guido van Rossum85e3b011991-06-03 12:42:10 +00003205 /* If we get here it's definitely an error */
3206
Martin v. Löwis114619e2002-10-07 06:44:21 +00003207 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003208 release_bytes(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003209 return posix_error();
3210}
3211
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003213PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003214"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003215Execute a path with arguments and environment, replacing current process.\n\
3216\n\
3217 path: path of executable file\n\
3218 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003219 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003220
Barry Warsaw53699e91996-12-10 23:23:01 +00003221static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003222posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003223{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003224 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003225 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003226 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003227 char **argvlist;
3228 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003229 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003230 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003231 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003232 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003233
3234 /* execve has three arguments: (path, argv, env), where
3235 argv is a list or tuple of strings and env is a dictionary
3236 like posix.environ. */
3237
Martin v. Löwis011e8422009-05-05 04:43:17 +00003238 if (!PyArg_ParseTuple(args, "O&OO:execve",
3239 PyUnicode_FSConverter,
3240 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003241 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003242 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003243 if (PyList_Check(argv)) {
3244 argc = PyList_Size(argv);
3245 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003246 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003247 else if (PyTuple_Check(argv)) {
3248 argc = PyTuple_Size(argv);
3249 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003250 }
3251 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003252 PyErr_SetString(PyExc_TypeError,
3253 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003254 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003255 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003256 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003257 PyErr_SetString(PyExc_TypeError,
3258 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003259 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003260 }
3261
Barry Warsaw53699e91996-12-10 23:23:01 +00003262 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003263 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003264 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003265 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003266 }
3267 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003268 if (!fsconvert_strdup((*getitem)(argv, i),
3269 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003270 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003271 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003272 goto fail_1;
3273 }
3274 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003275 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003276 argvlist[argc] = NULL;
3277
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003278 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003279 if (i < 0)
3280 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003281 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003282 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003283 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003284 goto fail_1;
3285 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003286 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003287 keys = PyMapping_Keys(env);
3288 vals = PyMapping_Values(env);
3289 if (!keys || !vals)
3290 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003291 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3292 PyErr_SetString(PyExc_TypeError,
3293 "execve(): env.keys() or env.values() is not a list");
3294 goto fail_2;
3295 }
Tim Peters5aa91602002-01-30 05:46:57 +00003296
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003297 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003298 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003299 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003300
3301 key = PyList_GetItem(keys, pos);
3302 val = PyList_GetItem(vals, pos);
3303 if (!key || !val)
3304 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003305
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003306 if (!PyArg_Parse(
3307 key,
3308 "s;execve() arg 3 contains a non-string key",
3309 &k) ||
3310 !PyArg_Parse(
3311 val,
3312 "s;execve() arg 3 contains a non-string value",
3313 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003314 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003315 goto fail_2;
3316 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003317
3318#if defined(PYOS_OS2)
3319 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3320 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3321#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003322 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003323 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003324 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003325 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003326 goto fail_2;
3327 }
Tim Petersc8996f52001-12-03 20:41:00 +00003328 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003329 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003330#if defined(PYOS_OS2)
3331 }
3332#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003333 }
3334 envlist[envc] = 0;
3335
3336 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003337
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003338 /* If we get here it's definitely an error */
3339
3340 (void) posix_error();
3341
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003342 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003343 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003344 PyMem_DEL(envlist[envc]);
3345 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003346 fail_1:
3347 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003348 Py_XDECREF(vals);
3349 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003350 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003351 release_bytes(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003352 return NULL;
3353}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003354#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003355
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003356
Guido van Rossuma1065681999-01-25 23:20:23 +00003357#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003358PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003359"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003360Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003361\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003362 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003363 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003364 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003365
3366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003367posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003368{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003369 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003370 char *path;
3371 PyObject *argv;
3372 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003373 int mode, i;
3374 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003375 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003376 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003377
3378 /* spawnv has three arguments: (mode, path, argv), where
3379 argv is a list or tuple of strings. */
3380
Martin v. Löwis011e8422009-05-05 04:43:17 +00003381 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3382 PyUnicode_FSConverter,
3383 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003384 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003385 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003386 if (PyList_Check(argv)) {
3387 argc = PyList_Size(argv);
3388 getitem = PyList_GetItem;
3389 }
3390 else if (PyTuple_Check(argv)) {
3391 argc = PyTuple_Size(argv);
3392 getitem = PyTuple_GetItem;
3393 }
3394 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003395 PyErr_SetString(PyExc_TypeError,
3396 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003397 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003398 return NULL;
3399 }
3400
3401 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003402 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003403 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003404 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003405 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003406 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003407 if (!fsconvert_strdup((*getitem)(argv, i),
3408 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003409 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003410 PyErr_SetString(
3411 PyExc_TypeError,
3412 "spawnv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003413 release_bytes(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003414 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003415 }
3416 }
3417 argvlist[argc] = NULL;
3418
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003419#if defined(PYOS_OS2) && defined(PYCC_GCC)
3420 Py_BEGIN_ALLOW_THREADS
3421 spawnval = spawnv(mode, path, argvlist);
3422 Py_END_ALLOW_THREADS
3423#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003424 if (mode == _OLD_P_OVERLAY)
3425 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003426
Tim Peters25059d32001-12-07 20:35:43 +00003427 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003428 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003429 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003430#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003431
Martin v. Löwis114619e2002-10-07 06:44:21 +00003432 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003433 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003434
Fred Drake699f3522000-06-29 21:12:41 +00003435 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003436 return posix_error();
3437 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003438#if SIZEOF_LONG == SIZEOF_VOID_P
3439 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003440#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003441 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003442#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003443}
3444
3445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003446PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003447"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003448Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003449\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003450 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003451 path: path of executable file\n\
3452 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003453 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003454
3455static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003456posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003457{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003458 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003459 char *path;
3460 PyObject *argv, *env;
3461 char **argvlist;
3462 char **envlist;
3463 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003464 int mode, pos, envc;
3465 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003466 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003467 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003468 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003469
3470 /* spawnve has four arguments: (mode, path, argv, env), where
3471 argv is a list or tuple of strings and env is a dictionary
3472 like posix.environ. */
3473
Martin v. Löwis011e8422009-05-05 04:43:17 +00003474 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3475 PyUnicode_FSConverter,
3476 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003477 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003478 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003479 if (PyList_Check(argv)) {
3480 argc = PyList_Size(argv);
3481 getitem = PyList_GetItem;
3482 }
3483 else if (PyTuple_Check(argv)) {
3484 argc = PyTuple_Size(argv);
3485 getitem = PyTuple_GetItem;
3486 }
3487 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003488 PyErr_SetString(PyExc_TypeError,
3489 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003490 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003491 }
3492 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003493 PyErr_SetString(PyExc_TypeError,
3494 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003495 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003496 }
3497
3498 argvlist = PyMem_NEW(char *, argc+1);
3499 if (argvlist == NULL) {
3500 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003501 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003502 }
3503 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003504 if (!fsconvert_strdup((*getitem)(argv, i),
3505 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003506 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003507 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003508 goto fail_1;
3509 }
3510 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003511 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003512 argvlist[argc] = NULL;
3513
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003514 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003515 if (i < 0)
3516 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003517 envlist = PyMem_NEW(char *, i + 1);
3518 if (envlist == NULL) {
3519 PyErr_NoMemory();
3520 goto fail_1;
3521 }
3522 envc = 0;
3523 keys = PyMapping_Keys(env);
3524 vals = PyMapping_Values(env);
3525 if (!keys || !vals)
3526 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003527 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3528 PyErr_SetString(PyExc_TypeError,
3529 "spawnve(): env.keys() or env.values() is not a list");
3530 goto fail_2;
3531 }
Tim Peters5aa91602002-01-30 05:46:57 +00003532
Guido van Rossuma1065681999-01-25 23:20:23 +00003533 for (pos = 0; pos < i; pos++) {
3534 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003535 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003536
3537 key = PyList_GetItem(keys, pos);
3538 val = PyList_GetItem(vals, pos);
3539 if (!key || !val)
3540 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003541
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003542 if (!PyArg_Parse(
3543 key,
3544 "s;spawnve() arg 3 contains a non-string key",
3545 &k) ||
3546 !PyArg_Parse(
3547 val,
3548 "s;spawnve() arg 3 contains a non-string value",
3549 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003550 {
3551 goto fail_2;
3552 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003553 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003554 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003555 if (p == NULL) {
3556 PyErr_NoMemory();
3557 goto fail_2;
3558 }
Tim Petersc8996f52001-12-03 20:41:00 +00003559 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003560 envlist[envc++] = p;
3561 }
3562 envlist[envc] = 0;
3563
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003564#if defined(PYOS_OS2) && defined(PYCC_GCC)
3565 Py_BEGIN_ALLOW_THREADS
3566 spawnval = spawnve(mode, path, argvlist, envlist);
3567 Py_END_ALLOW_THREADS
3568#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003569 if (mode == _OLD_P_OVERLAY)
3570 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003571
3572 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003573 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003574 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003575#endif
Tim Peters25059d32001-12-07 20:35:43 +00003576
Fred Drake699f3522000-06-29 21:12:41 +00003577 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003578 (void) posix_error();
3579 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003580#if SIZEOF_LONG == SIZEOF_VOID_P
3581 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003582#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003583 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003584#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003585
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003586 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003587 while (--envc >= 0)
3588 PyMem_DEL(envlist[envc]);
3589 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003590 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003591 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003592 Py_XDECREF(vals);
3593 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003594 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003595 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003596 return res;
3597}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003598
3599/* OS/2 supports spawnvp & spawnvpe natively */
3600#if defined(PYOS_OS2)
3601PyDoc_STRVAR(posix_spawnvp__doc__,
3602"spawnvp(mode, file, args)\n\n\
3603Execute the program 'file' in a new process, using the environment\n\
3604search path to find the file.\n\
3605\n\
3606 mode: mode of process creation\n\
3607 file: executable file name\n\
3608 args: tuple or list of strings");
3609
3610static PyObject *
3611posix_spawnvp(PyObject *self, PyObject *args)
3612{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003613 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003614 char *path;
3615 PyObject *argv;
3616 char **argvlist;
3617 int mode, i, argc;
3618 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003619 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003620
3621 /* spawnvp has three arguments: (mode, path, argv), where
3622 argv is a list or tuple of strings. */
3623
Martin v. Löwis011e8422009-05-05 04:43:17 +00003624 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3625 PyUnicode_FSConverter,
3626 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003627 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003628 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003629 if (PyList_Check(argv)) {
3630 argc = PyList_Size(argv);
3631 getitem = PyList_GetItem;
3632 }
3633 else if (PyTuple_Check(argv)) {
3634 argc = PyTuple_Size(argv);
3635 getitem = PyTuple_GetItem;
3636 }
3637 else {
3638 PyErr_SetString(PyExc_TypeError,
3639 "spawnvp() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003640 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003641 return NULL;
3642 }
3643
3644 argvlist = PyMem_NEW(char *, argc+1);
3645 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003646 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003647 return PyErr_NoMemory();
3648 }
3649 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003650 if (!fsconvert_strdup((*getitem)(argv, i),
3651 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003652 free_string_array(argvlist, i);
3653 PyErr_SetString(
3654 PyExc_TypeError,
3655 "spawnvp() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003656 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003657 return NULL;
3658 }
3659 }
3660 argvlist[argc] = NULL;
3661
3662 Py_BEGIN_ALLOW_THREADS
3663#if defined(PYCC_GCC)
3664 spawnval = spawnvp(mode, path, argvlist);
3665#else
3666 spawnval = _spawnvp(mode, path, argvlist);
3667#endif
3668 Py_END_ALLOW_THREADS
3669
3670 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003671 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003672
3673 if (spawnval == -1)
3674 return posix_error();
3675 else
3676 return Py_BuildValue("l", (long) spawnval);
3677}
3678
3679
3680PyDoc_STRVAR(posix_spawnvpe__doc__,
3681"spawnvpe(mode, file, args, env)\n\n\
3682Execute the program 'file' in a new process, using the environment\n\
3683search path to find the file.\n\
3684\n\
3685 mode: mode of process creation\n\
3686 file: executable file name\n\
3687 args: tuple or list of arguments\n\
3688 env: dictionary of strings mapping to strings");
3689
3690static PyObject *
3691posix_spawnvpe(PyObject *self, PyObject *args)
3692{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003693 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003694 char *path;
3695 PyObject *argv, *env;
3696 char **argvlist;
3697 char **envlist;
3698 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3699 int mode, i, pos, argc, envc;
3700 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003701 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003702 int lastarg = 0;
3703
3704 /* spawnvpe has four arguments: (mode, path, argv, env), where
3705 argv is a list or tuple of strings and env is a dictionary
3706 like posix.environ. */
3707
3708 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003709 PyUnicode_FSConverter,
3710 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003711 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003712 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003713 if (PyList_Check(argv)) {
3714 argc = PyList_Size(argv);
3715 getitem = PyList_GetItem;
3716 }
3717 else if (PyTuple_Check(argv)) {
3718 argc = PyTuple_Size(argv);
3719 getitem = PyTuple_GetItem;
3720 }
3721 else {
3722 PyErr_SetString(PyExc_TypeError,
3723 "spawnvpe() arg 2 must be a tuple or list");
3724 goto fail_0;
3725 }
3726 if (!PyMapping_Check(env)) {
3727 PyErr_SetString(PyExc_TypeError,
3728 "spawnvpe() arg 3 must be a mapping object");
3729 goto fail_0;
3730 }
3731
3732 argvlist = PyMem_NEW(char *, argc+1);
3733 if (argvlist == NULL) {
3734 PyErr_NoMemory();
3735 goto fail_0;
3736 }
3737 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003738 if (!fsconvert_strdup((*getitem)(argv, i),
3739 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003740 {
3741 lastarg = i;
3742 goto fail_1;
3743 }
3744 }
3745 lastarg = argc;
3746 argvlist[argc] = NULL;
3747
3748 i = PyMapping_Size(env);
3749 if (i < 0)
3750 goto fail_1;
3751 envlist = PyMem_NEW(char *, i + 1);
3752 if (envlist == NULL) {
3753 PyErr_NoMemory();
3754 goto fail_1;
3755 }
3756 envc = 0;
3757 keys = PyMapping_Keys(env);
3758 vals = PyMapping_Values(env);
3759 if (!keys || !vals)
3760 goto fail_2;
3761 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3762 PyErr_SetString(PyExc_TypeError,
3763 "spawnvpe(): env.keys() or env.values() is not a list");
3764 goto fail_2;
3765 }
3766
3767 for (pos = 0; pos < i; pos++) {
3768 char *p, *k, *v;
3769 size_t len;
3770
3771 key = PyList_GetItem(keys, pos);
3772 val = PyList_GetItem(vals, pos);
3773 if (!key || !val)
3774 goto fail_2;
3775
3776 if (!PyArg_Parse(
3777 key,
3778 "s;spawnvpe() arg 3 contains a non-string key",
3779 &k) ||
3780 !PyArg_Parse(
3781 val,
3782 "s;spawnvpe() arg 3 contains a non-string value",
3783 &v))
3784 {
3785 goto fail_2;
3786 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003787 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003788 p = PyMem_NEW(char, len);
3789 if (p == NULL) {
3790 PyErr_NoMemory();
3791 goto fail_2;
3792 }
3793 PyOS_snprintf(p, len, "%s=%s", k, v);
3794 envlist[envc++] = p;
3795 }
3796 envlist[envc] = 0;
3797
3798 Py_BEGIN_ALLOW_THREADS
3799#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003800 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003801#else
Christian Heimes292d3512008-02-03 16:51:08 +00003802 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003803#endif
3804 Py_END_ALLOW_THREADS
3805
3806 if (spawnval == -1)
3807 (void) posix_error();
3808 else
3809 res = Py_BuildValue("l", (long) spawnval);
3810
3811 fail_2:
3812 while (--envc >= 0)
3813 PyMem_DEL(envlist[envc]);
3814 PyMem_DEL(envlist);
3815 fail_1:
3816 free_string_array(argvlist, lastarg);
3817 Py_XDECREF(vals);
3818 Py_XDECREF(keys);
3819 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003820 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003821 return res;
3822}
3823#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003824#endif /* HAVE_SPAWNV */
3825
3826
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003827#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003829"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003830Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3831\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003832Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003833
3834static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003835posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003836{
Christian Heimes400adb02008-02-01 08:12:03 +00003837 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003838 if (pid == -1)
3839 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003840 if (pid == 0)
3841 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003842 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003843}
3844#endif
3845
3846
Guido van Rossumad0ee831995-03-01 10:34:45 +00003847#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003848PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003849"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003850Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003851Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003852
Barry Warsaw53699e91996-12-10 23:23:01 +00003853static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003854posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003855{
Christian Heimes400adb02008-02-01 08:12:03 +00003856 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003857 if (pid == -1)
3858 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003859 if (pid == 0)
3860 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003861 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003862}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003863#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003864
Neal Norwitzb59798b2003-03-21 01:43:31 +00003865/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003866/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3867#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003868#define DEV_PTY_FILE "/dev/ptc"
3869#define HAVE_DEV_PTMX
3870#else
3871#define DEV_PTY_FILE "/dev/ptmx"
3872#endif
3873
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003874#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003875#ifdef HAVE_PTY_H
3876#include <pty.h>
3877#else
3878#ifdef HAVE_LIBUTIL_H
3879#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003880#endif /* HAVE_LIBUTIL_H */
3881#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003882#ifdef HAVE_STROPTS_H
3883#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003884#endif
3885#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003886
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003887#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003888PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003889"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003890Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003891
3892static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003893posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003894{
3895 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003896#ifndef HAVE_OPENPTY
3897 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003898#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003899#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003900 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003901#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003902 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003903#endif
3904#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003905
Thomas Wouters70c21a12000-07-14 14:28:33 +00003906#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003907 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3908 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003909#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003910 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3911 if (slave_name == NULL)
3912 return posix_error();
3913
3914 slave_fd = open(slave_name, O_RDWR);
3915 if (slave_fd < 0)
3916 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003917#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003918 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003919 if (master_fd < 0)
3920 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003921 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003922 /* change permission of slave */
3923 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003924 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003925 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003926 }
3927 /* unlock slave */
3928 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003929 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003930 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003931 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003932 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003933 slave_name = ptsname(master_fd); /* get name of slave */
3934 if (slave_name == NULL)
3935 return posix_error();
3936 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3937 if (slave_fd < 0)
3938 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003939#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003940 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3941 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003942#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003943 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003944#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003945#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003946#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003947
Fred Drake8cef4cf2000-06-28 16:40:38 +00003948 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003949
Fred Drake8cef4cf2000-06-28 16:40:38 +00003950}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003951#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003952
3953#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003954PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003955"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003956Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3957Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003958To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003959
3960static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003961posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003962{
Christian Heimes400adb02008-02-01 08:12:03 +00003963 int master_fd = -1;
3964 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003965
Fred Drake8cef4cf2000-06-28 16:40:38 +00003966 pid = forkpty(&master_fd, NULL, NULL, NULL);
3967 if (pid == -1)
3968 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003969 if (pid == 0)
3970 PyOS_AfterFork();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003971 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003972}
3973#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003974
Guido van Rossumad0ee831995-03-01 10:34:45 +00003975#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003976PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003977"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003978Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003979
Barry Warsaw53699e91996-12-10 23:23:01 +00003980static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003981posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003982{
Christian Heimes217cfd12007-12-02 14:31:20 +00003983 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003984}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003985#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003986
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003987
Guido van Rossumad0ee831995-03-01 10:34:45 +00003988#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003989PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003990"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003991Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003992
Barry Warsaw53699e91996-12-10 23:23:01 +00003993static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003994posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003995{
Christian Heimes217cfd12007-12-02 14:31:20 +00003996 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003997}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003998#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003999
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004000
Guido van Rossumad0ee831995-03-01 10:34:45 +00004001#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004002PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004003"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004004Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004005
Barry Warsaw53699e91996-12-10 23:23:01 +00004006static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004007posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004008{
Christian Heimes217cfd12007-12-02 14:31:20 +00004009 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004010}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004011#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004012
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004013
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004014PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004015"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004016Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004017
Barry Warsaw53699e91996-12-10 23:23:01 +00004018static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004019posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004020{
Christian Heimes217cfd12007-12-02 14:31:20 +00004021 return PyLong_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004022}
4023
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004024
Fred Drakec9680921999-12-13 16:37:25 +00004025#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004026PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004027"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004028Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004029
4030static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004031posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004032{
4033 PyObject *result = NULL;
4034
Fred Drakec9680921999-12-13 16:37:25 +00004035#ifdef NGROUPS_MAX
4036#define MAX_GROUPS NGROUPS_MAX
4037#else
4038 /* defined to be 16 on Solaris7, so this should be a small number */
4039#define MAX_GROUPS 64
4040#endif
4041 gid_t grouplist[MAX_GROUPS];
4042 int n;
4043
4044 n = getgroups(MAX_GROUPS, grouplist);
4045 if (n < 0)
4046 posix_error();
4047 else {
4048 result = PyList_New(n);
4049 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00004050 int i;
4051 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004052 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00004053 if (o == NULL) {
4054 Py_DECREF(result);
4055 result = NULL;
4056 break;
4057 }
4058 PyList_SET_ITEM(result, i, o);
4059 }
4060 }
4061 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004062
Fred Drakec9680921999-12-13 16:37:25 +00004063 return result;
4064}
4065#endif
4066
Martin v. Löwis606edc12002-06-13 21:09:11 +00004067#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004068PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004069"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004070Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004071
4072static PyObject *
4073posix_getpgid(PyObject *self, PyObject *args)
4074{
4075 int pid, pgid;
4076 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
4077 return NULL;
4078 pgid = getpgid(pid);
4079 if (pgid < 0)
4080 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004081 return PyLong_FromLong((long)pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004082}
4083#endif /* HAVE_GETPGID */
4084
4085
Guido van Rossumb6775db1994-08-01 11:34:53 +00004086#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004087PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004088"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004089Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004090
Barry Warsaw53699e91996-12-10 23:23:01 +00004091static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004092posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004093{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004094#ifdef GETPGRP_HAVE_ARG
Christian Heimes217cfd12007-12-02 14:31:20 +00004095 return PyLong_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004096#else /* GETPGRP_HAVE_ARG */
Christian Heimes217cfd12007-12-02 14:31:20 +00004097 return PyLong_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004098#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004099}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004100#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004101
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004102
Guido van Rossumb6775db1994-08-01 11:34:53 +00004103#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004104PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004105"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004106Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004107
Barry Warsaw53699e91996-12-10 23:23:01 +00004108static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004109posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004110{
Guido van Rossum64933891994-10-20 21:56:42 +00004111#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004112 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004113#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004114 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004115#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004116 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004117 Py_INCREF(Py_None);
4118 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004119}
4120
Guido van Rossumb6775db1994-08-01 11:34:53 +00004121#endif /* HAVE_SETPGRP */
4122
Guido van Rossumad0ee831995-03-01 10:34:45 +00004123#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004124PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004125"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004126Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004127
Barry Warsaw53699e91996-12-10 23:23:01 +00004128static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004129posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004130{
Christian Heimes217cfd12007-12-02 14:31:20 +00004131 return PyLong_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004132}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004133#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004134
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004135
Fred Drake12c6e2d1999-12-14 21:25:03 +00004136#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004137PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004138"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004139Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004140
4141static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004142posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004143{
Neal Norwitze241ce82003-02-17 18:17:05 +00004144 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004145 char *name;
4146 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004147
Fred Drakea30680b2000-12-06 21:24:28 +00004148 errno = 0;
4149 name = getlogin();
4150 if (name == NULL) {
4151 if (errno)
4152 posix_error();
4153 else
4154 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004155 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004156 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004157 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004158 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004159 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004160
Fred Drake12c6e2d1999-12-14 21:25:03 +00004161 return result;
4162}
4163#endif
4164
Guido van Rossumad0ee831995-03-01 10:34:45 +00004165#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004166PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004167"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004168Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004169
Barry Warsaw53699e91996-12-10 23:23:01 +00004170static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004171posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004172{
Christian Heimes217cfd12007-12-02 14:31:20 +00004173 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004174}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004175#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004176
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004177
Guido van Rossumad0ee831995-03-01 10:34:45 +00004178#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004179PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004180"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004181Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004182
Barry Warsaw53699e91996-12-10 23:23:01 +00004183static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004184posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004185{
Christian Heimes292d3512008-02-03 16:51:08 +00004186 pid_t pid;
4187 int sig;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004188 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004189 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004190#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004191 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4192 APIRET rc;
4193 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004194 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004195
4196 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4197 APIRET rc;
4198 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004199 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004200
4201 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004202 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004203#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004204 if (kill(pid, sig) == -1)
4205 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004206#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004207 Py_INCREF(Py_None);
4208 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004209}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004210#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004211
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004212#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004213PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004214"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004215Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004216
4217static PyObject *
4218posix_killpg(PyObject *self, PyObject *args)
4219{
4220 int pgid, sig;
4221 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
4222 return NULL;
4223 if (killpg(pgid, sig) == -1)
4224 return posix_error();
4225 Py_INCREF(Py_None);
4226 return Py_None;
4227}
4228#endif
4229
Guido van Rossumc0125471996-06-28 18:55:32 +00004230#ifdef HAVE_PLOCK
4231
4232#ifdef HAVE_SYS_LOCK_H
4233#include <sys/lock.h>
4234#endif
4235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004236PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004237"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004238Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004239
Barry Warsaw53699e91996-12-10 23:23:01 +00004240static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004241posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004242{
4243 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004244 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004245 return NULL;
4246 if (plock(op) == -1)
4247 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004248 Py_INCREF(Py_None);
4249 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004250}
4251#endif
4252
Guido van Rossumb6775db1994-08-01 11:34:53 +00004253#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004254PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004255"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004256Set the current process's user id.");
4257
Barry Warsaw53699e91996-12-10 23:23:01 +00004258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004259posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004260{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004261 long uid_arg;
4262 uid_t uid;
4263 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004264 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004265 uid = uid_arg;
4266 if (uid != uid_arg) {
4267 PyErr_SetString(PyExc_OverflowError, "user id too big");
4268 return NULL;
4269 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004270 if (setuid(uid) < 0)
4271 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004272 Py_INCREF(Py_None);
4273 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004274}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004275#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004277
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004278#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004279PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004280"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004281Set the current process's effective user id.");
4282
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004283static PyObject *
4284posix_seteuid (PyObject *self, PyObject *args)
4285{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004286 long euid_arg;
4287 uid_t euid;
4288 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004289 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004290 euid = euid_arg;
4291 if (euid != euid_arg) {
4292 PyErr_SetString(PyExc_OverflowError, "user id too big");
4293 return NULL;
4294 }
4295 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004296 return posix_error();
4297 } else {
4298 Py_INCREF(Py_None);
4299 return Py_None;
4300 }
4301}
4302#endif /* HAVE_SETEUID */
4303
4304#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004305PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004306"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004307Set the current process's effective group id.");
4308
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004309static PyObject *
4310posix_setegid (PyObject *self, PyObject *args)
4311{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004312 long egid_arg;
4313 gid_t egid;
4314 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004315 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004316 egid = egid_arg;
4317 if (egid != egid_arg) {
4318 PyErr_SetString(PyExc_OverflowError, "group id too big");
4319 return NULL;
4320 }
4321 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004322 return posix_error();
4323 } else {
4324 Py_INCREF(Py_None);
4325 return Py_None;
4326 }
4327}
4328#endif /* HAVE_SETEGID */
4329
4330#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004331PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004332"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004333Set the current process's real and effective user ids.");
4334
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004335static PyObject *
4336posix_setreuid (PyObject *self, PyObject *args)
4337{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004338 long ruid_arg, euid_arg;
4339 uid_t ruid, euid;
4340 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004341 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004342 ruid = ruid_arg;
4343 euid = euid_arg;
4344 if (euid != euid_arg || ruid != ruid_arg) {
4345 PyErr_SetString(PyExc_OverflowError, "user id too big");
4346 return NULL;
4347 }
4348 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004349 return posix_error();
4350 } else {
4351 Py_INCREF(Py_None);
4352 return Py_None;
4353 }
4354}
4355#endif /* HAVE_SETREUID */
4356
4357#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004358PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004359"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004360Set the current process's real and effective group ids.");
4361
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004362static PyObject *
4363posix_setregid (PyObject *self, PyObject *args)
4364{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004365 long rgid_arg, egid_arg;
4366 gid_t rgid, egid;
4367 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004368 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004369 rgid = rgid_arg;
4370 egid = egid_arg;
4371 if (egid != egid_arg || rgid != rgid_arg) {
4372 PyErr_SetString(PyExc_OverflowError, "group id too big");
4373 return NULL;
4374 }
4375 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004376 return posix_error();
4377 } else {
4378 Py_INCREF(Py_None);
4379 return Py_None;
4380 }
4381}
4382#endif /* HAVE_SETREGID */
4383
Guido van Rossumb6775db1994-08-01 11:34:53 +00004384#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004385PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004386"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004387Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004388
Barry Warsaw53699e91996-12-10 23:23:01 +00004389static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004390posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004391{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004392 long gid_arg;
4393 gid_t gid;
4394 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004395 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004396 gid = gid_arg;
4397 if (gid != gid_arg) {
4398 PyErr_SetString(PyExc_OverflowError, "group id too big");
4399 return NULL;
4400 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004401 if (setgid(gid) < 0)
4402 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004403 Py_INCREF(Py_None);
4404 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004405}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004406#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004407
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004408#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004409PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004410"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004411Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004412
4413static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004414posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004415{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004416 int i, len;
4417 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004418
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004419 if (!PySequence_Check(groups)) {
4420 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4421 return NULL;
4422 }
4423 len = PySequence_Size(groups);
4424 if (len > MAX_GROUPS) {
4425 PyErr_SetString(PyExc_ValueError, "too many groups");
4426 return NULL;
4427 }
4428 for(i = 0; i < len; i++) {
4429 PyObject *elem;
4430 elem = PySequence_GetItem(groups, i);
4431 if (!elem)
4432 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004433 if (!PyLong_Check(elem)) {
4434 PyErr_SetString(PyExc_TypeError,
4435 "groups must be integers");
4436 Py_DECREF(elem);
4437 return NULL;
4438 } else {
4439 unsigned long x = PyLong_AsUnsignedLong(elem);
4440 if (PyErr_Occurred()) {
4441 PyErr_SetString(PyExc_TypeError,
4442 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004443 Py_DECREF(elem);
4444 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004445 }
Georg Brandla13c2442005-11-22 19:30:31 +00004446 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004447 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004448 if (grouplist[i] != x) {
4449 PyErr_SetString(PyExc_TypeError,
4450 "group id too big");
4451 Py_DECREF(elem);
4452 return NULL;
4453 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004454 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004455 Py_DECREF(elem);
4456 }
4457
4458 if (setgroups(len, grouplist) < 0)
4459 return posix_error();
4460 Py_INCREF(Py_None);
4461 return Py_None;
4462}
4463#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004464
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004465#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4466static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004467wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004468{
4469 PyObject *result;
4470 static PyObject *struct_rusage;
4471
4472 if (pid == -1)
4473 return posix_error();
4474
4475 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004476 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004477 if (m == NULL)
4478 return NULL;
4479 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4480 Py_DECREF(m);
4481 if (struct_rusage == NULL)
4482 return NULL;
4483 }
4484
4485 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4486 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4487 if (!result)
4488 return NULL;
4489
4490#ifndef doubletime
4491#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4492#endif
4493
4494 PyStructSequence_SET_ITEM(result, 0,
4495 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4496 PyStructSequence_SET_ITEM(result, 1,
4497 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4498#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004499 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004500 SET_INT(result, 2, ru->ru_maxrss);
4501 SET_INT(result, 3, ru->ru_ixrss);
4502 SET_INT(result, 4, ru->ru_idrss);
4503 SET_INT(result, 5, ru->ru_isrss);
4504 SET_INT(result, 6, ru->ru_minflt);
4505 SET_INT(result, 7, ru->ru_majflt);
4506 SET_INT(result, 8, ru->ru_nswap);
4507 SET_INT(result, 9, ru->ru_inblock);
4508 SET_INT(result, 10, ru->ru_oublock);
4509 SET_INT(result, 11, ru->ru_msgsnd);
4510 SET_INT(result, 12, ru->ru_msgrcv);
4511 SET_INT(result, 13, ru->ru_nsignals);
4512 SET_INT(result, 14, ru->ru_nvcsw);
4513 SET_INT(result, 15, ru->ru_nivcsw);
4514#undef SET_INT
4515
4516 if (PyErr_Occurred()) {
4517 Py_DECREF(result);
4518 return NULL;
4519 }
4520
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004521 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004522}
4523#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4524
4525#ifdef HAVE_WAIT3
4526PyDoc_STRVAR(posix_wait3__doc__,
4527"wait3(options) -> (pid, status, rusage)\n\n\
4528Wait for completion of a child process.");
4529
4530static PyObject *
4531posix_wait3(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
4539 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4540 return NULL;
4541
4542 Py_BEGIN_ALLOW_THREADS
4543 pid = wait3(&status, options, &ru);
4544 Py_END_ALLOW_THREADS
4545
4546 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4547}
4548#endif /* HAVE_WAIT3 */
4549
4550#ifdef HAVE_WAIT4
4551PyDoc_STRVAR(posix_wait4__doc__,
4552"wait4(pid, options) -> (pid, status, rusage)\n\n\
4553Wait for completion of a given child process.");
4554
4555static PyObject *
4556posix_wait4(PyObject *self, PyObject *args)
4557{
Christian Heimes292d3512008-02-03 16:51:08 +00004558 pid_t pid;
4559 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004560 struct rusage ru;
4561 WAIT_TYPE status;
4562 WAIT_STATUS_INT(status) = 0;
4563
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004564 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004565 return NULL;
4566
4567 Py_BEGIN_ALLOW_THREADS
4568 pid = wait4(pid, &status, options, &ru);
4569 Py_END_ALLOW_THREADS
4570
4571 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4572}
4573#endif /* HAVE_WAIT4 */
4574
Guido van Rossumb6775db1994-08-01 11:34:53 +00004575#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004576PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004577"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004578Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004579
Barry Warsaw53699e91996-12-10 23:23:01 +00004580static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004581posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004582{
Christian Heimes292d3512008-02-03 16:51:08 +00004583 pid_t pid;
4584 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004585 WAIT_TYPE status;
4586 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004587
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004588 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004589 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004590 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004591 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004592 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004593 if (pid == -1)
4594 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004595
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004596 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004597}
4598
Tim Petersab034fa2002-02-01 11:27:43 +00004599#elif defined(HAVE_CWAIT)
4600
4601/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004602PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004603"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004604"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004605
4606static PyObject *
4607posix_waitpid(PyObject *self, PyObject *args)
4608{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004609 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004610 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004611
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004612 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004613 return NULL;
4614 Py_BEGIN_ALLOW_THREADS
4615 pid = _cwait(&status, pid, options);
4616 Py_END_ALLOW_THREADS
4617 if (pid == -1)
4618 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004619
4620 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004621 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004622}
4623#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Guido van Rossumad0ee831995-03-01 10:34:45 +00004625#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004626PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004627"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004628Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004629
Barry Warsaw53699e91996-12-10 23:23:01 +00004630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004631posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004632{
Christian Heimes292d3512008-02-03 16:51:08 +00004633 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004634 WAIT_TYPE status;
4635 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004636
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004637 Py_BEGIN_ALLOW_THREADS
4638 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004639 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004640 if (pid == -1)
4641 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004642
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004643 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004644}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004645#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004646
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004648PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004649"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004650Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004651
Barry Warsaw53699e91996-12-10 23:23:01 +00004652static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004653posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004654{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004655#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004656 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004657#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004658#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004659 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004660#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004661 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004662#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004663#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004664}
4665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004666
Guido van Rossumb6775db1994-08-01 11:34:53 +00004667#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004668PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004669"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004670Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004671
Barry Warsaw53699e91996-12-10 23:23:01 +00004672static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004673posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004674{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004675 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004676 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004677 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004678 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004679 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004680 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004681
Martin v. Löwis011e8422009-05-05 04:43:17 +00004682 if (!PyArg_ParseTuple(args, "O&:readlink",
4683 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004684 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004685 path = bytes2str(opath, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004686 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004687 if (v == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00004688 release_bytes(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004689 return NULL;
4690 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004691
4692 if (PyUnicode_Check(v)) {
4693 arg_is_unicode = 1;
4694 }
4695 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004696
Barry Warsaw53699e91996-12-10 23:23:01 +00004697 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004698 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004699 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004700 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004701 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004702
Martin v. Löwis011e8422009-05-05 04:43:17 +00004703 release_bytes(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004704 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004705 if (arg_is_unicode) {
4706 PyObject *w;
4707
4708 w = PyUnicode_FromEncodedObject(v,
4709 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004710 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004711 if (w != NULL) {
4712 Py_DECREF(v);
4713 v = w;
4714 }
4715 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004716 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004717 }
4718 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004719 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004720}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004721#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004722
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004723
Guido van Rossumb6775db1994-08-01 11:34:53 +00004724#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004725PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004726"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004727Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004728
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004729static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004730posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004731{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004732 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004733}
4734#endif /* HAVE_SYMLINK */
4735
4736
4737#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004738#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4739static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004740system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004741{
4742 ULONG value = 0;
4743
4744 Py_BEGIN_ALLOW_THREADS
4745 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4746 Py_END_ALLOW_THREADS
4747
4748 return value;
4749}
4750
4751static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004752posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004753{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004754 /* Currently Only Uptime is Provided -- Others Later */
4755 return Py_BuildValue("ddddd",
4756 (double)0 /* t.tms_utime / HZ */,
4757 (double)0 /* t.tms_stime / HZ */,
4758 (double)0 /* t.tms_cutime / HZ */,
4759 (double)0 /* t.tms_cstime / HZ */,
4760 (double)system_uptime() / 1000);
4761}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004762#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004763#define NEED_TICKS_PER_SECOND
4764static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004765static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004766posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004767{
4768 struct tms t;
4769 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004770 errno = 0;
4771 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004772 if (c == (clock_t) -1)
4773 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004774 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004775 (double)t.tms_utime / ticks_per_second,
4776 (double)t.tms_stime / ticks_per_second,
4777 (double)t.tms_cutime / ticks_per_second,
4778 (double)t.tms_cstime / ticks_per_second,
4779 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004780}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004781#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004782#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004783
4784
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004785#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004786#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004787static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004788posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004789{
4790 FILETIME create, exit, kernel, user;
4791 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004792 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004793 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4794 /* The fields of a FILETIME structure are the hi and lo part
4795 of a 64-bit value expressed in 100 nanosecond units.
4796 1e7 is one second in such units; 1e-7 the inverse.
4797 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4798 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004799 return Py_BuildValue(
4800 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004801 (double)(user.dwHighDateTime*429.4967296 +
4802 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004803 (double)(kernel.dwHighDateTime*429.4967296 +
4804 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004805 (double)0,
4806 (double)0,
4807 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004808}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004809#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004810
4811#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004812PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004813"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004814Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004815#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004816
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004817
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004818#ifdef HAVE_GETSID
4819PyDoc_STRVAR(posix_getsid__doc__,
4820"getsid(pid) -> sid\n\n\
4821Call the system call getsid().");
4822
4823static PyObject *
4824posix_getsid(PyObject *self, PyObject *args)
4825{
Christian Heimes292d3512008-02-03 16:51:08 +00004826 pid_t pid;
4827 int sid;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004828 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004829 return NULL;
4830 sid = getsid(pid);
4831 if (sid < 0)
4832 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004833 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004834}
4835#endif /* HAVE_GETSID */
4836
4837
Guido van Rossumb6775db1994-08-01 11:34:53 +00004838#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004839PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004840"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004841Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004842
Barry Warsaw53699e91996-12-10 23:23:01 +00004843static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004844posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004845{
Guido van Rossum687dd131993-05-17 08:34:16 +00004846 if (setsid() < 0)
4847 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004848 Py_INCREF(Py_None);
4849 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004850}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004851#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004852
Guido van Rossumb6775db1994-08-01 11:34:53 +00004853#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004854PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004855"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004856Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004857
Barry Warsaw53699e91996-12-10 23:23:01 +00004858static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004859posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004860{
Christian Heimes292d3512008-02-03 16:51:08 +00004861 pid_t pid;
4862 int pgrp;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004863 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004864 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004865 if (setpgid(pid, pgrp) < 0)
4866 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004867 Py_INCREF(Py_None);
4868 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004869}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004870#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004871
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004872
Guido van Rossumb6775db1994-08-01 11:34:53 +00004873#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004874PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004875"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004876Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004877
Barry Warsaw53699e91996-12-10 23:23:01 +00004878static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004879posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004880{
Christian Heimes15ebc882008-02-04 18:48:49 +00004881 int fd;
4882 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004883 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004884 return NULL;
4885 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004886 if (pgid < 0)
4887 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004888 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004889}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004890#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004891
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004892
Guido van Rossumb6775db1994-08-01 11:34:53 +00004893#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004894PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004895"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004896Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004897
Barry Warsaw53699e91996-12-10 23:23:01 +00004898static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004899posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004900{
4901 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004902 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004903 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004904 if (tcsetpgrp(fd, pgid) < 0)
4905 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004906 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004907 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004908}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004909#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004910
Guido van Rossum687dd131993-05-17 08:34:16 +00004911/* Functions acting on file descriptors */
4912
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004913PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004914"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004915Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004916
Barry Warsaw53699e91996-12-10 23:23:01 +00004917static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004918posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004919{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004920 PyObject *ofile;
4921 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004922 int flag;
4923 int mode = 0777;
4924 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004925
4926#ifdef MS_WINDOWS
4927 if (unicode_file_names()) {
4928 PyUnicodeObject *po;
4929 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4930 Py_BEGIN_ALLOW_THREADS
4931 /* PyUnicode_AS_UNICODE OK without thread
4932 lock as it is a simple dereference. */
4933 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4934 Py_END_ALLOW_THREADS
4935 if (fd < 0)
4936 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004937 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004938 }
4939 /* Drop the argument parsing error as narrow strings
4940 are also valid. */
4941 PyErr_Clear();
4942 }
4943#endif
4944
Martin v. Löwis011e8422009-05-05 04:43:17 +00004945 if (!PyArg_ParseTuple(args, "O&i|i",
4946 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004947 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004948 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004949 file = bytes2str(ofile, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00004950 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004951 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004952 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004953 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004954 return posix_error_with_allocated_filename(ofile);
4955 release_bytes(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004956 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004957}
4958
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004959
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004960PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004961"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004962Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004963
Barry Warsaw53699e91996-12-10 23:23:01 +00004964static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004965posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004966{
4967 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004968 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004969 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004970 if (!_PyVerify_fd(fd))
4971 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004972 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004973 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004974 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004975 if (res < 0)
4976 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004977 Py_INCREF(Py_None);
4978 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004979}
4980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004981
Christian Heimesfdab48e2008-01-20 09:06:41 +00004982PyDoc_STRVAR(posix_closerange__doc__,
4983"closerange(fd_low, fd_high)\n\n\
4984Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4985
4986static PyObject *
4987posix_closerange(PyObject *self, PyObject *args)
4988{
4989 int fd_from, fd_to, i;
4990 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4991 return NULL;
4992 Py_BEGIN_ALLOW_THREADS
4993 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004994 if (_PyVerify_fd(i))
4995 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00004996 Py_END_ALLOW_THREADS
4997 Py_RETURN_NONE;
4998}
4999
5000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005001PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005002"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005003Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005004
Barry Warsaw53699e91996-12-10 23:23:01 +00005005static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005006posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005007{
5008 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005009 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005010 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005011 if (!_PyVerify_fd(fd))
5012 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005013 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005014 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005015 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005016 if (fd < 0)
5017 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005018 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005019}
5020
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005022PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005023"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005024Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005025
Barry Warsaw53699e91996-12-10 23:23:01 +00005026static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005027posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005028{
5029 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005030 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005031 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005032 if (!_PyVerify_fd_dup2(fd, fd2))
5033 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005034 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005035 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005036 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005037 if (res < 0)
5038 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005039 Py_INCREF(Py_None);
5040 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005041}
5042
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005044PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005045"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005046Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005047
Barry Warsaw53699e91996-12-10 23:23:01 +00005048static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005049posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005050{
5051 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005052#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005053 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005054#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005055 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005056#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005057 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005058 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005059 return NULL;
5060#ifdef SEEK_SET
5061 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5062 switch (how) {
5063 case 0: how = SEEK_SET; break;
5064 case 1: how = SEEK_CUR; break;
5065 case 2: how = SEEK_END; break;
5066 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005067#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005068
5069#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005070 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005071#else
5072 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005073 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005074#endif
5075 if (PyErr_Occurred())
5076 return NULL;
5077
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005078 if (!_PyVerify_fd(fd))
5079 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005080 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005081#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005082 res = _lseeki64(fd, pos, how);
5083#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005084 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005085#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005086 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005087 if (res < 0)
5088 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005089
5090#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005091 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005092#else
5093 return PyLong_FromLongLong(res);
5094#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005095}
5096
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005098PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005099"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005100Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005101
Barry Warsaw53699e91996-12-10 23:23:01 +00005102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005103posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005104{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005105 int fd, size;
5106 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005107 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005108 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005109 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005110 if (size < 0) {
5111 errno = EINVAL;
5112 return posix_error();
5113 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005114 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005115 if (buffer == NULL)
5116 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005117 if (!_PyVerify_fd(fd))
5118 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005119 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005120 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005121 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005122 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005123 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005124 return posix_error();
5125 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005126 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005127 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005128 return buffer;
5129}
5130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005132PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005133"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005134Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005135
Barry Warsaw53699e91996-12-10 23:23:01 +00005136static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005137posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005138{
Martin v. Löwis423be952008-08-13 15:53:07 +00005139 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005140 int fd;
5141 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005142
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005143 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005144 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005145 if (!_PyVerify_fd(fd))
5146 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005147 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005148 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005149 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005150 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005151 if (size < 0)
5152 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005153 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005154}
5155
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005157PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005158"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005159Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005160
Barry Warsaw53699e91996-12-10 23:23:01 +00005161static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005162posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005163{
5164 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005165 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005166 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005167 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005168 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005169#ifdef __VMS
5170 /* on OpenVMS we must ensure that all bytes are written to the file */
5171 fsync(fd);
5172#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005173 if (!_PyVerify_fd(fd))
5174 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005175 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005176 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005177 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005178 if (res != 0) {
5179#ifdef MS_WINDOWS
5180 return win32_error("fstat", NULL);
5181#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005182 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005183#endif
5184 }
Tim Peters5aa91602002-01-30 05:46:57 +00005185
Martin v. Löwis14694662006-02-03 12:54:16 +00005186 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005187}
5188
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005189PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005190"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005191Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005192connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005193
5194static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005195posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005196{
5197 int fd;
5198 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5199 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005200 if (!_PyVerify_fd(fd))
5201 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005202 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005203}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005204
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005205#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005206PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005207"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005208Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005209
Barry Warsaw53699e91996-12-10 23:23:01 +00005210static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005211posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005212{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005213#if defined(PYOS_OS2)
5214 HFILE read, write;
5215 APIRET rc;
5216
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005217 Py_BEGIN_ALLOW_THREADS
5218 rc = DosCreatePipe( &read, &write, 4096);
5219 Py_END_ALLOW_THREADS
5220 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005221 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005222
5223 return Py_BuildValue("(ii)", read, write);
5224#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005225#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005226 int fds[2];
5227 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005228 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005229 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005230 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005231 if (res != 0)
5232 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005233 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005234#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005235 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005236 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005237 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005238 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005239 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005240 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005241 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005242 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005243 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5244 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005245 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005246#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005247#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005248}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005249#endif /* HAVE_PIPE */
5250
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005251
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005252#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005253PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005254"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005255Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005256
Barry Warsaw53699e91996-12-10 23:23:01 +00005257static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005258posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005259{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005260 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005261 int mode = 0666;
5262 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005263 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005264 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005265 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005266 res = mkfifo(filename, mode);
5267 Py_END_ALLOW_THREADS
5268 if (res < 0)
5269 return posix_error();
5270 Py_INCREF(Py_None);
5271 return Py_None;
5272}
5273#endif
5274
5275
Neal Norwitz11690112002-07-30 01:08:28 +00005276#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005277PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005278"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005279Create a filesystem node (file, device special file or named pipe)\n\
5280named filename. mode specifies both the permissions to use and the\n\
5281type of node to be created, being combined (bitwise OR) with one of\n\
5282S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005283device defines the newly created device special file (probably using\n\
5284os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005285
5286
5287static PyObject *
5288posix_mknod(PyObject *self, PyObject *args)
5289{
5290 char *filename;
5291 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005292 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005293 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005294 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005295 return NULL;
5296 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005297 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005298 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005299 if (res < 0)
5300 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005301 Py_INCREF(Py_None);
5302 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005303}
5304#endif
5305
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005306#ifdef HAVE_DEVICE_MACROS
5307PyDoc_STRVAR(posix_major__doc__,
5308"major(device) -> major number\n\
5309Extracts a device major number from a raw device number.");
5310
5311static PyObject *
5312posix_major(PyObject *self, PyObject *args)
5313{
5314 int device;
5315 if (!PyArg_ParseTuple(args, "i:major", &device))
5316 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005317 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005318}
5319
5320PyDoc_STRVAR(posix_minor__doc__,
5321"minor(device) -> minor number\n\
5322Extracts a device minor number from a raw device number.");
5323
5324static PyObject *
5325posix_minor(PyObject *self, PyObject *args)
5326{
5327 int device;
5328 if (!PyArg_ParseTuple(args, "i:minor", &device))
5329 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005330 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005331}
5332
5333PyDoc_STRVAR(posix_makedev__doc__,
5334"makedev(major, minor) -> device number\n\
5335Composes a raw device number from the major and minor device numbers.");
5336
5337static PyObject *
5338posix_makedev(PyObject *self, PyObject *args)
5339{
5340 int major, minor;
5341 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5342 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005343 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005344}
5345#endif /* device macros */
5346
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005347
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005348#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005349PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005350"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005351Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005352
Barry Warsaw53699e91996-12-10 23:23:01 +00005353static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005354posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005355{
5356 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005357 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005358 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005359 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005360
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005361 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005362 return NULL;
5363
5364#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005365 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005366#else
5367 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005368 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005369#endif
5370 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005371 return NULL;
5372
Barry Warsaw53699e91996-12-10 23:23:01 +00005373 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005374 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005375 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005376 if (res < 0)
5377 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005378 Py_INCREF(Py_None);
5379 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005380}
5381#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005382
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005383#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005384PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005385"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005386Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005387
Fred Drake762e2061999-08-26 17:23:54 +00005388/* Save putenv() parameters as values here, so we can collect them when they
5389 * get re-set with another call for the same key. */
5390static PyObject *posix_putenv_garbage;
5391
Tim Peters5aa91602002-01-30 05:46:57 +00005392static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005393posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005394{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005395#ifdef MS_WINDOWS
5396 wchar_t *s1, *s2;
5397 wchar_t *newenv;
5398#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005399 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005400 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005401 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005402#endif
Fred Drake762e2061999-08-26 17:23:54 +00005403 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005404 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005405
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005406#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005407 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005408 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005409 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005410 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005411#else
5412 if (!PyArg_ParseTuple(args,
5413 "O&O&:putenv",
5414 PyUnicode_FSConverter, &os1,
5415 PyUnicode_FSConverter, &os2))
5416 return NULL;
5417 s1 = bytes2str(os1, 1);
5418 s2 = bytes2str(os2, 1);
5419#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005420
5421#if defined(PYOS_OS2)
5422 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5423 APIRET rc;
5424
Guido van Rossumd48f2521997-12-05 22:19:34 +00005425 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5426 if (rc != NO_ERROR)
5427 return os2_error(rc);
5428
5429 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5430 APIRET rc;
5431
Guido van Rossumd48f2521997-12-05 22:19:34 +00005432 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5433 if (rc != NO_ERROR)
5434 return os2_error(rc);
5435 } else {
5436#endif
Fred Drake762e2061999-08-26 17:23:54 +00005437 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005438 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005439 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005440#ifdef MS_WINDOWS
5441 len = wcslen(s1) + wcslen(s2) + 2;
5442 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5443#else
5444 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005445 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005446#endif
Fred Drake762e2061999-08-26 17:23:54 +00005447 if (newstr == NULL)
5448 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005449#ifdef MS_WINDOWS
5450 newenv = PyUnicode_AsUnicode(newstr);
5451 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5452 if (_wputenv(newenv)) {
5453 Py_DECREF(newstr);
5454 posix_error();
5455 return NULL;
5456 }
5457#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005458 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005459 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5460 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005461 Py_DECREF(newstr);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005462 release_bytes(os1);
5463 release_bytes(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005464 posix_error();
5465 return NULL;
5466 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005467#endif
Fred Drake762e2061999-08-26 17:23:54 +00005468 /* Install the first arg and newstr in posix_putenv_garbage;
5469 * this will cause previous value to be collected. This has to
5470 * happen after the real putenv() call because the old value
5471 * was still accessible until then. */
5472 if (PyDict_SetItem(posix_putenv_garbage,
5473 PyTuple_GET_ITEM(args, 0), newstr)) {
5474 /* really not much we can do; just leak */
5475 PyErr_Clear();
5476 }
5477 else {
5478 Py_DECREF(newstr);
5479 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005480
5481#if defined(PYOS_OS2)
5482 }
5483#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005484#ifndef MS_WINDOWS
5485 release_bytes(os1);
5486 release_bytes(os2);
5487#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005488 Py_INCREF(Py_None);
5489 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005490}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005491#endif /* putenv */
5492
Guido van Rossumc524d952001-10-19 01:31:59 +00005493#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005494PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005495"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005496Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005497
5498static PyObject *
5499posix_unsetenv(PyObject *self, PyObject *args)
5500{
5501 char *s1;
5502
5503 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5504 return NULL;
5505
5506 unsetenv(s1);
5507
5508 /* Remove the key from posix_putenv_garbage;
5509 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005510 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005511 * old value was still accessible until then.
5512 */
5513 if (PyDict_DelItem(posix_putenv_garbage,
5514 PyTuple_GET_ITEM(args, 0))) {
5515 /* really not much we can do; just leak */
5516 PyErr_Clear();
5517 }
5518
5519 Py_INCREF(Py_None);
5520 return Py_None;
5521}
5522#endif /* unsetenv */
5523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005525"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005526Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005527
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005528static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005529posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005530{
5531 int code;
5532 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005533 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005534 return NULL;
5535 message = strerror(code);
5536 if (message == NULL) {
5537 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005538 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005539 return NULL;
5540 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005541 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005542}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005543
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005544
Guido van Rossumc9641791998-08-04 15:26:23 +00005545#ifdef HAVE_SYS_WAIT_H
5546
Fred Drake106c1a02002-04-23 15:58:02 +00005547#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005548PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005549"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005550Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005551
5552static PyObject *
5553posix_WCOREDUMP(PyObject *self, PyObject *args)
5554{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005555 WAIT_TYPE status;
5556 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005557
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005558 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005559 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005560
5561 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005562}
5563#endif /* WCOREDUMP */
5564
5565#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005567"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005568Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005569job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005570
5571static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005572posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005573{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005574 WAIT_TYPE status;
5575 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005576
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005577 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005578 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005579
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005580 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005581}
5582#endif /* WIFCONTINUED */
5583
Guido van Rossumc9641791998-08-04 15:26:23 +00005584#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005585PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005586"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005587Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005588
5589static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005590posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005591{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005592 WAIT_TYPE status;
5593 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005594
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005595 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005596 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005597
Fred Drake106c1a02002-04-23 15:58:02 +00005598 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005599}
5600#endif /* WIFSTOPPED */
5601
5602#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005603PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005604"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005605Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005606
5607static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005608posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005609{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005610 WAIT_TYPE status;
5611 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005612
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005613 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005614 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005615
Fred Drake106c1a02002-04-23 15:58:02 +00005616 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005617}
5618#endif /* WIFSIGNALED */
5619
5620#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005621PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005622"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005623Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005624system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005625
5626static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005627posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005628{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005629 WAIT_TYPE status;
5630 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005631
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005632 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005633 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005634
Fred Drake106c1a02002-04-23 15:58:02 +00005635 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005636}
5637#endif /* WIFEXITED */
5638
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005639#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005640PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005641"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005642Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005643
5644static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005645posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005646{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005647 WAIT_TYPE status;
5648 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005649
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005650 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005651 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005652
Guido van Rossumc9641791998-08-04 15:26:23 +00005653 return Py_BuildValue("i", WEXITSTATUS(status));
5654}
5655#endif /* WEXITSTATUS */
5656
5657#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005658PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005659"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005660Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005661value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005662
5663static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005664posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005665{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005666 WAIT_TYPE status;
5667 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005668
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005669 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005670 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005671
Guido van Rossumc9641791998-08-04 15:26:23 +00005672 return Py_BuildValue("i", WTERMSIG(status));
5673}
5674#endif /* WTERMSIG */
5675
5676#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005677PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005678"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005679Return the signal that stopped the process that provided\n\
5680the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005681
5682static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005683posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005684{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005685 WAIT_TYPE status;
5686 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005687
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005688 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005689 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005690
Guido van Rossumc9641791998-08-04 15:26:23 +00005691 return Py_BuildValue("i", WSTOPSIG(status));
5692}
5693#endif /* WSTOPSIG */
5694
5695#endif /* HAVE_SYS_WAIT_H */
5696
5697
Thomas Wouters477c8d52006-05-27 19:21:47 +00005698#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005699#ifdef _SCO_DS
5700/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5701 needed definitions in sys/statvfs.h */
5702#define _SVID3
5703#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005704#include <sys/statvfs.h>
5705
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005706static PyObject*
5707_pystatvfs_fromstructstatvfs(struct statvfs st) {
5708 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5709 if (v == NULL)
5710 return NULL;
5711
5712#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005713 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5714 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5715 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5716 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5717 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5718 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5719 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5720 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5721 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5722 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005723#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005724 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5725 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005726 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005727 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005728 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005729 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005730 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005731 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005732 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005733 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005734 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005735 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005736 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005737 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005738 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5739 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005740#endif
5741
5742 return v;
5743}
5744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005745PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005746"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005747Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005748
5749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005750posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005751{
5752 int fd, res;
5753 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005754
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005755 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005756 return NULL;
5757 Py_BEGIN_ALLOW_THREADS
5758 res = fstatvfs(fd, &st);
5759 Py_END_ALLOW_THREADS
5760 if (res != 0)
5761 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005762
5763 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005764}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005765#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005766
5767
Thomas Wouters477c8d52006-05-27 19:21:47 +00005768#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005769#include <sys/statvfs.h>
5770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005771PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005772"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005773Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005774
5775static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005776posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005777{
5778 char *path;
5779 int res;
5780 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005781 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005782 return NULL;
5783 Py_BEGIN_ALLOW_THREADS
5784 res = statvfs(path, &st);
5785 Py_END_ALLOW_THREADS
5786 if (res != 0)
5787 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005788
5789 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005790}
5791#endif /* HAVE_STATVFS */
5792
Fred Drakec9680921999-12-13 16:37:25 +00005793/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5794 * It maps strings representing configuration variable names to
5795 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005796 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005797 * rarely-used constants. There are three separate tables that use
5798 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005799 *
5800 * This code is always included, even if none of the interfaces that
5801 * need it are included. The #if hackery needed to avoid it would be
5802 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005803 */
5804struct constdef {
5805 char *name;
5806 long value;
5807};
5808
Fred Drake12c6e2d1999-12-14 21:25:03 +00005809static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005810conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005811 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005812{
Christian Heimes217cfd12007-12-02 14:31:20 +00005813 if (PyLong_Check(arg)) {
5814 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005815 return 1;
5816 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005817 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005818 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005819 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005820 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005821 size_t hi = tablesize;
5822 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005823 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005824 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005825 PyErr_SetString(PyExc_TypeError,
5826 "configuration names must be strings or integers");
5827 return 0;
5828 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005829 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005830 if (confname == NULL)
5831 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005832 while (lo < hi) {
5833 mid = (lo + hi) / 2;
5834 cmp = strcmp(confname, table[mid].name);
5835 if (cmp < 0)
5836 hi = mid;
5837 else if (cmp > 0)
5838 lo = mid + 1;
5839 else {
5840 *valuep = table[mid].value;
5841 return 1;
5842 }
5843 }
5844 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005845 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005846 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005847}
5848
5849
5850#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5851static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005852#ifdef _PC_ABI_AIO_XFER_MAX
5853 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5854#endif
5855#ifdef _PC_ABI_ASYNC_IO
5856 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5857#endif
Fred Drakec9680921999-12-13 16:37:25 +00005858#ifdef _PC_ASYNC_IO
5859 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5860#endif
5861#ifdef _PC_CHOWN_RESTRICTED
5862 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5863#endif
5864#ifdef _PC_FILESIZEBITS
5865 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5866#endif
5867#ifdef _PC_LAST
5868 {"PC_LAST", _PC_LAST},
5869#endif
5870#ifdef _PC_LINK_MAX
5871 {"PC_LINK_MAX", _PC_LINK_MAX},
5872#endif
5873#ifdef _PC_MAX_CANON
5874 {"PC_MAX_CANON", _PC_MAX_CANON},
5875#endif
5876#ifdef _PC_MAX_INPUT
5877 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5878#endif
5879#ifdef _PC_NAME_MAX
5880 {"PC_NAME_MAX", _PC_NAME_MAX},
5881#endif
5882#ifdef _PC_NO_TRUNC
5883 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5884#endif
5885#ifdef _PC_PATH_MAX
5886 {"PC_PATH_MAX", _PC_PATH_MAX},
5887#endif
5888#ifdef _PC_PIPE_BUF
5889 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5890#endif
5891#ifdef _PC_PRIO_IO
5892 {"PC_PRIO_IO", _PC_PRIO_IO},
5893#endif
5894#ifdef _PC_SOCK_MAXBUF
5895 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5896#endif
5897#ifdef _PC_SYNC_IO
5898 {"PC_SYNC_IO", _PC_SYNC_IO},
5899#endif
5900#ifdef _PC_VDISABLE
5901 {"PC_VDISABLE", _PC_VDISABLE},
5902#endif
5903};
5904
Fred Drakec9680921999-12-13 16:37:25 +00005905static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005906conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005907{
5908 return conv_confname(arg, valuep, posix_constants_pathconf,
5909 sizeof(posix_constants_pathconf)
5910 / sizeof(struct constdef));
5911}
5912#endif
5913
5914#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005915PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005916"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005917Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005918If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005919
5920static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005921posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005922{
5923 PyObject *result = NULL;
5924 int name, fd;
5925
Fred Drake12c6e2d1999-12-14 21:25:03 +00005926 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5927 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005928 long limit;
5929
5930 errno = 0;
5931 limit = fpathconf(fd, name);
5932 if (limit == -1 && errno != 0)
5933 posix_error();
5934 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005935 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005936 }
5937 return result;
5938}
5939#endif
5940
5941
5942#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005943PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005944"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005945Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005946If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005947
5948static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005949posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005950{
5951 PyObject *result = NULL;
5952 int name;
5953 char *path;
5954
5955 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5956 conv_path_confname, &name)) {
5957 long limit;
5958
5959 errno = 0;
5960 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005961 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005962 if (errno == EINVAL)
5963 /* could be a path or name problem */
5964 posix_error();
5965 else
5966 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005967 }
Fred Drakec9680921999-12-13 16:37:25 +00005968 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005969 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005970 }
5971 return result;
5972}
5973#endif
5974
5975#ifdef HAVE_CONFSTR
5976static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005977#ifdef _CS_ARCHITECTURE
5978 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5979#endif
5980#ifdef _CS_HOSTNAME
5981 {"CS_HOSTNAME", _CS_HOSTNAME},
5982#endif
5983#ifdef _CS_HW_PROVIDER
5984 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5985#endif
5986#ifdef _CS_HW_SERIAL
5987 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5988#endif
5989#ifdef _CS_INITTAB_NAME
5990 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5991#endif
Fred Drakec9680921999-12-13 16:37:25 +00005992#ifdef _CS_LFS64_CFLAGS
5993 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5994#endif
5995#ifdef _CS_LFS64_LDFLAGS
5996 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5997#endif
5998#ifdef _CS_LFS64_LIBS
5999 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6000#endif
6001#ifdef _CS_LFS64_LINTFLAGS
6002 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6003#endif
6004#ifdef _CS_LFS_CFLAGS
6005 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6006#endif
6007#ifdef _CS_LFS_LDFLAGS
6008 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6009#endif
6010#ifdef _CS_LFS_LIBS
6011 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6012#endif
6013#ifdef _CS_LFS_LINTFLAGS
6014 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6015#endif
Fred Draked86ed291999-12-15 15:34:33 +00006016#ifdef _CS_MACHINE
6017 {"CS_MACHINE", _CS_MACHINE},
6018#endif
Fred Drakec9680921999-12-13 16:37:25 +00006019#ifdef _CS_PATH
6020 {"CS_PATH", _CS_PATH},
6021#endif
Fred Draked86ed291999-12-15 15:34:33 +00006022#ifdef _CS_RELEASE
6023 {"CS_RELEASE", _CS_RELEASE},
6024#endif
6025#ifdef _CS_SRPC_DOMAIN
6026 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6027#endif
6028#ifdef _CS_SYSNAME
6029 {"CS_SYSNAME", _CS_SYSNAME},
6030#endif
6031#ifdef _CS_VERSION
6032 {"CS_VERSION", _CS_VERSION},
6033#endif
Fred Drakec9680921999-12-13 16:37:25 +00006034#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6035 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6036#endif
6037#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6038 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6039#endif
6040#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6041 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6042#endif
6043#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6044 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6045#endif
6046#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6047 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6048#endif
6049#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6050 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6051#endif
6052#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6053 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6054#endif
6055#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6056 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6057#endif
6058#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6059 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6060#endif
6061#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6062 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6063#endif
6064#ifdef _CS_XBS5_LP64_OFF64_LIBS
6065 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6066#endif
6067#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6068 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6069#endif
6070#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6071 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6072#endif
6073#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6074 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6075#endif
6076#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6077 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6078#endif
6079#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6080 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6081#endif
Fred Draked86ed291999-12-15 15:34:33 +00006082#ifdef _MIPS_CS_AVAIL_PROCESSORS
6083 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6084#endif
6085#ifdef _MIPS_CS_BASE
6086 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6087#endif
6088#ifdef _MIPS_CS_HOSTID
6089 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6090#endif
6091#ifdef _MIPS_CS_HW_NAME
6092 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6093#endif
6094#ifdef _MIPS_CS_NUM_PROCESSORS
6095 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6096#endif
6097#ifdef _MIPS_CS_OSREL_MAJ
6098 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6099#endif
6100#ifdef _MIPS_CS_OSREL_MIN
6101 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6102#endif
6103#ifdef _MIPS_CS_OSREL_PATCH
6104 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6105#endif
6106#ifdef _MIPS_CS_OS_NAME
6107 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6108#endif
6109#ifdef _MIPS_CS_OS_PROVIDER
6110 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6111#endif
6112#ifdef _MIPS_CS_PROCESSORS
6113 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6114#endif
6115#ifdef _MIPS_CS_SERIAL
6116 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6117#endif
6118#ifdef _MIPS_CS_VENDOR
6119 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6120#endif
Fred Drakec9680921999-12-13 16:37:25 +00006121};
6122
6123static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006124conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006125{
6126 return conv_confname(arg, valuep, posix_constants_confstr,
6127 sizeof(posix_constants_confstr)
6128 / sizeof(struct constdef));
6129}
6130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006131PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006132"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006133Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006134
6135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006136posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006137{
6138 PyObject *result = NULL;
6139 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006140 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006141
6142 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006143 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006144
Fred Drakec9680921999-12-13 16:37:25 +00006145 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006146 len = confstr(name, buffer, sizeof(buffer));
6147 if (len == 0) {
6148 if (errno) {
6149 posix_error();
6150 }
6151 else {
6152 result = Py_None;
6153 Py_INCREF(Py_None);
6154 }
Fred Drakec9680921999-12-13 16:37:25 +00006155 }
6156 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006157 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006158 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006159 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006160 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006161 }
6162 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006163 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006164 }
6165 }
6166 return result;
6167}
6168#endif
6169
6170
6171#ifdef HAVE_SYSCONF
6172static struct constdef posix_constants_sysconf[] = {
6173#ifdef _SC_2_CHAR_TERM
6174 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6175#endif
6176#ifdef _SC_2_C_BIND
6177 {"SC_2_C_BIND", _SC_2_C_BIND},
6178#endif
6179#ifdef _SC_2_C_DEV
6180 {"SC_2_C_DEV", _SC_2_C_DEV},
6181#endif
6182#ifdef _SC_2_C_VERSION
6183 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6184#endif
6185#ifdef _SC_2_FORT_DEV
6186 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6187#endif
6188#ifdef _SC_2_FORT_RUN
6189 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6190#endif
6191#ifdef _SC_2_LOCALEDEF
6192 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6193#endif
6194#ifdef _SC_2_SW_DEV
6195 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6196#endif
6197#ifdef _SC_2_UPE
6198 {"SC_2_UPE", _SC_2_UPE},
6199#endif
6200#ifdef _SC_2_VERSION
6201 {"SC_2_VERSION", _SC_2_VERSION},
6202#endif
Fred Draked86ed291999-12-15 15:34:33 +00006203#ifdef _SC_ABI_ASYNCHRONOUS_IO
6204 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6205#endif
6206#ifdef _SC_ACL
6207 {"SC_ACL", _SC_ACL},
6208#endif
Fred Drakec9680921999-12-13 16:37:25 +00006209#ifdef _SC_AIO_LISTIO_MAX
6210 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6211#endif
Fred Drakec9680921999-12-13 16:37:25 +00006212#ifdef _SC_AIO_MAX
6213 {"SC_AIO_MAX", _SC_AIO_MAX},
6214#endif
6215#ifdef _SC_AIO_PRIO_DELTA_MAX
6216 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6217#endif
6218#ifdef _SC_ARG_MAX
6219 {"SC_ARG_MAX", _SC_ARG_MAX},
6220#endif
6221#ifdef _SC_ASYNCHRONOUS_IO
6222 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6223#endif
6224#ifdef _SC_ATEXIT_MAX
6225 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6226#endif
Fred Draked86ed291999-12-15 15:34:33 +00006227#ifdef _SC_AUDIT
6228 {"SC_AUDIT", _SC_AUDIT},
6229#endif
Fred Drakec9680921999-12-13 16:37:25 +00006230#ifdef _SC_AVPHYS_PAGES
6231 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6232#endif
6233#ifdef _SC_BC_BASE_MAX
6234 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6235#endif
6236#ifdef _SC_BC_DIM_MAX
6237 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6238#endif
6239#ifdef _SC_BC_SCALE_MAX
6240 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6241#endif
6242#ifdef _SC_BC_STRING_MAX
6243 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6244#endif
Fred Draked86ed291999-12-15 15:34:33 +00006245#ifdef _SC_CAP
6246 {"SC_CAP", _SC_CAP},
6247#endif
Fred Drakec9680921999-12-13 16:37:25 +00006248#ifdef _SC_CHARCLASS_NAME_MAX
6249 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6250#endif
6251#ifdef _SC_CHAR_BIT
6252 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6253#endif
6254#ifdef _SC_CHAR_MAX
6255 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6256#endif
6257#ifdef _SC_CHAR_MIN
6258 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6259#endif
6260#ifdef _SC_CHILD_MAX
6261 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6262#endif
6263#ifdef _SC_CLK_TCK
6264 {"SC_CLK_TCK", _SC_CLK_TCK},
6265#endif
6266#ifdef _SC_COHER_BLKSZ
6267 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6268#endif
6269#ifdef _SC_COLL_WEIGHTS_MAX
6270 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6271#endif
6272#ifdef _SC_DCACHE_ASSOC
6273 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6274#endif
6275#ifdef _SC_DCACHE_BLKSZ
6276 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6277#endif
6278#ifdef _SC_DCACHE_LINESZ
6279 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6280#endif
6281#ifdef _SC_DCACHE_SZ
6282 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6283#endif
6284#ifdef _SC_DCACHE_TBLKSZ
6285 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6286#endif
6287#ifdef _SC_DELAYTIMER_MAX
6288 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6289#endif
6290#ifdef _SC_EQUIV_CLASS_MAX
6291 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6292#endif
6293#ifdef _SC_EXPR_NEST_MAX
6294 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6295#endif
6296#ifdef _SC_FSYNC
6297 {"SC_FSYNC", _SC_FSYNC},
6298#endif
6299#ifdef _SC_GETGR_R_SIZE_MAX
6300 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6301#endif
6302#ifdef _SC_GETPW_R_SIZE_MAX
6303 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6304#endif
6305#ifdef _SC_ICACHE_ASSOC
6306 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6307#endif
6308#ifdef _SC_ICACHE_BLKSZ
6309 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6310#endif
6311#ifdef _SC_ICACHE_LINESZ
6312 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6313#endif
6314#ifdef _SC_ICACHE_SZ
6315 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6316#endif
Fred Draked86ed291999-12-15 15:34:33 +00006317#ifdef _SC_INF
6318 {"SC_INF", _SC_INF},
6319#endif
Fred Drakec9680921999-12-13 16:37:25 +00006320#ifdef _SC_INT_MAX
6321 {"SC_INT_MAX", _SC_INT_MAX},
6322#endif
6323#ifdef _SC_INT_MIN
6324 {"SC_INT_MIN", _SC_INT_MIN},
6325#endif
6326#ifdef _SC_IOV_MAX
6327 {"SC_IOV_MAX", _SC_IOV_MAX},
6328#endif
Fred Draked86ed291999-12-15 15:34:33 +00006329#ifdef _SC_IP_SECOPTS
6330 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6331#endif
Fred Drakec9680921999-12-13 16:37:25 +00006332#ifdef _SC_JOB_CONTROL
6333 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6334#endif
Fred Draked86ed291999-12-15 15:34:33 +00006335#ifdef _SC_KERN_POINTERS
6336 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6337#endif
6338#ifdef _SC_KERN_SIM
6339 {"SC_KERN_SIM", _SC_KERN_SIM},
6340#endif
Fred Drakec9680921999-12-13 16:37:25 +00006341#ifdef _SC_LINE_MAX
6342 {"SC_LINE_MAX", _SC_LINE_MAX},
6343#endif
6344#ifdef _SC_LOGIN_NAME_MAX
6345 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6346#endif
6347#ifdef _SC_LOGNAME_MAX
6348 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6349#endif
6350#ifdef _SC_LONG_BIT
6351 {"SC_LONG_BIT", _SC_LONG_BIT},
6352#endif
Fred Draked86ed291999-12-15 15:34:33 +00006353#ifdef _SC_MAC
6354 {"SC_MAC", _SC_MAC},
6355#endif
Fred Drakec9680921999-12-13 16:37:25 +00006356#ifdef _SC_MAPPED_FILES
6357 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6358#endif
6359#ifdef _SC_MAXPID
6360 {"SC_MAXPID", _SC_MAXPID},
6361#endif
6362#ifdef _SC_MB_LEN_MAX
6363 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6364#endif
6365#ifdef _SC_MEMLOCK
6366 {"SC_MEMLOCK", _SC_MEMLOCK},
6367#endif
6368#ifdef _SC_MEMLOCK_RANGE
6369 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6370#endif
6371#ifdef _SC_MEMORY_PROTECTION
6372 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6373#endif
6374#ifdef _SC_MESSAGE_PASSING
6375 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6376#endif
Fred Draked86ed291999-12-15 15:34:33 +00006377#ifdef _SC_MMAP_FIXED_ALIGNMENT
6378 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6379#endif
Fred Drakec9680921999-12-13 16:37:25 +00006380#ifdef _SC_MQ_OPEN_MAX
6381 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6382#endif
6383#ifdef _SC_MQ_PRIO_MAX
6384 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6385#endif
Fred Draked86ed291999-12-15 15:34:33 +00006386#ifdef _SC_NACLS_MAX
6387 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6388#endif
Fred Drakec9680921999-12-13 16:37:25 +00006389#ifdef _SC_NGROUPS_MAX
6390 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6391#endif
6392#ifdef _SC_NL_ARGMAX
6393 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6394#endif
6395#ifdef _SC_NL_LANGMAX
6396 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6397#endif
6398#ifdef _SC_NL_MSGMAX
6399 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6400#endif
6401#ifdef _SC_NL_NMAX
6402 {"SC_NL_NMAX", _SC_NL_NMAX},
6403#endif
6404#ifdef _SC_NL_SETMAX
6405 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6406#endif
6407#ifdef _SC_NL_TEXTMAX
6408 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6409#endif
6410#ifdef _SC_NPROCESSORS_CONF
6411 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6412#endif
6413#ifdef _SC_NPROCESSORS_ONLN
6414 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6415#endif
Fred Draked86ed291999-12-15 15:34:33 +00006416#ifdef _SC_NPROC_CONF
6417 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6418#endif
6419#ifdef _SC_NPROC_ONLN
6420 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6421#endif
Fred Drakec9680921999-12-13 16:37:25 +00006422#ifdef _SC_NZERO
6423 {"SC_NZERO", _SC_NZERO},
6424#endif
6425#ifdef _SC_OPEN_MAX
6426 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6427#endif
6428#ifdef _SC_PAGESIZE
6429 {"SC_PAGESIZE", _SC_PAGESIZE},
6430#endif
6431#ifdef _SC_PAGE_SIZE
6432 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6433#endif
6434#ifdef _SC_PASS_MAX
6435 {"SC_PASS_MAX", _SC_PASS_MAX},
6436#endif
6437#ifdef _SC_PHYS_PAGES
6438 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6439#endif
6440#ifdef _SC_PII
6441 {"SC_PII", _SC_PII},
6442#endif
6443#ifdef _SC_PII_INTERNET
6444 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6445#endif
6446#ifdef _SC_PII_INTERNET_DGRAM
6447 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6448#endif
6449#ifdef _SC_PII_INTERNET_STREAM
6450 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6451#endif
6452#ifdef _SC_PII_OSI
6453 {"SC_PII_OSI", _SC_PII_OSI},
6454#endif
6455#ifdef _SC_PII_OSI_CLTS
6456 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6457#endif
6458#ifdef _SC_PII_OSI_COTS
6459 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6460#endif
6461#ifdef _SC_PII_OSI_M
6462 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6463#endif
6464#ifdef _SC_PII_SOCKET
6465 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6466#endif
6467#ifdef _SC_PII_XTI
6468 {"SC_PII_XTI", _SC_PII_XTI},
6469#endif
6470#ifdef _SC_POLL
6471 {"SC_POLL", _SC_POLL},
6472#endif
6473#ifdef _SC_PRIORITIZED_IO
6474 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6475#endif
6476#ifdef _SC_PRIORITY_SCHEDULING
6477 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6478#endif
6479#ifdef _SC_REALTIME_SIGNALS
6480 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6481#endif
6482#ifdef _SC_RE_DUP_MAX
6483 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6484#endif
6485#ifdef _SC_RTSIG_MAX
6486 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6487#endif
6488#ifdef _SC_SAVED_IDS
6489 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6490#endif
6491#ifdef _SC_SCHAR_MAX
6492 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6493#endif
6494#ifdef _SC_SCHAR_MIN
6495 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6496#endif
6497#ifdef _SC_SELECT
6498 {"SC_SELECT", _SC_SELECT},
6499#endif
6500#ifdef _SC_SEMAPHORES
6501 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6502#endif
6503#ifdef _SC_SEM_NSEMS_MAX
6504 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6505#endif
6506#ifdef _SC_SEM_VALUE_MAX
6507 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6508#endif
6509#ifdef _SC_SHARED_MEMORY_OBJECTS
6510 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6511#endif
6512#ifdef _SC_SHRT_MAX
6513 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6514#endif
6515#ifdef _SC_SHRT_MIN
6516 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6517#endif
6518#ifdef _SC_SIGQUEUE_MAX
6519 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6520#endif
6521#ifdef _SC_SIGRT_MAX
6522 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6523#endif
6524#ifdef _SC_SIGRT_MIN
6525 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6526#endif
Fred Draked86ed291999-12-15 15:34:33 +00006527#ifdef _SC_SOFTPOWER
6528 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6529#endif
Fred Drakec9680921999-12-13 16:37:25 +00006530#ifdef _SC_SPLIT_CACHE
6531 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6532#endif
6533#ifdef _SC_SSIZE_MAX
6534 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6535#endif
6536#ifdef _SC_STACK_PROT
6537 {"SC_STACK_PROT", _SC_STACK_PROT},
6538#endif
6539#ifdef _SC_STREAM_MAX
6540 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6541#endif
6542#ifdef _SC_SYNCHRONIZED_IO
6543 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6544#endif
6545#ifdef _SC_THREADS
6546 {"SC_THREADS", _SC_THREADS},
6547#endif
6548#ifdef _SC_THREAD_ATTR_STACKADDR
6549 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6550#endif
6551#ifdef _SC_THREAD_ATTR_STACKSIZE
6552 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6553#endif
6554#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6555 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6556#endif
6557#ifdef _SC_THREAD_KEYS_MAX
6558 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6559#endif
6560#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6561 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6562#endif
6563#ifdef _SC_THREAD_PRIO_INHERIT
6564 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6565#endif
6566#ifdef _SC_THREAD_PRIO_PROTECT
6567 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6568#endif
6569#ifdef _SC_THREAD_PROCESS_SHARED
6570 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6571#endif
6572#ifdef _SC_THREAD_SAFE_FUNCTIONS
6573 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6574#endif
6575#ifdef _SC_THREAD_STACK_MIN
6576 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6577#endif
6578#ifdef _SC_THREAD_THREADS_MAX
6579 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6580#endif
6581#ifdef _SC_TIMERS
6582 {"SC_TIMERS", _SC_TIMERS},
6583#endif
6584#ifdef _SC_TIMER_MAX
6585 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6586#endif
6587#ifdef _SC_TTY_NAME_MAX
6588 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6589#endif
6590#ifdef _SC_TZNAME_MAX
6591 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6592#endif
6593#ifdef _SC_T_IOV_MAX
6594 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6595#endif
6596#ifdef _SC_UCHAR_MAX
6597 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6598#endif
6599#ifdef _SC_UINT_MAX
6600 {"SC_UINT_MAX", _SC_UINT_MAX},
6601#endif
6602#ifdef _SC_UIO_MAXIOV
6603 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6604#endif
6605#ifdef _SC_ULONG_MAX
6606 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6607#endif
6608#ifdef _SC_USHRT_MAX
6609 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6610#endif
6611#ifdef _SC_VERSION
6612 {"SC_VERSION", _SC_VERSION},
6613#endif
6614#ifdef _SC_WORD_BIT
6615 {"SC_WORD_BIT", _SC_WORD_BIT},
6616#endif
6617#ifdef _SC_XBS5_ILP32_OFF32
6618 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6619#endif
6620#ifdef _SC_XBS5_ILP32_OFFBIG
6621 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6622#endif
6623#ifdef _SC_XBS5_LP64_OFF64
6624 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6625#endif
6626#ifdef _SC_XBS5_LPBIG_OFFBIG
6627 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6628#endif
6629#ifdef _SC_XOPEN_CRYPT
6630 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6631#endif
6632#ifdef _SC_XOPEN_ENH_I18N
6633 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6634#endif
6635#ifdef _SC_XOPEN_LEGACY
6636 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6637#endif
6638#ifdef _SC_XOPEN_REALTIME
6639 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6640#endif
6641#ifdef _SC_XOPEN_REALTIME_THREADS
6642 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6643#endif
6644#ifdef _SC_XOPEN_SHM
6645 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6646#endif
6647#ifdef _SC_XOPEN_UNIX
6648 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6649#endif
6650#ifdef _SC_XOPEN_VERSION
6651 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6652#endif
6653#ifdef _SC_XOPEN_XCU_VERSION
6654 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6655#endif
6656#ifdef _SC_XOPEN_XPG2
6657 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6658#endif
6659#ifdef _SC_XOPEN_XPG3
6660 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6661#endif
6662#ifdef _SC_XOPEN_XPG4
6663 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6664#endif
6665};
6666
6667static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006668conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006669{
6670 return conv_confname(arg, valuep, posix_constants_sysconf,
6671 sizeof(posix_constants_sysconf)
6672 / sizeof(struct constdef));
6673}
6674
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006675PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006676"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006677Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006678
6679static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006680posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006681{
6682 PyObject *result = NULL;
6683 int name;
6684
6685 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6686 int value;
6687
6688 errno = 0;
6689 value = sysconf(name);
6690 if (value == -1 && errno != 0)
6691 posix_error();
6692 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006693 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006694 }
6695 return result;
6696}
6697#endif
6698
6699
Fred Drakebec628d1999-12-15 18:31:10 +00006700/* This code is used to ensure that the tables of configuration value names
6701 * are in sorted order as required by conv_confname(), and also to build the
6702 * the exported dictionaries that are used to publish information about the
6703 * names available on the host platform.
6704 *
6705 * Sorting the table at runtime ensures that the table is properly ordered
6706 * when used, even for platforms we're not able to test on. It also makes
6707 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006708 */
Fred Drakebec628d1999-12-15 18:31:10 +00006709
6710static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006711cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006712{
6713 const struct constdef *c1 =
6714 (const struct constdef *) v1;
6715 const struct constdef *c2 =
6716 (const struct constdef *) v2;
6717
6718 return strcmp(c1->name, c2->name);
6719}
6720
6721static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006722setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006723 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006724{
Fred Drakebec628d1999-12-15 18:31:10 +00006725 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006726 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006727
6728 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6729 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006730 if (d == NULL)
6731 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006732
Barry Warsaw3155db32000-04-13 15:20:40 +00006733 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006734 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006735 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6736 Py_XDECREF(o);
6737 Py_DECREF(d);
6738 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006739 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006740 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006741 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006742 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006743}
6744
Fred Drakebec628d1999-12-15 18:31:10 +00006745/* Return -1 on failure, 0 on success. */
6746static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006747setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006748{
6749#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006750 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006751 sizeof(posix_constants_pathconf)
6752 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006753 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006754 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006755#endif
6756#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006757 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006758 sizeof(posix_constants_confstr)
6759 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006760 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006761 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006762#endif
6763#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006764 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006765 sizeof(posix_constants_sysconf)
6766 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006767 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006768 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006769#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006770 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006771}
Fred Draked86ed291999-12-15 15:34:33 +00006772
6773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006774PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006775"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006776Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006777in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006778
6779static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006780posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006781{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006782 abort();
6783 /*NOTREACHED*/
6784 Py_FatalError("abort() called from Python code didn't abort!");
6785 return NULL;
6786}
Fred Drakebec628d1999-12-15 18:31:10 +00006787
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006788#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006789PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006790"startfile(filepath [, operation]) - Start a file with its associated\n\
6791application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006792\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006793When \"operation\" is not specified or \"open\", this acts like\n\
6794double-clicking the file in Explorer, or giving the file name as an\n\
6795argument to the DOS \"start\" command: the file is opened with whatever\n\
6796application (if any) its extension is associated.\n\
6797When another \"operation\" is given, it specifies what should be done with\n\
6798the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006799\n\
6800startfile returns as soon as the associated application is launched.\n\
6801There is no option to wait for the application to close, and no way\n\
6802to retrieve the application's exit status.\n\
6803\n\
6804The filepath is relative to the current directory. If you want to use\n\
6805an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006806the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006807
6808static PyObject *
6809win32_startfile(PyObject *self, PyObject *args)
6810{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006811 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006812 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006813 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006814 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006815
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006816 if (unicode_file_names()) {
6817 PyObject *unipath, *woperation = NULL;
6818 if (!PyArg_ParseTuple(args, "U|s:startfile",
6819 &unipath, &operation)) {
6820 PyErr_Clear();
6821 goto normal;
6822 }
6823
6824
6825 if (operation) {
6826 woperation = PyUnicode_DecodeASCII(operation,
6827 strlen(operation), NULL);
6828 if (!woperation) {
6829 PyErr_Clear();
6830 operation = NULL;
6831 goto normal;
6832 }
6833 }
6834
6835 Py_BEGIN_ALLOW_THREADS
6836 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6837 PyUnicode_AS_UNICODE(unipath),
6838 NULL, NULL, SW_SHOWNORMAL);
6839 Py_END_ALLOW_THREADS
6840
6841 Py_XDECREF(woperation);
6842 if (rc <= (HINSTANCE)32) {
6843 PyObject *errval = win32_error_unicode("startfile",
6844 PyUnicode_AS_UNICODE(unipath));
6845 return errval;
6846 }
6847 Py_INCREF(Py_None);
6848 return Py_None;
6849 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006850
6851normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006852 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6853 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006854 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006855 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006856 filepath = bytes2str(ofilepath, 1);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006857 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006858 rc = ShellExecute((HWND)0, operation, filepath,
6859 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006860 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006861 if (rc <= (HINSTANCE)32) {
6862 PyObject *errval = win32_error("startfile", filepath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006863 release_bytes(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006864 return errval;
6865 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00006866 release_bytes(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006867 Py_INCREF(Py_None);
6868 return Py_None;
6869}
6870#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006871
Martin v. Löwis438b5342002-12-27 10:16:42 +00006872#ifdef HAVE_GETLOADAVG
6873PyDoc_STRVAR(posix_getloadavg__doc__,
6874"getloadavg() -> (float, float, float)\n\n\
6875Return the number of processes in the system run queue averaged over\n\
6876the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6877was unobtainable");
6878
6879static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006880posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006881{
6882 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006883 if (getloadavg(loadavg, 3)!=3) {
6884 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6885 return NULL;
6886 } else
6887 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6888}
6889#endif
6890
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006891#ifdef MS_WINDOWS
6892
6893PyDoc_STRVAR(win32_urandom__doc__,
6894"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006895Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006896
6897typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6898 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6899 DWORD dwFlags );
6900typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6901 BYTE *pbBuffer );
6902
6903static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006904/* This handle is never explicitly released. Instead, the operating
6905 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006906static HCRYPTPROV hCryptProv = 0;
6907
Tim Peters4ad82172004-08-30 17:02:04 +00006908static PyObject*
6909win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006910{
Tim Petersd3115382004-08-30 17:36:46 +00006911 int howMany;
6912 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006913
Tim Peters4ad82172004-08-30 17:02:04 +00006914 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006915 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006916 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006917 if (howMany < 0)
6918 return PyErr_Format(PyExc_ValueError,
6919 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006920
Tim Peters4ad82172004-08-30 17:02:04 +00006921 if (hCryptProv == 0) {
6922 HINSTANCE hAdvAPI32 = NULL;
6923 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006924
Tim Peters4ad82172004-08-30 17:02:04 +00006925 /* Obtain handle to the DLL containing CryptoAPI
6926 This should not fail */
6927 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6928 if(hAdvAPI32 == NULL)
6929 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006930
Tim Peters4ad82172004-08-30 17:02:04 +00006931 /* Obtain pointers to the CryptoAPI functions
6932 This will fail on some early versions of Win95 */
6933 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6934 hAdvAPI32,
6935 "CryptAcquireContextA");
6936 if (pCryptAcquireContext == NULL)
6937 return PyErr_Format(PyExc_NotImplementedError,
6938 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006939
Tim Peters4ad82172004-08-30 17:02:04 +00006940 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6941 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006942 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006943 return PyErr_Format(PyExc_NotImplementedError,
6944 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006945
Tim Peters4ad82172004-08-30 17:02:04 +00006946 /* Acquire context */
6947 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6948 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6949 return win32_error("CryptAcquireContext", NULL);
6950 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006951
Tim Peters4ad82172004-08-30 17:02:04 +00006952 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006953 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006954 if (result != NULL) {
6955 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006956 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006957 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006958 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006959 Py_DECREF(result);
6960 return win32_error("CryptGenRandom", NULL);
6961 }
Tim Peters4ad82172004-08-30 17:02:04 +00006962 }
Tim Petersd3115382004-08-30 17:36:46 +00006963 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006964}
6965#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006966
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006967PyDoc_STRVAR(device_encoding__doc__,
6968"device_encoding(fd) -> str\n\n\
6969Return a string describing the encoding of the device\n\
6970if the output is a terminal; else return None.");
6971
6972static PyObject *
6973device_encoding(PyObject *self, PyObject *args)
6974{
6975 int fd;
6976 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6977 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006978 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006979 Py_INCREF(Py_None);
6980 return Py_None;
6981 }
6982#if defined(MS_WINDOWS) || defined(MS_WIN64)
6983 if (fd == 0) {
6984 char buf[100];
6985 sprintf(buf, "cp%d", GetConsoleCP());
6986 return PyUnicode_FromString(buf);
6987 }
6988 if (fd == 1 || fd == 2) {
6989 char buf[100];
6990 sprintf(buf, "cp%d", GetConsoleOutputCP());
6991 return PyUnicode_FromString(buf);
6992 }
6993#elif defined(CODESET)
6994 {
6995 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006996 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006997 return PyUnicode_FromString(codeset);
6998 }
6999#endif
7000 Py_INCREF(Py_None);
7001 return Py_None;
7002}
7003
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007004#ifdef __VMS
7005/* Use openssl random routine */
7006#include <openssl/rand.h>
7007PyDoc_STRVAR(vms_urandom__doc__,
7008"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007009Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007010
7011static PyObject*
7012vms_urandom(PyObject *self, PyObject *args)
7013{
7014 int howMany;
7015 PyObject* result;
7016
7017 /* Read arguments */
7018 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7019 return NULL;
7020 if (howMany < 0)
7021 return PyErr_Format(PyExc_ValueError,
7022 "negative argument not allowed");
7023
7024 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00007025 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007026 if (result != NULL) {
7027 /* Get random data */
7028 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00007029 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007030 howMany) < 0) {
7031 Py_DECREF(result);
7032 return PyErr_Format(PyExc_ValueError,
7033 "RAND_pseudo_bytes");
7034 }
7035 }
7036 return result;
7037}
7038#endif
7039
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007040static PyMethodDef posix_methods[] = {
7041 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7042#ifdef HAVE_TTYNAME
7043 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7044#endif
7045 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007046#ifdef HAVE_CHFLAGS
7047 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7048#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007049 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007050#ifdef HAVE_FCHMOD
7051 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
7052#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007053#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007054 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007055#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007056#ifdef HAVE_LCHMOD
7057 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
7058#endif /* HAVE_LCHMOD */
7059#ifdef HAVE_FCHOWN
7060 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
7061#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007062#ifdef HAVE_LCHFLAGS
7063 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
7064#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007065#ifdef HAVE_LCHOWN
7066 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7067#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007068#ifdef HAVE_CHROOT
7069 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7070#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007071#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007072 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007073#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007074#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00007075 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7076 METH_NOARGS, posix_getcwd__doc__},
7077 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7078 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007079#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007080#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007081 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007082#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007083 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7084 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7085 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007086#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007087 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007088#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007089#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007090 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007091#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007092 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7093 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7094 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007095 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007096#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007097 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007098#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007099#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007100 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007101#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007102 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007103#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007104 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007105#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007106 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7107 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7108 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007109#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007110 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007111#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007112 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007113#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007114 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7115 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007116#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007117#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007118 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7119 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007120#if defined(PYOS_OS2)
7121 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7122 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7123#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007124#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007125#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007126 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007127#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007128#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007129 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007130#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007131#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007132 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007133#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007134#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007135 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007136#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007137#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007138 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007139#endif /* HAVE_GETEGID */
7140#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007141 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007142#endif /* HAVE_GETEUID */
7143#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007144 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007145#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007146#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007147 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007148#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007149 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007150#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007151 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007152#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007153#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007154 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007155#endif /* HAVE_GETPPID */
7156#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007157 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007158#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007159#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007160 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007161#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007162#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007163 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007164#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007165#ifdef HAVE_KILLPG
7166 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7167#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007168#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007169 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007170#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007171#ifdef MS_WINDOWS
7172 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7173#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007174#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007175 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007176#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007177#ifdef HAVE_SETEUID
7178 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7179#endif /* HAVE_SETEUID */
7180#ifdef HAVE_SETEGID
7181 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7182#endif /* HAVE_SETEGID */
7183#ifdef HAVE_SETREUID
7184 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7185#endif /* HAVE_SETREUID */
7186#ifdef HAVE_SETREGID
7187 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7188#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007189#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007190 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007191#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007192#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007193 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007194#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007195#ifdef HAVE_GETPGID
7196 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7197#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007198#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007199 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007200#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007201#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007202 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007203#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007204#ifdef HAVE_WAIT3
7205 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7206#endif /* HAVE_WAIT3 */
7207#ifdef HAVE_WAIT4
7208 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7209#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007210#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007211 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007212#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007213#ifdef HAVE_GETSID
7214 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7215#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007216#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007217 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007218#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007219#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007220 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007221#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007222#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007223 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007224#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007225#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007226 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007227#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007228 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7229 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007230 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007231 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007232 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7233 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7234 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7235 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7236 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7237 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007238 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007239#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007240 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007241#endif
7242#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007243 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007244#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007245#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007246 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7247#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007248#ifdef HAVE_DEVICE_MACROS
7249 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7250 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7251 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7252#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007253#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007254 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007255#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007256#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007257 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007258#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007259#ifdef HAVE_UNSETENV
7260 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7261#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007262 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007263#ifdef HAVE_FCHDIR
7264 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7265#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007266#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007267 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007268#endif
7269#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007270 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007271#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007272#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007273#ifdef WCOREDUMP
7274 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7275#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007276#ifdef WIFCONTINUED
7277 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7278#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007279#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007280 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007281#endif /* WIFSTOPPED */
7282#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007283 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007284#endif /* WIFSIGNALED */
7285#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007286 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007287#endif /* WIFEXITED */
7288#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007289 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007290#endif /* WEXITSTATUS */
7291#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007292 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007293#endif /* WTERMSIG */
7294#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007295 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007296#endif /* WSTOPSIG */
7297#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007298#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007299 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007300#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007301#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007302 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007303#endif
Fred Drakec9680921999-12-13 16:37:25 +00007304#ifdef HAVE_CONFSTR
7305 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7306#endif
7307#ifdef HAVE_SYSCONF
7308 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7309#endif
7310#ifdef HAVE_FPATHCONF
7311 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7312#endif
7313#ifdef HAVE_PATHCONF
7314 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7315#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007316 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007317#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007318 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7319#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007320#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007321 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007322#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007323 #ifdef MS_WINDOWS
7324 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7325 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007326 #ifdef __VMS
7327 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7328 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007329 {NULL, NULL} /* Sentinel */
7330};
7331
7332
Barry Warsaw4a342091996-12-19 23:50:02 +00007333static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007334ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007335{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007336 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007337}
7338
Guido van Rossumd48f2521997-12-05 22:19:34 +00007339#if defined(PYOS_OS2)
7340/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007341static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007342{
7343 APIRET rc;
7344 ULONG values[QSV_MAX+1];
7345 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007346 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007347
7348 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007349 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007350 Py_END_ALLOW_THREADS
7351
7352 if (rc != NO_ERROR) {
7353 os2_error(rc);
7354 return -1;
7355 }
7356
Fred Drake4d1e64b2002-04-15 19:40:07 +00007357 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7358 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7359 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7360 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7361 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7362 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7363 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007364
7365 switch (values[QSV_VERSION_MINOR]) {
7366 case 0: ver = "2.00"; break;
7367 case 10: ver = "2.10"; break;
7368 case 11: ver = "2.11"; break;
7369 case 30: ver = "3.00"; break;
7370 case 40: ver = "4.00"; break;
7371 case 50: ver = "5.00"; break;
7372 default:
Tim Peters885d4572001-11-28 20:27:42 +00007373 PyOS_snprintf(tmp, sizeof(tmp),
7374 "%d-%d", values[QSV_VERSION_MAJOR],
7375 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007376 ver = &tmp[0];
7377 }
7378
7379 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007380 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007381 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007382
7383 /* Add Indicator of Which Drive was Used to Boot the System */
7384 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7385 tmp[1] = ':';
7386 tmp[2] = '\0';
7387
Fred Drake4d1e64b2002-04-15 19:40:07 +00007388 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007389}
7390#endif
7391
Barry Warsaw4a342091996-12-19 23:50:02 +00007392static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007393all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007394{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007395#ifdef F_OK
7396 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007397#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007398#ifdef R_OK
7399 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007400#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007401#ifdef W_OK
7402 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007403#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007404#ifdef X_OK
7405 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007406#endif
Fred Drakec9680921999-12-13 16:37:25 +00007407#ifdef NGROUPS_MAX
7408 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7409#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007410#ifdef TMP_MAX
7411 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7412#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007413#ifdef WCONTINUED
7414 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7415#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007416#ifdef WNOHANG
7417 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007418#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007419#ifdef WUNTRACED
7420 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7421#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007422#ifdef O_RDONLY
7423 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7424#endif
7425#ifdef O_WRONLY
7426 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7427#endif
7428#ifdef O_RDWR
7429 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7430#endif
7431#ifdef O_NDELAY
7432 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7433#endif
7434#ifdef O_NONBLOCK
7435 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7436#endif
7437#ifdef O_APPEND
7438 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7439#endif
7440#ifdef O_DSYNC
7441 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7442#endif
7443#ifdef O_RSYNC
7444 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7445#endif
7446#ifdef O_SYNC
7447 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7448#endif
7449#ifdef O_NOCTTY
7450 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7451#endif
7452#ifdef O_CREAT
7453 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7454#endif
7455#ifdef O_EXCL
7456 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7457#endif
7458#ifdef O_TRUNC
7459 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7460#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007461#ifdef O_BINARY
7462 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7463#endif
7464#ifdef O_TEXT
7465 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7466#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007467#ifdef O_LARGEFILE
7468 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7469#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007470#ifdef O_SHLOCK
7471 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7472#endif
7473#ifdef O_EXLOCK
7474 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7475#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007476
Tim Peters5aa91602002-01-30 05:46:57 +00007477/* MS Windows */
7478#ifdef O_NOINHERIT
7479 /* Don't inherit in child processes. */
7480 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7481#endif
7482#ifdef _O_SHORT_LIVED
7483 /* Optimize for short life (keep in memory). */
7484 /* MS forgot to define this one with a non-underscore form too. */
7485 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7486#endif
7487#ifdef O_TEMPORARY
7488 /* Automatically delete when last handle is closed. */
7489 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7490#endif
7491#ifdef O_RANDOM
7492 /* Optimize for random access. */
7493 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7494#endif
7495#ifdef O_SEQUENTIAL
7496 /* Optimize for sequential access. */
7497 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7498#endif
7499
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007500/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007501#ifdef O_ASYNC
7502 /* Send a SIGIO signal whenever input or output
7503 becomes available on file descriptor */
7504 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7505#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007506#ifdef O_DIRECT
7507 /* Direct disk access. */
7508 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7509#endif
7510#ifdef O_DIRECTORY
7511 /* Must be a directory. */
7512 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7513#endif
7514#ifdef O_NOFOLLOW
7515 /* Do not follow links. */
7516 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7517#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007518#ifdef O_NOATIME
7519 /* Do not update the access time. */
7520 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7521#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007522
Barry Warsaw5676bd12003-01-07 20:57:09 +00007523 /* These come from sysexits.h */
7524#ifdef EX_OK
7525 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007526#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007527#ifdef EX_USAGE
7528 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007529#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007530#ifdef EX_DATAERR
7531 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007532#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007533#ifdef EX_NOINPUT
7534 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007535#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007536#ifdef EX_NOUSER
7537 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007538#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007539#ifdef EX_NOHOST
7540 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007541#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007542#ifdef EX_UNAVAILABLE
7543 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007544#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007545#ifdef EX_SOFTWARE
7546 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007547#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007548#ifdef EX_OSERR
7549 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007550#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007551#ifdef EX_OSFILE
7552 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007553#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007554#ifdef EX_CANTCREAT
7555 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007556#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007557#ifdef EX_IOERR
7558 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007559#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007560#ifdef EX_TEMPFAIL
7561 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007562#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007563#ifdef EX_PROTOCOL
7564 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007565#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007566#ifdef EX_NOPERM
7567 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007568#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007569#ifdef EX_CONFIG
7570 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007571#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007572#ifdef EX_NOTFOUND
7573 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007574#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007575
Guido van Rossum246bc171999-02-01 23:54:31 +00007576#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007577#if defined(PYOS_OS2) && defined(PYCC_GCC)
7578 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7579 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7580 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7581 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7582 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7583 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7584 if (ins(d, "P_PM", (long)P_PM)) return -1;
7585 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7586 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7587 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7588 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7589 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7590 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7591 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7592 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7593 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7594 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7595 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7596 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7597 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7598#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007599 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7600 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7601 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7602 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7603 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007604#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007605#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007606
Guido van Rossumd48f2521997-12-05 22:19:34 +00007607#if defined(PYOS_OS2)
7608 if (insertvalues(d)) return -1;
7609#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007610 return 0;
7611}
7612
7613
Tim Peters5aa91602002-01-30 05:46:57 +00007614#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007615#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007616#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007617
7618#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007619#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007620#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007621
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007622#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007623#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007624#define MODNAME "posix"
7625#endif
7626
Martin v. Löwis1a214512008-06-11 05:26:20 +00007627static struct PyModuleDef posixmodule = {
7628 PyModuleDef_HEAD_INIT,
7629 MODNAME,
7630 posix__doc__,
7631 -1,
7632 posix_methods,
7633 NULL,
7634 NULL,
7635 NULL,
7636 NULL
7637};
7638
7639
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007640PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007641INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007642{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007643 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007644
Martin v. Löwis1a214512008-06-11 05:26:20 +00007645 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007646 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007647 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007648
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007649 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007650 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007651 Py_XINCREF(v);
7652 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007653 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007654 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007655
Fred Drake4d1e64b2002-04-15 19:40:07 +00007656 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007657 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007658
Fred Drake4d1e64b2002-04-15 19:40:07 +00007659 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007660 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007661
Fred Drake4d1e64b2002-04-15 19:40:07 +00007662 Py_INCREF(PyExc_OSError);
7663 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007664
Guido van Rossumb3d39562000-01-31 18:41:26 +00007665#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007666 if (posix_putenv_garbage == NULL)
7667 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007668#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007669
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007670 if (!initialized) {
7671 stat_result_desc.name = MODNAME ".stat_result";
7672 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7673 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7674 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7675 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7676 structseq_new = StatResultType.tp_new;
7677 StatResultType.tp_new = statresult_new;
7678
7679 statvfs_result_desc.name = MODNAME ".statvfs_result";
7680 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007681#ifdef NEED_TICKS_PER_SECOND
7682# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7683 ticks_per_second = sysconf(_SC_CLK_TCK);
7684# elif defined(HZ)
7685 ticks_per_second = HZ;
7686# else
7687 ticks_per_second = 60; /* magic fallback value; may be bogus */
7688# endif
7689#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007690 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007691 Py_INCREF((PyObject*) &StatResultType);
7692 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007693 Py_INCREF((PyObject*) &StatVFSResultType);
7694 PyModule_AddObject(m, "statvfs_result",
7695 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007696 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007697
7698#ifdef __APPLE__
7699 /*
7700 * Step 2 of weak-linking support on Mac OS X.
7701 *
7702 * The code below removes functions that are not available on the
7703 * currently active platform.
7704 *
7705 * This block allow one to use a python binary that was build on
7706 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7707 * OSX 10.4.
7708 */
7709#ifdef HAVE_FSTATVFS
7710 if (fstatvfs == NULL) {
7711 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007712 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007713 }
7714 }
7715#endif /* HAVE_FSTATVFS */
7716
7717#ifdef HAVE_STATVFS
7718 if (statvfs == NULL) {
7719 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007720 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007721 }
7722 }
7723#endif /* HAVE_STATVFS */
7724
7725# ifdef HAVE_LCHOWN
7726 if (lchown == NULL) {
7727 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007728 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007729 }
7730 }
7731#endif /* HAVE_LCHOWN */
7732
7733
7734#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007735 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007736
Guido van Rossumb6775db1994-08-01 11:34:53 +00007737}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007738
7739#ifdef __cplusplus
7740}
7741#endif