blob: 814c9bad1b5bbc088f73e9f004fd61eede23b585 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
72#include <sys/wait.h> /* For WNOHANG */
73#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
107#define HAVE_SYSTEM 1
108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
114#ifdef __BORLANDC__ /* Borland compiler */
115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#else
122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000128#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define HAVE_FSYNC 1
130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#else /* all other compilers */
135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_SYSTEM 1
151#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000189/*#ifdef HAVE_FCHMOD
190extern int fchmod(int, mode_t);
191#endif*/
192/*#ifdef HAVE_LCHMOD
193extern int lchmod(const char *, mode_t);
194#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int chown(const char *, uid_t, gid_t);
196extern char *getcwd(char *, int);
197extern char *strerror(int);
198extern int link(const char *, const char *);
199extern int rename(const char *, const char *);
200extern int stat(const char *, struct stat *);
201extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000266#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000268#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
Guido van Rossumd48f2521997-12-05 22:19:34 +0000271#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000276#if defined(PATH_MAX) && PATH_MAX > 1024
277#define MAXPATHLEN PATH_MAX
278#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000279#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000281#endif /* MAXPATHLEN */
282
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000283#ifdef UNION_WAIT
284/* Emulate some macros on systems that have a union instead of macros */
285
286#ifndef WIFEXITED
287#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
288#endif
289
290#ifndef WEXITSTATUS
291#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
292#endif
293
294#ifndef WTERMSIG
295#define WTERMSIG(u_wait) ((u_wait).w_termsig)
296#endif
297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298#define WAIT_TYPE union wait
299#define WAIT_STATUS_INT(s) (s.w_status)
300
301#else /* !UNION_WAIT */
302#define WAIT_TYPE int
303#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000304#endif /* UNION_WAIT */
305
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000306/* Issue #1983: pid_t can be longer than a C long on some systems */
Antoine Pitrou7852c422009-05-24 11:58:35 +0000307#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000308#define PARSE_PID "i"
309#define PyLong_FromPid PyLong_FromLong
310#define PyLong_AsPid PyLong_AsLong
311#elif SIZEOF_PID_T == SIZEOF_LONG
312#define PARSE_PID "l"
313#define PyLong_FromPid PyLong_FromLong
314#define PyLong_AsPid PyLong_AsLong
315#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
316#define PARSE_PID "L"
317#define PyLong_FromPid PyLong_FromLongLong
318#define PyLong_AsPid PyLong_AsLongLong
319#else
320#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
Antoine Pitrouc3ee1662009-05-23 16:02:33 +0000321#endif /* SIZEOF_PID_T */
322
Greg Wardb48bc172000-03-01 21:51:56 +0000323/* Don't use the "_r" form if we don't need it (also, won't have a
324 prototype for it, at least on Solaris -- maybe others as well?). */
325#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
326#define USE_CTERMID_R
327#endif
328
Fred Drake699f3522000-06-29 21:12:41 +0000329/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000330#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000331#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000332# define STAT win32_stat
333# define FSTAT win32_fstat
334# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000335#else
336# define STAT stat
337# define FSTAT fstat
338# define STRUCT_STAT struct stat
339#endif
340
Tim Peters11b23062003-04-23 02:39:17 +0000341#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000342#include <sys/mkdev.h>
343#else
344#if defined(MAJOR_IN_SYSMACROS)
345#include <sys/sysmacros.h>
346#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000347#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
348#include <sys/mkdev.h>
349#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000350#endif
Fred Drake699f3522000-06-29 21:12:41 +0000351
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000352#if defined _MSC_VER && _MSC_VER >= 1400
353/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
354 * valid and throw an assertion if it isn't.
355 * Normally, an invalid fd is likely to be a C program error and therefore
356 * an assertion can be useful, but it does contradict the POSIX standard
357 * which for write(2) states:
358 * "Otherwise, -1 shall be returned and errno set to indicate the error."
359 * "[EBADF] The fildes argument is not a valid file descriptor open for
360 * writing."
361 * Furthermore, python allows the user to enter any old integer
362 * as a fd and should merely raise a python exception on error.
363 * The Microsoft CRT doesn't provide an official way to check for the
364 * validity of a file descriptor, but we can emulate its internal behaviour
365 * by using the exported __pinfo data member and knowledge of the
366 * internal structures involved.
367 * The structures below must be updated for each version of visual studio
368 * according to the file internal.h in the CRT source, until MS comes
369 * up with a less hacky way to do this.
370 * (all of this is to avoid globally modifying the CRT behaviour using
371 * _set_invalid_parameter_handler() and _CrtSetReportMode())
372 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000373/* The actual size of the structure is determined at runtime.
374 * Only the first items must be present.
375 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000376typedef struct {
377 intptr_t osfhnd;
378 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000381extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000382#define IOINFO_L2E 5
383#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
384#define IOINFO_ARRAYS 64
385#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
386#define FOPEN 0x01
387#define _NO_CONSOLE_FILENO (intptr_t)-2
388
389/* This function emulates what the windows CRT does to validate file handles */
390int
391_PyVerify_fd(int fd)
392{
393 const int i1 = fd >> IOINFO_L2E;
394 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000395
396 static int sizeof_ioinfo = 0;
397
398 /* Determine the actual size of the ioinfo structure,
399 * as used by the CRT loaded in memory
400 */
401 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
402 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
403 }
404 if (sizeof_ioinfo == 0) {
405 /* This should not happen... */
406 goto fail;
407 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408
409 /* See that it isn't a special CLEAR fileno */
410 if (fd != _NO_CONSOLE_FILENO) {
411 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
412 * we check pointer validity and other info
413 */
414 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
415 /* finally, check that the file is open */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000416 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
417 if (info->osfile & FOPEN) {
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000418 return 1;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000419 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000420 }
421 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000422 fail:
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000423 errno = EBADF;
424 return 0;
425}
426
427/* the special case of checking dup2. The target fd must be in a sensible range */
428static int
429_PyVerify_fd_dup2(int fd1, int fd2)
430{
431 if (!_PyVerify_fd(fd1))
432 return 0;
433 if (fd2 == _NO_CONSOLE_FILENO)
434 return 0;
435 if ((unsigned)fd2 < _NHANDLE_)
436 return 1;
437 else
438 return 0;
439}
440#else
441/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
442#define _PyVerify_fd_dup2(A, B) (1)
443#endif
444
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000446#ifdef WITH_NEXT_FRAMEWORK
447/* On Darwin/MacOSX a shared library or framework has no access to
448** environ directly, we must obtain it with _NSGetEnviron().
449*/
450#include <crt_externs.h>
451static char **environ;
452#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000454#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455
Barry Warsaw53699e91996-12-10 23:23:01 +0000456static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000457convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458{
Barry Warsaw53699e91996-12-10 23:23:01 +0000459 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000460#ifdef MS_WINDOWS
461 wchar_t **e;
462#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000464#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000465 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466 if (d == NULL)
467 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000468#ifdef WITH_NEXT_FRAMEWORK
469 if (environ == NULL)
470 environ = *_NSGetEnviron();
471#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000472#ifdef MS_WINDOWS
473 /* _wenviron must be initialized in this way if the program is started
474 through main() instead of wmain(). */
475 _wgetenv(L"");
476 if (_wenviron == NULL)
477 return d;
478 /* This part ignores errors */
479 for (e = _wenviron; *e != NULL; e++) {
480 PyObject *k;
481 PyObject *v;
482 wchar_t *p = wcschr(*e, L'=');
483 if (p == NULL)
484 continue;
485 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
486 if (k == NULL) {
487 PyErr_Clear();
488 continue;
489 }
490 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
491 if (v == NULL) {
492 PyErr_Clear();
493 Py_DECREF(k);
494 continue;
495 }
496 if (PyDict_GetItem(d, k) == NULL) {
497 if (PyDict_SetItem(d, k, v) != 0)
498 PyErr_Clear();
499 }
500 Py_DECREF(k);
501 Py_DECREF(v);
502 }
503#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000504 if (environ == NULL)
505 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000506 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000508 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000509 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510 char *p = strchr(*e, '=');
511 if (p == NULL)
512 continue;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000513 k = PyUnicode_Decode(*e, (int)(p-*e),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000514 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000515 if (k == NULL) {
516 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000518 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000519 v = PyUnicode_Decode(p+1, strlen(p+1),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000520 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000521 if (v == NULL) {
522 PyErr_Clear();
523 Py_DECREF(k);
524 continue;
525 }
526 if (PyDict_GetItem(d, k) == NULL) {
527 if (PyDict_SetItem(d, k, v) != 0)
528 PyErr_Clear();
529 }
530 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000531 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000533#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000534#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000535 {
536 APIRET rc;
537 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
538
539 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000540 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000541 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000542 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000543 Py_DECREF(v);
544 }
545 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
546 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000547 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000548 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000549 Py_DECREF(v);
550 }
551 }
552#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553 return d;
554}
555
Martin v. Löwis011e8422009-05-05 04:43:17 +0000556/* Convert a bytes object to a char*. Optionally lock the buffer if it is a
557 bytes array. */
558
559static char*
560bytes2str(PyObject* o, int lock)
561{
562 if(PyBytes_Check(o))
563 return PyBytes_AsString(o);
564 else if(PyByteArray_Check(o)) {
565 if (lock && PyObject_GetBuffer(o, NULL, 0) < 0)
566 /* On a bytearray, this should not fail. */
567 PyErr_BadInternalCall();
568 return PyByteArray_AsString(o);
569 } else {
570 /* The FS converter should have verified that this
571 is either bytes or bytearray. */
572 Py_FatalError("bad object passed to bytes2str");
573 /* not reached. */
574 return "";
575 }
576}
577
578/* Release the lock, decref the object. */
579static void
580release_bytes(PyObject* o)
581{
582 if (PyByteArray_Check(o))
Antoine Pitrou1b643312010-01-17 12:19:45 +0000583 o->ob_type->tp_as_buffer->bf_releasebuffer(o, 0);
Martin v. Löwis011e8422009-05-05 04:43:17 +0000584 Py_DECREF(o);
585}
586
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588/* Set a POSIX-specific error from errno, and return NULL */
589
Barry Warsawd58d7641998-07-23 16:14:40 +0000590static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000591posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000592{
Barry Warsawca74da41999-02-09 19:31:45 +0000593 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594}
Barry Warsawd58d7641998-07-23 16:14:40 +0000595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000596posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000597{
Barry Warsawca74da41999-02-09 19:31:45 +0000598 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000599}
600
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000601#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000602static PyObject *
603posix_error_with_unicode_filename(Py_UNICODE* name)
604{
605 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
606}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000607#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000608
609
Mark Hammondef8b6542001-05-13 08:04:26 +0000610static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000611posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000612{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000613 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
614 bytes2str(name, 0));
615 release_bytes(name);
Mark Hammondef8b6542001-05-13 08:04:26 +0000616 return rc;
617}
618
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000619#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000620static PyObject *
621win32_error(char* function, char* filename)
622{
Mark Hammond33a6da92000-08-15 00:46:38 +0000623 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000624 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000625 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000626 Windows error object, which is non-trivial.
627 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000628 errno = GetLastError();
629 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000630 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000631 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000632 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000633}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000634
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000635static PyObject *
636win32_error_unicode(char* function, Py_UNICODE* filename)
637{
638 /* XXX - see win32_error for comments on 'function' */
639 errno = GetLastError();
640 if (filename)
641 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
642 else
643 return PyErr_SetFromWindowsErr(errno);
644}
645
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000647convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000649 if (PyUnicode_CheckExact(*param))
650 Py_INCREF(*param);
651 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000652 /* For a Unicode subtype that's not a Unicode object,
653 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000654 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
655 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000657 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 Py_FileSystemDefaultEncoding,
659 "strict");
660 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000661}
662
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000663#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664
Guido van Rossumd48f2521997-12-05 22:19:34 +0000665#if defined(PYOS_OS2)
666/**********************************************************************
667 * Helper Function to Trim and Format OS/2 Messages
668 **********************************************************************/
669 static void
670os2_formatmsg(char *msgbuf, int msglen, char *reason)
671{
672 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
673
674 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
675 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
676
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000677 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000678 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
679 }
680
681 /* Add Optional Reason Text */
682 if (reason) {
683 strcat(msgbuf, " : ");
684 strcat(msgbuf, reason);
685 }
686}
687
688/**********************************************************************
689 * Decode an OS/2 Operating System Error Code
690 *
691 * A convenience function to lookup an OS/2 error code and return a
692 * text message we can use to raise a Python exception.
693 *
694 * Notes:
695 * The messages for errors returned from the OS/2 kernel reside in
696 * the file OSO001.MSG in the \OS2 directory hierarchy.
697 *
698 **********************************************************************/
699 static char *
700os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
701{
702 APIRET rc;
703 ULONG msglen;
704
705 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
706 Py_BEGIN_ALLOW_THREADS
707 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
708 errorcode, "oso001.msg", &msglen);
709 Py_END_ALLOW_THREADS
710
711 if (rc == NO_ERROR)
712 os2_formatmsg(msgbuf, msglen, reason);
713 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000714 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000715 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000716
717 return msgbuf;
718}
719
720/* Set an OS/2-specific error and return NULL. OS/2 kernel
721 errors are not in a global variable e.g. 'errno' nor are
722 they congruent with posix error numbers. */
723
724static PyObject * os2_error(int code)
725{
726 char text[1024];
727 PyObject *v;
728
729 os2_strerror(text, sizeof(text), code, "");
730
731 v = Py_BuildValue("(is)", code, text);
732 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000733 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000734 Py_DECREF(v);
735 }
736 return NULL; /* Signal to Python that an Exception is Pending */
737}
738
739#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000740
741/* POSIX generic methods */
742
Barry Warsaw53699e91996-12-10 23:23:01 +0000743static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000744posix_fildes(PyObject *fdobj, int (*func)(int))
745{
746 int fd;
747 int res;
748 fd = PyObject_AsFileDescriptor(fdobj);
749 if (fd < 0)
750 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000751 if (!_PyVerify_fd(fd))
752 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000753 Py_BEGIN_ALLOW_THREADS
754 res = (*func)(fd);
755 Py_END_ALLOW_THREADS
756 if (res < 0)
757 return posix_error();
758 Py_INCREF(Py_None);
759 return Py_None;
760}
Guido van Rossum21142a01999-01-08 21:05:37 +0000761
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000762#ifdef MS_WINDOWS
Tim Peters11b23062003-04-23 02:39:17 +0000763static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000764unicode_file_names(void)
765{
766 static int canusewide = -1;
767 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000768 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000769 the Windows NT family. */
770 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
771 }
772 return canusewide;
773}
774#endif
Tim Peters11b23062003-04-23 02:39:17 +0000775
Guido van Rossum21142a01999-01-08 21:05:37 +0000776static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000779 PyObject *opath1 = NULL;
780 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000781 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000782 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000783 PyUnicode_FSConverter, &opath1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000785 path1 = bytes2str(opath1, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000786 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000787 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000788 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000789 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000790 return posix_error_with_allocated_filename(opath1);
791 release_bytes(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000792 Py_INCREF(Py_None);
793 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794}
795
Barry Warsaw53699e91996-12-10 23:23:01 +0000796static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000797posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000798 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000800{
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000801 PyObject *opath1 = NULL, *opath2 = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000802 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000803 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000804 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000805 PyUnicode_FSConverter, &opath1,
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000806 PyUnicode_FSConverter, &opath2)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000807 return NULL;
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000808 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000809 path1 = bytes2str(opath1, 1);
810 path2 = bytes2str(opath2, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000811 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000812 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000813 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +0000814 release_bytes(opath1);
815 release_bytes(opath2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000816 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000817 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000819 Py_INCREF(Py_None);
820 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000821}
822
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000823#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824static PyObject*
825win32_1str(PyObject* args, char* func,
826 char* format, BOOL (__stdcall *funcA)(LPCSTR),
827 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
828{
829 PyObject *uni;
830 char *ansi;
831 BOOL result;
832 if (unicode_file_names()) {
833 if (!PyArg_ParseTuple(args, wformat, &uni))
834 PyErr_Clear();
835 else {
836 Py_BEGIN_ALLOW_THREADS
837 result = funcW(PyUnicode_AsUnicode(uni));
838 Py_END_ALLOW_THREADS
839 if (!result)
840 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
841 Py_INCREF(Py_None);
842 return Py_None;
843 }
844 }
845 if (!PyArg_ParseTuple(args, format, &ansi))
846 return NULL;
847 Py_BEGIN_ALLOW_THREADS
848 result = funcA(ansi);
849 Py_END_ALLOW_THREADS
850 if (!result)
851 return win32_error(func, ansi);
852 Py_INCREF(Py_None);
853 return Py_None;
854
855}
856
857/* This is a reimplementation of the C library's chdir function,
858 but one that produces Win32 errors instead of DOS error codes.
859 chdir is essentially a wrapper around SetCurrentDirectory; however,
860 it also needs to set "magic" environment variables indicating
861 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000862static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863win32_chdir(LPCSTR path)
864{
865 char new_path[MAX_PATH+1];
866 int result;
867 char env[4] = "=x:";
868
869 if(!SetCurrentDirectoryA(path))
870 return FALSE;
871 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
872 if (!result)
873 return FALSE;
874 /* In the ANSI API, there should not be any paths longer
875 than MAX_PATH. */
876 assert(result <= MAX_PATH+1);
877 if (strncmp(new_path, "\\\\", 2) == 0 ||
878 strncmp(new_path, "//", 2) == 0)
879 /* UNC path, nothing to do. */
880 return TRUE;
881 env[1] = new_path[0];
882 return SetEnvironmentVariableA(env, new_path);
883}
884
885/* The Unicode version differs from the ANSI version
886 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000887static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000888win32_wchdir(LPCWSTR path)
889{
890 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
891 int result;
892 wchar_t env[4] = L"=x:";
893
894 if(!SetCurrentDirectoryW(path))
895 return FALSE;
896 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
897 if (!result)
898 return FALSE;
899 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000900 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000901 if (!new_path) {
902 SetLastError(ERROR_OUTOFMEMORY);
903 return FALSE;
904 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000905 result = GetCurrentDirectoryW(result, new_path);
906 if (!result) {
907 free(new_path);
908 return FALSE;
909 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000910 }
911 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
912 wcsncmp(new_path, L"//", 2) == 0)
913 /* UNC path, nothing to do. */
914 return TRUE;
915 env[1] = new_path[0];
916 result = SetEnvironmentVariableW(env, new_path);
917 if (new_path != _new_path)
918 free(new_path);
919 return result;
920}
921#endif
922
Martin v. Löwis14694662006-02-03 12:54:16 +0000923#ifdef MS_WINDOWS
924/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
925 - time stamps are restricted to second resolution
926 - file modification times suffer from forth-and-back conversions between
927 UTC and local time
928 Therefore, we implement our own stat, based on the Win32 API directly.
929*/
930#define HAVE_STAT_NSEC 1
931
932struct win32_stat{
933 int st_dev;
934 __int64 st_ino;
935 unsigned short st_mode;
936 int st_nlink;
937 int st_uid;
938 int st_gid;
939 int st_rdev;
940 __int64 st_size;
941 int st_atime;
942 int st_atime_nsec;
943 int st_mtime;
944 int st_mtime_nsec;
945 int st_ctime;
946 int st_ctime_nsec;
947};
948
949static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
950
951static void
952FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
953{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000954 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
955 /* Cannot simply cast and dereference in_ptr,
956 since it might not be aligned properly */
957 __int64 in;
958 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000959 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
960 /* XXX Win32 supports time stamps past 2038; we currently don't */
961 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
962}
963
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964static void
965time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
966{
967 /* XXX endianness */
968 __int64 out;
969 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000970 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000971 memcpy(out_ptr, &out, sizeof(out));
972}
973
Martin v. Löwis14694662006-02-03 12:54:16 +0000974/* Below, we *know* that ugo+r is 0444 */
975#if _S_IREAD != 0400
976#error Unsupported C library
977#endif
978static int
979attributes_to_mode(DWORD attr)
980{
981 int m = 0;
982 if (attr & FILE_ATTRIBUTE_DIRECTORY)
983 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
984 else
985 m |= _S_IFREG;
986 if (attr & FILE_ATTRIBUTE_READONLY)
987 m |= 0444;
988 else
989 m |= 0666;
990 return m;
991}
992
993static int
994attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
995{
996 memset(result, 0, sizeof(*result));
997 result->st_mode = attributes_to_mode(info->dwFileAttributes);
998 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
999 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1000 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1001 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1002
1003 return 0;
1004}
1005
Thomas Wouters89f507f2006-12-13 04:49:30 +00001006/* Emulate GetFileAttributesEx[AW] on Windows 95 */
1007static int checked = 0;
1008static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1009static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
1010static void
1011check_gfax()
1012{
1013 HINSTANCE hKernel32;
1014 if (checked)
1015 return;
1016 checked = 1;
1017 hKernel32 = GetModuleHandle("KERNEL32");
1018 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
1019 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
1020}
1021
Guido van Rossumd8faa362007-04-27 19:54:29 +00001022static BOOL
1023attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1024{
1025 HANDLE hFindFile;
1026 WIN32_FIND_DATAA FileData;
1027 hFindFile = FindFirstFileA(pszFile, &FileData);
1028 if (hFindFile == INVALID_HANDLE_VALUE)
1029 return FALSE;
1030 FindClose(hFindFile);
1031 pfad->dwFileAttributes = FileData.dwFileAttributes;
1032 pfad->ftCreationTime = FileData.ftCreationTime;
1033 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1034 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1035 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1036 pfad->nFileSizeLow = FileData.nFileSizeLow;
1037 return TRUE;
1038}
1039
1040static BOOL
1041attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1042{
1043 HANDLE hFindFile;
1044 WIN32_FIND_DATAW FileData;
1045 hFindFile = FindFirstFileW(pszFile, &FileData);
1046 if (hFindFile == INVALID_HANDLE_VALUE)
1047 return FALSE;
1048 FindClose(hFindFile);
1049 pfad->dwFileAttributes = FileData.dwFileAttributes;
1050 pfad->ftCreationTime = FileData.ftCreationTime;
1051 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1052 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1053 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1054 pfad->nFileSizeLow = FileData.nFileSizeLow;
1055 return TRUE;
1056}
1057
Thomas Wouters89f507f2006-12-13 04:49:30 +00001058static BOOL WINAPI
1059Py_GetFileAttributesExA(LPCSTR pszFile,
1060 GET_FILEEX_INFO_LEVELS level,
1061 LPVOID pv)
1062{
1063 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001064 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1065 /* First try to use the system's implementation, if that is
1066 available and either succeeds to gives an error other than
1067 that it isn't implemented. */
1068 check_gfax();
1069 if (gfaxa) {
1070 result = gfaxa(pszFile, level, pv);
1071 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1072 return result;
1073 }
1074 /* It's either not present, or not implemented.
1075 Emulate using FindFirstFile. */
1076 if (level != GetFileExInfoStandard) {
1077 SetLastError(ERROR_INVALID_PARAMETER);
1078 return FALSE;
1079 }
1080 /* Use GetFileAttributes to validate that the file name
1081 does not contain wildcards (which FindFirstFile would
1082 accept). */
1083 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
1084 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001085 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001086}
1087
1088static BOOL WINAPI
1089Py_GetFileAttributesExW(LPCWSTR pszFile,
1090 GET_FILEEX_INFO_LEVELS level,
1091 LPVOID pv)
1092{
1093 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001094 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1095 /* First try to use the system's implementation, if that is
1096 available and either succeeds to gives an error other than
1097 that it isn't implemented. */
1098 check_gfax();
1099 if (gfaxa) {
1100 result = gfaxw(pszFile, level, pv);
1101 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1102 return result;
1103 }
1104 /* It's either not present, or not implemented.
1105 Emulate using FindFirstFile. */
1106 if (level != GetFileExInfoStandard) {
1107 SetLastError(ERROR_INVALID_PARAMETER);
1108 return FALSE;
1109 }
1110 /* Use GetFileAttributes to validate that the file name
1111 does not contain wildcards (which FindFirstFile would
1112 accept). */
1113 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
1114 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001115 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001116}
1117
Martin v. Löwis14694662006-02-03 12:54:16 +00001118static int
1119win32_stat(const char* path, struct win32_stat *result)
1120{
1121 WIN32_FILE_ATTRIBUTE_DATA info;
1122 int code;
1123 char *dot;
1124 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001125 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001126 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1127 /* Protocol violation: we explicitly clear errno, instead of
1128 setting it to a POSIX error. Callers should use GetLastError. */
1129 errno = 0;
1130 return -1;
1131 } else {
1132 /* Could not get attributes on open file. Fall back to
1133 reading the directory. */
1134 if (!attributes_from_dir(path, &info)) {
1135 /* Very strange. This should not fail now */
1136 errno = 0;
1137 return -1;
1138 }
1139 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001140 }
1141 code = attribute_data_to_stat(&info, result);
1142 if (code != 0)
1143 return code;
1144 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1145 dot = strrchr(path, '.');
1146 if (dot) {
1147 if (stricmp(dot, ".bat") == 0 ||
1148 stricmp(dot, ".cmd") == 0 ||
1149 stricmp(dot, ".exe") == 0 ||
1150 stricmp(dot, ".com") == 0)
1151 result->st_mode |= 0111;
1152 }
1153 return code;
1154}
1155
1156static int
1157win32_wstat(const wchar_t* path, struct win32_stat *result)
1158{
1159 int code;
1160 const wchar_t *dot;
1161 WIN32_FILE_ATTRIBUTE_DATA info;
1162 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001163 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001164 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1165 /* Protocol violation: we explicitly clear errno, instead of
1166 setting it to a POSIX error. Callers should use GetLastError. */
1167 errno = 0;
1168 return -1;
1169 } else {
1170 /* Could not get attributes on open file. Fall back to
1171 reading the directory. */
1172 if (!attributes_from_dir_w(path, &info)) {
1173 /* Very strange. This should not fail now */
1174 errno = 0;
1175 return -1;
1176 }
1177 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001178 }
1179 code = attribute_data_to_stat(&info, result);
1180 if (code < 0)
1181 return code;
1182 /* Set IFEXEC if it is an .exe, .bat, ... */
1183 dot = wcsrchr(path, '.');
1184 if (dot) {
1185 if (_wcsicmp(dot, L".bat") == 0 ||
1186 _wcsicmp(dot, L".cmd") == 0 ||
1187 _wcsicmp(dot, L".exe") == 0 ||
1188 _wcsicmp(dot, L".com") == 0)
1189 result->st_mode |= 0111;
1190 }
1191 return code;
1192}
1193
1194static int
1195win32_fstat(int file_number, struct win32_stat *result)
1196{
1197 BY_HANDLE_FILE_INFORMATION info;
1198 HANDLE h;
1199 int type;
1200
1201 h = (HANDLE)_get_osfhandle(file_number);
1202
1203 /* Protocol violation: we explicitly clear errno, instead of
1204 setting it to a POSIX error. Callers should use GetLastError. */
1205 errno = 0;
1206
1207 if (h == INVALID_HANDLE_VALUE) {
1208 /* This is really a C library error (invalid file handle).
1209 We set the Win32 error to the closes one matching. */
1210 SetLastError(ERROR_INVALID_HANDLE);
1211 return -1;
1212 }
1213 memset(result, 0, sizeof(*result));
1214
1215 type = GetFileType(h);
1216 if (type == FILE_TYPE_UNKNOWN) {
1217 DWORD error = GetLastError();
1218 if (error != 0) {
1219 return -1;
1220 }
1221 /* else: valid but unknown file */
1222 }
1223
1224 if (type != FILE_TYPE_DISK) {
1225 if (type == FILE_TYPE_CHAR)
1226 result->st_mode = _S_IFCHR;
1227 else if (type == FILE_TYPE_PIPE)
1228 result->st_mode = _S_IFIFO;
1229 return 0;
1230 }
1231
1232 if (!GetFileInformationByHandle(h, &info)) {
1233 return -1;
1234 }
1235
1236 /* similar to stat() */
1237 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1238 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1239 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1240 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1241 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1242 /* specific to fstat() */
1243 result->st_nlink = info.nNumberOfLinks;
1244 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1245 return 0;
1246}
1247
1248#endif /* MS_WINDOWS */
1249
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001250PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001251"stat_result: Result from stat or lstat.\n\n\
1252This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001253 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001254or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1255\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001256Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1257or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001258\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001259See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001260
1261static PyStructSequence_Field stat_result_fields[] = {
1262 {"st_mode", "protection bits"},
1263 {"st_ino", "inode"},
1264 {"st_dev", "device"},
1265 {"st_nlink", "number of hard links"},
1266 {"st_uid", "user ID of owner"},
1267 {"st_gid", "group ID of owner"},
1268 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001269 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1270 {NULL, "integer time of last access"},
1271 {NULL, "integer time of last modification"},
1272 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001273 {"st_atime", "time of last access"},
1274 {"st_mtime", "time of last modification"},
1275 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001276#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001277 {"st_blksize", "blocksize for filesystem I/O"},
1278#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001279#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001280 {"st_blocks", "number of blocks allocated"},
1281#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001282#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001283 {"st_rdev", "device type (if inode device)"},
1284#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001285#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1286 {"st_flags", "user defined flags for file"},
1287#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001288#ifdef HAVE_STRUCT_STAT_ST_GEN
1289 {"st_gen", "generation number"},
1290#endif
1291#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1292 {"st_birthtime", "time of creation"},
1293#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001294 {0}
1295};
1296
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001297#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001298#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001299#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001300#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001301#endif
1302
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001303#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001304#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1305#else
1306#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1307#endif
1308
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001309#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001310#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1311#else
1312#define ST_RDEV_IDX ST_BLOCKS_IDX
1313#endif
1314
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001315#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1316#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1317#else
1318#define ST_FLAGS_IDX ST_RDEV_IDX
1319#endif
1320
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001321#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001322#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001323#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001324#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001325#endif
1326
1327#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1328#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1329#else
1330#define ST_BIRTHTIME_IDX ST_GEN_IDX
1331#endif
1332
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001333static PyStructSequence_Desc stat_result_desc = {
1334 "stat_result", /* name */
1335 stat_result__doc__, /* doc */
1336 stat_result_fields,
1337 10
1338};
1339
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001340PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001341"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1342This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001343 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001344or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001345\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001346See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001347
1348static PyStructSequence_Field statvfs_result_fields[] = {
1349 {"f_bsize", },
1350 {"f_frsize", },
1351 {"f_blocks", },
1352 {"f_bfree", },
1353 {"f_bavail", },
1354 {"f_files", },
1355 {"f_ffree", },
1356 {"f_favail", },
1357 {"f_flag", },
1358 {"f_namemax",},
1359 {0}
1360};
1361
1362static PyStructSequence_Desc statvfs_result_desc = {
1363 "statvfs_result", /* name */
1364 statvfs_result__doc__, /* doc */
1365 statvfs_result_fields,
1366 10
1367};
1368
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001370static PyTypeObject StatResultType;
1371static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001372static newfunc structseq_new;
1373
1374static PyObject *
1375statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1376{
1377 PyStructSequence *result;
1378 int i;
1379
1380 result = (PyStructSequence*)structseq_new(type, args, kwds);
1381 if (!result)
1382 return NULL;
1383 /* If we have been initialized from a tuple,
1384 st_?time might be set to None. Initialize it
1385 from the int slots. */
1386 for (i = 7; i <= 9; i++) {
1387 if (result->ob_item[i+3] == Py_None) {
1388 Py_DECREF(Py_None);
1389 Py_INCREF(result->ob_item[i]);
1390 result->ob_item[i+3] = result->ob_item[i];
1391 }
1392 }
1393 return (PyObject*)result;
1394}
1395
1396
1397
1398/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001399static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001400
1401PyDoc_STRVAR(stat_float_times__doc__,
1402"stat_float_times([newval]) -> oldval\n\n\
1403Determine whether os.[lf]stat represents time stamps as float objects.\n\
1404If newval is True, future calls to stat() return floats, if it is False,\n\
1405future calls return ints. \n\
1406If newval is omitted, return the current setting.\n");
1407
1408static PyObject*
1409stat_float_times(PyObject* self, PyObject *args)
1410{
1411 int newval = -1;
1412 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1413 return NULL;
1414 if (newval == -1)
1415 /* Return old value */
1416 return PyBool_FromLong(_stat_float_times);
1417 _stat_float_times = newval;
1418 Py_INCREF(Py_None);
1419 return Py_None;
1420}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001421
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001422static void
1423fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1424{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001425 PyObject *fval,*ival;
1426#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001427 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001428#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001429 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001430#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001431 if (!ival)
1432 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001433 if (_stat_float_times) {
1434 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1435 } else {
1436 fval = ival;
1437 Py_INCREF(fval);
1438 }
1439 PyStructSequence_SET_ITEM(v, index, ival);
1440 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001441}
1442
Tim Peters5aa91602002-01-30 05:46:57 +00001443/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001444 (used by posix_stat() and posix_fstat()) */
1445static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001446_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001447{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001448 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001449 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001450 if (v == NULL)
1451 return NULL;
1452
Christian Heimes217cfd12007-12-02 14:31:20 +00001453 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001454#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001455 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001456 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001457#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001458 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001459#endif
1460#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001461 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001462 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001463#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001464 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001465#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001466 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1467 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1468 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001469#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001470 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001471 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001472#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001473 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001474#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001475
Martin v. Löwis14694662006-02-03 12:54:16 +00001476#if defined(HAVE_STAT_TV_NSEC)
1477 ansec = st->st_atim.tv_nsec;
1478 mnsec = st->st_mtim.tv_nsec;
1479 cnsec = st->st_ctim.tv_nsec;
1480#elif defined(HAVE_STAT_TV_NSEC2)
1481 ansec = st->st_atimespec.tv_nsec;
1482 mnsec = st->st_mtimespec.tv_nsec;
1483 cnsec = st->st_ctimespec.tv_nsec;
1484#elif defined(HAVE_STAT_NSEC)
1485 ansec = st->st_atime_nsec;
1486 mnsec = st->st_mtime_nsec;
1487 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001488#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001489 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001490#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001491 fill_time(v, 7, st->st_atime, ansec);
1492 fill_time(v, 8, st->st_mtime, mnsec);
1493 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001494
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001495#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001496 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001497 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001498#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001499#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001500 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001501 PyLong_FromLong((long)st->st_blocks));
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_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001504 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001505 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001506#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001507#ifdef HAVE_STRUCT_STAT_ST_GEN
1508 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001509 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001510#endif
1511#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1512 {
1513 PyObject *val;
1514 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001515 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001516#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001517 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001518#else
1519 bnsec = 0;
1520#endif
1521 if (_stat_float_times) {
1522 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1523 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001524 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001525 }
1526 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1527 val);
1528 }
1529#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001530#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1531 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001532 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001533#endif
Fred Drake699f3522000-06-29 21:12:41 +00001534
1535 if (PyErr_Occurred()) {
1536 Py_DECREF(v);
1537 return NULL;
1538 }
1539
1540 return v;
1541}
1542
Martin v. Löwisd8948722004-06-02 09:57:56 +00001543#ifdef MS_WINDOWS
1544
1545/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1546 where / can be used in place of \ and the trailing slash is optional.
1547 Both SERVER and SHARE must have at least one character.
1548*/
1549
1550#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1551#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001552#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001553#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001554#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001555
Tim Peters4ad82172004-08-30 17:02:04 +00001556static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001557IsUNCRootA(char *path, int pathlen)
1558{
1559 #define ISSLASH ISSLASHA
1560
1561 int i, share;
1562
1563 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1564 /* minimum UNCRoot is \\x\y */
1565 return FALSE;
1566 for (i = 2; i < pathlen ; i++)
1567 if (ISSLASH(path[i])) break;
1568 if (i == 2 || i == pathlen)
1569 /* do not allow \\\SHARE or \\SERVER */
1570 return FALSE;
1571 share = i+1;
1572 for (i = share; i < pathlen; i++)
1573 if (ISSLASH(path[i])) break;
1574 return (i != share && (i == pathlen || i == pathlen-1));
1575
1576 #undef ISSLASH
1577}
1578
Tim Peters4ad82172004-08-30 17:02:04 +00001579static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001580IsUNCRootW(Py_UNICODE *path, int pathlen)
1581{
1582 #define ISSLASH ISSLASHW
1583
1584 int i, share;
1585
1586 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1587 /* minimum UNCRoot is \\x\y */
1588 return FALSE;
1589 for (i = 2; i < pathlen ; i++)
1590 if (ISSLASH(path[i])) break;
1591 if (i == 2 || i == pathlen)
1592 /* do not allow \\\SHARE or \\SERVER */
1593 return FALSE;
1594 share = i+1;
1595 for (i = share; i < pathlen; i++)
1596 if (ISSLASH(path[i])) break;
1597 return (i != share && (i == pathlen || i == pathlen-1));
1598
1599 #undef ISSLASH
1600}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001601#endif /* MS_WINDOWS */
1602
Barry Warsaw53699e91996-12-10 23:23:01 +00001603static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001604posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001605 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001606#ifdef __VMS
1607 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1608#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001609 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001610#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001611 char *wformat,
1612 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001613{
Fred Drake699f3522000-06-29 21:12:41 +00001614 STRUCT_STAT st;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001615 PyObject *opath;
1616 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001617 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001618 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001619
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001620#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001621 /* If on wide-character-capable OS see if argument
1622 is Unicode and if so use wide API. */
1623 if (unicode_file_names()) {
1624 PyUnicodeObject *po;
1625 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001626 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1627
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001628 Py_BEGIN_ALLOW_THREADS
1629 /* PyUnicode_AS_UNICODE result OK without
1630 thread lock as it is a simple dereference. */
1631 res = wstatfunc(wpath, &st);
1632 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001633
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001634 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001635 return win32_error_unicode("stat", wpath);
1636 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001637 }
1638 /* Drop the argument parsing error as narrow strings
1639 are also valid. */
1640 PyErr_Clear();
1641 }
1642#endif
1643
Tim Peters5aa91602002-01-30 05:46:57 +00001644 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +00001645 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001646 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001647 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00001648 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001649 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001650 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001651
1652 if (res != 0) {
1653#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001654 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001655#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001656 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001657#endif
1658 }
1659 else
1660 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001661
Martin v. Löwis011e8422009-05-05 04:43:17 +00001662 release_bytes(opath);
Martin v. Löwis14694662006-02-03 12:54:16 +00001663 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001664}
1665
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001666/* POSIX methods */
1667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001668PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001669"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001670Use the real uid/gid to test for access to a path. Note that most\n\
1671operations will use the effective uid/gid, therefore this routine can\n\
1672be used in a suid/sgid environment to test if the invoking user has the\n\
1673specified access to the path. The mode argument can be F_OK to test\n\
1674existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001675
1676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001677posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001678{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001679 PyObject *opath;
Guido van Rossum015f22a1999-01-06 22:52:38 +00001680 char *path;
1681 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001682
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001683#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001684 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001685 if (unicode_file_names()) {
1686 PyUnicodeObject *po;
1687 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1688 Py_BEGIN_ALLOW_THREADS
1689 /* PyUnicode_AS_UNICODE OK without thread lock as
1690 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001691 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001692 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001693 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001694 }
1695 /* Drop the argument parsing error as narrow strings
1696 are also valid. */
1697 PyErr_Clear();
1698 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001699 if (!PyArg_ParseTuple(args, "O&i:access",
1700 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001701 return 0;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001702 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001703 Py_BEGIN_ALLOW_THREADS
1704 attr = GetFileAttributesA(path);
1705 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001706 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001707finish:
1708 if (attr == 0xFFFFFFFF)
1709 /* File does not exist, or cannot read attributes */
1710 return PyBool_FromLong(0);
1711 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001712 the file isn't read-only, or if it's a directory, as there are
1713 no read-only directories on Windows. */
1714 return PyBool_FromLong(!(mode & 2)
1715 || !(attr & FILE_ATTRIBUTE_READONLY)
1716 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001717#else
1718 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001719 if (!PyArg_ParseTuple(args, "O&i:access",
1720 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001721 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001722 path = bytes2str(opath, 1);
Guido van Rossum015f22a1999-01-06 22:52:38 +00001723 Py_BEGIN_ALLOW_THREADS
1724 res = access(path, mode);
1725 Py_END_ALLOW_THREADS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001726 release_bytes(opath);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001727 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001728#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001729}
1730
Guido van Rossumd371ff11999-01-25 16:12:23 +00001731#ifndef F_OK
1732#define F_OK 0
1733#endif
1734#ifndef R_OK
1735#define R_OK 4
1736#endif
1737#ifndef W_OK
1738#define W_OK 2
1739#endif
1740#ifndef X_OK
1741#define X_OK 1
1742#endif
1743
1744#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001745PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001746"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001747Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001748
1749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001750posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001751{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001752 int id;
1753 char *ret;
1754
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001755 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001756 return NULL;
1757
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001758#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001759 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001760 if (id == 0) {
1761 ret = ttyname();
1762 }
1763 else {
1764 ret = NULL;
1765 }
1766#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001767 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001768#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001769 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001770 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001771 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001772}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001773#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001774
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001775#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001776PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001777"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001778Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001779
1780static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001781posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001782{
1783 char *ret;
1784 char buffer[L_ctermid];
1785
Greg Wardb48bc172000-03-01 21:51:56 +00001786#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001787 ret = ctermid_r(buffer);
1788#else
1789 ret = ctermid(buffer);
1790#endif
1791 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001792 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001793 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001794}
1795#endif
1796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001797PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001798"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001800
Barry Warsaw53699e91996-12-10 23:23:01 +00001801static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001802posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001803{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001804#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001805 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001806#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001807 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001808#elif defined(__VMS)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001809 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001810#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001811 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001812#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001813}
1814
Fred Drake4d1e64b2002-04-15 19:40:07 +00001815#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001816PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001817"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001818Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001820
1821static PyObject *
1822posix_fchdir(PyObject *self, PyObject *fdobj)
1823{
1824 return posix_fildes(fdobj, fchdir);
1825}
1826#endif /* HAVE_FCHDIR */
1827
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001829PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001830"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001832
Barry Warsaw53699e91996-12-10 23:23:01 +00001833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001834posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001835{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001836 PyObject *opath = NULL;
Mark Hammondef8b6542001-05-13 08:04:26 +00001837 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001838 int i;
1839 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001840#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001841 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001842 if (unicode_file_names()) {
1843 PyUnicodeObject *po;
1844 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1845 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001846 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1847 if (attr != 0xFFFFFFFF) {
1848 if (i & _S_IWRITE)
1849 attr &= ~FILE_ATTRIBUTE_READONLY;
1850 else
1851 attr |= FILE_ATTRIBUTE_READONLY;
1852 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1853 }
1854 else
1855 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001856 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001857 if (!res)
1858 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001859 PyUnicode_AS_UNICODE(po));
1860 Py_INCREF(Py_None);
1861 return Py_None;
1862 }
1863 /* Drop the argument parsing error as narrow strings
1864 are also valid. */
1865 PyErr_Clear();
1866 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001867 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1868 &opath, &i))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001869 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001870 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001871 Py_BEGIN_ALLOW_THREADS
1872 attr = GetFileAttributesA(path);
1873 if (attr != 0xFFFFFFFF) {
1874 if (i & _S_IWRITE)
1875 attr &= ~FILE_ATTRIBUTE_READONLY;
1876 else
1877 attr |= FILE_ATTRIBUTE_READONLY;
1878 res = SetFileAttributesA(path, attr);
1879 }
1880 else
1881 res = 0;
1882 Py_END_ALLOW_THREADS
1883 if (!res) {
1884 win32_error("chmod", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00001885 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001886 return NULL;
1887 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00001888 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001889 Py_INCREF(Py_None);
1890 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001891#else /* MS_WINDOWS */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001892 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1893 &opath, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001894 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001895 path = bytes2str(opath, 1);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001896 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001897 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001898 Py_END_ALLOW_THREADS
1899 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001900 return posix_error_with_allocated_filename(opath);
1901 release_bytes(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001902 Py_INCREF(Py_None);
1903 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001904#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001905}
1906
Christian Heimes4e30a842007-11-30 22:12:06 +00001907#ifdef HAVE_FCHMOD
1908PyDoc_STRVAR(posix_fchmod__doc__,
1909"fchmod(fd, mode)\n\n\
1910Change the access permissions of the file given by file\n\
1911descriptor fd.");
1912
1913static PyObject *
1914posix_fchmod(PyObject *self, PyObject *args)
1915{
1916 int fd, mode, res;
1917 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1918 return NULL;
1919 Py_BEGIN_ALLOW_THREADS
1920 res = fchmod(fd, mode);
1921 Py_END_ALLOW_THREADS
1922 if (res < 0)
1923 return posix_error();
1924 Py_RETURN_NONE;
1925}
1926#endif /* HAVE_FCHMOD */
1927
1928#ifdef HAVE_LCHMOD
1929PyDoc_STRVAR(posix_lchmod__doc__,
1930"lchmod(path, mode)\n\n\
1931Change the access permissions of a file. If path is a symlink, this\n\
1932affects the link itself rather than the target.");
1933
1934static PyObject *
1935posix_lchmod(PyObject *self, PyObject *args)
1936{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001937 PyObject *opath;
1938 char *path;
Christian Heimes4e30a842007-11-30 22:12:06 +00001939 int i;
1940 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001941 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1942 &opath, &i))
Christian Heimes4e30a842007-11-30 22:12:06 +00001943 return NULL;
Eric Smith86a05ec2009-05-05 13:07:30 +00001944 path = bytes2str(opath, 1);
Christian Heimes4e30a842007-11-30 22:12:06 +00001945 Py_BEGIN_ALLOW_THREADS
1946 res = lchmod(path, i);
1947 Py_END_ALLOW_THREADS
1948 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001949 return posix_error_with_allocated_filename(opath);
1950 release_bytes(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001951 Py_RETURN_NONE;
1952}
1953#endif /* HAVE_LCHMOD */
1954
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001955
Thomas Wouterscf297e42007-02-23 15:07:44 +00001956#ifdef HAVE_CHFLAGS
1957PyDoc_STRVAR(posix_chflags__doc__,
1958"chflags(path, flags)\n\n\
1959Set file flags.");
1960
1961static PyObject *
1962posix_chflags(PyObject *self, PyObject *args)
1963{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001964 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001965 char *path;
1966 unsigned long flags;
1967 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001968 if (!PyArg_ParseTuple(args, "O&k:chflags",
1969 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001970 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001971 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001972 Py_BEGIN_ALLOW_THREADS
1973 res = chflags(path, flags);
1974 Py_END_ALLOW_THREADS
1975 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001976 return posix_error_with_allocated_filename(opath);
1977 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001978 Py_INCREF(Py_None);
1979 return Py_None;
1980}
1981#endif /* HAVE_CHFLAGS */
1982
1983#ifdef HAVE_LCHFLAGS
1984PyDoc_STRVAR(posix_lchflags__doc__,
1985"lchflags(path, flags)\n\n\
1986Set file flags.\n\
1987This function will not follow symbolic links.");
1988
1989static PyObject *
1990posix_lchflags(PyObject *self, PyObject *args)
1991{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001992 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001993 char *path;
1994 unsigned long flags;
1995 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001996 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Martin v. Löwis4adbc342009-05-05 17:17:55 +00001997 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001998 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001999 path = bytes2str(opath, 1);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002000 Py_BEGIN_ALLOW_THREADS
2001 res = lchflags(path, flags);
2002 Py_END_ALLOW_THREADS
2003 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002004 return posix_error_with_allocated_filename(opath);
2005 release_bytes(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002006 Py_INCREF(Py_None);
2007 return Py_None;
2008}
2009#endif /* HAVE_LCHFLAGS */
2010
Martin v. Löwis244edc82001-10-04 22:44:26 +00002011#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002012PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002013"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002014Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002015
2016static PyObject *
2017posix_chroot(PyObject *self, PyObject *args)
2018{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002019 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002020}
2021#endif
2022
Guido van Rossum21142a01999-01-08 21:05:37 +00002023#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002024PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002025"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002026force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002027
2028static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002029posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002030{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002031 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002032}
2033#endif /* HAVE_FSYNC */
2034
2035#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002036
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002037#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002038extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2039#endif
2040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002041PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002042"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002043force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002044 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002045
2046static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002047posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002048{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002049 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002050}
2051#endif /* HAVE_FDATASYNC */
2052
2053
Fredrik Lundh10723342000-07-10 16:38:09 +00002054#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002056"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002058
Barry Warsaw53699e91996-12-10 23:23:01 +00002059static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002060posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002061{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002062 PyObject *opath;
2063 char *path;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002064 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00002065 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002066 if (!PyArg_ParseTuple(args, "O&ll:chown",
2067 PyUnicode_FSConverter, &opath,
Mark Hammondef8b6542001-05-13 08:04:26 +00002068 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00002069 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002070 path = bytes2str(opath, 1);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002071 Py_BEGIN_ALLOW_THREADS
2072 res = chown(path, (uid_t) uid, (gid_t) gid);
2073 Py_END_ALLOW_THREADS
2074 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002075 return posix_error_with_allocated_filename(opath);
2076 release_bytes(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00002077 Py_INCREF(Py_None);
2078 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002079}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002080#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081
Christian Heimes4e30a842007-11-30 22:12:06 +00002082#ifdef HAVE_FCHOWN
2083PyDoc_STRVAR(posix_fchown__doc__,
2084"fchown(fd, uid, gid)\n\n\
2085Change the owner and group id of the file given by file descriptor\n\
2086fd to the numeric uid and gid.");
2087
2088static PyObject *
2089posix_fchown(PyObject *self, PyObject *args)
2090{
Benjamin Peterson68dbebc2009-12-31 03:30:26 +00002091 int fd;
2092 long uid, gid;
Christian Heimes4e30a842007-11-30 22:12:06 +00002093 int res;
Benjamin Peterson68dbebc2009-12-31 03:30:26 +00002094 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
Christian Heimes4e30a842007-11-30 22:12:06 +00002095 return NULL;
2096 Py_BEGIN_ALLOW_THREADS
2097 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2098 Py_END_ALLOW_THREADS
2099 if (res < 0)
2100 return posix_error();
2101 Py_RETURN_NONE;
2102}
2103#endif /* HAVE_FCHOWN */
2104
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002105#ifdef HAVE_LCHOWN
2106PyDoc_STRVAR(posix_lchown__doc__,
2107"lchown(path, uid, gid)\n\n\
2108Change the owner and group id of path to the numeric uid and gid.\n\
2109This function will not follow symbolic links.");
2110
2111static PyObject *
2112posix_lchown(PyObject *self, PyObject *args)
2113{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002114 PyObject *opath;
2115 char *path;
Benjamin Peterson68dbebc2009-12-31 03:30:26 +00002116 long uid, gid;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002117 int res;
Benjamin Peterson68dbebc2009-12-31 03:30:26 +00002118 if (!PyArg_ParseTuple(args, "O&ll:lchown",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002119 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002120 &uid, &gid))
2121 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002122 path = bytes2str(opath, 1);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002123 Py_BEGIN_ALLOW_THREADS
2124 res = lchown(path, (uid_t) uid, (gid_t) gid);
2125 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00002126 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002127 return posix_error_with_allocated_filename(opath);
2128 release_bytes(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002129 Py_INCREF(Py_None);
2130 return Py_None;
2131}
2132#endif /* HAVE_LCHOWN */
2133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002134
Guido van Rossum36bc6801995-06-14 22:54:23 +00002135#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002136static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002137posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002138{
2139 char buf[1026];
2140 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002141
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002142#ifdef MS_WINDOWS
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002143 if (!use_bytes && unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002144 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002145 wchar_t *wbuf2 = wbuf;
2146 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002147 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002148 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002149 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2150 /* If the buffer is large enough, len does not include the
2151 terminating \0. If the buffer is too small, len includes
2152 the space needed for the terminator. */
2153 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2154 wbuf2 = malloc(len * sizeof(wchar_t));
2155 if (wbuf2)
2156 len = GetCurrentDirectoryW(len, wbuf2);
2157 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002158 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002159 if (!wbuf2) {
2160 PyErr_NoMemory();
2161 return NULL;
2162 }
2163 if (!len) {
2164 if (wbuf2 != wbuf) free(wbuf2);
2165 return win32_error("getcwdu", NULL);
2166 }
2167 resobj = PyUnicode_FromWideChar(wbuf2, len);
2168 if (wbuf2 != wbuf) free(wbuf2);
2169 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002170 }
2171#endif
2172
2173 Py_BEGIN_ALLOW_THREADS
2174#if defined(PYOS_OS2) && defined(PYCC_GCC)
2175 res = _getcwd2(buf, sizeof buf);
2176#else
2177 res = getcwd(buf, sizeof buf);
2178#endif
2179 Py_END_ALLOW_THREADS
2180 if (res == NULL)
2181 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002182 if (use_bytes)
2183 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002184 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002185}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002186
2187PyDoc_STRVAR(posix_getcwd__doc__,
2188"getcwd() -> path\n\n\
2189Return a unicode string representing the current working directory.");
2190
2191static PyObject *
2192posix_getcwd_unicode(PyObject *self)
2193{
2194 return posix_getcwd(0);
2195}
2196
2197PyDoc_STRVAR(posix_getcwdb__doc__,
2198"getcwdb() -> path\n\n\
2199Return a bytes string representing the current working directory.");
2200
2201static PyObject *
2202posix_getcwd_bytes(PyObject *self)
2203{
2204 return posix_getcwd(1);
2205}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002206#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002208
Guido van Rossumb6775db1994-08-01 11:34:53 +00002209#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002210PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002211"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002212Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002213
Barry Warsaw53699e91996-12-10 23:23:01 +00002214static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002215posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002216{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002217 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002218}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002219#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002220
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002221
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002222PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002223"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002224Return a list containing the names of the entries in the directory.\n\
2225\n\
2226 path: path of directory to list\n\
2227\n\
2228The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002229entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002230
Barry Warsaw53699e91996-12-10 23:23:01 +00002231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002232posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002233{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002234 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002235 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002236#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002237
Barry Warsaw53699e91996-12-10 23:23:01 +00002238 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002239 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002240 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002241 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002242 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002243 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002244 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002245 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002246
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002247 /* If on wide-character-capable OS see if argument
2248 is Unicode and if so use wide API. */
2249 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002250 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002251 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2252 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002253 Py_UNICODE *wnamebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002254 /* Overallocate for \\*.*\0 */
2255 len = PyUnicode_GET_SIZE(po);
2256 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2257 if (!wnamebuf) {
2258 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002259 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002260 }
2261 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002262 if (len > 0) {
2263 Py_UNICODE wch = wnamebuf[len-1];
2264 if (wch != L'/' && wch != L'\\' && wch != L':')
2265 wnamebuf[len++] = L'\\';
2266 wcscpy(wnamebuf + len, L"*.*");
2267 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002268 if ((d = PyList_New(0)) == NULL) {
2269 free(wnamebuf);
2270 return NULL;
2271 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002272 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2273 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002274 int error = GetLastError();
2275 if (error == ERROR_FILE_NOT_FOUND) {
2276 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002277 return d;
2278 }
2279 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002280 win32_error_unicode("FindFirstFileW", wnamebuf);
2281 free(wnamebuf);
2282 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002283 }
2284 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002285 /* Skip over . and .. */
2286 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2287 wcscmp(wFileData.cFileName, L"..") != 0) {
2288 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2289 if (v == NULL) {
2290 Py_DECREF(d);
2291 d = NULL;
2292 break;
2293 }
2294 if (PyList_Append(d, v) != 0) {
2295 Py_DECREF(v);
2296 Py_DECREF(d);
2297 d = NULL;
2298 break;
2299 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002300 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002301 }
Georg Brandl622927b2006-03-07 12:48:03 +00002302 Py_BEGIN_ALLOW_THREADS
2303 result = FindNextFileW(hFindFile, &wFileData);
2304 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002305 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2306 it got to the end of the directory. */
2307 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2308 Py_DECREF(d);
2309 win32_error_unicode("FindNextFileW", wnamebuf);
2310 FindClose(hFindFile);
2311 free(wnamebuf);
2312 return NULL;
2313 }
Georg Brandl622927b2006-03-07 12:48:03 +00002314 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002315
2316 if (FindClose(hFindFile) == FALSE) {
2317 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002318 win32_error_unicode("FindClose", wnamebuf);
2319 free(wnamebuf);
2320 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002321 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002322 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002323 return d;
2324 }
2325 /* Drop the argument parsing error as narrow strings
2326 are also valid. */
2327 PyErr_Clear();
2328 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002329
Martin v. Löwis011e8422009-05-05 04:43:17 +00002330 if (!PyArg_ParseTuple(args, "O&:listdir",
2331 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002332 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002333 if (PyObject_Size(opath)+1 > MAX_PATH) {
2334 PyErr_SetString(PyExc_ValueError, "path too long");
2335 Py_DECREF(opath);
2336 return NULL;
2337 }
2338 strcpy(namebuf, bytes2str(opath, 0));
2339 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002340 if (len > 0) {
2341 char ch = namebuf[len-1];
2342 if (ch != SEP && ch != ALTSEP && ch != ':')
2343 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002344 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002345 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002346
Barry Warsaw53699e91996-12-10 23:23:01 +00002347 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002348 return NULL;
2349
2350 hFindFile = FindFirstFile(namebuf, &FileData);
2351 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002352 int error = GetLastError();
2353 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002354 return d;
2355 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002356 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002357 }
2358 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002359 /* Skip over . and .. */
2360 if (strcmp(FileData.cFileName, ".") != 0 &&
2361 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002362 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002363 if (v == NULL) {
2364 Py_DECREF(d);
2365 d = NULL;
2366 break;
2367 }
2368 if (PyList_Append(d, v) != 0) {
2369 Py_DECREF(v);
2370 Py_DECREF(d);
2371 d = NULL;
2372 break;
2373 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002374 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002375 }
Georg Brandl622927b2006-03-07 12:48:03 +00002376 Py_BEGIN_ALLOW_THREADS
2377 result = FindNextFile(hFindFile, &FileData);
2378 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002379 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2380 it got to the end of the directory. */
2381 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2382 Py_DECREF(d);
2383 win32_error("FindNextFile", namebuf);
2384 FindClose(hFindFile);
2385 return NULL;
2386 }
Georg Brandl622927b2006-03-07 12:48:03 +00002387 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002388
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002389 if (FindClose(hFindFile) == FALSE) {
2390 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002391 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002392 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002393
2394 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002395
Tim Peters0bb44a42000-09-15 07:44:49 +00002396#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002397
2398#ifndef MAX_PATH
2399#define MAX_PATH CCHMAXPATH
2400#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002401 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002402 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002403 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002404 PyObject *d, *v;
2405 char namebuf[MAX_PATH+5];
2406 HDIR hdir = 1;
2407 ULONG srchcnt = 1;
2408 FILEFINDBUF3 ep;
2409 APIRET rc;
2410
Martin v. Löwis011e8422009-05-05 04:43:17 +00002411 if (!PyArg_ParseTuple(args, "O&:listdir",
2412 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002413 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002414 name = bytes2str(oname);
2415 len = PyObject_Size(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002416 if (len >= MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002417 release_bytes(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002418 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002419 return NULL;
2420 }
2421 strcpy(namebuf, name);
2422 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002423 if (*pt == ALTSEP)
2424 *pt = SEP;
2425 if (namebuf[len-1] != SEP)
2426 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002427 strcpy(namebuf + len, "*.*");
2428
Neal Norwitz6c913782007-10-14 03:23:09 +00002429 if ((d = PyList_New(0)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002430 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002431 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002432 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002433
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002434 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2435 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002436 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002437 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2438 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2439 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002440
2441 if (rc != NO_ERROR) {
2442 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002443 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002444 }
2445
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002446 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002447 do {
2448 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002449 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002450 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002451
2452 strcpy(namebuf, ep.achName);
2453
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002454 /* Leave Case of Name Alone -- In Native Form */
2455 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002456
Christian Heimes72b710a2008-05-26 13:28:38 +00002457 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002458 if (v == NULL) {
2459 Py_DECREF(d);
2460 d = NULL;
2461 break;
2462 }
2463 if (PyList_Append(d, v) != 0) {
2464 Py_DECREF(v);
2465 Py_DECREF(d);
2466 d = NULL;
2467 break;
2468 }
2469 Py_DECREF(v);
2470 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2471 }
2472
Martin v. Löwis011e8422009-05-05 04:43:17 +00002473 release_bytes(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002474 return d;
2475#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002476 PyObject *oname;
2477 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002478 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002479 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002480 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002481 int arg_is_unicode = 1;
2482
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002483 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002484 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2485 arg_is_unicode = 0;
2486 PyErr_Clear();
2487 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002488 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002489 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002490 name = bytes2str(oname, 1);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002491 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002492 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002493 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002494 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002495 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002496 release_bytes(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002497 return NULL;
2498 }
Georg Brandl622927b2006-03-07 12:48:03 +00002499 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002500 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002501 Py_BEGIN_ALLOW_THREADS
2502 ep = readdir(dirp);
2503 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002504 if (ep == NULL) {
2505 if (errno == 0) {
2506 break;
2507 } else {
2508 closedir(dirp);
2509 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002510 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002511 }
2512 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002513 if (ep->d_name[0] == '.' &&
2514 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002515 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002516 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002517 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002518 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002519 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002520 d = NULL;
2521 break;
2522 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002523 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002524 PyObject *w;
2525
2526 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002527 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002528 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002529 Py_DECREF(v);
2530 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002531 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002532 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002533 /* Encoding failed to decode ASCII bytes.
2534 Raise exception. */
2535 Py_DECREF(d);
2536 d = NULL;
2537 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002538 }
Just van Rossum46c97842003-02-25 21:42:15 +00002539 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002540 if (PyList_Append(d, v) != 0) {
2541 Py_DECREF(v);
2542 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002543 d = NULL;
2544 break;
2545 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002546 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002547 }
2548 closedir(dirp);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002549 release_bytes(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002550
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002551 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002552
Tim Peters0bb44a42000-09-15 07:44:49 +00002553#endif /* which OS */
2554} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002555
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002556#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002557/* A helper function for abspath on win32 */
2558static PyObject *
2559posix__getfullpathname(PyObject *self, PyObject *args)
2560{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002561 PyObject *opath;
2562 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002563 char outbuf[MAX_PATH*2];
2564 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002565#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002566 if (unicode_file_names()) {
2567 PyUnicodeObject *po;
2568 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Benjamin Petersonf608c612008-11-16 18:33:53 +00002569 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2570 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002571 Py_UNICODE *wtemp;
Benjamin Petersonf608c612008-11-16 18:33:53 +00002572 DWORD result;
2573 PyObject *v;
2574 result = GetFullPathNameW(wpath,
2575 sizeof(woutbuf)/sizeof(woutbuf[0]),
2576 woutbuf, &wtemp);
2577 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2578 woutbufp = malloc(result * sizeof(Py_UNICODE));
2579 if (!woutbufp)
2580 return PyErr_NoMemory();
2581 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2582 }
2583 if (result)
2584 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2585 else
2586 v = win32_error_unicode("GetFullPathNameW", wpath);
2587 if (woutbufp != woutbuf)
2588 free(woutbufp);
2589 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002590 }
2591 /* Drop the argument parsing error as narrow strings
2592 are also valid. */
2593 PyErr_Clear();
2594 }
2595#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002596 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2597 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002598 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002599 path = bytes2str(opath, 1);
2600 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2601 outbuf, &temp)) {
2602 win32_error("GetFullPathName", path);
2603 release_bytes(opath);
2604 return NULL;
2605 }
2606 release_bytes(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002607 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2608 return PyUnicode_Decode(outbuf, strlen(outbuf),
2609 Py_FileSystemDefaultEncoding, NULL);
2610 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002611 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002612} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002613#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002615PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002616"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002617Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002618
Barry Warsaw53699e91996-12-10 23:23:01 +00002619static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002620posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002621{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002622 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002623 PyObject *opath;
2624 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002625 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002626
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002627#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002628 if (unicode_file_names()) {
2629 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002630 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002631 Py_BEGIN_ALLOW_THREADS
2632 /* PyUnicode_AS_UNICODE OK without thread lock as
2633 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002634 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002635 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002636 if (!res)
2637 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002638 Py_INCREF(Py_None);
2639 return Py_None;
2640 }
2641 /* Drop the argument parsing error as narrow strings
2642 are also valid. */
2643 PyErr_Clear();
2644 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002645 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2646 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002647 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002648 path = bytes2str(opath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002649 Py_BEGIN_ALLOW_THREADS
2650 /* PyUnicode_AS_UNICODE OK without thread lock as
2651 it is a simple dereference. */
2652 res = CreateDirectoryA(path, NULL);
2653 Py_END_ALLOW_THREADS
2654 if (!res) {
2655 win32_error("mkdir", path);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002656 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002657 return NULL;
2658 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002659 release_bytes(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002660 Py_INCREF(Py_None);
2661 return Py_None;
2662#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002663
Martin v. Löwis011e8422009-05-05 04:43:17 +00002664 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2665 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002666 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002667 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00002668 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002669#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002670 res = mkdir(path);
2671#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002672 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002673#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002674 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002675 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002676 return posix_error_with_allocated_filename(opath);
2677 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002678 Py_INCREF(Py_None);
2679 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002680#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002681}
2682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002683
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002684/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2685#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002686#include <sys/resource.h>
2687#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002688
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002689
2690#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002691PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002692"nice(inc) -> new_priority\n\n\
2693Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002694
Barry Warsaw53699e91996-12-10 23:23:01 +00002695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002696posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002697{
2698 int increment, value;
2699
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002700 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002701 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002702
2703 /* There are two flavours of 'nice': one that returns the new
2704 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002705 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2706 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002707
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002708 If we are of the nice family that returns the new priority, we
2709 need to clear errno before the call, and check if errno is filled
2710 before calling posix_error() on a returnvalue of -1, because the
2711 -1 may be the actual new priority! */
2712
2713 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002714 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002715#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002716 if (value == 0)
2717 value = getpriority(PRIO_PROCESS, 0);
2718#endif
2719 if (value == -1 && errno != 0)
2720 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002721 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002722 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002723}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002724#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002726PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002727"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002728Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002729
Barry Warsaw53699e91996-12-10 23:23:01 +00002730static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002731posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002732{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002733#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002734 PyObject *o1, *o2;
2735 char *p1, *p2;
2736 BOOL result;
2737 if (unicode_file_names()) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002738 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2739 goto error;
2740 if (!convert_to_unicode(&o1))
2741 goto error;
2742 if (!convert_to_unicode(&o2)) {
2743 Py_DECREF(o1);
2744 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002745 }
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002746 Py_BEGIN_ALLOW_THREADS
2747 result = MoveFileW(PyUnicode_AsUnicode(o1),
2748 PyUnicode_AsUnicode(o2));
2749 Py_END_ALLOW_THREADS
2750 Py_DECREF(o1);
2751 Py_DECREF(o2);
2752 if (!result)
2753 return win32_error("rename", NULL);
2754 Py_INCREF(Py_None);
2755 return Py_None;
2756error:
2757 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002758 }
2759 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2760 return NULL;
2761 Py_BEGIN_ALLOW_THREADS
2762 result = MoveFileA(p1, p2);
2763 Py_END_ALLOW_THREADS
2764 if (!result)
2765 return win32_error("rename", NULL);
2766 Py_INCREF(Py_None);
2767 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002768#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002769 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002770#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002771}
2772
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002775"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002777
Barry Warsaw53699e91996-12-10 23:23:01 +00002778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002779posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002780{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002781#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002782 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002783#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002784 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002785#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002786}
2787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002789PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002790"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002791Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002792
Barry Warsaw53699e91996-12-10 23:23:01 +00002793static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002794posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002795{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002796#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002797 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002798#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002799 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002800#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002801}
2802
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002803
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002804#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002805PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002806"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002807Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002808
Barry Warsaw53699e91996-12-10 23:23:01 +00002809static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002810posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002811{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002812 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002813#ifdef MS_WINDOWS
2814 wchar_t *command;
2815 if (!PyArg_ParseTuple(args, "u:system", &command))
2816 return NULL;
2817#else
2818 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002819 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002820 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002821#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002822 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002823#ifdef MS_WINDOWS
2824 sts = _wsystem(command);
2825#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002826 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002827#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002828 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002829 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002830}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002831#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002832
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002834PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002835"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002836Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002837
Barry Warsaw53699e91996-12-10 23:23:01 +00002838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002839posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002840{
2841 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002842 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002843 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002844 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002845 if (i < 0)
2846 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002847 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002848}
2849
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002850
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002851PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002852"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002853Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002854
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002855PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002856"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002857Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002858
Barry Warsaw53699e91996-12-10 23:23:01 +00002859static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002860posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002861{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002862#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002863 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002864#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002865 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002866#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002867}
2868
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002869
Guido van Rossumb6775db1994-08-01 11:34:53 +00002870#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002871PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002872"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002873Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002874
Barry Warsaw53699e91996-12-10 23:23:01 +00002875static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002876posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002877{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002878 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002879 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002880
Barry Warsaw53699e91996-12-10 23:23:01 +00002881 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002882 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002883 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002884 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002885 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002886 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002887 u.sysname,
2888 u.nodename,
2889 u.release,
2890 u.version,
2891 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002892}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002893#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002894
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002895static int
2896extract_time(PyObject *t, long* sec, long* usec)
2897{
2898 long intval;
2899 if (PyFloat_Check(t)) {
2900 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002901 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002902 if (!intobj)
2903 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002904 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002905 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002906 if (intval == -1 && PyErr_Occurred())
2907 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002908 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002909 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002910 if (*usec < 0)
2911 /* If rounding gave us a negative number,
2912 truncate. */
2913 *usec = 0;
2914 return 0;
2915 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002916 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002917 if (intval == -1 && PyErr_Occurred())
2918 return -1;
2919 *sec = intval;
2920 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002921 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002922}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002924PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002925"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002926utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002927Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002928second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002929
Barry Warsaw53699e91996-12-10 23:23:01 +00002930static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002931posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002932{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002933#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002934 PyObject *arg;
2935 PyUnicodeObject *obwpath;
2936 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002937 PyObject *oapath;
2938 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002939 HANDLE hFile;
2940 long atimesec, mtimesec, ausec, musec;
2941 FILETIME atime, mtime;
2942 PyObject *result = NULL;
2943
2944 if (unicode_file_names()) {
2945 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2946 wpath = PyUnicode_AS_UNICODE(obwpath);
2947 Py_BEGIN_ALLOW_THREADS
2948 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002949 NULL, OPEN_EXISTING,
2950 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002951 Py_END_ALLOW_THREADS
2952 if (hFile == INVALID_HANDLE_VALUE)
2953 return win32_error_unicode("utime", wpath);
2954 } else
2955 /* Drop the argument parsing error as narrow strings
2956 are also valid. */
2957 PyErr_Clear();
2958 }
2959 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002960 if (!PyArg_ParseTuple(args, "O&O:utime",
2961 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002962 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002963 apath = bytes2str(oapath, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002964 Py_BEGIN_ALLOW_THREADS
2965 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002966 NULL, OPEN_EXISTING,
2967 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002968 Py_END_ALLOW_THREADS
2969 if (hFile == INVALID_HANDLE_VALUE) {
2970 win32_error("utime", apath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002971 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002972 return NULL;
2973 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002974 release_bytes(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002975 }
2976
2977 if (arg == Py_None) {
2978 SYSTEMTIME now;
2979 GetSystemTime(&now);
2980 if (!SystemTimeToFileTime(&now, &mtime) ||
2981 !SystemTimeToFileTime(&now, &atime)) {
2982 win32_error("utime", NULL);
2983 goto done;
2984 }
2985 }
2986 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2987 PyErr_SetString(PyExc_TypeError,
2988 "utime() arg 2 must be a tuple (atime, mtime)");
2989 goto done;
2990 }
2991 else {
2992 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2993 &atimesec, &ausec) == -1)
2994 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002995 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002996 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2997 &mtimesec, &musec) == -1)
2998 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002999 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003000 }
3001 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3002 /* Avoid putting the file name into the error here,
3003 as that may confuse the user into believing that
3004 something is wrong with the file, when it also
3005 could be the time stamp that gives a problem. */
3006 win32_error("utime", NULL);
3007 }
3008 Py_INCREF(Py_None);
3009 result = Py_None;
3010done:
3011 CloseHandle(hFile);
3012 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003013#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003014
Martin v. Löwis011e8422009-05-05 04:43:17 +00003015 PyObject *opath;
3016 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003017 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00003018 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00003019 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003020
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003021#if defined(HAVE_UTIMES)
3022 struct timeval buf[2];
3023#define ATIME buf[0].tv_sec
3024#define MTIME buf[1].tv_sec
3025#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003026/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003027 struct utimbuf buf;
3028#define ATIME buf.actime
3029#define MTIME buf.modtime
3030#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003031#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003032 time_t buf[2];
3033#define ATIME buf[0]
3034#define MTIME buf[1]
3035#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003036#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003037
Mark Hammond817c9292003-12-03 01:22:38 +00003038
Martin v. Löwis011e8422009-05-05 04:43:17 +00003039 if (!PyArg_ParseTuple(args, "O&O:utime",
3040 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003041 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003042 path = bytes2str(opath, 1);
Barry Warsaw3cef8562000-05-01 16:17:24 +00003043 if (arg == Py_None) {
3044 /* optional time values not given */
3045 Py_BEGIN_ALLOW_THREADS
3046 res = utime(path, NULL);
3047 Py_END_ALLOW_THREADS
3048 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003049 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00003050 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003051 "utime() arg 2 must be a tuple (atime, mtime)");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003052 release_bytes(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00003053 return NULL;
3054 }
3055 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003056 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00003057 &atime, &ausec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003058 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003059 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00003060 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003061 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00003062 &mtime, &musec) == -1) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003063 release_bytes(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003064 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00003065 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00003066 ATIME = atime;
3067 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003068#ifdef HAVE_UTIMES
3069 buf[0].tv_usec = ausec;
3070 buf[1].tv_usec = musec;
3071 Py_BEGIN_ALLOW_THREADS
3072 res = utimes(path, buf);
3073 Py_END_ALLOW_THREADS
3074#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00003075 Py_BEGIN_ALLOW_THREADS
3076 res = utime(path, UTIME_ARG);
3077 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003078#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00003079 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00003080 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003081 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00003082 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00003083 release_bytes(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00003084 Py_INCREF(Py_None);
3085 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003086#undef UTIME_ARG
3087#undef ATIME
3088#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003089#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003090}
3091
Guido van Rossum85e3b011991-06-03 12:42:10 +00003092
Guido van Rossum3b066191991-06-04 19:40:25 +00003093/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003094
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003095PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003096"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003097Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003098
Barry Warsaw53699e91996-12-10 23:23:01 +00003099static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003100posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003101{
3102 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003103 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003104 return NULL;
3105 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00003106 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003107}
3108
Martin v. Löwis114619e2002-10-07 06:44:21 +00003109#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3110static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003111free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003112{
Martin v. Löwis725507b2006-03-07 12:08:51 +00003113 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00003114 for (i = 0; i < count; i++)
3115 PyMem_Free(array[i]);
3116 PyMem_DEL(array);
3117}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003118
Antoine Pitrou69f71142009-05-24 21:25:49 +00003119static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003120int fsconvert_strdup(PyObject *o, char**out)
3121{
3122 PyObject *bytes;
3123 Py_ssize_t size;
3124 if (!PyUnicode_FSConverter(o, &bytes))
3125 return 0;
3126 size = PyObject_Size(bytes);
3127 *out = PyMem_Malloc(size+1);
3128 if (!*out)
3129 return 0;
3130 /* Don't lock bytes, as we hold the GIL */
3131 memcpy(*out, bytes2str(bytes, 0), size+1);
3132 Py_DECREF(bytes);
3133 return 1;
3134}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003135#endif
3136
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003137
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003138#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003139PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003140"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003141Execute an executable path with arguments, replacing current process.\n\
3142\n\
3143 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003144 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003145
Barry Warsaw53699e91996-12-10 23:23:01 +00003146static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003147posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003148{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003149 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003150 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003151 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003152 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003153 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003154 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003155
Guido van Rossum89b33251993-10-22 14:26:06 +00003156 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003157 argv is a list or tuple of strings. */
3158
Martin v. Löwis011e8422009-05-05 04:43:17 +00003159 if (!PyArg_ParseTuple(args, "O&O:execv",
3160 PyUnicode_FSConverter,
3161 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003162 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003163 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003164 if (PyList_Check(argv)) {
3165 argc = PyList_Size(argv);
3166 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003167 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003168 else if (PyTuple_Check(argv)) {
3169 argc = PyTuple_Size(argv);
3170 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003171 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003172 else {
Fred Drake661ea262000-10-24 19:57:45 +00003173 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003174 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003175 return NULL;
3176 }
Thomas Heller6790d602007-08-30 17:15:14 +00003177 if (argc < 1) {
3178 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003179 release_bytes(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003180 return NULL;
3181 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003182
Barry Warsaw53699e91996-12-10 23:23:01 +00003183 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003184 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003185 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003186 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003187 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003188 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003189 if (!fsconvert_strdup((*getitem)(argv, i),
3190 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003191 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003192 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003193 "execv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003194 release_bytes(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003195 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003196
Guido van Rossum85e3b011991-06-03 12:42:10 +00003197 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003198 }
3199 argvlist[argc] = NULL;
3200
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003201 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003202
Guido van Rossum85e3b011991-06-03 12:42:10 +00003203 /* If we get here it's definitely an error */
3204
Martin v. Löwis114619e2002-10-07 06:44:21 +00003205 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003206 release_bytes(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003207 return posix_error();
3208}
3209
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003211PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003212"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213Execute a path with arguments and environment, replacing current process.\n\
3214\n\
3215 path: path of executable file\n\
3216 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003217 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003218
Barry Warsaw53699e91996-12-10 23:23:01 +00003219static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003220posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003221{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003222 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003223 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003224 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003225 char **argvlist;
3226 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003227 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003228 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003229 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003230 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003231
3232 /* execve has three arguments: (path, argv, env), where
3233 argv is a list or tuple of strings and env is a dictionary
3234 like posix.environ. */
3235
Martin v. Löwis011e8422009-05-05 04:43:17 +00003236 if (!PyArg_ParseTuple(args, "O&OO:execve",
3237 PyUnicode_FSConverter,
3238 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003239 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003240 path = bytes2str(opath, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00003241 if (PyList_Check(argv)) {
3242 argc = PyList_Size(argv);
3243 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003244 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003245 else if (PyTuple_Check(argv)) {
3246 argc = PyTuple_Size(argv);
3247 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003248 }
3249 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003250 PyErr_SetString(PyExc_TypeError,
3251 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003252 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003253 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003254 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003255 PyErr_SetString(PyExc_TypeError,
3256 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003257 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003258 }
3259
Barry Warsaw53699e91996-12-10 23:23:01 +00003260 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003261 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003262 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003263 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003264 }
3265 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003266 if (!fsconvert_strdup((*getitem)(argv, i),
3267 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003268 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003269 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003270 goto fail_1;
3271 }
3272 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003273 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003274 argvlist[argc] = NULL;
3275
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003276 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003277 if (i < 0)
3278 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003279 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003280 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003281 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003282 goto fail_1;
3283 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003284 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003285 keys = PyMapping_Keys(env);
3286 vals = PyMapping_Values(env);
3287 if (!keys || !vals)
3288 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003289 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3290 PyErr_SetString(PyExc_TypeError,
3291 "execve(): env.keys() or env.values() is not a list");
3292 goto fail_2;
3293 }
Tim Peters5aa91602002-01-30 05:46:57 +00003294
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003295 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003296 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003297 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003298
3299 key = PyList_GetItem(keys, pos);
3300 val = PyList_GetItem(vals, pos);
3301 if (!key || !val)
3302 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003303
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003304 if (!PyArg_Parse(
3305 key,
3306 "s;execve() arg 3 contains a non-string key",
3307 &k) ||
3308 !PyArg_Parse(
3309 val,
3310 "s;execve() arg 3 contains a non-string value",
3311 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003312 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003313 goto fail_2;
3314 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003315
3316#if defined(PYOS_OS2)
3317 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3318 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3319#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003320 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003321 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003322 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003323 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003324 goto fail_2;
3325 }
Tim Petersc8996f52001-12-03 20:41:00 +00003326 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003327 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003328#if defined(PYOS_OS2)
3329 }
3330#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003331 }
3332 envlist[envc] = 0;
3333
3334 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003335
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003336 /* If we get here it's definitely an error */
3337
3338 (void) posix_error();
3339
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003340 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003341 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003342 PyMem_DEL(envlist[envc]);
3343 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003344 fail_1:
3345 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003346 Py_XDECREF(vals);
3347 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003348 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003349 release_bytes(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003350 return NULL;
3351}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003352#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003353
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003354
Guido van Rossuma1065681999-01-25 23:20:23 +00003355#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003356PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003357"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003358Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003359\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003360 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003361 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003362 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003363
3364static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003365posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003366{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003367 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003368 char *path;
3369 PyObject *argv;
3370 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003371 int mode, i;
3372 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003373 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003374 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003375
3376 /* spawnv has three arguments: (mode, path, argv), where
3377 argv is a list or tuple of strings. */
3378
Martin v. Löwis011e8422009-05-05 04:43:17 +00003379 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3380 PyUnicode_FSConverter,
3381 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003382 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003383 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003384 if (PyList_Check(argv)) {
3385 argc = PyList_Size(argv);
3386 getitem = PyList_GetItem;
3387 }
3388 else if (PyTuple_Check(argv)) {
3389 argc = PyTuple_Size(argv);
3390 getitem = PyTuple_GetItem;
3391 }
3392 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003393 PyErr_SetString(PyExc_TypeError,
3394 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003395 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003396 return NULL;
3397 }
3398
3399 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003400 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003401 release_bytes(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003402 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003403 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003404 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003405 if (!fsconvert_strdup((*getitem)(argv, i),
3406 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003407 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003408 PyErr_SetString(
3409 PyExc_TypeError,
3410 "spawnv() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003411 release_bytes(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003412 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003413 }
3414 }
3415 argvlist[argc] = NULL;
3416
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003417#if defined(PYOS_OS2) && defined(PYCC_GCC)
3418 Py_BEGIN_ALLOW_THREADS
3419 spawnval = spawnv(mode, path, argvlist);
3420 Py_END_ALLOW_THREADS
3421#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003422 if (mode == _OLD_P_OVERLAY)
3423 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003424
Tim Peters25059d32001-12-07 20:35:43 +00003425 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003426 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003427 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003428#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003429
Martin v. Löwis114619e2002-10-07 06:44:21 +00003430 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003431 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003432
Fred Drake699f3522000-06-29 21:12:41 +00003433 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003434 return posix_error();
3435 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003436#if SIZEOF_LONG == SIZEOF_VOID_P
3437 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003438#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003439 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003440#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003441}
3442
3443
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003444PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003445"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003446Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003447\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003448 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003449 path: path of executable file\n\
3450 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003451 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003452
3453static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003454posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003455{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003456 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003457 char *path;
3458 PyObject *argv, *env;
3459 char **argvlist;
3460 char **envlist;
3461 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003462 int mode, pos, envc;
3463 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003464 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003465 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003466 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003467
3468 /* spawnve has four arguments: (mode, path, argv, env), where
3469 argv is a list or tuple of strings and env is a dictionary
3470 like posix.environ. */
3471
Martin v. Löwis011e8422009-05-05 04:43:17 +00003472 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3473 PyUnicode_FSConverter,
3474 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003475 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003476 path = bytes2str(opath, 1);
Guido van Rossuma1065681999-01-25 23:20:23 +00003477 if (PyList_Check(argv)) {
3478 argc = PyList_Size(argv);
3479 getitem = PyList_GetItem;
3480 }
3481 else if (PyTuple_Check(argv)) {
3482 argc = PyTuple_Size(argv);
3483 getitem = PyTuple_GetItem;
3484 }
3485 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003486 PyErr_SetString(PyExc_TypeError,
3487 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003488 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003489 }
3490 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003491 PyErr_SetString(PyExc_TypeError,
3492 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003493 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003494 }
3495
3496 argvlist = PyMem_NEW(char *, argc+1);
3497 if (argvlist == NULL) {
3498 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003499 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003500 }
3501 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003502 if (!fsconvert_strdup((*getitem)(argv, i),
3503 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003504 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003505 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003506 goto fail_1;
3507 }
3508 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003509 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003510 argvlist[argc] = NULL;
3511
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003512 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003513 if (i < 0)
3514 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003515 envlist = PyMem_NEW(char *, i + 1);
3516 if (envlist == NULL) {
3517 PyErr_NoMemory();
3518 goto fail_1;
3519 }
3520 envc = 0;
3521 keys = PyMapping_Keys(env);
3522 vals = PyMapping_Values(env);
3523 if (!keys || !vals)
3524 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003525 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3526 PyErr_SetString(PyExc_TypeError,
3527 "spawnve(): env.keys() or env.values() is not a list");
3528 goto fail_2;
3529 }
Tim Peters5aa91602002-01-30 05:46:57 +00003530
Guido van Rossuma1065681999-01-25 23:20:23 +00003531 for (pos = 0; pos < i; pos++) {
3532 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003533 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003534
3535 key = PyList_GetItem(keys, pos);
3536 val = PyList_GetItem(vals, pos);
3537 if (!key || !val)
3538 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003539
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003540 if (!PyArg_Parse(
3541 key,
3542 "s;spawnve() arg 3 contains a non-string key",
3543 &k) ||
3544 !PyArg_Parse(
3545 val,
3546 "s;spawnve() arg 3 contains a non-string value",
3547 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003548 {
3549 goto fail_2;
3550 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003551 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003552 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003553 if (p == NULL) {
3554 PyErr_NoMemory();
3555 goto fail_2;
3556 }
Tim Petersc8996f52001-12-03 20:41:00 +00003557 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003558 envlist[envc++] = p;
3559 }
3560 envlist[envc] = 0;
3561
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003562#if defined(PYOS_OS2) && defined(PYCC_GCC)
3563 Py_BEGIN_ALLOW_THREADS
3564 spawnval = spawnve(mode, path, argvlist, envlist);
3565 Py_END_ALLOW_THREADS
3566#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003567 if (mode == _OLD_P_OVERLAY)
3568 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003569
3570 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003571 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003572 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003573#endif
Tim Peters25059d32001-12-07 20:35:43 +00003574
Fred Drake699f3522000-06-29 21:12:41 +00003575 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003576 (void) posix_error();
3577 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003578#if SIZEOF_LONG == SIZEOF_VOID_P
3579 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003580#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003581 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003582#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003583
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003584 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003585 while (--envc >= 0)
3586 PyMem_DEL(envlist[envc]);
3587 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003588 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003589 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003590 Py_XDECREF(vals);
3591 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003592 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003593 release_bytes(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003594 return res;
3595}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003596
3597/* OS/2 supports spawnvp & spawnvpe natively */
3598#if defined(PYOS_OS2)
3599PyDoc_STRVAR(posix_spawnvp__doc__,
3600"spawnvp(mode, file, args)\n\n\
3601Execute the program 'file' in a new process, using the environment\n\
3602search path to find the file.\n\
3603\n\
3604 mode: mode of process creation\n\
3605 file: executable file name\n\
3606 args: tuple or list of strings");
3607
3608static PyObject *
3609posix_spawnvp(PyObject *self, PyObject *args)
3610{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003611 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003612 char *path;
3613 PyObject *argv;
3614 char **argvlist;
3615 int mode, i, argc;
3616 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003617 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003618
3619 /* spawnvp has three arguments: (mode, path, argv), where
3620 argv is a list or tuple of strings. */
3621
Martin v. Löwis011e8422009-05-05 04:43:17 +00003622 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3623 PyUnicode_FSConverter,
3624 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003625 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003626 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003627 if (PyList_Check(argv)) {
3628 argc = PyList_Size(argv);
3629 getitem = PyList_GetItem;
3630 }
3631 else if (PyTuple_Check(argv)) {
3632 argc = PyTuple_Size(argv);
3633 getitem = PyTuple_GetItem;
3634 }
3635 else {
3636 PyErr_SetString(PyExc_TypeError,
3637 "spawnvp() arg 2 must be a tuple or list");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003638 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003639 return NULL;
3640 }
3641
3642 argvlist = PyMem_NEW(char *, argc+1);
3643 if (argvlist == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003644 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003645 return PyErr_NoMemory();
3646 }
3647 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003648 if (!fsconvert_strdup((*getitem)(argv, i),
3649 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003650 free_string_array(argvlist, i);
3651 PyErr_SetString(
3652 PyExc_TypeError,
3653 "spawnvp() arg 2 must contain only strings");
Martin v. Löwis011e8422009-05-05 04:43:17 +00003654 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003655 return NULL;
3656 }
3657 }
3658 argvlist[argc] = NULL;
3659
3660 Py_BEGIN_ALLOW_THREADS
3661#if defined(PYCC_GCC)
3662 spawnval = spawnvp(mode, path, argvlist);
3663#else
3664 spawnval = _spawnvp(mode, path, argvlist);
3665#endif
3666 Py_END_ALLOW_THREADS
3667
3668 free_string_array(argvlist, argc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00003669 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003670
3671 if (spawnval == -1)
3672 return posix_error();
3673 else
3674 return Py_BuildValue("l", (long) spawnval);
3675}
3676
3677
3678PyDoc_STRVAR(posix_spawnvpe__doc__,
3679"spawnvpe(mode, file, args, env)\n\n\
3680Execute the program 'file' in a new process, using the environment\n\
3681search path to find the file.\n\
3682\n\
3683 mode: mode of process creation\n\
3684 file: executable file name\n\
3685 args: tuple or list of arguments\n\
3686 env: dictionary of strings mapping to strings");
3687
3688static PyObject *
3689posix_spawnvpe(PyObject *self, PyObject *args)
3690{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003691 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003692 char *path;
3693 PyObject *argv, *env;
3694 char **argvlist;
3695 char **envlist;
3696 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3697 int mode, i, pos, argc, envc;
3698 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003699 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003700 int lastarg = 0;
3701
3702 /* spawnvpe has four arguments: (mode, path, argv, env), where
3703 argv is a list or tuple of strings and env is a dictionary
3704 like posix.environ. */
3705
3706 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003707 PyUnicode_FSConverter,
3708 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003709 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003710 path = bytes2str(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003711 if (PyList_Check(argv)) {
3712 argc = PyList_Size(argv);
3713 getitem = PyList_GetItem;
3714 }
3715 else if (PyTuple_Check(argv)) {
3716 argc = PyTuple_Size(argv);
3717 getitem = PyTuple_GetItem;
3718 }
3719 else {
3720 PyErr_SetString(PyExc_TypeError,
3721 "spawnvpe() arg 2 must be a tuple or list");
3722 goto fail_0;
3723 }
3724 if (!PyMapping_Check(env)) {
3725 PyErr_SetString(PyExc_TypeError,
3726 "spawnvpe() arg 3 must be a mapping object");
3727 goto fail_0;
3728 }
3729
3730 argvlist = PyMem_NEW(char *, argc+1);
3731 if (argvlist == NULL) {
3732 PyErr_NoMemory();
3733 goto fail_0;
3734 }
3735 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003736 if (!fsconvert_strdup((*getitem)(argv, i),
3737 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003738 {
3739 lastarg = i;
3740 goto fail_1;
3741 }
3742 }
3743 lastarg = argc;
3744 argvlist[argc] = NULL;
3745
3746 i = PyMapping_Size(env);
3747 if (i < 0)
3748 goto fail_1;
3749 envlist = PyMem_NEW(char *, i + 1);
3750 if (envlist == NULL) {
3751 PyErr_NoMemory();
3752 goto fail_1;
3753 }
3754 envc = 0;
3755 keys = PyMapping_Keys(env);
3756 vals = PyMapping_Values(env);
3757 if (!keys || !vals)
3758 goto fail_2;
3759 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3760 PyErr_SetString(PyExc_TypeError,
3761 "spawnvpe(): env.keys() or env.values() is not a list");
3762 goto fail_2;
3763 }
3764
3765 for (pos = 0; pos < i; pos++) {
3766 char *p, *k, *v;
3767 size_t len;
3768
3769 key = PyList_GetItem(keys, pos);
3770 val = PyList_GetItem(vals, pos);
3771 if (!key || !val)
3772 goto fail_2;
3773
3774 if (!PyArg_Parse(
3775 key,
3776 "s;spawnvpe() arg 3 contains a non-string key",
3777 &k) ||
3778 !PyArg_Parse(
3779 val,
3780 "s;spawnvpe() arg 3 contains a non-string value",
3781 &v))
3782 {
3783 goto fail_2;
3784 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003785 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003786 p = PyMem_NEW(char, len);
3787 if (p == NULL) {
3788 PyErr_NoMemory();
3789 goto fail_2;
3790 }
3791 PyOS_snprintf(p, len, "%s=%s", k, v);
3792 envlist[envc++] = p;
3793 }
3794 envlist[envc] = 0;
3795
3796 Py_BEGIN_ALLOW_THREADS
3797#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003798 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003799#else
Christian Heimes292d3512008-02-03 16:51:08 +00003800 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003801#endif
3802 Py_END_ALLOW_THREADS
3803
3804 if (spawnval == -1)
3805 (void) posix_error();
3806 else
3807 res = Py_BuildValue("l", (long) spawnval);
3808
3809 fail_2:
3810 while (--envc >= 0)
3811 PyMem_DEL(envlist[envc]);
3812 PyMem_DEL(envlist);
3813 fail_1:
3814 free_string_array(argvlist, lastarg);
3815 Py_XDECREF(vals);
3816 Py_XDECREF(keys);
3817 fail_0:
Martin v. Löwis011e8422009-05-05 04:43:17 +00003818 release_bytes(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003819 return res;
3820}
3821#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003822#endif /* HAVE_SPAWNV */
3823
3824
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003825#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003827"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003828Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3829\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003830Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003831
3832static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003833posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003834{
Benjamin Peterson51838562009-10-04 20:35:30 +00003835 pid_t pid;
3836 int result;
3837 _PyImport_AcquireLock();
3838 pid = fork1();
3839 result = _PyImport_ReleaseLock();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003840 if (pid == -1)
3841 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003842 if (pid == 0)
3843 PyOS_AfterFork();
Benjamin Peterson51838562009-10-04 20:35:30 +00003844 if (result < 0) {
3845 /* Don't clobber the OSError if the fork failed. */
3846 PyErr_SetString(PyExc_RuntimeError,
3847 "not holding the import lock");
3848 return NULL;
3849 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003850 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003851}
3852#endif
3853
3854
Guido van Rossumad0ee831995-03-01 10:34:45 +00003855#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003856PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003857"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003858Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003859Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003860
Barry Warsaw53699e91996-12-10 23:23:01 +00003861static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003862posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003863{
Benjamin Peterson51838562009-10-04 20:35:30 +00003864 pid_t pid;
3865 int result;
3866 _PyImport_AcquireLock();
3867 pid = fork();
3868 result = _PyImport_ReleaseLock();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003869 if (pid == -1)
3870 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003871 if (pid == 0)
3872 PyOS_AfterFork();
Benjamin Peterson51838562009-10-04 20:35:30 +00003873 if (result < 0) {
3874 /* Don't clobber the OSError if the fork failed. */
3875 PyErr_SetString(PyExc_RuntimeError,
3876 "not holding the import lock");
3877 return NULL;
3878 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003879 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003880}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003881#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003882
Neal Norwitzb59798b2003-03-21 01:43:31 +00003883/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003884/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3885#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003886#define DEV_PTY_FILE "/dev/ptc"
3887#define HAVE_DEV_PTMX
3888#else
3889#define DEV_PTY_FILE "/dev/ptmx"
3890#endif
3891
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003892#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003893#ifdef HAVE_PTY_H
3894#include <pty.h>
3895#else
3896#ifdef HAVE_LIBUTIL_H
3897#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003898#endif /* HAVE_LIBUTIL_H */
3899#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003900#ifdef HAVE_STROPTS_H
3901#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003902#endif
3903#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003904
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003905#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003906PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003907"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003908Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003909
3910static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003911posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003912{
3913 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003914#ifndef HAVE_OPENPTY
3915 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003916#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003917#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003918 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003919#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003920 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003921#endif
3922#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003923
Thomas Wouters70c21a12000-07-14 14:28:33 +00003924#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003925 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3926 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003927#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003928 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3929 if (slave_name == NULL)
3930 return posix_error();
3931
3932 slave_fd = open(slave_name, O_RDWR);
3933 if (slave_fd < 0)
3934 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003935#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003936 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003937 if (master_fd < 0)
3938 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003939 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003940 /* change permission of slave */
3941 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003942 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003943 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003944 }
3945 /* unlock slave */
3946 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003947 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003948 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003949 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003950 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003951 slave_name = ptsname(master_fd); /* get name of slave */
3952 if (slave_name == NULL)
3953 return posix_error();
3954 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3955 if (slave_fd < 0)
3956 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003957#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003958 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3959 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003960#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003961 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003962#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003963#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003964#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003965
Fred Drake8cef4cf2000-06-28 16:40:38 +00003966 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003967
Fred Drake8cef4cf2000-06-28 16:40:38 +00003968}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003969#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003970
3971#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003972PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003973"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003974Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3975Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003976To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003977
3978static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003979posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003980{
Benjamin Peterson51838562009-10-04 20:35:30 +00003981 int master_fd = -1, result;
Christian Heimes400adb02008-02-01 08:12:03 +00003982 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003983
Benjamin Peterson51838562009-10-04 20:35:30 +00003984 _PyImport_AcquireLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003985 pid = forkpty(&master_fd, NULL, NULL, NULL);
Benjamin Peterson51838562009-10-04 20:35:30 +00003986 result = _PyImport_ReleaseLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003987 if (pid == -1)
3988 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003989 if (pid == 0)
3990 PyOS_AfterFork();
Benjamin Peterson51838562009-10-04 20:35:30 +00003991 if (result < 0) {
3992 /* Don't clobber the OSError if the fork failed. */
3993 PyErr_SetString(PyExc_RuntimeError,
3994 "not holding the import lock");
3995 return NULL;
3996 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003997 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003998}
3999#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004000
Guido van Rossumad0ee831995-03-01 10:34:45 +00004001#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004002PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004003"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004004Return the current process's effective 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_getegid(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)getegid());
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
Guido van Rossumad0ee831995-03-01 10:34:45 +00004014#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004016"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004018
Barry Warsaw53699e91996-12-10 23:23:01 +00004019static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004020posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004021{
Christian Heimes217cfd12007-12-02 14:31:20 +00004022 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004023}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004024#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004025
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004026
Guido van Rossumad0ee831995-03-01 10:34:45 +00004027#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004028PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004029"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004030Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004031
Barry Warsaw53699e91996-12-10 23:23:01 +00004032static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004033posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004034{
Christian Heimes217cfd12007-12-02 14:31:20 +00004035 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004036}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004037#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004038
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004040PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004041"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004042Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004043
Barry Warsaw53699e91996-12-10 23:23:01 +00004044static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004045posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004046{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004047 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004048}
4049
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004050
Fred Drakec9680921999-12-13 16:37:25 +00004051#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004052PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004053"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004054Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004055
4056static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004057posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004058{
4059 PyObject *result = NULL;
4060
Fred Drakec9680921999-12-13 16:37:25 +00004061#ifdef NGROUPS_MAX
4062#define MAX_GROUPS NGROUPS_MAX
4063#else
4064 /* defined to be 16 on Solaris7, so this should be a small number */
4065#define MAX_GROUPS 64
4066#endif
4067 gid_t grouplist[MAX_GROUPS];
4068 int n;
4069
4070 n = getgroups(MAX_GROUPS, grouplist);
4071 if (n < 0)
4072 posix_error();
4073 else {
4074 result = PyList_New(n);
4075 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00004076 int i;
4077 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004078 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00004079 if (o == NULL) {
4080 Py_DECREF(result);
4081 result = NULL;
4082 break;
4083 }
4084 PyList_SET_ITEM(result, i, o);
4085 }
4086 }
4087 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004088
Fred Drakec9680921999-12-13 16:37:25 +00004089 return result;
4090}
4091#endif
4092
Martin v. Löwis606edc12002-06-13 21:09:11 +00004093#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004094PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004095"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004096Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004097
4098static PyObject *
4099posix_getpgid(PyObject *self, PyObject *args)
4100{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004101 pid_t pid, pgid;
4102 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00004103 return NULL;
4104 pgid = getpgid(pid);
4105 if (pgid < 0)
4106 return posix_error();
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004107 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004108}
4109#endif /* HAVE_GETPGID */
4110
4111
Guido van Rossumb6775db1994-08-01 11:34:53 +00004112#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004113PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004114"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004115Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004116
Barry Warsaw53699e91996-12-10 23:23:01 +00004117static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004118posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004119{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004120#ifdef GETPGRP_HAVE_ARG
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004121 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004122#else /* GETPGRP_HAVE_ARG */
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004123 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004124#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004125}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004126#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004127
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004128
Guido van Rossumb6775db1994-08-01 11:34:53 +00004129#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004130PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004131"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004132Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004133
Barry Warsaw53699e91996-12-10 23:23:01 +00004134static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004135posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004136{
Guido van Rossum64933891994-10-20 21:56:42 +00004137#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00004138 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004139#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004140 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004141#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00004142 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004143 Py_INCREF(Py_None);
4144 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004145}
4146
Guido van Rossumb6775db1994-08-01 11:34:53 +00004147#endif /* HAVE_SETPGRP */
4148
Guido van Rossumad0ee831995-03-01 10:34:45 +00004149#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004150PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004151"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004152Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004153
Barry Warsaw53699e91996-12-10 23:23:01 +00004154static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004155posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004156{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004157 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004158}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004159#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004161
Fred Drake12c6e2d1999-12-14 21:25:03 +00004162#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004163PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004164"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004165Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004166
4167static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004168posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004169{
Neal Norwitze241ce82003-02-17 18:17:05 +00004170 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00004171 char *name;
4172 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004173
Fred Drakea30680b2000-12-06 21:24:28 +00004174 errno = 0;
4175 name = getlogin();
4176 if (name == NULL) {
4177 if (errno)
4178 posix_error();
4179 else
4180 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00004181 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00004182 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00004183 else
Neal Norwitz93c56822007-08-26 07:10:06 +00004184 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00004185 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004186
Fred Drake12c6e2d1999-12-14 21:25:03 +00004187 return result;
4188}
4189#endif
4190
Guido van Rossumad0ee831995-03-01 10:34:45 +00004191#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004192PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004193"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004194Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004195
Barry Warsaw53699e91996-12-10 23:23:01 +00004196static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004197posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004198{
Christian Heimes217cfd12007-12-02 14:31:20 +00004199 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004200}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004201#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004202
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004203
Guido van Rossumad0ee831995-03-01 10:34:45 +00004204#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004205PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004206"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004207Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004208
Barry Warsaw53699e91996-12-10 23:23:01 +00004209static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004210posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004211{
Christian Heimes292d3512008-02-03 16:51:08 +00004212 pid_t pid;
4213 int sig;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004214 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004215 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004216#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004217 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4218 APIRET rc;
4219 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004220 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004221
4222 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4223 APIRET rc;
4224 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004225 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004226
4227 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004228 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004229#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004230 if (kill(pid, sig) == -1)
4231 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004232#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004233 Py_INCREF(Py_None);
4234 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004235}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004236#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004237
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004238#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004239PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004240"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004241Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004242
4243static PyObject *
4244posix_killpg(PyObject *self, PyObject *args)
4245{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004246 int sig;
4247 pid_t pgid;
4248 /* XXX some man pages make the `pgid` parameter an int, others
4249 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4250 take the same type. Moreover, pid_t is always at least as wide as
4251 int (else compilation of this module fails), which is safe. */
4252 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004253 return NULL;
4254 if (killpg(pgid, sig) == -1)
4255 return posix_error();
4256 Py_INCREF(Py_None);
4257 return Py_None;
4258}
4259#endif
4260
Guido van Rossumc0125471996-06-28 18:55:32 +00004261#ifdef HAVE_PLOCK
4262
4263#ifdef HAVE_SYS_LOCK_H
4264#include <sys/lock.h>
4265#endif
4266
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004267PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004268"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004269Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004270
Barry Warsaw53699e91996-12-10 23:23:01 +00004271static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004272posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004273{
4274 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004275 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004276 return NULL;
4277 if (plock(op) == -1)
4278 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004279 Py_INCREF(Py_None);
4280 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004281}
4282#endif
4283
Guido van Rossumb6775db1994-08-01 11:34:53 +00004284#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004285PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004286"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004287Set the current process's user id.");
4288
Barry Warsaw53699e91996-12-10 23:23:01 +00004289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004290posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004291{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004292 long uid_arg;
4293 uid_t uid;
4294 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004295 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004296 uid = uid_arg;
4297 if (uid != uid_arg) {
4298 PyErr_SetString(PyExc_OverflowError, "user id too big");
4299 return NULL;
4300 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004301 if (setuid(uid) < 0)
4302 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004303 Py_INCREF(Py_None);
4304 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004305}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004306#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004307
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004308
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004309#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004310PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004311"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004312Set the current process's effective user id.");
4313
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004314static PyObject *
4315posix_seteuid (PyObject *self, PyObject *args)
4316{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004317 long euid_arg;
4318 uid_t euid;
4319 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004320 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004321 euid = euid_arg;
4322 if (euid != euid_arg) {
4323 PyErr_SetString(PyExc_OverflowError, "user id too big");
4324 return NULL;
4325 }
4326 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004327 return posix_error();
4328 } else {
4329 Py_INCREF(Py_None);
4330 return Py_None;
4331 }
4332}
4333#endif /* HAVE_SETEUID */
4334
4335#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004336PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004337"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004338Set the current process's effective group id.");
4339
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004340static PyObject *
4341posix_setegid (PyObject *self, PyObject *args)
4342{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004343 long egid_arg;
4344 gid_t egid;
4345 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004346 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004347 egid = egid_arg;
4348 if (egid != egid_arg) {
4349 PyErr_SetString(PyExc_OverflowError, "group id too big");
4350 return NULL;
4351 }
4352 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004353 return posix_error();
4354 } else {
4355 Py_INCREF(Py_None);
4356 return Py_None;
4357 }
4358}
4359#endif /* HAVE_SETEGID */
4360
4361#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004362PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004363"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004364Set the current process's real and effective user ids.");
4365
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004366static PyObject *
4367posix_setreuid (PyObject *self, PyObject *args)
4368{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004369 long ruid_arg, euid_arg;
4370 uid_t ruid, euid;
4371 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004372 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004373 ruid = ruid_arg;
4374 euid = euid_arg;
4375 if (euid != euid_arg || ruid != ruid_arg) {
4376 PyErr_SetString(PyExc_OverflowError, "user id too big");
4377 return NULL;
4378 }
4379 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004380 return posix_error();
4381 } else {
4382 Py_INCREF(Py_None);
4383 return Py_None;
4384 }
4385}
4386#endif /* HAVE_SETREUID */
4387
4388#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004389PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004390"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004391Set the current process's real and effective group ids.");
4392
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004393static PyObject *
4394posix_setregid (PyObject *self, PyObject *args)
4395{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004396 long rgid_arg, egid_arg;
4397 gid_t rgid, egid;
4398 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004399 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004400 rgid = rgid_arg;
4401 egid = egid_arg;
4402 if (egid != egid_arg || rgid != rgid_arg) {
4403 PyErr_SetString(PyExc_OverflowError, "group id too big");
4404 return NULL;
4405 }
4406 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004407 return posix_error();
4408 } else {
4409 Py_INCREF(Py_None);
4410 return Py_None;
4411 }
4412}
4413#endif /* HAVE_SETREGID */
4414
Guido van Rossumb6775db1994-08-01 11:34:53 +00004415#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004416PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004417"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004418Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004419
Barry Warsaw53699e91996-12-10 23:23:01 +00004420static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004421posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004422{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004423 long gid_arg;
4424 gid_t gid;
4425 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004426 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004427 gid = gid_arg;
4428 if (gid != gid_arg) {
4429 PyErr_SetString(PyExc_OverflowError, "group id too big");
4430 return NULL;
4431 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004432 if (setgid(gid) < 0)
4433 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004434 Py_INCREF(Py_None);
4435 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004436}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004437#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004438
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004439#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004440PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004441"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004442Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004443
4444static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004445posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004446{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004447 int i, len;
4448 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004449
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004450 if (!PySequence_Check(groups)) {
4451 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4452 return NULL;
4453 }
4454 len = PySequence_Size(groups);
4455 if (len > MAX_GROUPS) {
4456 PyErr_SetString(PyExc_ValueError, "too many groups");
4457 return NULL;
4458 }
4459 for(i = 0; i < len; i++) {
4460 PyObject *elem;
4461 elem = PySequence_GetItem(groups, i);
4462 if (!elem)
4463 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004464 if (!PyLong_Check(elem)) {
4465 PyErr_SetString(PyExc_TypeError,
4466 "groups must be integers");
4467 Py_DECREF(elem);
4468 return NULL;
4469 } else {
4470 unsigned long x = PyLong_AsUnsignedLong(elem);
4471 if (PyErr_Occurred()) {
4472 PyErr_SetString(PyExc_TypeError,
4473 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004474 Py_DECREF(elem);
4475 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004476 }
Georg Brandla13c2442005-11-22 19:30:31 +00004477 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004478 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004479 if (grouplist[i] != x) {
4480 PyErr_SetString(PyExc_TypeError,
4481 "group id too big");
4482 Py_DECREF(elem);
4483 return NULL;
4484 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004485 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004486 Py_DECREF(elem);
4487 }
4488
4489 if (setgroups(len, grouplist) < 0)
4490 return posix_error();
4491 Py_INCREF(Py_None);
4492 return Py_None;
4493}
4494#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004495
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004496#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4497static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004498wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004499{
4500 PyObject *result;
4501 static PyObject *struct_rusage;
4502
4503 if (pid == -1)
4504 return posix_error();
4505
4506 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004507 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004508 if (m == NULL)
4509 return NULL;
4510 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4511 Py_DECREF(m);
4512 if (struct_rusage == NULL)
4513 return NULL;
4514 }
4515
4516 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4517 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4518 if (!result)
4519 return NULL;
4520
4521#ifndef doubletime
4522#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4523#endif
4524
4525 PyStructSequence_SET_ITEM(result, 0,
4526 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4527 PyStructSequence_SET_ITEM(result, 1,
4528 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4529#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004530 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004531 SET_INT(result, 2, ru->ru_maxrss);
4532 SET_INT(result, 3, ru->ru_ixrss);
4533 SET_INT(result, 4, ru->ru_idrss);
4534 SET_INT(result, 5, ru->ru_isrss);
4535 SET_INT(result, 6, ru->ru_minflt);
4536 SET_INT(result, 7, ru->ru_majflt);
4537 SET_INT(result, 8, ru->ru_nswap);
4538 SET_INT(result, 9, ru->ru_inblock);
4539 SET_INT(result, 10, ru->ru_oublock);
4540 SET_INT(result, 11, ru->ru_msgsnd);
4541 SET_INT(result, 12, ru->ru_msgrcv);
4542 SET_INT(result, 13, ru->ru_nsignals);
4543 SET_INT(result, 14, ru->ru_nvcsw);
4544 SET_INT(result, 15, ru->ru_nivcsw);
4545#undef SET_INT
4546
4547 if (PyErr_Occurred()) {
4548 Py_DECREF(result);
4549 return NULL;
4550 }
4551
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004552 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004553}
4554#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4555
4556#ifdef HAVE_WAIT3
4557PyDoc_STRVAR(posix_wait3__doc__,
4558"wait3(options) -> (pid, status, rusage)\n\n\
4559Wait for completion of a child process.");
4560
4561static PyObject *
4562posix_wait3(PyObject *self, PyObject *args)
4563{
Christian Heimes292d3512008-02-03 16:51:08 +00004564 pid_t pid;
4565 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004566 struct rusage ru;
4567 WAIT_TYPE status;
4568 WAIT_STATUS_INT(status) = 0;
4569
4570 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4571 return NULL;
4572
4573 Py_BEGIN_ALLOW_THREADS
4574 pid = wait3(&status, options, &ru);
4575 Py_END_ALLOW_THREADS
4576
4577 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4578}
4579#endif /* HAVE_WAIT3 */
4580
4581#ifdef HAVE_WAIT4
4582PyDoc_STRVAR(posix_wait4__doc__,
4583"wait4(pid, options) -> (pid, status, rusage)\n\n\
4584Wait for completion of a given child process.");
4585
4586static PyObject *
4587posix_wait4(PyObject *self, PyObject *args)
4588{
Christian Heimes292d3512008-02-03 16:51:08 +00004589 pid_t pid;
4590 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004591 struct rusage ru;
4592 WAIT_TYPE status;
4593 WAIT_STATUS_INT(status) = 0;
4594
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004595 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004596 return NULL;
4597
4598 Py_BEGIN_ALLOW_THREADS
4599 pid = wait4(pid, &status, options, &ru);
4600 Py_END_ALLOW_THREADS
4601
4602 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4603}
4604#endif /* HAVE_WAIT4 */
4605
Guido van Rossumb6775db1994-08-01 11:34:53 +00004606#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004607PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004608"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004609Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004610
Barry Warsaw53699e91996-12-10 23:23:01 +00004611static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004612posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004613{
Christian Heimes292d3512008-02-03 16:51:08 +00004614 pid_t pid;
4615 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004616 WAIT_TYPE status;
4617 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004618
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004619 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004620 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004621 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004622 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004623 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004624 if (pid == -1)
4625 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004626
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004627 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004628}
4629
Tim Petersab034fa2002-02-01 11:27:43 +00004630#elif defined(HAVE_CWAIT)
4631
4632/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004633PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004634"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004635"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004636
4637static PyObject *
4638posix_waitpid(PyObject *self, PyObject *args)
4639{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004640 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004641 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004642
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004643 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004644 return NULL;
4645 Py_BEGIN_ALLOW_THREADS
4646 pid = _cwait(&status, pid, options);
4647 Py_END_ALLOW_THREADS
4648 if (pid == -1)
4649 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004650
4651 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004652 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004653}
4654#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004655
Guido van Rossumad0ee831995-03-01 10:34:45 +00004656#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004657PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004658"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004659Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004660
Barry Warsaw53699e91996-12-10 23:23:01 +00004661static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004662posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004663{
Christian Heimes292d3512008-02-03 16:51:08 +00004664 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004665 WAIT_TYPE status;
4666 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004667
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004668 Py_BEGIN_ALLOW_THREADS
4669 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004670 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004671 if (pid == -1)
4672 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004673
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004674 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004675}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004676#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004677
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004679PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004680"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004681Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004682
Barry Warsaw53699e91996-12-10 23:23:01 +00004683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004684posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004685{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004686#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004687 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004688#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004689#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004690 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004691#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004692 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004693#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004694#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004695}
4696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004697
Guido van Rossumb6775db1994-08-01 11:34:53 +00004698#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004699PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004700"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004701Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004702
Barry Warsaw53699e91996-12-10 23:23:01 +00004703static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004704posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004705{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004706 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004707 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004708 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004709 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004710 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004711 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004712
Martin v. Löwis011e8422009-05-05 04:43:17 +00004713 if (!PyArg_ParseTuple(args, "O&:readlink",
4714 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004715 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004716 path = bytes2str(opath, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004717 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004718 if (v == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00004719 release_bytes(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004720 return NULL;
4721 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004722
4723 if (PyUnicode_Check(v)) {
4724 arg_is_unicode = 1;
4725 }
4726 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004727
Barry Warsaw53699e91996-12-10 23:23:01 +00004728 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004729 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004730 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004731 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004732 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004733
Martin v. Löwis011e8422009-05-05 04:43:17 +00004734 release_bytes(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004735 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004736 if (arg_is_unicode) {
4737 PyObject *w;
4738
4739 w = PyUnicode_FromEncodedObject(v,
4740 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004741 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004742 if (w != NULL) {
4743 Py_DECREF(v);
4744 v = w;
4745 }
4746 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004747 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004748 }
4749 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004750 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004751}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004752#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004753
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004754
Guido van Rossumb6775db1994-08-01 11:34:53 +00004755#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004756PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004757"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004758Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004759
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004761posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004762{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004763 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004764}
4765#endif /* HAVE_SYMLINK */
4766
4767
4768#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004769#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4770static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004771system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004772{
4773 ULONG value = 0;
4774
4775 Py_BEGIN_ALLOW_THREADS
4776 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4777 Py_END_ALLOW_THREADS
4778
4779 return value;
4780}
4781
4782static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004783posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004784{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004785 /* Currently Only Uptime is Provided -- Others Later */
4786 return Py_BuildValue("ddddd",
4787 (double)0 /* t.tms_utime / HZ */,
4788 (double)0 /* t.tms_stime / HZ */,
4789 (double)0 /* t.tms_cutime / HZ */,
4790 (double)0 /* t.tms_cstime / HZ */,
4791 (double)system_uptime() / 1000);
4792}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004793#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004794#define NEED_TICKS_PER_SECOND
4795static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004796static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004797posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004798{
4799 struct tms t;
4800 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004801 errno = 0;
4802 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004803 if (c == (clock_t) -1)
4804 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004805 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004806 (double)t.tms_utime / ticks_per_second,
4807 (double)t.tms_stime / ticks_per_second,
4808 (double)t.tms_cutime / ticks_per_second,
4809 (double)t.tms_cstime / ticks_per_second,
4810 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004811}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004812#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004813#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004814
4815
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004816#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004817#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004818static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004819posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004820{
4821 FILETIME create, exit, kernel, user;
4822 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004823 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004824 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4825 /* The fields of a FILETIME structure are the hi and lo part
4826 of a 64-bit value expressed in 100 nanosecond units.
4827 1e7 is one second in such units; 1e-7 the inverse.
4828 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4829 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004830 return Py_BuildValue(
4831 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004832 (double)(user.dwHighDateTime*429.4967296 +
4833 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004834 (double)(kernel.dwHighDateTime*429.4967296 +
4835 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004836 (double)0,
4837 (double)0,
4838 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004839}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004840#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004841
4842#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004843PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004844"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004845Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004846#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004847
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004848
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004849#ifdef HAVE_GETSID
4850PyDoc_STRVAR(posix_getsid__doc__,
4851"getsid(pid) -> sid\n\n\
4852Call the system call getsid().");
4853
4854static PyObject *
4855posix_getsid(PyObject *self, PyObject *args)
4856{
Christian Heimes292d3512008-02-03 16:51:08 +00004857 pid_t pid;
4858 int sid;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004859 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004860 return NULL;
4861 sid = getsid(pid);
4862 if (sid < 0)
4863 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004864 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004865}
4866#endif /* HAVE_GETSID */
4867
4868
Guido van Rossumb6775db1994-08-01 11:34:53 +00004869#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004870PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004871"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004872Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004873
Barry Warsaw53699e91996-12-10 23:23:01 +00004874static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004875posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004876{
Guido van Rossum687dd131993-05-17 08:34:16 +00004877 if (setsid() < 0)
4878 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004879 Py_INCREF(Py_None);
4880 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004881}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004882#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004883
Guido van Rossumb6775db1994-08-01 11:34:53 +00004884#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004885PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004886"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004887Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004888
Barry Warsaw53699e91996-12-10 23:23:01 +00004889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004890posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004891{
Christian Heimes292d3512008-02-03 16:51:08 +00004892 pid_t pid;
4893 int pgrp;
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004894 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004895 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004896 if (setpgid(pid, pgrp) < 0)
4897 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004898 Py_INCREF(Py_None);
4899 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004900}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004901#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004902
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004903
Guido van Rossumb6775db1994-08-01 11:34:53 +00004904#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004905PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004906"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004907Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004908
Barry Warsaw53699e91996-12-10 23:23:01 +00004909static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004910posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004911{
Christian Heimes15ebc882008-02-04 18:48:49 +00004912 int fd;
4913 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004914 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004915 return NULL;
4916 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004917 if (pgid < 0)
4918 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004919 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004920}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004921#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004922
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004923
Guido van Rossumb6775db1994-08-01 11:34:53 +00004924#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004925PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004926"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004927Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004928
Barry Warsaw53699e91996-12-10 23:23:01 +00004929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004930posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004931{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004932 int fd;
4933 pid_t pgid;
4934 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004935 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004936 if (tcsetpgrp(fd, pgid) < 0)
4937 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004938 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004939 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004940}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004941#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004942
Guido van Rossum687dd131993-05-17 08:34:16 +00004943/* Functions acting on file descriptors */
4944
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004945PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004946"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004947Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004948
Barry Warsaw53699e91996-12-10 23:23:01 +00004949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004950posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004951{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004952 PyObject *ofile;
4953 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004954 int flag;
4955 int mode = 0777;
4956 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004957
4958#ifdef MS_WINDOWS
4959 if (unicode_file_names()) {
4960 PyUnicodeObject *po;
4961 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4962 Py_BEGIN_ALLOW_THREADS
4963 /* PyUnicode_AS_UNICODE OK without thread
4964 lock as it is a simple dereference. */
4965 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4966 Py_END_ALLOW_THREADS
4967 if (fd < 0)
4968 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004969 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004970 }
4971 /* Drop the argument parsing error as narrow strings
4972 are also valid. */
4973 PyErr_Clear();
4974 }
4975#endif
4976
Martin v. Löwis011e8422009-05-05 04:43:17 +00004977 if (!PyArg_ParseTuple(args, "O&i|i",
4978 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004979 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004980 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004981 file = bytes2str(ofile, 1);
Barry Warsaw53699e91996-12-10 23:23:01 +00004982 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004983 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004984 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004985 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004986 return posix_error_with_allocated_filename(ofile);
4987 release_bytes(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004988 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004989}
4990
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004992PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004993"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004994Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004995
Barry Warsaw53699e91996-12-10 23:23:01 +00004996static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004997posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004998{
4999 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005000 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005001 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005002 if (!_PyVerify_fd(fd))
5003 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005004 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005005 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005006 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005007 if (res < 0)
5008 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005009 Py_INCREF(Py_None);
5010 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005011}
5012
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005013
Christian Heimesfdab48e2008-01-20 09:06:41 +00005014PyDoc_STRVAR(posix_closerange__doc__,
5015"closerange(fd_low, fd_high)\n\n\
5016Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5017
5018static PyObject *
5019posix_closerange(PyObject *self, PyObject *args)
5020{
5021 int fd_from, fd_to, i;
5022 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5023 return NULL;
5024 Py_BEGIN_ALLOW_THREADS
5025 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005026 if (_PyVerify_fd(i))
5027 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00005028 Py_END_ALLOW_THREADS
5029 Py_RETURN_NONE;
5030}
5031
5032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005033PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005034"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005035Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005036
Barry Warsaw53699e91996-12-10 23:23:01 +00005037static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005038posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005039{
5040 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005041 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005042 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005043 if (!_PyVerify_fd(fd))
5044 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005045 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005046 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005047 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005048 if (fd < 0)
5049 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005050 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005051}
5052
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005054PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005055"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005056Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005057
Barry Warsaw53699e91996-12-10 23:23:01 +00005058static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005059posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005060{
5061 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005062 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005063 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005064 if (!_PyVerify_fd_dup2(fd, fd2))
5065 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005066 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005067 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005068 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005069 if (res < 0)
5070 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005071 Py_INCREF(Py_None);
5072 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005073}
5074
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005076PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005077"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005078Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005079
Barry Warsaw53699e91996-12-10 23:23:01 +00005080static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005081posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005082{
5083 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005084#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005085 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005086#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005087 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005088#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005089 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005090 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005091 return NULL;
5092#ifdef SEEK_SET
5093 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5094 switch (how) {
5095 case 0: how = SEEK_SET; break;
5096 case 1: how = SEEK_CUR; break;
5097 case 2: how = SEEK_END; break;
5098 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005099#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005100
5101#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005102 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005103#else
5104 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005105 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005106#endif
5107 if (PyErr_Occurred())
5108 return NULL;
5109
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005110 if (!_PyVerify_fd(fd))
5111 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005112 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005113#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005114 res = _lseeki64(fd, pos, how);
5115#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005116 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005117#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005118 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005119 if (res < 0)
5120 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005121
5122#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005123 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005124#else
5125 return PyLong_FromLongLong(res);
5126#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005127}
5128
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005129
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005130PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005131"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005132Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005133
Barry Warsaw53699e91996-12-10 23:23:01 +00005134static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005135posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005136{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005137 int fd, size;
5138 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005139 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005140 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005141 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005142 if (size < 0) {
5143 errno = EINVAL;
5144 return posix_error();
5145 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005146 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005147 if (buffer == NULL)
5148 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005149 if (!_PyVerify_fd(fd))
5150 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005151 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005152 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005153 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005154 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005155 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005156 return posix_error();
5157 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005158 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005159 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005160 return buffer;
5161}
5162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005164PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005165"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005166Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005167
Barry Warsaw53699e91996-12-10 23:23:01 +00005168static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005169posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005170{
Martin v. Löwis423be952008-08-13 15:53:07 +00005171 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005172 int fd;
5173 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005174
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005175 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005176 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005177 if (!_PyVerify_fd(fd))
5178 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005179 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005180 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005181 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005182 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005183 if (size < 0)
5184 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005185 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005186}
5187
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005188
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005189PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005190"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005191Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005192
Barry Warsaw53699e91996-12-10 23:23:01 +00005193static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005194posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005195{
5196 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005197 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005198 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005199 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005200 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005201#ifdef __VMS
5202 /* on OpenVMS we must ensure that all bytes are written to the file */
5203 fsync(fd);
5204#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005205 if (!_PyVerify_fd(fd))
5206 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005207 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005208 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005209 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005210 if (res != 0) {
5211#ifdef MS_WINDOWS
5212 return win32_error("fstat", NULL);
5213#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005214 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005215#endif
5216 }
Tim Peters5aa91602002-01-30 05:46:57 +00005217
Martin v. Löwis14694662006-02-03 12:54:16 +00005218 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005219}
5220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005221PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005222"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005223Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005224connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005225
5226static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005227posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005228{
5229 int fd;
5230 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5231 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005232 if (!_PyVerify_fd(fd))
5233 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005234 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005235}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005236
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005237#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005238PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005239"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005240Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005241
Barry Warsaw53699e91996-12-10 23:23:01 +00005242static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005243posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005244{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005245#if defined(PYOS_OS2)
5246 HFILE read, write;
5247 APIRET rc;
5248
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005249 Py_BEGIN_ALLOW_THREADS
5250 rc = DosCreatePipe( &read, &write, 4096);
5251 Py_END_ALLOW_THREADS
5252 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005253 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005254
5255 return Py_BuildValue("(ii)", read, write);
5256#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005257#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005258 int fds[2];
5259 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005260 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005261 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005262 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005263 if (res != 0)
5264 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005265 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005266#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005267 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005268 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005269 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005270 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005271 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005272 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005273 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005274 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005275 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5276 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005277 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005278#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005279#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005280}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005281#endif /* HAVE_PIPE */
5282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005283
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005284#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005285PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005286"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005287Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005288
Barry Warsaw53699e91996-12-10 23:23:01 +00005289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005290posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005291{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005292 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005293 int mode = 0666;
5294 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005295 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005296 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005297 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005298 res = mkfifo(filename, mode);
5299 Py_END_ALLOW_THREADS
5300 if (res < 0)
5301 return posix_error();
5302 Py_INCREF(Py_None);
5303 return Py_None;
5304}
5305#endif
5306
5307
Neal Norwitz11690112002-07-30 01:08:28 +00005308#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005309PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005310"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005311Create a filesystem node (file, device special file or named pipe)\n\
5312named filename. mode specifies both the permissions to use and the\n\
5313type of node to be created, being combined (bitwise OR) with one of\n\
5314S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005315device defines the newly created device special file (probably using\n\
5316os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005317
5318
5319static PyObject *
5320posix_mknod(PyObject *self, PyObject *args)
5321{
5322 char *filename;
5323 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005324 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005325 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005326 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005327 return NULL;
5328 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005329 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005330 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005331 if (res < 0)
5332 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005333 Py_INCREF(Py_None);
5334 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005335}
5336#endif
5337
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005338#ifdef HAVE_DEVICE_MACROS
5339PyDoc_STRVAR(posix_major__doc__,
5340"major(device) -> major number\n\
5341Extracts a device major number from a raw device number.");
5342
5343static PyObject *
5344posix_major(PyObject *self, PyObject *args)
5345{
5346 int device;
5347 if (!PyArg_ParseTuple(args, "i:major", &device))
5348 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005349 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005350}
5351
5352PyDoc_STRVAR(posix_minor__doc__,
5353"minor(device) -> minor number\n\
5354Extracts a device minor number from a raw device number.");
5355
5356static PyObject *
5357posix_minor(PyObject *self, PyObject *args)
5358{
5359 int device;
5360 if (!PyArg_ParseTuple(args, "i:minor", &device))
5361 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005362 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005363}
5364
5365PyDoc_STRVAR(posix_makedev__doc__,
5366"makedev(major, minor) -> device number\n\
5367Composes a raw device number from the major and minor device numbers.");
5368
5369static PyObject *
5370posix_makedev(PyObject *self, PyObject *args)
5371{
5372 int major, minor;
5373 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5374 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005375 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005376}
5377#endif /* device macros */
5378
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005379
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005380#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005381PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005382"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005383Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005384
Barry Warsaw53699e91996-12-10 23:23:01 +00005385static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005386posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005387{
5388 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005389 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005390 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005391 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005392
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005393 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005394 return NULL;
5395
5396#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005397 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005398#else
5399 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005400 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005401#endif
5402 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005403 return NULL;
5404
Barry Warsaw53699e91996-12-10 23:23:01 +00005405 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005406 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005407 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005408 if (res < 0)
5409 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005410 Py_INCREF(Py_None);
5411 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005412}
5413#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005414
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005415#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005416PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005417"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005418Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005419
Fred Drake762e2061999-08-26 17:23:54 +00005420/* Save putenv() parameters as values here, so we can collect them when they
5421 * get re-set with another call for the same key. */
5422static PyObject *posix_putenv_garbage;
5423
Tim Peters5aa91602002-01-30 05:46:57 +00005424static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005425posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005426{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005427#ifdef MS_WINDOWS
5428 wchar_t *s1, *s2;
5429 wchar_t *newenv;
5430#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005431 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005432 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005433 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005434#endif
Fred Drake762e2061999-08-26 17:23:54 +00005435 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005436 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005437
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005438#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005439 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005440 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005441 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005442 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005443#else
5444 if (!PyArg_ParseTuple(args,
5445 "O&O&:putenv",
5446 PyUnicode_FSConverter, &os1,
5447 PyUnicode_FSConverter, &os2))
5448 return NULL;
5449 s1 = bytes2str(os1, 1);
5450 s2 = bytes2str(os2, 1);
5451#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005452
5453#if defined(PYOS_OS2)
5454 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5455 APIRET rc;
5456
Guido van Rossumd48f2521997-12-05 22:19:34 +00005457 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5458 if (rc != NO_ERROR)
5459 return os2_error(rc);
5460
5461 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5462 APIRET rc;
5463
Guido van Rossumd48f2521997-12-05 22:19:34 +00005464 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5465 if (rc != NO_ERROR)
5466 return os2_error(rc);
5467 } else {
5468#endif
Fred Drake762e2061999-08-26 17:23:54 +00005469 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005470 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005471 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005472#ifdef MS_WINDOWS
5473 len = wcslen(s1) + wcslen(s2) + 2;
5474 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5475#else
5476 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005477 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005478#endif
Fred Drake762e2061999-08-26 17:23:54 +00005479 if (newstr == NULL)
5480 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005481#ifdef MS_WINDOWS
5482 newenv = PyUnicode_AsUnicode(newstr);
5483 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5484 if (_wputenv(newenv)) {
5485 Py_DECREF(newstr);
5486 posix_error();
5487 return NULL;
5488 }
5489#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005490 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005491 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5492 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005493 Py_DECREF(newstr);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005494 release_bytes(os1);
5495 release_bytes(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005496 posix_error();
5497 return NULL;
5498 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005499#endif
Fred Drake762e2061999-08-26 17:23:54 +00005500 /* Install the first arg and newstr in posix_putenv_garbage;
5501 * this will cause previous value to be collected. This has to
5502 * happen after the real putenv() call because the old value
5503 * was still accessible until then. */
5504 if (PyDict_SetItem(posix_putenv_garbage,
5505 PyTuple_GET_ITEM(args, 0), newstr)) {
5506 /* really not much we can do; just leak */
5507 PyErr_Clear();
5508 }
5509 else {
5510 Py_DECREF(newstr);
5511 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005512
5513#if defined(PYOS_OS2)
5514 }
5515#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005516#ifndef MS_WINDOWS
5517 release_bytes(os1);
5518 release_bytes(os2);
5519#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005520 Py_INCREF(Py_None);
5521 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005522}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005523#endif /* putenv */
5524
Guido van Rossumc524d952001-10-19 01:31:59 +00005525#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005526PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005527"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005528Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005529
5530static PyObject *
5531posix_unsetenv(PyObject *self, PyObject *args)
5532{
5533 char *s1;
5534
5535 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5536 return NULL;
5537
5538 unsetenv(s1);
5539
5540 /* Remove the key from posix_putenv_garbage;
5541 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005542 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005543 * old value was still accessible until then.
5544 */
5545 if (PyDict_DelItem(posix_putenv_garbage,
5546 PyTuple_GET_ITEM(args, 0))) {
5547 /* really not much we can do; just leak */
5548 PyErr_Clear();
5549 }
5550
5551 Py_INCREF(Py_None);
5552 return Py_None;
5553}
5554#endif /* unsetenv */
5555
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005556PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005557"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005559
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005560static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005561posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005562{
5563 int code;
5564 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005565 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005566 return NULL;
5567 message = strerror(code);
5568 if (message == NULL) {
5569 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005570 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005571 return NULL;
5572 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005573 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005574}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005575
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005576
Guido van Rossumc9641791998-08-04 15:26:23 +00005577#ifdef HAVE_SYS_WAIT_H
5578
Fred Drake106c1a02002-04-23 15:58:02 +00005579#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005581"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005582Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005583
5584static PyObject *
5585posix_WCOREDUMP(PyObject *self, PyObject *args)
5586{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005587 WAIT_TYPE status;
5588 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005589
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005590 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005591 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005592
5593 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005594}
5595#endif /* WCOREDUMP */
5596
5597#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005599"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005600Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005601job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005602
5603static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005604posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005605{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005606 WAIT_TYPE status;
5607 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005609 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005610 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005611
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005612 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005613}
5614#endif /* WIFCONTINUED */
5615
Guido van Rossumc9641791998-08-04 15:26:23 +00005616#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005617PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005618"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005619Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005620
5621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005622posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005623{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005624 WAIT_TYPE status;
5625 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005626
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005627 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005628 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005629
Fred Drake106c1a02002-04-23 15:58:02 +00005630 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005631}
5632#endif /* WIFSTOPPED */
5633
5634#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005635PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005636"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005637Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005638
5639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005640posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005641{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005642 WAIT_TYPE status;
5643 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005644
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005645 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005646 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005647
Fred Drake106c1a02002-04-23 15:58:02 +00005648 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005649}
5650#endif /* WIFSIGNALED */
5651
5652#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005653PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005654"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005655Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005656system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005657
5658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005659posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005660{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005661 WAIT_TYPE status;
5662 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005663
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005664 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005665 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005666
Fred Drake106c1a02002-04-23 15:58:02 +00005667 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005668}
5669#endif /* WIFEXITED */
5670
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005671#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005672PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005673"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005674Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005675
5676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005677posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005678{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005679 WAIT_TYPE status;
5680 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005681
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005682 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005683 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005684
Guido van Rossumc9641791998-08-04 15:26:23 +00005685 return Py_BuildValue("i", WEXITSTATUS(status));
5686}
5687#endif /* WEXITSTATUS */
5688
5689#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005690PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005691"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005692Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005693value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005694
5695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005696posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005697{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005698 WAIT_TYPE status;
5699 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005700
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005701 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005702 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005703
Guido van Rossumc9641791998-08-04 15:26:23 +00005704 return Py_BuildValue("i", WTERMSIG(status));
5705}
5706#endif /* WTERMSIG */
5707
5708#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005709PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005710"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005711Return the signal that stopped the process that provided\n\
5712the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005713
5714static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005715posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005716{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005717 WAIT_TYPE status;
5718 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005719
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005720 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005721 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005722
Guido van Rossumc9641791998-08-04 15:26:23 +00005723 return Py_BuildValue("i", WSTOPSIG(status));
5724}
5725#endif /* WSTOPSIG */
5726
5727#endif /* HAVE_SYS_WAIT_H */
5728
5729
Thomas Wouters477c8d52006-05-27 19:21:47 +00005730#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005731#ifdef _SCO_DS
5732/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5733 needed definitions in sys/statvfs.h */
5734#define _SVID3
5735#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005736#include <sys/statvfs.h>
5737
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005738static PyObject*
5739_pystatvfs_fromstructstatvfs(struct statvfs st) {
5740 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5741 if (v == NULL)
5742 return NULL;
5743
5744#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005745 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5746 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5747 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5748 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5749 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5750 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5751 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5752 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5753 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5754 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005755#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005756 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5757 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005758 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005759 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005760 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005761 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005762 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005763 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005764 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005765 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005766 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005767 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005768 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005769 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005770 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5771 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005772#endif
5773
5774 return v;
5775}
5776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005777PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005778"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005779Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005780
5781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005782posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005783{
5784 int fd, res;
5785 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005786
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005787 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005788 return NULL;
5789 Py_BEGIN_ALLOW_THREADS
5790 res = fstatvfs(fd, &st);
5791 Py_END_ALLOW_THREADS
5792 if (res != 0)
5793 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005794
5795 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005796}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005797#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005798
5799
Thomas Wouters477c8d52006-05-27 19:21:47 +00005800#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005801#include <sys/statvfs.h>
5802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005803PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005804"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005805Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005806
5807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005808posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005809{
5810 char *path;
5811 int res;
5812 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005813 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005814 return NULL;
5815 Py_BEGIN_ALLOW_THREADS
5816 res = statvfs(path, &st);
5817 Py_END_ALLOW_THREADS
5818 if (res != 0)
5819 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005820
5821 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005822}
5823#endif /* HAVE_STATVFS */
5824
Fred Drakec9680921999-12-13 16:37:25 +00005825/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5826 * It maps strings representing configuration variable names to
5827 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005828 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005829 * rarely-used constants. There are three separate tables that use
5830 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005831 *
5832 * This code is always included, even if none of the interfaces that
5833 * need it are included. The #if hackery needed to avoid it would be
5834 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005835 */
5836struct constdef {
5837 char *name;
5838 long value;
5839};
5840
Fred Drake12c6e2d1999-12-14 21:25:03 +00005841static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005842conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005843 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005844{
Christian Heimes217cfd12007-12-02 14:31:20 +00005845 if (PyLong_Check(arg)) {
5846 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005847 return 1;
5848 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005849 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005850 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005851 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005852 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005853 size_t hi = tablesize;
5854 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005855 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005856 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005857 PyErr_SetString(PyExc_TypeError,
5858 "configuration names must be strings or integers");
5859 return 0;
5860 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005861 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005862 if (confname == NULL)
5863 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005864 while (lo < hi) {
5865 mid = (lo + hi) / 2;
5866 cmp = strcmp(confname, table[mid].name);
5867 if (cmp < 0)
5868 hi = mid;
5869 else if (cmp > 0)
5870 lo = mid + 1;
5871 else {
5872 *valuep = table[mid].value;
5873 return 1;
5874 }
5875 }
5876 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005877 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005878 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005879}
5880
5881
5882#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5883static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005884#ifdef _PC_ABI_AIO_XFER_MAX
5885 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5886#endif
5887#ifdef _PC_ABI_ASYNC_IO
5888 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5889#endif
Fred Drakec9680921999-12-13 16:37:25 +00005890#ifdef _PC_ASYNC_IO
5891 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5892#endif
5893#ifdef _PC_CHOWN_RESTRICTED
5894 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5895#endif
5896#ifdef _PC_FILESIZEBITS
5897 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5898#endif
5899#ifdef _PC_LAST
5900 {"PC_LAST", _PC_LAST},
5901#endif
5902#ifdef _PC_LINK_MAX
5903 {"PC_LINK_MAX", _PC_LINK_MAX},
5904#endif
5905#ifdef _PC_MAX_CANON
5906 {"PC_MAX_CANON", _PC_MAX_CANON},
5907#endif
5908#ifdef _PC_MAX_INPUT
5909 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5910#endif
5911#ifdef _PC_NAME_MAX
5912 {"PC_NAME_MAX", _PC_NAME_MAX},
5913#endif
5914#ifdef _PC_NO_TRUNC
5915 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5916#endif
5917#ifdef _PC_PATH_MAX
5918 {"PC_PATH_MAX", _PC_PATH_MAX},
5919#endif
5920#ifdef _PC_PIPE_BUF
5921 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5922#endif
5923#ifdef _PC_PRIO_IO
5924 {"PC_PRIO_IO", _PC_PRIO_IO},
5925#endif
5926#ifdef _PC_SOCK_MAXBUF
5927 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5928#endif
5929#ifdef _PC_SYNC_IO
5930 {"PC_SYNC_IO", _PC_SYNC_IO},
5931#endif
5932#ifdef _PC_VDISABLE
5933 {"PC_VDISABLE", _PC_VDISABLE},
5934#endif
5935};
5936
Fred Drakec9680921999-12-13 16:37:25 +00005937static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005938conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005939{
5940 return conv_confname(arg, valuep, posix_constants_pathconf,
5941 sizeof(posix_constants_pathconf)
5942 / sizeof(struct constdef));
5943}
5944#endif
5945
5946#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005947PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005948"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005949Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005950If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005951
5952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005953posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005954{
5955 PyObject *result = NULL;
5956 int name, fd;
5957
Fred Drake12c6e2d1999-12-14 21:25:03 +00005958 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5959 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005960 long limit;
5961
5962 errno = 0;
5963 limit = fpathconf(fd, name);
5964 if (limit == -1 && errno != 0)
5965 posix_error();
5966 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005967 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005968 }
5969 return result;
5970}
5971#endif
5972
5973
5974#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005975PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005976"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005977Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005978If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005979
5980static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005981posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005982{
5983 PyObject *result = NULL;
5984 int name;
5985 char *path;
5986
5987 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5988 conv_path_confname, &name)) {
5989 long limit;
5990
5991 errno = 0;
5992 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005993 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005994 if (errno == EINVAL)
5995 /* could be a path or name problem */
5996 posix_error();
5997 else
5998 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005999 }
Fred Drakec9680921999-12-13 16:37:25 +00006000 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006001 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006002 }
6003 return result;
6004}
6005#endif
6006
6007#ifdef HAVE_CONFSTR
6008static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006009#ifdef _CS_ARCHITECTURE
6010 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6011#endif
6012#ifdef _CS_HOSTNAME
6013 {"CS_HOSTNAME", _CS_HOSTNAME},
6014#endif
6015#ifdef _CS_HW_PROVIDER
6016 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6017#endif
6018#ifdef _CS_HW_SERIAL
6019 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6020#endif
6021#ifdef _CS_INITTAB_NAME
6022 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6023#endif
Fred Drakec9680921999-12-13 16:37:25 +00006024#ifdef _CS_LFS64_CFLAGS
6025 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6026#endif
6027#ifdef _CS_LFS64_LDFLAGS
6028 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6029#endif
6030#ifdef _CS_LFS64_LIBS
6031 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6032#endif
6033#ifdef _CS_LFS64_LINTFLAGS
6034 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6035#endif
6036#ifdef _CS_LFS_CFLAGS
6037 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6038#endif
6039#ifdef _CS_LFS_LDFLAGS
6040 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6041#endif
6042#ifdef _CS_LFS_LIBS
6043 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6044#endif
6045#ifdef _CS_LFS_LINTFLAGS
6046 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6047#endif
Fred Draked86ed291999-12-15 15:34:33 +00006048#ifdef _CS_MACHINE
6049 {"CS_MACHINE", _CS_MACHINE},
6050#endif
Fred Drakec9680921999-12-13 16:37:25 +00006051#ifdef _CS_PATH
6052 {"CS_PATH", _CS_PATH},
6053#endif
Fred Draked86ed291999-12-15 15:34:33 +00006054#ifdef _CS_RELEASE
6055 {"CS_RELEASE", _CS_RELEASE},
6056#endif
6057#ifdef _CS_SRPC_DOMAIN
6058 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6059#endif
6060#ifdef _CS_SYSNAME
6061 {"CS_SYSNAME", _CS_SYSNAME},
6062#endif
6063#ifdef _CS_VERSION
6064 {"CS_VERSION", _CS_VERSION},
6065#endif
Fred Drakec9680921999-12-13 16:37:25 +00006066#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6067 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6068#endif
6069#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6070 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6071#endif
6072#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6073 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6074#endif
6075#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6076 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6077#endif
6078#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6079 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6080#endif
6081#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6082 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6083#endif
6084#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6085 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6086#endif
6087#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6088 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6089#endif
6090#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6091 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6092#endif
6093#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6094 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6095#endif
6096#ifdef _CS_XBS5_LP64_OFF64_LIBS
6097 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6098#endif
6099#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6100 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6101#endif
6102#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6103 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6104#endif
6105#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6106 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6107#endif
6108#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6109 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6110#endif
6111#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6112 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6113#endif
Fred Draked86ed291999-12-15 15:34:33 +00006114#ifdef _MIPS_CS_AVAIL_PROCESSORS
6115 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6116#endif
6117#ifdef _MIPS_CS_BASE
6118 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6119#endif
6120#ifdef _MIPS_CS_HOSTID
6121 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6122#endif
6123#ifdef _MIPS_CS_HW_NAME
6124 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6125#endif
6126#ifdef _MIPS_CS_NUM_PROCESSORS
6127 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6128#endif
6129#ifdef _MIPS_CS_OSREL_MAJ
6130 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6131#endif
6132#ifdef _MIPS_CS_OSREL_MIN
6133 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6134#endif
6135#ifdef _MIPS_CS_OSREL_PATCH
6136 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6137#endif
6138#ifdef _MIPS_CS_OS_NAME
6139 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6140#endif
6141#ifdef _MIPS_CS_OS_PROVIDER
6142 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6143#endif
6144#ifdef _MIPS_CS_PROCESSORS
6145 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6146#endif
6147#ifdef _MIPS_CS_SERIAL
6148 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6149#endif
6150#ifdef _MIPS_CS_VENDOR
6151 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6152#endif
Fred Drakec9680921999-12-13 16:37:25 +00006153};
6154
6155static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006156conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006157{
6158 return conv_confname(arg, valuep, posix_constants_confstr,
6159 sizeof(posix_constants_confstr)
6160 / sizeof(struct constdef));
6161}
6162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006163PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006164"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006165Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006166
6167static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006168posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006169{
6170 PyObject *result = NULL;
6171 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006172 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006173
6174 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006175 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006176
Fred Drakec9680921999-12-13 16:37:25 +00006177 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006178 len = confstr(name, buffer, sizeof(buffer));
6179 if (len == 0) {
6180 if (errno) {
6181 posix_error();
6182 }
6183 else {
6184 result = Py_None;
6185 Py_INCREF(Py_None);
6186 }
Fred Drakec9680921999-12-13 16:37:25 +00006187 }
6188 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006189 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006190 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006191 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006192 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006193 }
6194 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006195 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006196 }
6197 }
6198 return result;
6199}
6200#endif
6201
6202
6203#ifdef HAVE_SYSCONF
6204static struct constdef posix_constants_sysconf[] = {
6205#ifdef _SC_2_CHAR_TERM
6206 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6207#endif
6208#ifdef _SC_2_C_BIND
6209 {"SC_2_C_BIND", _SC_2_C_BIND},
6210#endif
6211#ifdef _SC_2_C_DEV
6212 {"SC_2_C_DEV", _SC_2_C_DEV},
6213#endif
6214#ifdef _SC_2_C_VERSION
6215 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6216#endif
6217#ifdef _SC_2_FORT_DEV
6218 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6219#endif
6220#ifdef _SC_2_FORT_RUN
6221 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6222#endif
6223#ifdef _SC_2_LOCALEDEF
6224 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6225#endif
6226#ifdef _SC_2_SW_DEV
6227 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6228#endif
6229#ifdef _SC_2_UPE
6230 {"SC_2_UPE", _SC_2_UPE},
6231#endif
6232#ifdef _SC_2_VERSION
6233 {"SC_2_VERSION", _SC_2_VERSION},
6234#endif
Fred Draked86ed291999-12-15 15:34:33 +00006235#ifdef _SC_ABI_ASYNCHRONOUS_IO
6236 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6237#endif
6238#ifdef _SC_ACL
6239 {"SC_ACL", _SC_ACL},
6240#endif
Fred Drakec9680921999-12-13 16:37:25 +00006241#ifdef _SC_AIO_LISTIO_MAX
6242 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6243#endif
Fred Drakec9680921999-12-13 16:37:25 +00006244#ifdef _SC_AIO_MAX
6245 {"SC_AIO_MAX", _SC_AIO_MAX},
6246#endif
6247#ifdef _SC_AIO_PRIO_DELTA_MAX
6248 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6249#endif
6250#ifdef _SC_ARG_MAX
6251 {"SC_ARG_MAX", _SC_ARG_MAX},
6252#endif
6253#ifdef _SC_ASYNCHRONOUS_IO
6254 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6255#endif
6256#ifdef _SC_ATEXIT_MAX
6257 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6258#endif
Fred Draked86ed291999-12-15 15:34:33 +00006259#ifdef _SC_AUDIT
6260 {"SC_AUDIT", _SC_AUDIT},
6261#endif
Fred Drakec9680921999-12-13 16:37:25 +00006262#ifdef _SC_AVPHYS_PAGES
6263 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6264#endif
6265#ifdef _SC_BC_BASE_MAX
6266 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6267#endif
6268#ifdef _SC_BC_DIM_MAX
6269 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6270#endif
6271#ifdef _SC_BC_SCALE_MAX
6272 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6273#endif
6274#ifdef _SC_BC_STRING_MAX
6275 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6276#endif
Fred Draked86ed291999-12-15 15:34:33 +00006277#ifdef _SC_CAP
6278 {"SC_CAP", _SC_CAP},
6279#endif
Fred Drakec9680921999-12-13 16:37:25 +00006280#ifdef _SC_CHARCLASS_NAME_MAX
6281 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6282#endif
6283#ifdef _SC_CHAR_BIT
6284 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6285#endif
6286#ifdef _SC_CHAR_MAX
6287 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6288#endif
6289#ifdef _SC_CHAR_MIN
6290 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6291#endif
6292#ifdef _SC_CHILD_MAX
6293 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6294#endif
6295#ifdef _SC_CLK_TCK
6296 {"SC_CLK_TCK", _SC_CLK_TCK},
6297#endif
6298#ifdef _SC_COHER_BLKSZ
6299 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6300#endif
6301#ifdef _SC_COLL_WEIGHTS_MAX
6302 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6303#endif
6304#ifdef _SC_DCACHE_ASSOC
6305 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6306#endif
6307#ifdef _SC_DCACHE_BLKSZ
6308 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6309#endif
6310#ifdef _SC_DCACHE_LINESZ
6311 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6312#endif
6313#ifdef _SC_DCACHE_SZ
6314 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6315#endif
6316#ifdef _SC_DCACHE_TBLKSZ
6317 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6318#endif
6319#ifdef _SC_DELAYTIMER_MAX
6320 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6321#endif
6322#ifdef _SC_EQUIV_CLASS_MAX
6323 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6324#endif
6325#ifdef _SC_EXPR_NEST_MAX
6326 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6327#endif
6328#ifdef _SC_FSYNC
6329 {"SC_FSYNC", _SC_FSYNC},
6330#endif
6331#ifdef _SC_GETGR_R_SIZE_MAX
6332 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6333#endif
6334#ifdef _SC_GETPW_R_SIZE_MAX
6335 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6336#endif
6337#ifdef _SC_ICACHE_ASSOC
6338 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6339#endif
6340#ifdef _SC_ICACHE_BLKSZ
6341 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6342#endif
6343#ifdef _SC_ICACHE_LINESZ
6344 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6345#endif
6346#ifdef _SC_ICACHE_SZ
6347 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6348#endif
Fred Draked86ed291999-12-15 15:34:33 +00006349#ifdef _SC_INF
6350 {"SC_INF", _SC_INF},
6351#endif
Fred Drakec9680921999-12-13 16:37:25 +00006352#ifdef _SC_INT_MAX
6353 {"SC_INT_MAX", _SC_INT_MAX},
6354#endif
6355#ifdef _SC_INT_MIN
6356 {"SC_INT_MIN", _SC_INT_MIN},
6357#endif
6358#ifdef _SC_IOV_MAX
6359 {"SC_IOV_MAX", _SC_IOV_MAX},
6360#endif
Fred Draked86ed291999-12-15 15:34:33 +00006361#ifdef _SC_IP_SECOPTS
6362 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6363#endif
Fred Drakec9680921999-12-13 16:37:25 +00006364#ifdef _SC_JOB_CONTROL
6365 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6366#endif
Fred Draked86ed291999-12-15 15:34:33 +00006367#ifdef _SC_KERN_POINTERS
6368 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6369#endif
6370#ifdef _SC_KERN_SIM
6371 {"SC_KERN_SIM", _SC_KERN_SIM},
6372#endif
Fred Drakec9680921999-12-13 16:37:25 +00006373#ifdef _SC_LINE_MAX
6374 {"SC_LINE_MAX", _SC_LINE_MAX},
6375#endif
6376#ifdef _SC_LOGIN_NAME_MAX
6377 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6378#endif
6379#ifdef _SC_LOGNAME_MAX
6380 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6381#endif
6382#ifdef _SC_LONG_BIT
6383 {"SC_LONG_BIT", _SC_LONG_BIT},
6384#endif
Fred Draked86ed291999-12-15 15:34:33 +00006385#ifdef _SC_MAC
6386 {"SC_MAC", _SC_MAC},
6387#endif
Fred Drakec9680921999-12-13 16:37:25 +00006388#ifdef _SC_MAPPED_FILES
6389 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6390#endif
6391#ifdef _SC_MAXPID
6392 {"SC_MAXPID", _SC_MAXPID},
6393#endif
6394#ifdef _SC_MB_LEN_MAX
6395 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6396#endif
6397#ifdef _SC_MEMLOCK
6398 {"SC_MEMLOCK", _SC_MEMLOCK},
6399#endif
6400#ifdef _SC_MEMLOCK_RANGE
6401 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6402#endif
6403#ifdef _SC_MEMORY_PROTECTION
6404 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6405#endif
6406#ifdef _SC_MESSAGE_PASSING
6407 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6408#endif
Fred Draked86ed291999-12-15 15:34:33 +00006409#ifdef _SC_MMAP_FIXED_ALIGNMENT
6410 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6411#endif
Fred Drakec9680921999-12-13 16:37:25 +00006412#ifdef _SC_MQ_OPEN_MAX
6413 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6414#endif
6415#ifdef _SC_MQ_PRIO_MAX
6416 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6417#endif
Fred Draked86ed291999-12-15 15:34:33 +00006418#ifdef _SC_NACLS_MAX
6419 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6420#endif
Fred Drakec9680921999-12-13 16:37:25 +00006421#ifdef _SC_NGROUPS_MAX
6422 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6423#endif
6424#ifdef _SC_NL_ARGMAX
6425 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6426#endif
6427#ifdef _SC_NL_LANGMAX
6428 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6429#endif
6430#ifdef _SC_NL_MSGMAX
6431 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6432#endif
6433#ifdef _SC_NL_NMAX
6434 {"SC_NL_NMAX", _SC_NL_NMAX},
6435#endif
6436#ifdef _SC_NL_SETMAX
6437 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6438#endif
6439#ifdef _SC_NL_TEXTMAX
6440 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6441#endif
6442#ifdef _SC_NPROCESSORS_CONF
6443 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6444#endif
6445#ifdef _SC_NPROCESSORS_ONLN
6446 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6447#endif
Fred Draked86ed291999-12-15 15:34:33 +00006448#ifdef _SC_NPROC_CONF
6449 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6450#endif
6451#ifdef _SC_NPROC_ONLN
6452 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6453#endif
Fred Drakec9680921999-12-13 16:37:25 +00006454#ifdef _SC_NZERO
6455 {"SC_NZERO", _SC_NZERO},
6456#endif
6457#ifdef _SC_OPEN_MAX
6458 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6459#endif
6460#ifdef _SC_PAGESIZE
6461 {"SC_PAGESIZE", _SC_PAGESIZE},
6462#endif
6463#ifdef _SC_PAGE_SIZE
6464 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6465#endif
6466#ifdef _SC_PASS_MAX
6467 {"SC_PASS_MAX", _SC_PASS_MAX},
6468#endif
6469#ifdef _SC_PHYS_PAGES
6470 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6471#endif
6472#ifdef _SC_PII
6473 {"SC_PII", _SC_PII},
6474#endif
6475#ifdef _SC_PII_INTERNET
6476 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6477#endif
6478#ifdef _SC_PII_INTERNET_DGRAM
6479 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6480#endif
6481#ifdef _SC_PII_INTERNET_STREAM
6482 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6483#endif
6484#ifdef _SC_PII_OSI
6485 {"SC_PII_OSI", _SC_PII_OSI},
6486#endif
6487#ifdef _SC_PII_OSI_CLTS
6488 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6489#endif
6490#ifdef _SC_PII_OSI_COTS
6491 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6492#endif
6493#ifdef _SC_PII_OSI_M
6494 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6495#endif
6496#ifdef _SC_PII_SOCKET
6497 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6498#endif
6499#ifdef _SC_PII_XTI
6500 {"SC_PII_XTI", _SC_PII_XTI},
6501#endif
6502#ifdef _SC_POLL
6503 {"SC_POLL", _SC_POLL},
6504#endif
6505#ifdef _SC_PRIORITIZED_IO
6506 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6507#endif
6508#ifdef _SC_PRIORITY_SCHEDULING
6509 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6510#endif
6511#ifdef _SC_REALTIME_SIGNALS
6512 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6513#endif
6514#ifdef _SC_RE_DUP_MAX
6515 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6516#endif
6517#ifdef _SC_RTSIG_MAX
6518 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6519#endif
6520#ifdef _SC_SAVED_IDS
6521 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6522#endif
6523#ifdef _SC_SCHAR_MAX
6524 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6525#endif
6526#ifdef _SC_SCHAR_MIN
6527 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6528#endif
6529#ifdef _SC_SELECT
6530 {"SC_SELECT", _SC_SELECT},
6531#endif
6532#ifdef _SC_SEMAPHORES
6533 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6534#endif
6535#ifdef _SC_SEM_NSEMS_MAX
6536 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6537#endif
6538#ifdef _SC_SEM_VALUE_MAX
6539 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6540#endif
6541#ifdef _SC_SHARED_MEMORY_OBJECTS
6542 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6543#endif
6544#ifdef _SC_SHRT_MAX
6545 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6546#endif
6547#ifdef _SC_SHRT_MIN
6548 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6549#endif
6550#ifdef _SC_SIGQUEUE_MAX
6551 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6552#endif
6553#ifdef _SC_SIGRT_MAX
6554 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6555#endif
6556#ifdef _SC_SIGRT_MIN
6557 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6558#endif
Fred Draked86ed291999-12-15 15:34:33 +00006559#ifdef _SC_SOFTPOWER
6560 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6561#endif
Fred Drakec9680921999-12-13 16:37:25 +00006562#ifdef _SC_SPLIT_CACHE
6563 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6564#endif
6565#ifdef _SC_SSIZE_MAX
6566 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6567#endif
6568#ifdef _SC_STACK_PROT
6569 {"SC_STACK_PROT", _SC_STACK_PROT},
6570#endif
6571#ifdef _SC_STREAM_MAX
6572 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6573#endif
6574#ifdef _SC_SYNCHRONIZED_IO
6575 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6576#endif
6577#ifdef _SC_THREADS
6578 {"SC_THREADS", _SC_THREADS},
6579#endif
6580#ifdef _SC_THREAD_ATTR_STACKADDR
6581 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6582#endif
6583#ifdef _SC_THREAD_ATTR_STACKSIZE
6584 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6585#endif
6586#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6587 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6588#endif
6589#ifdef _SC_THREAD_KEYS_MAX
6590 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6591#endif
6592#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6593 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6594#endif
6595#ifdef _SC_THREAD_PRIO_INHERIT
6596 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6597#endif
6598#ifdef _SC_THREAD_PRIO_PROTECT
6599 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6600#endif
6601#ifdef _SC_THREAD_PROCESS_SHARED
6602 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6603#endif
6604#ifdef _SC_THREAD_SAFE_FUNCTIONS
6605 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6606#endif
6607#ifdef _SC_THREAD_STACK_MIN
6608 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6609#endif
6610#ifdef _SC_THREAD_THREADS_MAX
6611 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6612#endif
6613#ifdef _SC_TIMERS
6614 {"SC_TIMERS", _SC_TIMERS},
6615#endif
6616#ifdef _SC_TIMER_MAX
6617 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6618#endif
6619#ifdef _SC_TTY_NAME_MAX
6620 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6621#endif
6622#ifdef _SC_TZNAME_MAX
6623 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6624#endif
6625#ifdef _SC_T_IOV_MAX
6626 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6627#endif
6628#ifdef _SC_UCHAR_MAX
6629 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6630#endif
6631#ifdef _SC_UINT_MAX
6632 {"SC_UINT_MAX", _SC_UINT_MAX},
6633#endif
6634#ifdef _SC_UIO_MAXIOV
6635 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6636#endif
6637#ifdef _SC_ULONG_MAX
6638 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6639#endif
6640#ifdef _SC_USHRT_MAX
6641 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6642#endif
6643#ifdef _SC_VERSION
6644 {"SC_VERSION", _SC_VERSION},
6645#endif
6646#ifdef _SC_WORD_BIT
6647 {"SC_WORD_BIT", _SC_WORD_BIT},
6648#endif
6649#ifdef _SC_XBS5_ILP32_OFF32
6650 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6651#endif
6652#ifdef _SC_XBS5_ILP32_OFFBIG
6653 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6654#endif
6655#ifdef _SC_XBS5_LP64_OFF64
6656 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6657#endif
6658#ifdef _SC_XBS5_LPBIG_OFFBIG
6659 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6660#endif
6661#ifdef _SC_XOPEN_CRYPT
6662 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6663#endif
6664#ifdef _SC_XOPEN_ENH_I18N
6665 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6666#endif
6667#ifdef _SC_XOPEN_LEGACY
6668 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6669#endif
6670#ifdef _SC_XOPEN_REALTIME
6671 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6672#endif
6673#ifdef _SC_XOPEN_REALTIME_THREADS
6674 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6675#endif
6676#ifdef _SC_XOPEN_SHM
6677 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6678#endif
6679#ifdef _SC_XOPEN_UNIX
6680 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6681#endif
6682#ifdef _SC_XOPEN_VERSION
6683 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6684#endif
6685#ifdef _SC_XOPEN_XCU_VERSION
6686 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6687#endif
6688#ifdef _SC_XOPEN_XPG2
6689 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6690#endif
6691#ifdef _SC_XOPEN_XPG3
6692 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6693#endif
6694#ifdef _SC_XOPEN_XPG4
6695 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6696#endif
6697};
6698
6699static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006700conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006701{
6702 return conv_confname(arg, valuep, posix_constants_sysconf,
6703 sizeof(posix_constants_sysconf)
6704 / sizeof(struct constdef));
6705}
6706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006707PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006708"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006709Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006710
6711static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006712posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006713{
6714 PyObject *result = NULL;
6715 int name;
6716
6717 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6718 int value;
6719
6720 errno = 0;
6721 value = sysconf(name);
6722 if (value == -1 && errno != 0)
6723 posix_error();
6724 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006725 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006726 }
6727 return result;
6728}
6729#endif
6730
6731
Fred Drakebec628d1999-12-15 18:31:10 +00006732/* This code is used to ensure that the tables of configuration value names
6733 * are in sorted order as required by conv_confname(), and also to build the
6734 * the exported dictionaries that are used to publish information about the
6735 * names available on the host platform.
6736 *
6737 * Sorting the table at runtime ensures that the table is properly ordered
6738 * when used, even for platforms we're not able to test on. It also makes
6739 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006740 */
Fred Drakebec628d1999-12-15 18:31:10 +00006741
6742static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006743cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006744{
6745 const struct constdef *c1 =
6746 (const struct constdef *) v1;
6747 const struct constdef *c2 =
6748 (const struct constdef *) v2;
6749
6750 return strcmp(c1->name, c2->name);
6751}
6752
6753static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006754setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006755 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006756{
Fred Drakebec628d1999-12-15 18:31:10 +00006757 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006758 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006759
6760 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6761 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006762 if (d == NULL)
6763 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006764
Barry Warsaw3155db32000-04-13 15:20:40 +00006765 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006766 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006767 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6768 Py_XDECREF(o);
6769 Py_DECREF(d);
6770 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006771 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006772 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006773 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006774 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006775}
6776
Fred Drakebec628d1999-12-15 18:31:10 +00006777/* Return -1 on failure, 0 on success. */
6778static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006779setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006780{
6781#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006782 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006783 sizeof(posix_constants_pathconf)
6784 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006785 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006786 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006787#endif
6788#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006789 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006790 sizeof(posix_constants_confstr)
6791 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006792 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006793 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006794#endif
6795#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006796 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006797 sizeof(posix_constants_sysconf)
6798 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006799 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006800 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006801#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006802 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006803}
Fred Draked86ed291999-12-15 15:34:33 +00006804
6805
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006806PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006807"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006808Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006809in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006810
6811static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006812posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006813{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006814 abort();
6815 /*NOTREACHED*/
6816 Py_FatalError("abort() called from Python code didn't abort!");
6817 return NULL;
6818}
Fred Drakebec628d1999-12-15 18:31:10 +00006819
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006820#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006821PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006822"startfile(filepath [, operation]) - Start a file with its associated\n\
6823application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006824\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006825When \"operation\" is not specified or \"open\", this acts like\n\
6826double-clicking the file in Explorer, or giving the file name as an\n\
6827argument to the DOS \"start\" command: the file is opened with whatever\n\
6828application (if any) its extension is associated.\n\
6829When another \"operation\" is given, it specifies what should be done with\n\
6830the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006831\n\
6832startfile returns as soon as the associated application is launched.\n\
6833There is no option to wait for the application to close, and no way\n\
6834to retrieve the application's exit status.\n\
6835\n\
6836The filepath is relative to the current directory. If you want to use\n\
6837an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006838the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006839
6840static PyObject *
6841win32_startfile(PyObject *self, PyObject *args)
6842{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006843 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006844 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006845 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006846 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006847
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006848 if (unicode_file_names()) {
6849 PyObject *unipath, *woperation = NULL;
6850 if (!PyArg_ParseTuple(args, "U|s:startfile",
6851 &unipath, &operation)) {
6852 PyErr_Clear();
6853 goto normal;
6854 }
6855
6856
6857 if (operation) {
6858 woperation = PyUnicode_DecodeASCII(operation,
6859 strlen(operation), NULL);
6860 if (!woperation) {
6861 PyErr_Clear();
6862 operation = NULL;
6863 goto normal;
6864 }
6865 }
6866
6867 Py_BEGIN_ALLOW_THREADS
6868 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6869 PyUnicode_AS_UNICODE(unipath),
6870 NULL, NULL, SW_SHOWNORMAL);
6871 Py_END_ALLOW_THREADS
6872
6873 Py_XDECREF(woperation);
6874 if (rc <= (HINSTANCE)32) {
6875 PyObject *errval = win32_error_unicode("startfile",
6876 PyUnicode_AS_UNICODE(unipath));
6877 return errval;
6878 }
6879 Py_INCREF(Py_None);
6880 return Py_None;
6881 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006882
6883normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006884 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6885 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006886 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006887 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006888 filepath = bytes2str(ofilepath, 1);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006889 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006890 rc = ShellExecute((HWND)0, operation, filepath,
6891 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006892 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006893 if (rc <= (HINSTANCE)32) {
6894 PyObject *errval = win32_error("startfile", filepath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006895 release_bytes(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006896 return errval;
6897 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00006898 release_bytes(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006899 Py_INCREF(Py_None);
6900 return Py_None;
6901}
6902#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006903
Martin v. Löwis438b5342002-12-27 10:16:42 +00006904#ifdef HAVE_GETLOADAVG
6905PyDoc_STRVAR(posix_getloadavg__doc__,
6906"getloadavg() -> (float, float, float)\n\n\
6907Return the number of processes in the system run queue averaged over\n\
6908the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6909was unobtainable");
6910
6911static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006912posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006913{
6914 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006915 if (getloadavg(loadavg, 3)!=3) {
6916 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6917 return NULL;
6918 } else
6919 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6920}
6921#endif
6922
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006923#ifdef MS_WINDOWS
6924
6925PyDoc_STRVAR(win32_urandom__doc__,
6926"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006927Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006928
6929typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6930 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6931 DWORD dwFlags );
6932typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6933 BYTE *pbBuffer );
6934
6935static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006936/* This handle is never explicitly released. Instead, the operating
6937 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006938static HCRYPTPROV hCryptProv = 0;
6939
Tim Peters4ad82172004-08-30 17:02:04 +00006940static PyObject*
6941win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006942{
Tim Petersd3115382004-08-30 17:36:46 +00006943 int howMany;
6944 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006945
Tim Peters4ad82172004-08-30 17:02:04 +00006946 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006947 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006948 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006949 if (howMany < 0)
6950 return PyErr_Format(PyExc_ValueError,
6951 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006952
Tim Peters4ad82172004-08-30 17:02:04 +00006953 if (hCryptProv == 0) {
6954 HINSTANCE hAdvAPI32 = NULL;
6955 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006956
Tim Peters4ad82172004-08-30 17:02:04 +00006957 /* Obtain handle to the DLL containing CryptoAPI
6958 This should not fail */
6959 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6960 if(hAdvAPI32 == NULL)
6961 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006962
Tim Peters4ad82172004-08-30 17:02:04 +00006963 /* Obtain pointers to the CryptoAPI functions
6964 This will fail on some early versions of Win95 */
6965 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6966 hAdvAPI32,
6967 "CryptAcquireContextA");
6968 if (pCryptAcquireContext == NULL)
6969 return PyErr_Format(PyExc_NotImplementedError,
6970 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006971
Tim Peters4ad82172004-08-30 17:02:04 +00006972 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6973 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006974 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006975 return PyErr_Format(PyExc_NotImplementedError,
6976 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006977
Tim Peters4ad82172004-08-30 17:02:04 +00006978 /* Acquire context */
6979 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6980 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6981 return win32_error("CryptAcquireContext", NULL);
6982 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006983
Tim Peters4ad82172004-08-30 17:02:04 +00006984 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006985 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006986 if (result != NULL) {
6987 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006988 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006989 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006990 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006991 Py_DECREF(result);
6992 return win32_error("CryptGenRandom", NULL);
6993 }
Tim Peters4ad82172004-08-30 17:02:04 +00006994 }
Tim Petersd3115382004-08-30 17:36:46 +00006995 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006996}
6997#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006998
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006999PyDoc_STRVAR(device_encoding__doc__,
7000"device_encoding(fd) -> str\n\n\
7001Return a string describing the encoding of the device\n\
7002if the output is a terminal; else return None.");
7003
7004static PyObject *
7005device_encoding(PyObject *self, PyObject *args)
7006{
7007 int fd;
7008 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7009 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00007010 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007011 Py_INCREF(Py_None);
7012 return Py_None;
7013 }
7014#if defined(MS_WINDOWS) || defined(MS_WIN64)
7015 if (fd == 0) {
7016 char buf[100];
7017 sprintf(buf, "cp%d", GetConsoleCP());
7018 return PyUnicode_FromString(buf);
7019 }
7020 if (fd == 1 || fd == 2) {
7021 char buf[100];
7022 sprintf(buf, "cp%d", GetConsoleOutputCP());
7023 return PyUnicode_FromString(buf);
7024 }
7025#elif defined(CODESET)
7026 {
7027 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00007028 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007029 return PyUnicode_FromString(codeset);
7030 }
7031#endif
7032 Py_INCREF(Py_None);
7033 return Py_None;
7034}
7035
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007036#ifdef __VMS
7037/* Use openssl random routine */
7038#include <openssl/rand.h>
7039PyDoc_STRVAR(vms_urandom__doc__,
7040"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007041Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007042
7043static PyObject*
7044vms_urandom(PyObject *self, PyObject *args)
7045{
7046 int howMany;
7047 PyObject* result;
7048
7049 /* Read arguments */
7050 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7051 return NULL;
7052 if (howMany < 0)
7053 return PyErr_Format(PyExc_ValueError,
7054 "negative argument not allowed");
7055
7056 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00007057 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007058 if (result != NULL) {
7059 /* Get random data */
7060 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00007061 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007062 howMany) < 0) {
7063 Py_DECREF(result);
7064 return PyErr_Format(PyExc_ValueError,
7065 "RAND_pseudo_bytes");
7066 }
7067 }
7068 return result;
7069}
7070#endif
7071
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007072static PyMethodDef posix_methods[] = {
7073 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7074#ifdef HAVE_TTYNAME
7075 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7076#endif
7077 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007078#ifdef HAVE_CHFLAGS
7079 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7080#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007081 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007082#ifdef HAVE_FCHMOD
7083 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
7084#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007085#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007086 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007087#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007088#ifdef HAVE_LCHMOD
7089 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
7090#endif /* HAVE_LCHMOD */
7091#ifdef HAVE_FCHOWN
7092 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
7093#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007094#ifdef HAVE_LCHFLAGS
7095 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
7096#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007097#ifdef HAVE_LCHOWN
7098 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7099#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007100#ifdef HAVE_CHROOT
7101 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7102#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007103#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007104 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007105#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007106#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00007107 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7108 METH_NOARGS, posix_getcwd__doc__},
7109 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7110 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007111#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007112#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007113 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007114#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007115 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7116 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7117 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007118#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007119 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007120#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007121#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007122 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007123#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007124 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7125 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7126 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007127 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007128#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007129 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007130#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007131#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007132 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007133#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007134 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007135#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007136 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007137#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007138 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7139 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7140 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007141#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007142 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007143#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007144 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007145#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007146 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7147 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007148#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007149#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007150 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7151 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007152#if defined(PYOS_OS2)
7153 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7154 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7155#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007156#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007157#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007158 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007159#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007160#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007161 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007162#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007163#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007164 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007165#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007166#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007167 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007168#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007169#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007170 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007171#endif /* HAVE_GETEGID */
7172#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007173 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007174#endif /* HAVE_GETEUID */
7175#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007176 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007177#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007178#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007179 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007180#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007181 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007182#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007183 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007184#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007185#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007186 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007187#endif /* HAVE_GETPPID */
7188#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007189 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007190#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007191#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007192 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007193#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007194#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007195 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007196#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007197#ifdef HAVE_KILLPG
7198 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7199#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007200#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007201 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007202#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007203#ifdef MS_WINDOWS
7204 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7205#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007206#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007207 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007208#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007209#ifdef HAVE_SETEUID
7210 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7211#endif /* HAVE_SETEUID */
7212#ifdef HAVE_SETEGID
7213 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7214#endif /* HAVE_SETEGID */
7215#ifdef HAVE_SETREUID
7216 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7217#endif /* HAVE_SETREUID */
7218#ifdef HAVE_SETREGID
7219 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7220#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007221#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007222 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007223#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007224#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007225 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007226#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007227#ifdef HAVE_GETPGID
7228 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7229#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007230#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007231 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007232#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007233#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007234 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007235#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007236#ifdef HAVE_WAIT3
7237 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7238#endif /* HAVE_WAIT3 */
7239#ifdef HAVE_WAIT4
7240 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7241#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007242#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007243 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007244#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007245#ifdef HAVE_GETSID
7246 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7247#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007248#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007249 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007250#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007251#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007252 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007253#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007254#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007255 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007256#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007257#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007258 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007259#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007260 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7261 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007262 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007263 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007264 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7265 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7266 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7267 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7268 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7269 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007270 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007271#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007272 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007273#endif
7274#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007275 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007276#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007277#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007278 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7279#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007280#ifdef HAVE_DEVICE_MACROS
7281 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7282 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7283 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7284#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007285#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007286 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007287#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007288#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007289 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007290#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007291#ifdef HAVE_UNSETENV
7292 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7293#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007294 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007295#ifdef HAVE_FCHDIR
7296 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7297#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007298#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007299 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007300#endif
7301#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007302 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007303#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007304#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007305#ifdef WCOREDUMP
7306 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7307#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007308#ifdef WIFCONTINUED
7309 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7310#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007311#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007312 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007313#endif /* WIFSTOPPED */
7314#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007315 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007316#endif /* WIFSIGNALED */
7317#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007318 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007319#endif /* WIFEXITED */
7320#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007321 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007322#endif /* WEXITSTATUS */
7323#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007324 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007325#endif /* WTERMSIG */
7326#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007327 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007328#endif /* WSTOPSIG */
7329#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007330#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007331 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007332#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007333#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007334 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007335#endif
Fred Drakec9680921999-12-13 16:37:25 +00007336#ifdef HAVE_CONFSTR
7337 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7338#endif
7339#ifdef HAVE_SYSCONF
7340 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7341#endif
7342#ifdef HAVE_FPATHCONF
7343 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7344#endif
7345#ifdef HAVE_PATHCONF
7346 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7347#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007348 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007349#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007350 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7351#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007352#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007353 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007354#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007355 #ifdef MS_WINDOWS
7356 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7357 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007358 #ifdef __VMS
7359 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7360 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007361 {NULL, NULL} /* Sentinel */
7362};
7363
7364
Barry Warsaw4a342091996-12-19 23:50:02 +00007365static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007366ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007367{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007368 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007369}
7370
Guido van Rossumd48f2521997-12-05 22:19:34 +00007371#if defined(PYOS_OS2)
7372/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007373static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007374{
7375 APIRET rc;
7376 ULONG values[QSV_MAX+1];
7377 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007378 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007379
7380 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007381 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007382 Py_END_ALLOW_THREADS
7383
7384 if (rc != NO_ERROR) {
7385 os2_error(rc);
7386 return -1;
7387 }
7388
Fred Drake4d1e64b2002-04-15 19:40:07 +00007389 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7390 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7391 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7392 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7393 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7394 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7395 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007396
7397 switch (values[QSV_VERSION_MINOR]) {
7398 case 0: ver = "2.00"; break;
7399 case 10: ver = "2.10"; break;
7400 case 11: ver = "2.11"; break;
7401 case 30: ver = "3.00"; break;
7402 case 40: ver = "4.00"; break;
7403 case 50: ver = "5.00"; break;
7404 default:
Tim Peters885d4572001-11-28 20:27:42 +00007405 PyOS_snprintf(tmp, sizeof(tmp),
7406 "%d-%d", values[QSV_VERSION_MAJOR],
7407 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007408 ver = &tmp[0];
7409 }
7410
7411 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007412 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007413 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007414
7415 /* Add Indicator of Which Drive was Used to Boot the System */
7416 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7417 tmp[1] = ':';
7418 tmp[2] = '\0';
7419
Fred Drake4d1e64b2002-04-15 19:40:07 +00007420 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007421}
7422#endif
7423
Barry Warsaw4a342091996-12-19 23:50:02 +00007424static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007425all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007426{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007427#ifdef F_OK
7428 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007429#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007430#ifdef R_OK
7431 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007432#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007433#ifdef W_OK
7434 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007435#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007436#ifdef X_OK
7437 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007438#endif
Fred Drakec9680921999-12-13 16:37:25 +00007439#ifdef NGROUPS_MAX
7440 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7441#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007442#ifdef TMP_MAX
7443 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7444#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007445#ifdef WCONTINUED
7446 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7447#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007448#ifdef WNOHANG
7449 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007450#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007451#ifdef WUNTRACED
7452 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7453#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007454#ifdef O_RDONLY
7455 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7456#endif
7457#ifdef O_WRONLY
7458 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7459#endif
7460#ifdef O_RDWR
7461 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7462#endif
7463#ifdef O_NDELAY
7464 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7465#endif
7466#ifdef O_NONBLOCK
7467 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7468#endif
7469#ifdef O_APPEND
7470 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7471#endif
7472#ifdef O_DSYNC
7473 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7474#endif
7475#ifdef O_RSYNC
7476 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7477#endif
7478#ifdef O_SYNC
7479 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7480#endif
7481#ifdef O_NOCTTY
7482 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7483#endif
7484#ifdef O_CREAT
7485 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7486#endif
7487#ifdef O_EXCL
7488 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7489#endif
7490#ifdef O_TRUNC
7491 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7492#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007493#ifdef O_BINARY
7494 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7495#endif
7496#ifdef O_TEXT
7497 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7498#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007499#ifdef O_LARGEFILE
7500 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7501#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007502#ifdef O_SHLOCK
7503 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7504#endif
7505#ifdef O_EXLOCK
7506 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7507#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007508
Tim Peters5aa91602002-01-30 05:46:57 +00007509/* MS Windows */
7510#ifdef O_NOINHERIT
7511 /* Don't inherit in child processes. */
7512 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7513#endif
7514#ifdef _O_SHORT_LIVED
7515 /* Optimize for short life (keep in memory). */
7516 /* MS forgot to define this one with a non-underscore form too. */
7517 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7518#endif
7519#ifdef O_TEMPORARY
7520 /* Automatically delete when last handle is closed. */
7521 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7522#endif
7523#ifdef O_RANDOM
7524 /* Optimize for random access. */
7525 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7526#endif
7527#ifdef O_SEQUENTIAL
7528 /* Optimize for sequential access. */
7529 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7530#endif
7531
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007532/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007533#ifdef O_ASYNC
7534 /* Send a SIGIO signal whenever input or output
7535 becomes available on file descriptor */
7536 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7537#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007538#ifdef O_DIRECT
7539 /* Direct disk access. */
7540 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7541#endif
7542#ifdef O_DIRECTORY
7543 /* Must be a directory. */
7544 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7545#endif
7546#ifdef O_NOFOLLOW
7547 /* Do not follow links. */
7548 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7549#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007550#ifdef O_NOATIME
7551 /* Do not update the access time. */
7552 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7553#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007554
Barry Warsaw5676bd12003-01-07 20:57:09 +00007555 /* These come from sysexits.h */
7556#ifdef EX_OK
7557 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007558#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007559#ifdef EX_USAGE
7560 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007561#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007562#ifdef EX_DATAERR
7563 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007564#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007565#ifdef EX_NOINPUT
7566 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007567#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007568#ifdef EX_NOUSER
7569 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007570#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007571#ifdef EX_NOHOST
7572 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007573#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007574#ifdef EX_UNAVAILABLE
7575 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007576#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007577#ifdef EX_SOFTWARE
7578 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007579#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007580#ifdef EX_OSERR
7581 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007582#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007583#ifdef EX_OSFILE
7584 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007585#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007586#ifdef EX_CANTCREAT
7587 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007588#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007589#ifdef EX_IOERR
7590 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007591#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007592#ifdef EX_TEMPFAIL
7593 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007594#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007595#ifdef EX_PROTOCOL
7596 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007597#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007598#ifdef EX_NOPERM
7599 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007600#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007601#ifdef EX_CONFIG
7602 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007603#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007604#ifdef EX_NOTFOUND
7605 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007606#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007607
Guido van Rossum246bc171999-02-01 23:54:31 +00007608#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007609#if defined(PYOS_OS2) && defined(PYCC_GCC)
7610 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7611 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7612 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7613 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7614 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7615 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7616 if (ins(d, "P_PM", (long)P_PM)) return -1;
7617 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7618 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7619 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7620 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7621 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7622 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7623 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7624 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7625 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7626 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7627 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7628 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7629 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7630#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007631 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7632 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7633 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7634 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7635 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007636#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007637#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007638
Guido van Rossumd48f2521997-12-05 22:19:34 +00007639#if defined(PYOS_OS2)
7640 if (insertvalues(d)) return -1;
7641#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007642 return 0;
7643}
7644
7645
Tim Peters5aa91602002-01-30 05:46:57 +00007646#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007647#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007648#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007649
7650#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007651#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007652#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007653
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007654#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007655#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007656#define MODNAME "posix"
7657#endif
7658
Martin v. Löwis1a214512008-06-11 05:26:20 +00007659static struct PyModuleDef posixmodule = {
7660 PyModuleDef_HEAD_INIT,
7661 MODNAME,
7662 posix__doc__,
7663 -1,
7664 posix_methods,
7665 NULL,
7666 NULL,
7667 NULL,
7668 NULL
7669};
7670
7671
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007672PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007673INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007674{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007675 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007676
Martin v. Löwis1a214512008-06-11 05:26:20 +00007677 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007678 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007679 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007680
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007681 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007682 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007683 Py_XINCREF(v);
7684 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007685 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007686 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007687
Fred Drake4d1e64b2002-04-15 19:40:07 +00007688 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007689 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007690
Fred Drake4d1e64b2002-04-15 19:40:07 +00007691 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007692 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007693
Fred Drake4d1e64b2002-04-15 19:40:07 +00007694 Py_INCREF(PyExc_OSError);
7695 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007696
Guido van Rossumb3d39562000-01-31 18:41:26 +00007697#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007698 if (posix_putenv_garbage == NULL)
7699 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007700#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007701
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007702 if (!initialized) {
7703 stat_result_desc.name = MODNAME ".stat_result";
7704 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7705 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7706 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7707 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7708 structseq_new = StatResultType.tp_new;
7709 StatResultType.tp_new = statresult_new;
7710
7711 statvfs_result_desc.name = MODNAME ".statvfs_result";
7712 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007713#ifdef NEED_TICKS_PER_SECOND
7714# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7715 ticks_per_second = sysconf(_SC_CLK_TCK);
7716# elif defined(HZ)
7717 ticks_per_second = HZ;
7718# else
7719 ticks_per_second = 60; /* magic fallback value; may be bogus */
7720# endif
7721#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007722 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007723 Py_INCREF((PyObject*) &StatResultType);
7724 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007725 Py_INCREF((PyObject*) &StatVFSResultType);
7726 PyModule_AddObject(m, "statvfs_result",
7727 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007728 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007729
7730#ifdef __APPLE__
7731 /*
7732 * Step 2 of weak-linking support on Mac OS X.
7733 *
7734 * The code below removes functions that are not available on the
7735 * currently active platform.
7736 *
7737 * This block allow one to use a python binary that was build on
7738 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7739 * OSX 10.4.
7740 */
7741#ifdef HAVE_FSTATVFS
7742 if (fstatvfs == NULL) {
7743 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007744 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007745 }
7746 }
7747#endif /* HAVE_FSTATVFS */
7748
7749#ifdef HAVE_STATVFS
7750 if (statvfs == NULL) {
7751 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007752 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007753 }
7754 }
7755#endif /* HAVE_STATVFS */
7756
7757# ifdef HAVE_LCHOWN
7758 if (lchown == NULL) {
7759 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007760 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007761 }
7762 }
7763#endif /* HAVE_LCHOWN */
7764
7765
7766#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007767 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007768
Guido van Rossumb6775db1994-08-01 11:34:53 +00007769}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007770
7771#ifdef __cplusplus
7772}
7773#endif