blob: 8835b0cc8046ef3d354e0f575ce10a0fbd593001 [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
Greg Wardb48bc172000-03-01 21:51:56 +0000306/* Don't use the "_r" form if we don't need it (also, won't have a
307 prototype for it, at least on Solaris -- maybe others as well?). */
308#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
309#define USE_CTERMID_R
310#endif
311
Fred Drake699f3522000-06-29 21:12:41 +0000312/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000313#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000314#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000315# define STAT win32_stat
316# define FSTAT win32_fstat
317# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000318#else
319# define STAT stat
320# define FSTAT fstat
321# define STRUCT_STAT struct stat
322#endif
323
Tim Peters11b23062003-04-23 02:39:17 +0000324#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000325#include <sys/mkdev.h>
326#else
327#if defined(MAJOR_IN_SYSMACROS)
328#include <sys/sysmacros.h>
329#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000330#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
331#include <sys/mkdev.h>
332#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000333#endif
Fred Drake699f3522000-06-29 21:12:41 +0000334
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000335#if defined _MSC_VER && _MSC_VER >= 1400
336/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
337 * valid and throw an assertion if it isn't.
338 * Normally, an invalid fd is likely to be a C program error and therefore
339 * an assertion can be useful, but it does contradict the POSIX standard
340 * which for write(2) states:
341 * "Otherwise, -1 shall be returned and errno set to indicate the error."
342 * "[EBADF] The fildes argument is not a valid file descriptor open for
343 * writing."
344 * Furthermore, python allows the user to enter any old integer
345 * as a fd and should merely raise a python exception on error.
346 * The Microsoft CRT doesn't provide an official way to check for the
347 * validity of a file descriptor, but we can emulate its internal behaviour
348 * by using the exported __pinfo data member and knowledge of the
349 * internal structures involved.
350 * The structures below must be updated for each version of visual studio
351 * according to the file internal.h in the CRT source, until MS comes
352 * up with a less hacky way to do this.
353 * (all of this is to avoid globally modifying the CRT behaviour using
354 * _set_invalid_parameter_handler() and _CrtSetReportMode())
355 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000356/* The actual size of the structure is determined at runtime.
357 * Only the first items must be present.
358 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000359typedef struct {
360 intptr_t osfhnd;
361 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000362} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000363
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000364extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000365#define IOINFO_L2E 5
366#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
367#define IOINFO_ARRAYS 64
368#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
369#define FOPEN 0x01
370#define _NO_CONSOLE_FILENO (intptr_t)-2
371
372/* This function emulates what the windows CRT does to validate file handles */
373int
374_PyVerify_fd(int fd)
375{
376 const int i1 = fd >> IOINFO_L2E;
377 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000378
379 static int sizeof_ioinfo = 0;
380
381 /* Determine the actual size of the ioinfo structure,
382 * as used by the CRT loaded in memory
383 */
384 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
385 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
386 }
387 if (sizeof_ioinfo == 0) {
388 /* This should not happen... */
389 goto fail;
390 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000391
392 /* See that it isn't a special CLEAR fileno */
393 if (fd != _NO_CONSOLE_FILENO) {
394 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
395 * we check pointer validity and other info
396 */
397 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
398 /* finally, check that the file is open */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000399 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
400 if (info->osfile & FOPEN) {
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000401 return 1;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000402 }
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000403 }
404 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000405 fail:
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000406 errno = EBADF;
407 return 0;
408}
409
410/* the special case of checking dup2. The target fd must be in a sensible range */
411static int
412_PyVerify_fd_dup2(int fd1, int fd2)
413{
414 if (!_PyVerify_fd(fd1))
415 return 0;
416 if (fd2 == _NO_CONSOLE_FILENO)
417 return 0;
418 if ((unsigned)fd2 < _NHANDLE_)
419 return 1;
420 else
421 return 0;
422}
423#else
424/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
425#define _PyVerify_fd_dup2(A, B) (1)
426#endif
427
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000429#ifdef WITH_NEXT_FRAMEWORK
430/* On Darwin/MacOSX a shared library or framework has no access to
431** environ directly, we must obtain it with _NSGetEnviron().
432*/
433#include <crt_externs.h>
434static char **environ;
435#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000437#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438
Barry Warsaw53699e91996-12-10 23:23:01 +0000439static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000440convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Barry Warsaw53699e91996-12-10 23:23:01 +0000442 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000443#ifdef MS_WINDOWS
444 wchar_t **e;
445#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000447#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000448 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 if (d == NULL)
450 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000451#ifdef WITH_NEXT_FRAMEWORK
452 if (environ == NULL)
453 environ = *_NSGetEnviron();
454#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000455#ifdef MS_WINDOWS
456 /* _wenviron must be initialized in this way if the program is started
457 through main() instead of wmain(). */
458 _wgetenv(L"");
459 if (_wenviron == NULL)
460 return d;
461 /* This part ignores errors */
462 for (e = _wenviron; *e != NULL; e++) {
463 PyObject *k;
464 PyObject *v;
465 wchar_t *p = wcschr(*e, L'=');
466 if (p == NULL)
467 continue;
468 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
469 if (k == NULL) {
470 PyErr_Clear();
471 continue;
472 }
473 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
474 if (v == NULL) {
475 PyErr_Clear();
476 Py_DECREF(k);
477 continue;
478 }
479 if (PyDict_GetItem(d, k) == NULL) {
480 if (PyDict_SetItem(d, k, v) != 0)
481 PyErr_Clear();
482 }
483 Py_DECREF(k);
484 Py_DECREF(v);
485 }
486#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487 if (environ == NULL)
488 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000489 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000491 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000492 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493 char *p = strchr(*e, '=');
494 if (p == NULL)
495 continue;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000496 k = PyUnicode_Decode(*e, (int)(p-*e),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000497 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000498 if (k == NULL) {
499 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000501 }
Martin v. Löwis011e8422009-05-05 04:43:17 +0000502 v = PyUnicode_Decode(p+1, strlen(p+1),
Martin v. Löwis43c57782009-05-10 08:15:24 +0000503 Py_FileSystemDefaultEncoding, "surrogateescape");
Guido van Rossum6a619f41999-08-03 19:41:10 +0000504 if (v == NULL) {
505 PyErr_Clear();
506 Py_DECREF(k);
507 continue;
508 }
509 if (PyDict_GetItem(d, k) == NULL) {
510 if (PyDict_SetItem(d, k, v) != 0)
511 PyErr_Clear();
512 }
513 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000514 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000516#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000517#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000518 {
519 APIRET rc;
520 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
521
522 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000523 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000524 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000525 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000526 Py_DECREF(v);
527 }
528 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
529 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000530 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000531 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000532 Py_DECREF(v);
533 }
534 }
535#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000536 return d;
537}
538
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539/* Set a POSIX-specific error from errno, and return NULL */
540
Barry Warsawd58d7641998-07-23 16:14:40 +0000541static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000542posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000543{
Barry Warsawca74da41999-02-09 19:31:45 +0000544 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545}
Barry Warsawd58d7641998-07-23 16:14:40 +0000546static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000547posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000548{
Barry Warsawca74da41999-02-09 19:31:45 +0000549 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000550}
551
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000552#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000553static PyObject *
554posix_error_with_unicode_filename(Py_UNICODE* name)
555{
556 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
557}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000558#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000559
560
Mark Hammondef8b6542001-05-13 08:04:26 +0000561static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000562posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000563{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000564 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
Victor Stinnerdcb24032010-04-22 12:08:36 +0000565 PyBytes_AsString(name));
566 Py_DECREF(name);
Mark Hammondef8b6542001-05-13 08:04:26 +0000567 return rc;
568}
569
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000570#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000571static PyObject *
572win32_error(char* function, char* filename)
573{
Mark Hammond33a6da92000-08-15 00:46:38 +0000574 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000575 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000576 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000577 Windows error object, which is non-trivial.
578 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000579 errno = GetLastError();
580 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000581 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000582 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000583 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000584}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000585
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000586static PyObject *
587win32_error_unicode(char* function, Py_UNICODE* filename)
588{
589 /* XXX - see win32_error for comments on 'function' */
590 errno = GetLastError();
591 if (filename)
592 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
593 else
594 return PyErr_SetFromWindowsErr(errno);
595}
596
Thomas Wouters477c8d52006-05-27 19:21:47 +0000597static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000598convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000600 if (PyUnicode_CheckExact(*param))
601 Py_INCREF(*param);
602 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000603 /* For a Unicode subtype that's not a Unicode object,
604 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000605 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
606 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000607 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000608 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000609 Py_FileSystemDefaultEncoding,
610 "strict");
611 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000612}
613
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000614#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615
Guido van Rossumd48f2521997-12-05 22:19:34 +0000616#if defined(PYOS_OS2)
617/**********************************************************************
618 * Helper Function to Trim and Format OS/2 Messages
619 **********************************************************************/
620 static void
621os2_formatmsg(char *msgbuf, int msglen, char *reason)
622{
623 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
624
625 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
626 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
627
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000628 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000629 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
630 }
631
632 /* Add Optional Reason Text */
633 if (reason) {
634 strcat(msgbuf, " : ");
635 strcat(msgbuf, reason);
636 }
637}
638
639/**********************************************************************
640 * Decode an OS/2 Operating System Error Code
641 *
642 * A convenience function to lookup an OS/2 error code and return a
643 * text message we can use to raise a Python exception.
644 *
645 * Notes:
646 * The messages for errors returned from the OS/2 kernel reside in
647 * the file OSO001.MSG in the \OS2 directory hierarchy.
648 *
649 **********************************************************************/
650 static char *
651os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
652{
653 APIRET rc;
654 ULONG msglen;
655
656 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
657 Py_BEGIN_ALLOW_THREADS
658 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
659 errorcode, "oso001.msg", &msglen);
660 Py_END_ALLOW_THREADS
661
662 if (rc == NO_ERROR)
663 os2_formatmsg(msgbuf, msglen, reason);
664 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000665 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000666 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000667
668 return msgbuf;
669}
670
671/* Set an OS/2-specific error and return NULL. OS/2 kernel
672 errors are not in a global variable e.g. 'errno' nor are
673 they congruent with posix error numbers. */
674
675static PyObject * os2_error(int code)
676{
677 char text[1024];
678 PyObject *v;
679
680 os2_strerror(text, sizeof(text), code, "");
681
682 v = Py_BuildValue("(is)", code, text);
683 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000684 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000685 Py_DECREF(v);
686 }
687 return NULL; /* Signal to Python that an Exception is Pending */
688}
689
690#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000691
692/* POSIX generic methods */
693
Barry Warsaw53699e91996-12-10 23:23:01 +0000694static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000695posix_fildes(PyObject *fdobj, int (*func)(int))
696{
697 int fd;
698 int res;
699 fd = PyObject_AsFileDescriptor(fdobj);
700 if (fd < 0)
701 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000702 if (!_PyVerify_fd(fd))
703 return posix_error();
Fred Drake4d1e64b2002-04-15 19:40:07 +0000704 Py_BEGIN_ALLOW_THREADS
705 res = (*func)(fd);
706 Py_END_ALLOW_THREADS
707 if (res < 0)
708 return posix_error();
709 Py_INCREF(Py_None);
710 return Py_None;
711}
Guido van Rossum21142a01999-01-08 21:05:37 +0000712
713static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000714posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715{
Martin v. Löwis011e8422009-05-05 04:43:17 +0000716 PyObject *opath1 = NULL;
717 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000718 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000719 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000720 PyUnicode_FSConverter, &opath1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000722 path1 = PyBytes_AsString(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000723 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000724 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000725 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000726 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000727 return posix_error_with_allocated_filename(opath1);
Victor Stinnerdcb24032010-04-22 12:08:36 +0000728 Py_DECREF(opath1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000729 Py_INCREF(Py_None);
730 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731}
732
Barry Warsaw53699e91996-12-10 23:23:01 +0000733static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000734posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000735 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000736 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000737{
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000738 PyObject *opath1 = NULL, *opath2 = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000739 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000740 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000741 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +0000742 PyUnicode_FSConverter, &opath1,
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000743 PyUnicode_FSConverter, &opath2)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744 return NULL;
Antoine Pitroua6bb9842009-05-10 22:27:00 +0000745 }
Victor Stinnerdcb24032010-04-22 12:08:36 +0000746 path1 = PyBytes_AsString(opath1);
747 path2 = PyBytes_AsString(opath2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000748 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000749 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000750 Py_END_ALLOW_THREADS
Victor Stinnerdcb24032010-04-22 12:08:36 +0000751 Py_DECREF(opath1);
752 Py_DECREF(opath2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000753 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000754 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000756 Py_INCREF(Py_None);
757 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758}
759
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000760#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000761static PyObject*
762win32_1str(PyObject* args, char* func,
763 char* format, BOOL (__stdcall *funcA)(LPCSTR),
764 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
765{
766 PyObject *uni;
767 char *ansi;
768 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000769
770 if (!PyArg_ParseTuple(args, wformat, &uni))
771 PyErr_Clear();
772 else {
773 Py_BEGIN_ALLOW_THREADS
774 result = funcW(PyUnicode_AsUnicode(uni));
775 Py_END_ALLOW_THREADS
776 if (!result)
777 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
778 Py_INCREF(Py_None);
779 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000780 }
781 if (!PyArg_ParseTuple(args, format, &ansi))
782 return NULL;
783 Py_BEGIN_ALLOW_THREADS
784 result = funcA(ansi);
785 Py_END_ALLOW_THREADS
786 if (!result)
787 return win32_error(func, ansi);
788 Py_INCREF(Py_None);
789 return Py_None;
790
791}
792
793/* This is a reimplementation of the C library's chdir function,
794 but one that produces Win32 errors instead of DOS error codes.
795 chdir is essentially a wrapper around SetCurrentDirectory; however,
796 it also needs to set "magic" environment variables indicating
797 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000798static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799win32_chdir(LPCSTR path)
800{
801 char new_path[MAX_PATH+1];
802 int result;
803 char env[4] = "=x:";
804
805 if(!SetCurrentDirectoryA(path))
806 return FALSE;
807 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
808 if (!result)
809 return FALSE;
810 /* In the ANSI API, there should not be any paths longer
811 than MAX_PATH. */
812 assert(result <= MAX_PATH+1);
813 if (strncmp(new_path, "\\\\", 2) == 0 ||
814 strncmp(new_path, "//", 2) == 0)
815 /* UNC path, nothing to do. */
816 return TRUE;
817 env[1] = new_path[0];
818 return SetEnvironmentVariableA(env, new_path);
819}
820
821/* The Unicode version differs from the ANSI version
822 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000823static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824win32_wchdir(LPCWSTR path)
825{
826 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
827 int result;
828 wchar_t env[4] = L"=x:";
829
830 if(!SetCurrentDirectoryW(path))
831 return FALSE;
832 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
833 if (!result)
834 return FALSE;
835 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000836 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837 if (!new_path) {
838 SetLastError(ERROR_OUTOFMEMORY);
839 return FALSE;
840 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000841 result = GetCurrentDirectoryW(result, new_path);
842 if (!result) {
843 free(new_path);
844 return FALSE;
845 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000846 }
847 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
848 wcsncmp(new_path, L"//", 2) == 0)
849 /* UNC path, nothing to do. */
850 return TRUE;
851 env[1] = new_path[0];
852 result = SetEnvironmentVariableW(env, new_path);
853 if (new_path != _new_path)
854 free(new_path);
855 return result;
856}
857#endif
858
Martin v. Löwis14694662006-02-03 12:54:16 +0000859#ifdef MS_WINDOWS
860/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
861 - time stamps are restricted to second resolution
862 - file modification times suffer from forth-and-back conversions between
863 UTC and local time
864 Therefore, we implement our own stat, based on the Win32 API directly.
865*/
866#define HAVE_STAT_NSEC 1
867
868struct win32_stat{
869 int st_dev;
870 __int64 st_ino;
871 unsigned short st_mode;
872 int st_nlink;
873 int st_uid;
874 int st_gid;
875 int st_rdev;
876 __int64 st_size;
877 int st_atime;
878 int st_atime_nsec;
879 int st_mtime;
880 int st_mtime_nsec;
881 int st_ctime;
882 int st_ctime_nsec;
883};
884
885static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
886
887static void
888FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
889{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
891 /* Cannot simply cast and dereference in_ptr,
892 since it might not be aligned properly */
893 __int64 in;
894 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000895 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
896 /* XXX Win32 supports time stamps past 2038; we currently don't */
897 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
898}
899
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900static void
901time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
902{
903 /* XXX endianness */
904 __int64 out;
905 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000906 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907 memcpy(out_ptr, &out, sizeof(out));
908}
909
Martin v. Löwis14694662006-02-03 12:54:16 +0000910/* Below, we *know* that ugo+r is 0444 */
911#if _S_IREAD != 0400
912#error Unsupported C library
913#endif
914static int
915attributes_to_mode(DWORD attr)
916{
917 int m = 0;
918 if (attr & FILE_ATTRIBUTE_DIRECTORY)
919 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
920 else
921 m |= _S_IFREG;
922 if (attr & FILE_ATTRIBUTE_READONLY)
923 m |= 0444;
924 else
925 m |= 0666;
926 return m;
927}
928
929static int
930attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
931{
932 memset(result, 0, sizeof(*result));
933 result->st_mode = attributes_to_mode(info->dwFileAttributes);
934 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
935 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
936 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
937 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
938
939 return 0;
940}
941
Guido van Rossumd8faa362007-04-27 19:54:29 +0000942static BOOL
943attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
944{
945 HANDLE hFindFile;
946 WIN32_FIND_DATAA FileData;
947 hFindFile = FindFirstFileA(pszFile, &FileData);
948 if (hFindFile == INVALID_HANDLE_VALUE)
949 return FALSE;
950 FindClose(hFindFile);
951 pfad->dwFileAttributes = FileData.dwFileAttributes;
952 pfad->ftCreationTime = FileData.ftCreationTime;
953 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
954 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
955 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
956 pfad->nFileSizeLow = FileData.nFileSizeLow;
957 return TRUE;
958}
959
960static BOOL
961attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
962{
963 HANDLE hFindFile;
964 WIN32_FIND_DATAW FileData;
965 hFindFile = FindFirstFileW(pszFile, &FileData);
966 if (hFindFile == INVALID_HANDLE_VALUE)
967 return FALSE;
968 FindClose(hFindFile);
969 pfad->dwFileAttributes = FileData.dwFileAttributes;
970 pfad->ftCreationTime = FileData.ftCreationTime;
971 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
972 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
973 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
974 pfad->nFileSizeLow = FileData.nFileSizeLow;
975 return TRUE;
976}
977
Martin v. Löwis14694662006-02-03 12:54:16 +0000978static int
979win32_stat(const char* path, struct win32_stat *result)
980{
981 WIN32_FILE_ATTRIBUTE_DATA info;
982 int code;
983 char *dot;
Hirokazu Yamamoto6fbdfda2009-06-29 11:37:19 +0000984 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000985 if (GetLastError() != ERROR_SHARING_VIOLATION) {
986 /* Protocol violation: we explicitly clear errno, instead of
987 setting it to a POSIX error. Callers should use GetLastError. */
988 errno = 0;
989 return -1;
990 } else {
991 /* Could not get attributes on open file. Fall back to
992 reading the directory. */
993 if (!attributes_from_dir(path, &info)) {
994 /* Very strange. This should not fail now */
995 errno = 0;
996 return -1;
997 }
998 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000999 }
1000 code = attribute_data_to_stat(&info, result);
1001 if (code != 0)
1002 return code;
1003 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1004 dot = strrchr(path, '.');
1005 if (dot) {
1006 if (stricmp(dot, ".bat") == 0 ||
1007 stricmp(dot, ".cmd") == 0 ||
1008 stricmp(dot, ".exe") == 0 ||
1009 stricmp(dot, ".com") == 0)
1010 result->st_mode |= 0111;
1011 }
1012 return code;
1013}
1014
1015static int
1016win32_wstat(const wchar_t* path, struct win32_stat *result)
1017{
1018 int code;
1019 const wchar_t *dot;
1020 WIN32_FILE_ATTRIBUTE_DATA info;
Hirokazu Yamamoto6fbdfda2009-06-29 11:37:19 +00001021 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001022 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1023 /* Protocol violation: we explicitly clear errno, instead of
1024 setting it to a POSIX error. Callers should use GetLastError. */
1025 errno = 0;
1026 return -1;
1027 } else {
1028 /* Could not get attributes on open file. Fall back to
1029 reading the directory. */
1030 if (!attributes_from_dir_w(path, &info)) {
1031 /* Very strange. This should not fail now */
1032 errno = 0;
1033 return -1;
1034 }
1035 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001036 }
1037 code = attribute_data_to_stat(&info, result);
1038 if (code < 0)
1039 return code;
1040 /* Set IFEXEC if it is an .exe, .bat, ... */
1041 dot = wcsrchr(path, '.');
1042 if (dot) {
1043 if (_wcsicmp(dot, L".bat") == 0 ||
1044 _wcsicmp(dot, L".cmd") == 0 ||
1045 _wcsicmp(dot, L".exe") == 0 ||
1046 _wcsicmp(dot, L".com") == 0)
1047 result->st_mode |= 0111;
1048 }
1049 return code;
1050}
1051
1052static int
1053win32_fstat(int file_number, struct win32_stat *result)
1054{
1055 BY_HANDLE_FILE_INFORMATION info;
1056 HANDLE h;
1057 int type;
1058
1059 h = (HANDLE)_get_osfhandle(file_number);
1060
1061 /* Protocol violation: we explicitly clear errno, instead of
1062 setting it to a POSIX error. Callers should use GetLastError. */
1063 errno = 0;
1064
1065 if (h == INVALID_HANDLE_VALUE) {
1066 /* This is really a C library error (invalid file handle).
1067 We set the Win32 error to the closes one matching. */
1068 SetLastError(ERROR_INVALID_HANDLE);
1069 return -1;
1070 }
1071 memset(result, 0, sizeof(*result));
1072
1073 type = GetFileType(h);
1074 if (type == FILE_TYPE_UNKNOWN) {
1075 DWORD error = GetLastError();
1076 if (error != 0) {
1077 return -1;
1078 }
1079 /* else: valid but unknown file */
1080 }
1081
1082 if (type != FILE_TYPE_DISK) {
1083 if (type == FILE_TYPE_CHAR)
1084 result->st_mode = _S_IFCHR;
1085 else if (type == FILE_TYPE_PIPE)
1086 result->st_mode = _S_IFIFO;
1087 return 0;
1088 }
1089
1090 if (!GetFileInformationByHandle(h, &info)) {
1091 return -1;
1092 }
1093
1094 /* similar to stat() */
1095 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1096 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1097 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1098 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1099 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1100 /* specific to fstat() */
1101 result->st_nlink = info.nNumberOfLinks;
1102 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1103 return 0;
1104}
1105
1106#endif /* MS_WINDOWS */
1107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001108PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001109"stat_result: Result from stat or lstat.\n\n\
1110This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001111 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001112or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1113\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001114Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1115or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001116\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001117See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001118
1119static PyStructSequence_Field stat_result_fields[] = {
1120 {"st_mode", "protection bits"},
1121 {"st_ino", "inode"},
1122 {"st_dev", "device"},
1123 {"st_nlink", "number of hard links"},
1124 {"st_uid", "user ID of owner"},
1125 {"st_gid", "group ID of owner"},
1126 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001127 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1128 {NULL, "integer time of last access"},
1129 {NULL, "integer time of last modification"},
1130 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001131 {"st_atime", "time of last access"},
1132 {"st_mtime", "time of last modification"},
1133 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001134#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001135 {"st_blksize", "blocksize for filesystem I/O"},
1136#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001137#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001138 {"st_blocks", "number of blocks allocated"},
1139#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001140#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001141 {"st_rdev", "device type (if inode device)"},
1142#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001143#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1144 {"st_flags", "user defined flags for file"},
1145#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001146#ifdef HAVE_STRUCT_STAT_ST_GEN
1147 {"st_gen", "generation number"},
1148#endif
1149#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1150 {"st_birthtime", "time of creation"},
1151#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001152 {0}
1153};
1154
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001155#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001156#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001157#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001158#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001159#endif
1160
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001161#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001162#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1163#else
1164#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1165#endif
1166
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001167#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001168#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1169#else
1170#define ST_RDEV_IDX ST_BLOCKS_IDX
1171#endif
1172
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001173#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1174#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1175#else
1176#define ST_FLAGS_IDX ST_RDEV_IDX
1177#endif
1178
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001179#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001180#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001181#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001182#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001183#endif
1184
1185#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1186#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1187#else
1188#define ST_BIRTHTIME_IDX ST_GEN_IDX
1189#endif
1190
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191static PyStructSequence_Desc stat_result_desc = {
1192 "stat_result", /* name */
1193 stat_result__doc__, /* doc */
1194 stat_result_fields,
1195 10
1196};
1197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001199"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1200This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001201 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001202or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001203\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001204See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001205
1206static PyStructSequence_Field statvfs_result_fields[] = {
1207 {"f_bsize", },
1208 {"f_frsize", },
1209 {"f_blocks", },
1210 {"f_bfree", },
1211 {"f_bavail", },
1212 {"f_files", },
1213 {"f_ffree", },
1214 {"f_favail", },
1215 {"f_flag", },
1216 {"f_namemax",},
1217 {0}
1218};
1219
1220static PyStructSequence_Desc statvfs_result_desc = {
1221 "statvfs_result", /* name */
1222 statvfs_result__doc__, /* doc */
1223 statvfs_result_fields,
1224 10
1225};
1226
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001228static PyTypeObject StatResultType;
1229static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001230static newfunc structseq_new;
1231
1232static PyObject *
1233statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1234{
1235 PyStructSequence *result;
1236 int i;
1237
1238 result = (PyStructSequence*)structseq_new(type, args, kwds);
1239 if (!result)
1240 return NULL;
1241 /* If we have been initialized from a tuple,
1242 st_?time might be set to None. Initialize it
1243 from the int slots. */
1244 for (i = 7; i <= 9; i++) {
1245 if (result->ob_item[i+3] == Py_None) {
1246 Py_DECREF(Py_None);
1247 Py_INCREF(result->ob_item[i]);
1248 result->ob_item[i+3] = result->ob_item[i];
1249 }
1250 }
1251 return (PyObject*)result;
1252}
1253
1254
1255
1256/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001257static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001258
1259PyDoc_STRVAR(stat_float_times__doc__,
1260"stat_float_times([newval]) -> oldval\n\n\
1261Determine whether os.[lf]stat represents time stamps as float objects.\n\
1262If newval is True, future calls to stat() return floats, if it is False,\n\
1263future calls return ints. \n\
1264If newval is omitted, return the current setting.\n");
1265
1266static PyObject*
1267stat_float_times(PyObject* self, PyObject *args)
1268{
1269 int newval = -1;
1270 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1271 return NULL;
1272 if (newval == -1)
1273 /* Return old value */
1274 return PyBool_FromLong(_stat_float_times);
1275 _stat_float_times = newval;
1276 Py_INCREF(Py_None);
1277 return Py_None;
1278}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001279
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001280static void
1281fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1282{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001283 PyObject *fval,*ival;
1284#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001285 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001286#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001287 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001288#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001289 if (!ival)
1290 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001291 if (_stat_float_times) {
1292 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1293 } else {
1294 fval = ival;
1295 Py_INCREF(fval);
1296 }
1297 PyStructSequence_SET_ITEM(v, index, ival);
1298 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001299}
1300
Tim Peters5aa91602002-01-30 05:46:57 +00001301/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001302 (used by posix_stat() and posix_fstat()) */
1303static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001304_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001305{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001306 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001307 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001308 if (v == NULL)
1309 return NULL;
1310
Christian Heimes217cfd12007-12-02 14:31:20 +00001311 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001312#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001313 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001314 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001315#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001316 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001317#endif
1318#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001319 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001320 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001321#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001322 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001323#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001324 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1325 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1326 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001327#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001328 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001329 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001330#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001331 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001332#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001333
Martin v. Löwis14694662006-02-03 12:54:16 +00001334#if defined(HAVE_STAT_TV_NSEC)
1335 ansec = st->st_atim.tv_nsec;
1336 mnsec = st->st_mtim.tv_nsec;
1337 cnsec = st->st_ctim.tv_nsec;
1338#elif defined(HAVE_STAT_TV_NSEC2)
1339 ansec = st->st_atimespec.tv_nsec;
1340 mnsec = st->st_mtimespec.tv_nsec;
1341 cnsec = st->st_ctimespec.tv_nsec;
1342#elif defined(HAVE_STAT_NSEC)
1343 ansec = st->st_atime_nsec;
1344 mnsec = st->st_mtime_nsec;
1345 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001346#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001347 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001348#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001349 fill_time(v, 7, st->st_atime, ansec);
1350 fill_time(v, 8, st->st_mtime, mnsec);
1351 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001352
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001353#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001354 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001355 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001356#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001357#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001358 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001359 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001360#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001361#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001362 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001363 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001364#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001365#ifdef HAVE_STRUCT_STAT_ST_GEN
1366 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001367 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001368#endif
1369#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1370 {
1371 PyObject *val;
1372 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001373 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001374#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001375 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001376#else
1377 bnsec = 0;
1378#endif
1379 if (_stat_float_times) {
1380 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1381 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001382 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001383 }
1384 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1385 val);
1386 }
1387#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001388#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1389 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001390 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001391#endif
Fred Drake699f3522000-06-29 21:12:41 +00001392
1393 if (PyErr_Occurred()) {
1394 Py_DECREF(v);
1395 return NULL;
1396 }
1397
1398 return v;
1399}
1400
Martin v. Löwisd8948722004-06-02 09:57:56 +00001401#ifdef MS_WINDOWS
1402
1403/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1404 where / can be used in place of \ and the trailing slash is optional.
1405 Both SERVER and SHARE must have at least one character.
1406*/
1407
1408#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1409#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001410#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001411#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001412#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001413
Tim Peters4ad82172004-08-30 17:02:04 +00001414static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001415IsUNCRootA(char *path, int pathlen)
1416{
1417 #define ISSLASH ISSLASHA
1418
1419 int i, share;
1420
1421 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1422 /* minimum UNCRoot is \\x\y */
1423 return FALSE;
1424 for (i = 2; i < pathlen ; i++)
1425 if (ISSLASH(path[i])) break;
1426 if (i == 2 || i == pathlen)
1427 /* do not allow \\\SHARE or \\SERVER */
1428 return FALSE;
1429 share = i+1;
1430 for (i = share; i < pathlen; i++)
1431 if (ISSLASH(path[i])) break;
1432 return (i != share && (i == pathlen || i == pathlen-1));
1433
1434 #undef ISSLASH
1435}
1436
Tim Peters4ad82172004-08-30 17:02:04 +00001437static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001438IsUNCRootW(Py_UNICODE *path, int pathlen)
1439{
1440 #define ISSLASH ISSLASHW
1441
1442 int i, share;
1443
1444 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1445 /* minimum UNCRoot is \\x\y */
1446 return FALSE;
1447 for (i = 2; i < pathlen ; i++)
1448 if (ISSLASH(path[i])) break;
1449 if (i == 2 || i == pathlen)
1450 /* do not allow \\\SHARE or \\SERVER */
1451 return FALSE;
1452 share = i+1;
1453 for (i = share; i < pathlen; i++)
1454 if (ISSLASH(path[i])) break;
1455 return (i != share && (i == pathlen || i == pathlen-1));
1456
1457 #undef ISSLASH
1458}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001459#endif /* MS_WINDOWS */
1460
Barry Warsaw53699e91996-12-10 23:23:01 +00001461static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001462posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001463 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001464#ifdef __VMS
1465 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1466#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001467 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001468#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001469 char *wformat,
1470 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001471{
Fred Drake699f3522000-06-29 21:12:41 +00001472 STRUCT_STAT st;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001473 PyObject *opath;
1474 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001475 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001476 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001477
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001478#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001479 PyUnicodeObject *po;
1480 if (PyArg_ParseTuple(args, wformat, &po)) {
1481 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001482
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001483 Py_BEGIN_ALLOW_THREADS
1484 /* PyUnicode_AS_UNICODE result OK without
1485 thread lock as it is a simple dereference. */
1486 res = wstatfunc(wpath, &st);
1487 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001488
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001489 if (res != 0)
1490 return win32_error_unicode("stat", wpath);
1491 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001492 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001493 /* Drop the argument parsing error as narrow strings
1494 are also valid. */
1495 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001496#endif
1497
Tim Peters5aa91602002-01-30 05:46:57 +00001498 if (!PyArg_ParseTuple(args, format,
Martin v. Löwis011e8422009-05-05 04:43:17 +00001499 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001500 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001501 path = PyBytes_AsString(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00001502 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001503 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001504 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001505
1506 if (res != 0) {
1507#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00001508 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001509#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001510 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001511#endif
1512 }
1513 else
1514 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001515
Victor Stinnerdcb24032010-04-22 12:08:36 +00001516 Py_DECREF(opath);
Martin v. Löwis14694662006-02-03 12:54:16 +00001517 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001518}
1519
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001520/* POSIX methods */
1521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001523"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001524Use the real uid/gid to test for access to a path. Note that most\n\
1525operations will use the effective uid/gid, therefore this routine can\n\
1526be used in a suid/sgid environment to test if the invoking user has the\n\
1527specified access to the path. The mode argument can be F_OK to test\n\
1528existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001529
1530static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001531posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001532{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001533 PyObject *opath;
Guido van Rossum015f22a1999-01-06 22:52:38 +00001534 char *path;
1535 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001536
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001537#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001538 DWORD attr;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001539 PyUnicodeObject *po;
1540 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1541 Py_BEGIN_ALLOW_THREADS
1542 /* PyUnicode_AS_UNICODE OK without thread lock as
1543 it is a simple dereference. */
1544 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1545 Py_END_ALLOW_THREADS
1546 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001547 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001548 /* Drop the argument parsing error as narrow strings
1549 are also valid. */
1550 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00001551 if (!PyArg_ParseTuple(args, "O&i:access",
1552 PyUnicode_FSConverter, &opath, &mode))
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001553 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001554 path = PyBytes_AsString(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001555 Py_BEGIN_ALLOW_THREADS
1556 attr = GetFileAttributesA(path);
1557 Py_END_ALLOW_THREADS
Victor Stinnerdcb24032010-04-22 12:08:36 +00001558 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001559finish:
1560 if (attr == 0xFFFFFFFF)
1561 /* File does not exist, or cannot read attributes */
1562 return PyBool_FromLong(0);
1563 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001564 the file isn't read-only, or if it's a directory, as there are
1565 no read-only directories on Windows. */
1566 return PyBool_FromLong(!(mode & 2)
1567 || !(attr & FILE_ATTRIBUTE_READONLY)
1568 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001569#else
1570 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001571 if (!PyArg_ParseTuple(args, "O&i:access",
1572 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001573 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001574 path = PyBytes_AsString(opath);
Guido van Rossum015f22a1999-01-06 22:52:38 +00001575 Py_BEGIN_ALLOW_THREADS
1576 res = access(path, mode);
1577 Py_END_ALLOW_THREADS
Victor Stinnerdcb24032010-04-22 12:08:36 +00001578 Py_DECREF(opath);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001579 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001580#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001581}
1582
Guido van Rossumd371ff11999-01-25 16:12:23 +00001583#ifndef F_OK
1584#define F_OK 0
1585#endif
1586#ifndef R_OK
1587#define R_OK 4
1588#endif
1589#ifndef W_OK
1590#define W_OK 2
1591#endif
1592#ifndef X_OK
1593#define X_OK 1
1594#endif
1595
1596#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001598"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001599Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001600
1601static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001602posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001603{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001604 int id;
1605 char *ret;
1606
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001607 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001608 return NULL;
1609
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001610#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001611 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001612 if (id == 0) {
1613 ret = ttyname();
1614 }
1615 else {
1616 ret = NULL;
1617 }
1618#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001619 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001620#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001621 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001622 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001623 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001624}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001625#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001626
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001627#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001628PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001629"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001631
1632static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001633posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001634{
1635 char *ret;
1636 char buffer[L_ctermid];
1637
Greg Wardb48bc172000-03-01 21:51:56 +00001638#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001639 ret = ctermid_r(buffer);
1640#else
1641 ret = ctermid(buffer);
1642#endif
1643 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001644 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001645 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001646}
1647#endif
1648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001649PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001650"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001651Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001652
Barry Warsaw53699e91996-12-10 23:23:01 +00001653static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001654posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001655{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001656#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001657 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001658#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001659 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001660#elif defined(__VMS)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001661 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001662#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00001663 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001664#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665}
1666
Fred Drake4d1e64b2002-04-15 19:40:07 +00001667#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001668PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001669"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001670Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001671opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001672
1673static PyObject *
1674posix_fchdir(PyObject *self, PyObject *fdobj)
1675{
1676 return posix_fildes(fdobj, fchdir);
1677}
1678#endif /* HAVE_FCHDIR */
1679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001682"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001683Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001684
Barry Warsaw53699e91996-12-10 23:23:01 +00001685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001686posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001687{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001688 PyObject *opath = NULL;
Mark Hammondef8b6542001-05-13 08:04:26 +00001689 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001690 int i;
1691 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001692#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001693 DWORD attr;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001694 PyUnicodeObject *po;
1695 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1696 Py_BEGIN_ALLOW_THREADS
1697 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1698 if (attr != 0xFFFFFFFF) {
1699 if (i & _S_IWRITE)
1700 attr &= ~FILE_ATTRIBUTE_READONLY;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001701 else
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001702 attr |= FILE_ATTRIBUTE_READONLY;
1703 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
Mark Hammond817c9292003-12-03 01:22:38 +00001704 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001705 else
1706 res = 0;
1707 Py_END_ALLOW_THREADS
1708 if (!res)
1709 return win32_error_unicode("chmod",
1710 PyUnicode_AS_UNICODE(po));
1711 Py_INCREF(Py_None);
1712 return Py_None;
Mark Hammond817c9292003-12-03 01:22:38 +00001713 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001714 /* Drop the argument parsing error as narrow strings
1715 are also valid. */
1716 PyErr_Clear();
1717
Martin v. Löwis011e8422009-05-05 04:43:17 +00001718 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1719 &opath, &i))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001720 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001721 path = PyBytes_AsString(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001722 Py_BEGIN_ALLOW_THREADS
1723 attr = GetFileAttributesA(path);
1724 if (attr != 0xFFFFFFFF) {
1725 if (i & _S_IWRITE)
1726 attr &= ~FILE_ATTRIBUTE_READONLY;
1727 else
1728 attr |= FILE_ATTRIBUTE_READONLY;
1729 res = SetFileAttributesA(path, attr);
1730 }
1731 else
1732 res = 0;
1733 Py_END_ALLOW_THREADS
1734 if (!res) {
1735 win32_error("chmod", path);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001736 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001737 return NULL;
1738 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00001739 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001740 Py_INCREF(Py_None);
1741 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001742#else /* MS_WINDOWS */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001743 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1744 &opath, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001745 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001746 path = PyBytes_AsString(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001747 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001748 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001749 Py_END_ALLOW_THREADS
1750 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001751 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001752 Py_DECREF(opath);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001753 Py_INCREF(Py_None);
1754 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001755#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001756}
1757
Christian Heimes4e30a842007-11-30 22:12:06 +00001758#ifdef HAVE_FCHMOD
1759PyDoc_STRVAR(posix_fchmod__doc__,
1760"fchmod(fd, mode)\n\n\
1761Change the access permissions of the file given by file\n\
1762descriptor fd.");
1763
1764static PyObject *
1765posix_fchmod(PyObject *self, PyObject *args)
1766{
1767 int fd, mode, res;
1768 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1769 return NULL;
1770 Py_BEGIN_ALLOW_THREADS
1771 res = fchmod(fd, mode);
1772 Py_END_ALLOW_THREADS
1773 if (res < 0)
1774 return posix_error();
1775 Py_RETURN_NONE;
1776}
1777#endif /* HAVE_FCHMOD */
1778
1779#ifdef HAVE_LCHMOD
1780PyDoc_STRVAR(posix_lchmod__doc__,
1781"lchmod(path, mode)\n\n\
1782Change the access permissions of a file. If path is a symlink, this\n\
1783affects the link itself rather than the target.");
1784
1785static PyObject *
1786posix_lchmod(PyObject *self, PyObject *args)
1787{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001788 PyObject *opath;
1789 char *path;
Christian Heimes4e30a842007-11-30 22:12:06 +00001790 int i;
1791 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001792 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1793 &opath, &i))
Christian Heimes4e30a842007-11-30 22:12:06 +00001794 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001795 path = PyBytes_AsString(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001796 Py_BEGIN_ALLOW_THREADS
1797 res = lchmod(path, i);
1798 Py_END_ALLOW_THREADS
1799 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001800 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001801 Py_DECREF(opath);
Christian Heimes4e30a842007-11-30 22:12:06 +00001802 Py_RETURN_NONE;
1803}
1804#endif /* HAVE_LCHMOD */
1805
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001806
Thomas Wouterscf297e42007-02-23 15:07:44 +00001807#ifdef HAVE_CHFLAGS
1808PyDoc_STRVAR(posix_chflags__doc__,
1809"chflags(path, flags)\n\n\
1810Set file flags.");
1811
1812static PyObject *
1813posix_chflags(PyObject *self, PyObject *args)
1814{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001815 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001816 char *path;
1817 unsigned long flags;
1818 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001819 if (!PyArg_ParseTuple(args, "O&k:chflags",
1820 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001821 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001822 path = PyBytes_AsString(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001823 Py_BEGIN_ALLOW_THREADS
1824 res = chflags(path, flags);
1825 Py_END_ALLOW_THREADS
1826 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001827 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001828 Py_DECREF(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001829 Py_INCREF(Py_None);
1830 return Py_None;
1831}
1832#endif /* HAVE_CHFLAGS */
1833
1834#ifdef HAVE_LCHFLAGS
1835PyDoc_STRVAR(posix_lchflags__doc__,
1836"lchflags(path, flags)\n\n\
1837Set file flags.\n\
1838This function will not follow symbolic links.");
1839
1840static PyObject *
1841posix_lchflags(PyObject *self, PyObject *args)
1842{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001843 PyObject *opath;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001844 char *path;
1845 unsigned long flags;
1846 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001847 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Martin v. Löwis4adbc342009-05-05 17:17:55 +00001848 PyUnicode_FSConverter, &opath, &flags))
Thomas Wouterscf297e42007-02-23 15:07:44 +00001849 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001850 path = PyBytes_AsString(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001851 Py_BEGIN_ALLOW_THREADS
1852 res = lchflags(path, flags);
1853 Py_END_ALLOW_THREADS
1854 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001855 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001856 Py_DECREF(opath);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001857 Py_INCREF(Py_None);
1858 return Py_None;
1859}
1860#endif /* HAVE_LCHFLAGS */
1861
Martin v. Löwis244edc82001-10-04 22:44:26 +00001862#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001864"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001866
1867static PyObject *
1868posix_chroot(PyObject *self, PyObject *args)
1869{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001870 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001871}
1872#endif
1873
Guido van Rossum21142a01999-01-08 21:05:37 +00001874#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001876"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001877force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001878
1879static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001880posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001881{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001882 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001883}
1884#endif /* HAVE_FSYNC */
1885
1886#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001887
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001888#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001889extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1890#endif
1891
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001892PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001893"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001894force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001896
1897static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001898posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001899{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001900 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001901}
1902#endif /* HAVE_FDATASYNC */
1903
1904
Fredrik Lundh10723342000-07-10 16:38:09 +00001905#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001906PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001907"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001909
Barry Warsaw53699e91996-12-10 23:23:01 +00001910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001911posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001912{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001913 PyObject *opath;
1914 char *path;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001915 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001916 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001917 if (!PyArg_ParseTuple(args, "O&ll:chown",
1918 PyUnicode_FSConverter, &opath,
Mark Hammondef8b6542001-05-13 08:04:26 +00001919 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001920 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001921 path = PyBytes_AsString(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001922 Py_BEGIN_ALLOW_THREADS
1923 res = chown(path, (uid_t) uid, (gid_t) gid);
1924 Py_END_ALLOW_THREADS
1925 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001926 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001927 Py_DECREF(opath);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001928 Py_INCREF(Py_None);
1929 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001930}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001931#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001932
Christian Heimes4e30a842007-11-30 22:12:06 +00001933#ifdef HAVE_FCHOWN
1934PyDoc_STRVAR(posix_fchown__doc__,
1935"fchown(fd, uid, gid)\n\n\
1936Change the owner and group id of the file given by file descriptor\n\
1937fd to the numeric uid and gid.");
1938
1939static PyObject *
1940posix_fchown(PyObject *self, PyObject *args)
1941{
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001942 int fd;
1943 long uid, gid;
Christian Heimes4e30a842007-11-30 22:12:06 +00001944 int res;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001945 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
Christian Heimes4e30a842007-11-30 22:12:06 +00001946 return NULL;
1947 Py_BEGIN_ALLOW_THREADS
1948 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1949 Py_END_ALLOW_THREADS
1950 if (res < 0)
1951 return posix_error();
1952 Py_RETURN_NONE;
1953}
1954#endif /* HAVE_FCHOWN */
1955
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001956#ifdef HAVE_LCHOWN
1957PyDoc_STRVAR(posix_lchown__doc__,
1958"lchown(path, uid, gid)\n\n\
1959Change the owner and group id of path to the numeric uid and gid.\n\
1960This function will not follow symbolic links.");
1961
1962static PyObject *
1963posix_lchown(PyObject *self, PyObject *args)
1964{
Martin v. Löwis011e8422009-05-05 04:43:17 +00001965 PyObject *opath;
1966 char *path;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001967 long uid, gid;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001968 int res;
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001969 if (!PyArg_ParseTuple(args, "O&ll:lchown",
Martin v. Löwis011e8422009-05-05 04:43:17 +00001970 PyUnicode_FSConverter, &opath,
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001971 &uid, &gid))
1972 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00001973 path = PyBytes_AsString(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001974 Py_BEGIN_ALLOW_THREADS
1975 res = lchown(path, (uid_t) uid, (gid_t) gid);
1976 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001977 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001978 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00001979 Py_DECREF(opath);
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001980 Py_INCREF(Py_None);
1981 return Py_None;
1982}
1983#endif /* HAVE_LCHOWN */
1984
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001985
Guido van Rossum36bc6801995-06-14 22:54:23 +00001986#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00001987static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001988posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001989{
1990 char buf[1026];
1991 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001992
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001993#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001994 if (!use_bytes) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001996 wchar_t *wbuf2 = wbuf;
1997 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001998 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001999 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002000 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2001 /* If the buffer is large enough, len does not include the
2002 terminating \0. If the buffer is too small, len includes
2003 the space needed for the terminator. */
2004 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2005 wbuf2 = malloc(len * sizeof(wchar_t));
2006 if (wbuf2)
2007 len = GetCurrentDirectoryW(len, wbuf2);
2008 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002009 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002010 if (!wbuf2) {
2011 PyErr_NoMemory();
2012 return NULL;
2013 }
2014 if (!len) {
2015 if (wbuf2 != wbuf) free(wbuf2);
2016 return win32_error("getcwdu", NULL);
2017 }
2018 resobj = PyUnicode_FromWideChar(wbuf2, len);
2019 if (wbuf2 != wbuf) free(wbuf2);
2020 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002021 }
2022#endif
2023
2024 Py_BEGIN_ALLOW_THREADS
2025#if defined(PYOS_OS2) && defined(PYCC_GCC)
2026 res = _getcwd2(buf, sizeof buf);
2027#else
2028 res = getcwd(buf, sizeof buf);
2029#endif
2030 Py_END_ALLOW_THREADS
2031 if (res == NULL)
2032 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002033 if (use_bytes)
2034 return PyBytes_FromStringAndSize(buf, strlen(buf));
Martin v. Löwis43c57782009-05-10 08:15:24 +00002035 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002036}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002037
2038PyDoc_STRVAR(posix_getcwd__doc__,
2039"getcwd() -> path\n\n\
2040Return a unicode string representing the current working directory.");
2041
2042static PyObject *
2043posix_getcwd_unicode(PyObject *self)
2044{
2045 return posix_getcwd(0);
2046}
2047
2048PyDoc_STRVAR(posix_getcwdb__doc__,
2049"getcwdb() -> path\n\n\
2050Return a bytes string representing the current working directory.");
2051
2052static PyObject *
2053posix_getcwd_bytes(PyObject *self)
2054{
2055 return posix_getcwd(1);
2056}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002057#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002058
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002059
Guido van Rossumb6775db1994-08-01 11:34:53 +00002060#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002061PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002062"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002063Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002064
Barry Warsaw53699e91996-12-10 23:23:01 +00002065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002066posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002067{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002068 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002069}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002070#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002073PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002074"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075Return a list containing the names of the entries in the directory.\n\
2076\n\
2077 path: path of directory to list\n\
2078\n\
2079The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002080entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002081
Barry Warsaw53699e91996-12-10 23:23:01 +00002082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002083posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002084{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002085 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002086 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002087#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002088
Barry Warsaw53699e91996-12-10 23:23:01 +00002089 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002090 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002091 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002092 WIN32_FIND_DATA FileData;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002093 PyObject *opath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002094 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002095 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002096 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002097
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002098 PyObject *po;
2099 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2100 WIN32_FIND_DATAW wFileData;
2101 Py_UNICODE *wnamebuf;
2102 /* Overallocate for \\*.*\0 */
2103 len = PyUnicode_GET_SIZE(po);
2104 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2105 if (!wnamebuf) {
2106 PyErr_NoMemory();
2107 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002108 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002109 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2110 if (len > 0) {
2111 Py_UNICODE wch = wnamebuf[len-1];
2112 if (wch != L'/' && wch != L'\\' && wch != L':')
2113 wnamebuf[len++] = L'\\';
2114 wcscpy(wnamebuf + len, L"*.*");
2115 }
2116 if ((d = PyList_New(0)) == NULL) {
2117 free(wnamebuf);
2118 return NULL;
2119 }
2120 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2121 if (hFindFile == INVALID_HANDLE_VALUE) {
2122 int error = GetLastError();
2123 if (error == ERROR_FILE_NOT_FOUND) {
2124 free(wnamebuf);
2125 return d;
2126 }
2127 Py_DECREF(d);
2128 win32_error_unicode("FindFirstFileW", wnamebuf);
2129 free(wnamebuf);
2130 return NULL;
2131 }
2132 do {
2133 /* Skip over . and .. */
2134 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2135 wcscmp(wFileData.cFileName, L"..") != 0) {
2136 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2137 if (v == NULL) {
2138 Py_DECREF(d);
2139 d = NULL;
2140 break;
2141 }
2142 if (PyList_Append(d, v) != 0) {
2143 Py_DECREF(v);
2144 Py_DECREF(d);
2145 d = NULL;
2146 break;
2147 }
2148 Py_DECREF(v);
2149 }
2150 Py_BEGIN_ALLOW_THREADS
2151 result = FindNextFileW(hFindFile, &wFileData);
2152 Py_END_ALLOW_THREADS
2153 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2154 it got to the end of the directory. */
2155 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2156 Py_DECREF(d);
2157 win32_error_unicode("FindNextFileW", wnamebuf);
2158 FindClose(hFindFile);
2159 free(wnamebuf);
2160 return NULL;
2161 }
2162 } while (result == TRUE);
2163
2164 if (FindClose(hFindFile) == FALSE) {
2165 Py_DECREF(d);
2166 win32_error_unicode("FindClose", wnamebuf);
2167 free(wnamebuf);
2168 return NULL;
2169 }
2170 free(wnamebuf);
2171 return d;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002172 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002173 /* Drop the argument parsing error as narrow strings
2174 are also valid. */
2175 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002176
Martin v. Löwis011e8422009-05-05 04:43:17 +00002177 if (!PyArg_ParseTuple(args, "O&:listdir",
2178 PyUnicode_FSConverter, &opath))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002179 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002180 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002181 PyErr_SetString(PyExc_ValueError, "path too long");
2182 Py_DECREF(opath);
2183 return NULL;
2184 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002185 strcpy(namebuf, PyBytes_AsString(opath));
Martin v. Löwis011e8422009-05-05 04:43:17 +00002186 len = PyObject_Size(opath);
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002187 if (len > 0) {
2188 char ch = namebuf[len-1];
2189 if (ch != SEP && ch != ALTSEP && ch != ':')
2190 namebuf[len++] = '/';
Hirokazu Yamamotobbb9be72009-05-04 05:56:46 +00002191 strcpy(namebuf + len, "*.*");
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002192 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002193
Barry Warsaw53699e91996-12-10 23:23:01 +00002194 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002195 return NULL;
2196
2197 hFindFile = FindFirstFile(namebuf, &FileData);
2198 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002199 int error = GetLastError();
2200 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002201 return d;
2202 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002203 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002204 }
2205 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002206 /* Skip over . and .. */
2207 if (strcmp(FileData.cFileName, ".") != 0 &&
2208 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002209 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002210 if (v == NULL) {
2211 Py_DECREF(d);
2212 d = NULL;
2213 break;
2214 }
2215 if (PyList_Append(d, v) != 0) {
2216 Py_DECREF(v);
2217 Py_DECREF(d);
2218 d = NULL;
2219 break;
2220 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002221 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002222 }
Georg Brandl622927b2006-03-07 12:48:03 +00002223 Py_BEGIN_ALLOW_THREADS
2224 result = FindNextFile(hFindFile, &FileData);
2225 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002226 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2227 it got to the end of the directory. */
2228 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2229 Py_DECREF(d);
2230 win32_error("FindNextFile", namebuf);
2231 FindClose(hFindFile);
2232 return NULL;
2233 }
Georg Brandl622927b2006-03-07 12:48:03 +00002234 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002235
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002236 if (FindClose(hFindFile) == FALSE) {
2237 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002238 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002239 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002240
2241 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002242
Tim Peters0bb44a42000-09-15 07:44:49 +00002243#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002244
2245#ifndef MAX_PATH
2246#define MAX_PATH CCHMAXPATH
2247#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002248 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002249 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002250 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002251 PyObject *d, *v;
2252 char namebuf[MAX_PATH+5];
2253 HDIR hdir = 1;
2254 ULONG srchcnt = 1;
2255 FILEFINDBUF3 ep;
2256 APIRET rc;
2257
Martin v. Löwis011e8422009-05-05 04:43:17 +00002258 if (!PyArg_ParseTuple(args, "O&:listdir",
2259 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002260 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002261 name = PyBytes_AsString(oname);
2262 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002263 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002264 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002265 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002266 return NULL;
2267 }
2268 strcpy(namebuf, name);
2269 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002270 if (*pt == ALTSEP)
2271 *pt = SEP;
2272 if (namebuf[len-1] != SEP)
2273 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002274 strcpy(namebuf + len, "*.*");
2275
Neal Norwitz6c913782007-10-14 03:23:09 +00002276 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002277 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002278 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002279 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002280
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002281 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2282 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002283 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002284 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2285 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2286 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002287
2288 if (rc != NO_ERROR) {
2289 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002290 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002291 }
2292
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002293 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002294 do {
2295 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002296 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002297 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002298
2299 strcpy(namebuf, ep.achName);
2300
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002301 /* Leave Case of Name Alone -- In Native Form */
2302 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002303
Christian Heimes72b710a2008-05-26 13:28:38 +00002304 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002305 if (v == NULL) {
2306 Py_DECREF(d);
2307 d = NULL;
2308 break;
2309 }
2310 if (PyList_Append(d, v) != 0) {
2311 Py_DECREF(v);
2312 Py_DECREF(d);
2313 d = NULL;
2314 break;
2315 }
2316 Py_DECREF(v);
2317 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2318 }
2319
Victor Stinnerdcb24032010-04-22 12:08:36 +00002320 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002321 return d;
2322#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002323 PyObject *oname;
2324 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00002325 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002326 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002327 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002328 int arg_is_unicode = 1;
2329
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002330 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002331 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2332 arg_is_unicode = 0;
2333 PyErr_Clear();
2334 }
Martin v. Löwis011e8422009-05-05 04:43:17 +00002335 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002336 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002337 name = PyBytes_AsString(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002338 if ((dirp = opendir(name)) == NULL) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002339 return posix_error_with_allocated_filename(oname);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002340 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002341 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002342 closedir(dirp);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002343 Py_DECREF(oname);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002344 return NULL;
2345 }
Georg Brandl622927b2006-03-07 12:48:03 +00002346 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002347 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002348 Py_BEGIN_ALLOW_THREADS
2349 ep = readdir(dirp);
2350 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002351 if (ep == NULL) {
2352 if (errno == 0) {
2353 break;
2354 } else {
2355 closedir(dirp);
2356 Py_DECREF(d);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002357 return posix_error_with_allocated_filename(oname);
Georg Brandl3dbca812008-07-23 16:10:53 +00002358 }
2359 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002360 if (ep->d_name[0] == '.' &&
2361 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002362 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002363 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002364 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002365 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002366 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002367 d = NULL;
2368 break;
2369 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002370 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002371 PyObject *w;
2372
2373 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002374 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00002375 "surrogateescape");
Martin v. Löwis011e8422009-05-05 04:43:17 +00002376 Py_DECREF(v);
2377 if (w != NULL)
Just van Rossum6a421832003-03-04 19:30:44 +00002378 v = w;
Just van Rossum6a421832003-03-04 19:30:44 +00002379 else {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002380 /* Encoding failed to decode ASCII bytes.
2381 Raise exception. */
2382 Py_DECREF(d);
2383 d = NULL;
2384 break;
Just van Rossum46c97842003-02-25 21:42:15 +00002385 }
Just van Rossum46c97842003-02-25 21:42:15 +00002386 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002387 if (PyList_Append(d, v) != 0) {
2388 Py_DECREF(v);
2389 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002390 d = NULL;
2391 break;
2392 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002393 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002394 }
2395 closedir(dirp);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002396 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002397
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002398 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002399
Tim Peters0bb44a42000-09-15 07:44:49 +00002400#endif /* which OS */
2401} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002402
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002403#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002404/* A helper function for abspath on win32 */
2405static PyObject *
2406posix__getfullpathname(PyObject *self, PyObject *args)
2407{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002408 PyObject *opath;
2409 char *path;
Mark Hammondef8b6542001-05-13 08:04:26 +00002410 char outbuf[MAX_PATH*2];
2411 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002412#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002413 PyUnicodeObject *po;
2414 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2415 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2416 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2417 Py_UNICODE *wtemp;
2418 DWORD result;
2419 PyObject *v;
2420 result = GetFullPathNameW(wpath,
2421 sizeof(woutbuf)/sizeof(woutbuf[0]),
2422 woutbuf, &wtemp);
2423 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2424 woutbufp = malloc(result * sizeof(Py_UNICODE));
2425 if (!woutbufp)
2426 return PyErr_NoMemory();
2427 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002428 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002429 if (result)
2430 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2431 else
2432 v = win32_error_unicode("GetFullPathNameW", wpath);
2433 if (woutbufp != woutbuf)
2434 free(woutbufp);
2435 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002436 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002437 /* Drop the argument parsing error as narrow strings
2438 are also valid. */
2439 PyErr_Clear();
2440
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002441#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002442 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2443 PyUnicode_FSConverter, &opath))
Mark Hammondef8b6542001-05-13 08:04:26 +00002444 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002445 path = PyBytes_AsString(opath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002446 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2447 outbuf, &temp)) {
2448 win32_error("GetFullPathName", path);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002449 Py_DECREF(opath);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002450 return NULL;
2451 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002452 Py_DECREF(opath);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002453 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2454 return PyUnicode_Decode(outbuf, strlen(outbuf),
2455 Py_FileSystemDefaultEncoding, NULL);
2456 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002457 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002458} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002459#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002461PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002462"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002464
Barry Warsaw53699e91996-12-10 23:23:01 +00002465static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002466posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002467{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002468 int res;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002469 PyObject *opath;
2470 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002471 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002472
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002473#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002474 PyUnicodeObject *po;
2475 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2476 Py_BEGIN_ALLOW_THREADS
2477 /* PyUnicode_AS_UNICODE OK without thread lock as
2478 it is a simple dereference. */
2479 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2480 Py_END_ALLOW_THREADS
2481 if (!res)
2482 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2483 Py_INCREF(Py_None);
2484 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002485 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002486 /* Drop the argument parsing error as narrow strings
2487 are also valid. */
2488 PyErr_Clear();
Martin v. Löwis011e8422009-05-05 04:43:17 +00002489 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2490 PyUnicode_FSConverter, &opath, &mode))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002491 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002492 path = PyBytes_AsString(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002493 Py_BEGIN_ALLOW_THREADS
2494 /* PyUnicode_AS_UNICODE OK without thread lock as
2495 it is a simple dereference. */
2496 res = CreateDirectoryA(path, NULL);
2497 Py_END_ALLOW_THREADS
2498 if (!res) {
2499 win32_error("mkdir", path);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002500 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002501 return NULL;
2502 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002503 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002504 Py_INCREF(Py_None);
2505 return Py_None;
2506#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002507
Martin v. Löwis011e8422009-05-05 04:43:17 +00002508 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2509 PyUnicode_FSConverter, &opath, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002510 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002511 path = PyBytes_AsString(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002512 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002513#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002514 res = mkdir(path);
2515#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002516 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002517#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002518 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002519 if (res < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00002520 return posix_error_with_allocated_filename(opath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002521 Py_DECREF(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002522 Py_INCREF(Py_None);
2523 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002524#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002525}
2526
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002527
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002528/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2529#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002530#include <sys/resource.h>
2531#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002532
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002533
2534#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002536"nice(inc) -> new_priority\n\n\
2537Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538
Barry Warsaw53699e91996-12-10 23:23:01 +00002539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002540posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002541{
2542 int increment, value;
2543
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002544 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002545 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002546
2547 /* There are two flavours of 'nice': one that returns the new
2548 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002549 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2550 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002551
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002552 If we are of the nice family that returns the new priority, we
2553 need to clear errno before the call, and check if errno is filled
2554 before calling posix_error() on a returnvalue of -1, because the
2555 -1 may be the actual new priority! */
2556
2557 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002558 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002559#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002560 if (value == 0)
2561 value = getpriority(PRIO_PROCESS, 0);
2562#endif
2563 if (value == -1 && errno != 0)
2564 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002565 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002566 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002567}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002568#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002570PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002571"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002572Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002573
Barry Warsaw53699e91996-12-10 23:23:01 +00002574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002575posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002576{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002577#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002578 PyObject *o1, *o2;
2579 char *p1, *p2;
2580 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002581 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002582 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002583 if (!convert_to_unicode(&o1))
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002584 goto error;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002585 if (!convert_to_unicode(&o2)) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002586 Py_DECREF(o1);
2587 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002588 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002589 Py_BEGIN_ALLOW_THREADS
2590 result = MoveFileW(PyUnicode_AsUnicode(o1),
2591 PyUnicode_AsUnicode(o2));
2592 Py_END_ALLOW_THREADS
2593 Py_DECREF(o1);
2594 Py_DECREF(o2);
2595 if (!result)
2596 return win32_error("rename", NULL);
2597 Py_INCREF(Py_None);
2598 return Py_None;
2599error:
2600 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002601 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2602 return NULL;
2603 Py_BEGIN_ALLOW_THREADS
2604 result = MoveFileA(p1, p2);
2605 Py_END_ALLOW_THREADS
2606 if (!result)
2607 return win32_error("rename", NULL);
2608 Py_INCREF(Py_None);
2609 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002610#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002611 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002612#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002613}
2614
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002616PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002617"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002618Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002619
Barry Warsaw53699e91996-12-10 23:23:01 +00002620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002621posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002622{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002623#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002624 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002625#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002626 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002627#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002628}
2629
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002631PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002632"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002633Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002634
Barry Warsaw53699e91996-12-10 23:23:01 +00002635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002636posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002637{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002638#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00002639 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002640#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002641 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002642#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002643}
2644
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002646#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002647PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002648"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002649Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002650
Barry Warsaw53699e91996-12-10 23:23:01 +00002651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002652posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002653{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002654 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002655#ifdef MS_WINDOWS
2656 wchar_t *command;
2657 if (!PyArg_ParseTuple(args, "u:system", &command))
2658 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002659
Barry Warsaw53699e91996-12-10 23:23:01 +00002660 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002661 sts = _wsystem(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002662 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002663#else
2664 PyObject *command_obj;
2665 char *command;
2666 if (!PyArg_ParseTuple(args, "O&:system",
2667 PyUnicode_FSConverter, &command_obj))
2668 return NULL;
2669
Victor Stinnerdcb24032010-04-22 12:08:36 +00002670 command = PyBytes_AsString(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002671 Py_BEGIN_ALLOW_THREADS
2672 sts = system(command);
2673 Py_END_ALLOW_THREADS
Victor Stinnerdcb24032010-04-22 12:08:36 +00002674 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002675#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00002676 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002677}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002678#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002682"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002683Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002684
Barry Warsaw53699e91996-12-10 23:23:01 +00002685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002686posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002687{
2688 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002689 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002690 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002691 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002692 if (i < 0)
2693 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002694 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002695}
2696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002699"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002700Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002703"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002704Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002705
Barry Warsaw53699e91996-12-10 23:23:01 +00002706static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002707posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002708{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002709#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002710 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002711#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00002712 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002713#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002714}
2715
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002716
Guido van Rossumb6775db1994-08-01 11:34:53 +00002717#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002718PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002719"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002720Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002721
Barry Warsaw53699e91996-12-10 23:23:01 +00002722static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002723posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002724{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002725 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002726 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002727
Barry Warsaw53699e91996-12-10 23:23:01 +00002728 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002729 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002730 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002731 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002732 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002733 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002734 u.sysname,
2735 u.nodename,
2736 u.release,
2737 u.version,
2738 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002739}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002740#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002741
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002742static int
2743extract_time(PyObject *t, long* sec, long* usec)
2744{
2745 long intval;
2746 if (PyFloat_Check(t)) {
2747 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002748 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002749 if (!intobj)
2750 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002751 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002752 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002753 if (intval == -1 && PyErr_Occurred())
2754 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002755 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002756 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002757 if (*usec < 0)
2758 /* If rounding gave us a negative number,
2759 truncate. */
2760 *usec = 0;
2761 return 0;
2762 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002763 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002764 if (intval == -1 && PyErr_Occurred())
2765 return -1;
2766 *sec = intval;
2767 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002768 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002769}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002771PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002772"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002773utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002774Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002775second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002776
Barry Warsaw53699e91996-12-10 23:23:01 +00002777static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002778posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002779{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002780#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002781 PyObject *arg;
2782 PyUnicodeObject *obwpath;
2783 wchar_t *wpath = NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002784 PyObject *oapath;
2785 char *apath;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002786 HANDLE hFile;
2787 long atimesec, mtimesec, ausec, musec;
2788 FILETIME atime, mtime;
2789 PyObject *result = NULL;
2790
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002791 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2792 wpath = PyUnicode_AS_UNICODE(obwpath);
2793 Py_BEGIN_ALLOW_THREADS
2794 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2795 NULL, OPEN_EXISTING,
2796 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2797 Py_END_ALLOW_THREADS
2798 if (hFile == INVALID_HANDLE_VALUE)
2799 return win32_error_unicode("utime", wpath);
2800 } else
2801 /* Drop the argument parsing error as narrow strings
2802 are also valid. */
2803 PyErr_Clear();
2804
Thomas Wouters477c8d52006-05-27 19:21:47 +00002805 if (!wpath) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002806 if (!PyArg_ParseTuple(args, "O&O:utime",
2807 PyUnicode_FSConverter, &oapath, &arg))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002808 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002809 apath = PyBytes_AsString(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002810 Py_BEGIN_ALLOW_THREADS
2811 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002812 NULL, OPEN_EXISTING,
2813 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002814 Py_END_ALLOW_THREADS
2815 if (hFile == INVALID_HANDLE_VALUE) {
2816 win32_error("utime", apath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00002817 Py_DECREF(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002818 return NULL;
2819 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002820 Py_DECREF(oapath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002821 }
2822
2823 if (arg == Py_None) {
2824 SYSTEMTIME now;
2825 GetSystemTime(&now);
2826 if (!SystemTimeToFileTime(&now, &mtime) ||
2827 !SystemTimeToFileTime(&now, &atime)) {
2828 win32_error("utime", NULL);
2829 goto done;
2830 }
2831 }
2832 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2833 PyErr_SetString(PyExc_TypeError,
2834 "utime() arg 2 must be a tuple (atime, mtime)");
2835 goto done;
2836 }
2837 else {
2838 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2839 &atimesec, &ausec) == -1)
2840 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002841 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002842 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2843 &mtimesec, &musec) == -1)
2844 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002845 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002846 }
2847 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2848 /* Avoid putting the file name into the error here,
2849 as that may confuse the user into believing that
2850 something is wrong with the file, when it also
2851 could be the time stamp that gives a problem. */
2852 win32_error("utime", NULL);
2853 }
2854 Py_INCREF(Py_None);
2855 result = Py_None;
2856done:
2857 CloseHandle(hFile);
2858 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002859#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002860
Martin v. Löwis011e8422009-05-05 04:43:17 +00002861 PyObject *opath;
2862 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002863 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002864 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002865 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002866
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002867#if defined(HAVE_UTIMES)
2868 struct timeval buf[2];
2869#define ATIME buf[0].tv_sec
2870#define MTIME buf[1].tv_sec
2871#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002872/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002873 struct utimbuf buf;
2874#define ATIME buf.actime
2875#define MTIME buf.modtime
2876#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002877#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002878 time_t buf[2];
2879#define ATIME buf[0]
2880#define MTIME buf[1]
2881#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002882#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002883
Mark Hammond817c9292003-12-03 01:22:38 +00002884
Martin v. Löwis011e8422009-05-05 04:43:17 +00002885 if (!PyArg_ParseTuple(args, "O&O:utime",
2886 PyUnicode_FSConverter, &opath, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002887 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002888 path = PyBytes_AsString(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002889 if (arg == Py_None) {
2890 /* optional time values not given */
2891 Py_BEGIN_ALLOW_THREADS
2892 res = utime(path, NULL);
2893 Py_END_ALLOW_THREADS
2894 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002895 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002896 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002897 "utime() arg 2 must be a tuple (atime, mtime)");
Victor Stinnerdcb24032010-04-22 12:08:36 +00002898 Py_DECREF(opath);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002899 return NULL;
2900 }
2901 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002902 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002903 &atime, &ausec) == -1) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002904 Py_DECREF(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002905 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002906 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002907 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002908 &mtime, &musec) == -1) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002909 Py_DECREF(opath);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002910 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002911 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002912 ATIME = atime;
2913 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002914#ifdef HAVE_UTIMES
2915 buf[0].tv_usec = ausec;
2916 buf[1].tv_usec = musec;
2917 Py_BEGIN_ALLOW_THREADS
2918 res = utimes(path, buf);
2919 Py_END_ALLOW_THREADS
2920#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002921 Py_BEGIN_ALLOW_THREADS
2922 res = utime(path, UTIME_ARG);
2923 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002924#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002925 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002926 if (res < 0) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00002927 return posix_error_with_allocated_filename(opath);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002928 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00002929 Py_DECREF(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00002930 Py_INCREF(Py_None);
2931 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002932#undef UTIME_ARG
2933#undef ATIME
2934#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002935#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002936}
2937
Guido van Rossum85e3b011991-06-03 12:42:10 +00002938
Guido van Rossum3b066191991-06-04 19:40:25 +00002939/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002940
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002941PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002942"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002943Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002944
Barry Warsaw53699e91996-12-10 23:23:01 +00002945static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002946posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002947{
2948 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002949 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002950 return NULL;
2951 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002952 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002953}
2954
Martin v. Löwis114619e2002-10-07 06:44:21 +00002955#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2956static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002957free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002958{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002959 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002960 for (i = 0; i < count; i++)
2961 PyMem_Free(array[i]);
2962 PyMem_DEL(array);
2963}
Martin v. Löwis011e8422009-05-05 04:43:17 +00002964
Antoine Pitrou69f71142009-05-24 21:25:49 +00002965static
Martin v. Löwis011e8422009-05-05 04:43:17 +00002966int fsconvert_strdup(PyObject *o, char**out)
2967{
2968 PyObject *bytes;
2969 Py_ssize_t size;
2970 if (!PyUnicode_FSConverter(o, &bytes))
2971 return 0;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002972 size = PyBytes_GET_SIZE(bytes);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002973 *out = PyMem_Malloc(size+1);
2974 if (!*out)
2975 return 0;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002976 memcpy(*out, PyBytes_AsString(bytes), size+1);
Martin v. Löwis011e8422009-05-05 04:43:17 +00002977 Py_DECREF(bytes);
2978 return 1;
2979}
Martin v. Löwis114619e2002-10-07 06:44:21 +00002980#endif
2981
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002982
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002983#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002984PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002985"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002986Execute an executable path with arguments, replacing current process.\n\
2987\n\
2988 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002989 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002990
Barry Warsaw53699e91996-12-10 23:23:01 +00002991static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002992posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002993{
Martin v. Löwis011e8422009-05-05 04:43:17 +00002994 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002995 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002996 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002997 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002998 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002999 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003000
Guido van Rossum89b33251993-10-22 14:26:06 +00003001 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00003002 argv is a list or tuple of strings. */
3003
Martin v. Löwis011e8422009-05-05 04:43:17 +00003004 if (!PyArg_ParseTuple(args, "O&O:execv",
3005 PyUnicode_FSConverter,
3006 &opath, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003007 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003008 path = PyBytes_AsString(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00003009 if (PyList_Check(argv)) {
3010 argc = PyList_Size(argv);
3011 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003012 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003013 else if (PyTuple_Check(argv)) {
3014 argc = PyTuple_Size(argv);
3015 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003016 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003017 else {
Fred Drake661ea262000-10-24 19:57:45 +00003018 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003019 Py_DECREF(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003020 return NULL;
3021 }
Thomas Heller6790d602007-08-30 17:15:14 +00003022 if (argc < 1) {
3023 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003024 Py_DECREF(opath);
Thomas Heller6790d602007-08-30 17:15:14 +00003025 return NULL;
3026 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003027
Barry Warsaw53699e91996-12-10 23:23:01 +00003028 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003029 if (argvlist == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00003030 Py_DECREF(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003031 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003032 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003033 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003034 if (!fsconvert_strdup((*getitem)(argv, i),
3035 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003036 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00003037 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003038 "execv() arg 2 must contain only strings");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003039 Py_DECREF(opath);
Guido van Rossum50422b42000-04-26 20:34:28 +00003040 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003041
Guido van Rossum85e3b011991-06-03 12:42:10 +00003042 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003043 }
3044 argvlist[argc] = NULL;
3045
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003046 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003047
Guido van Rossum85e3b011991-06-03 12:42:10 +00003048 /* If we get here it's definitely an error */
3049
Martin v. Löwis114619e2002-10-07 06:44:21 +00003050 free_string_array(argvlist, argc);
Victor Stinnerdcb24032010-04-22 12:08:36 +00003051 Py_DECREF(opath);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003052 return posix_error();
3053}
3054
Victor Stinner13bb71c2010-04-23 21:41:56 +00003055static char**
3056parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3057{
3058 char **envlist;
3059 Py_ssize_t i, pos, envc;
3060 PyObject *keys=NULL, *vals=NULL;
3061 PyObject *key, *val, *key2, *val2;
3062 char *p, *k, *v;
3063 size_t len;
3064
3065 i = PyMapping_Size(env);
3066 if (i < 0)
3067 return NULL;
3068 envlist = PyMem_NEW(char *, i + 1);
3069 if (envlist == NULL) {
3070 PyErr_NoMemory();
3071 return NULL;
3072 }
3073 envc = 0;
3074 keys = PyMapping_Keys(env);
3075 vals = PyMapping_Values(env);
3076 if (!keys || !vals)
3077 goto error;
3078 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3079 PyErr_Format(PyExc_TypeError,
3080 "env.keys() or env.values() is not a list");
3081 goto error;
3082 }
3083
3084 for (pos = 0; pos < i; pos++) {
3085 key = PyList_GetItem(keys, pos);
3086 val = PyList_GetItem(vals, pos);
3087 if (!key || !val)
3088 goto error;
3089
3090 if (PyUnicode_FSConverter(key, &key2) == 0)
3091 goto error;
3092 if (PyUnicode_FSConverter(val, &val2) == 0) {
3093 Py_DECREF(key2);
3094 goto error;
3095 }
3096
3097#if defined(PYOS_OS2)
3098 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3099 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3100#endif
3101 k = PyBytes_AsString(key2);
3102 v = PyBytes_AsString(val2);
3103 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
3104
3105 p = PyMem_NEW(char, len);
3106 if (p == NULL) {
3107 PyErr_NoMemory();
3108 Py_DECREF(key2);
3109 Py_DECREF(val2);
3110 goto error;
3111 }
3112 PyOS_snprintf(p, len, "%s=%s", k, v);
3113 envlist[envc++] = p;
3114 Py_DECREF(key2);
3115 Py_DECREF(val2);
3116#if defined(PYOS_OS2)
3117 }
3118#endif
3119 }
3120 Py_DECREF(vals);
3121 Py_DECREF(keys);
3122
3123 envlist[envc] = 0;
3124 *envc_ptr = envc;
3125 return envlist;
3126
3127error:
3128 Py_XDECREF(keys);
3129 Py_XDECREF(vals);
3130 while (--envc >= 0)
3131 PyMem_DEL(envlist[envc]);
3132 PyMem_DEL(envlist);
3133 return NULL;
3134}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003136PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003137"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003138Execute a path with arguments and environment, replacing current process.\n\
3139\n\
3140 path: path of executable file\n\
3141 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003142 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003143
Barry Warsaw53699e91996-12-10 23:23:01 +00003144static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003145posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003146{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003147 PyObject *opath;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003148 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003149 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003150 char **argvlist;
3151 char **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003152 Py_ssize_t i, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003153 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003154 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003155
3156 /* execve has three arguments: (path, argv, env), where
3157 argv is a list or tuple of strings and env is a dictionary
3158 like posix.environ. */
3159
Martin v. Löwis011e8422009-05-05 04:43:17 +00003160 if (!PyArg_ParseTuple(args, "O&OO:execve",
3161 PyUnicode_FSConverter,
3162 &opath, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003163 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003164 path = PyBytes_AsString(opath);
Barry Warsaw53699e91996-12-10 23:23:01 +00003165 if (PyList_Check(argv)) {
3166 argc = PyList_Size(argv);
3167 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003168 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003169 else if (PyTuple_Check(argv)) {
3170 argc = PyTuple_Size(argv);
3171 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003172 }
3173 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003174 PyErr_SetString(PyExc_TypeError,
3175 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003176 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003177 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003178 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003179 PyErr_SetString(PyExc_TypeError,
3180 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003181 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003182 }
3183
Barry Warsaw53699e91996-12-10 23:23:01 +00003184 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003185 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003186 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003187 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003188 }
3189 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003190 if (!fsconvert_strdup((*getitem)(argv, i),
3191 &argvlist[i]))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003192 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003193 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003194 goto fail_1;
3195 }
3196 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003197 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003198 argvlist[argc] = NULL;
3199
Victor Stinner13bb71c2010-04-23 21:41:56 +00003200 envlist = parse_envlist(env, &envc);
3201 if (envlist == NULL)
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003202 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003203
3204 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003205
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003206 /* If we get here it's definitely an error */
3207
3208 (void) posix_error();
3209
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003210 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003211 PyMem_DEL(envlist[envc]);
3212 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003213 fail_1:
3214 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003215 fail_0:
Victor Stinnerdcb24032010-04-22 12:08:36 +00003216 Py_DECREF(opath);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003217 return NULL;
3218}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003219#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003220
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003221
Guido van Rossuma1065681999-01-25 23:20:23 +00003222#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003223PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003224"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003225Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003226\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003227 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003228 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003229 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003230
3231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003232posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003233{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003234 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003235 char *path;
3236 PyObject *argv;
3237 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003238 int mode, i;
3239 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003240 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003241 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003242
3243 /* spawnv has three arguments: (mode, path, argv), where
3244 argv is a list or tuple of strings. */
3245
Martin v. Löwis011e8422009-05-05 04:43:17 +00003246 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3247 PyUnicode_FSConverter,
3248 &opath, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003249 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003250 path = PyBytes_AsString(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003251 if (PyList_Check(argv)) {
3252 argc = PyList_Size(argv);
3253 getitem = PyList_GetItem;
3254 }
3255 else if (PyTuple_Check(argv)) {
3256 argc = PyTuple_Size(argv);
3257 getitem = PyTuple_GetItem;
3258 }
3259 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003260 PyErr_SetString(PyExc_TypeError,
3261 "spawnv() arg 2 must be a tuple or list");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003262 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003263 return NULL;
3264 }
3265
3266 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003267 if (argvlist == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00003268 Py_DECREF(opath);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003269 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003270 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003271 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003272 if (!fsconvert_strdup((*getitem)(argv, i),
3273 &argvlist[i])) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003274 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003275 PyErr_SetString(
3276 PyExc_TypeError,
3277 "spawnv() arg 2 must contain only strings");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003278 Py_DECREF(opath);
Fred Drake137507e2000-06-01 02:02:46 +00003279 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003280 }
3281 }
3282 argvlist[argc] = NULL;
3283
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003284#if defined(PYOS_OS2) && defined(PYCC_GCC)
3285 Py_BEGIN_ALLOW_THREADS
3286 spawnval = spawnv(mode, path, argvlist);
3287 Py_END_ALLOW_THREADS
3288#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003289 if (mode == _OLD_P_OVERLAY)
3290 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003291
Tim Peters25059d32001-12-07 20:35:43 +00003292 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003293 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003294 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003295#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003296
Martin v. Löwis114619e2002-10-07 06:44:21 +00003297 free_string_array(argvlist, argc);
Victor Stinnerdcb24032010-04-22 12:08:36 +00003298 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003299
Fred Drake699f3522000-06-29 21:12:41 +00003300 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003301 return posix_error();
3302 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003303#if SIZEOF_LONG == SIZEOF_VOID_P
3304 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003305#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003306 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003307#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003308}
3309
3310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003311PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003312"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003313Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003314\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003315 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003316 path: path of executable file\n\
3317 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003318 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003319
3320static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003321posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003322{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003323 PyObject *opath;
Guido van Rossuma1065681999-01-25 23:20:23 +00003324 char *path;
3325 PyObject *argv, *env;
3326 char **argvlist;
3327 char **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003328 PyObject *res = NULL;
3329 int mode, envc;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003330 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003331 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003332 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003333 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003334
3335 /* spawnve has four arguments: (mode, path, argv, env), where
3336 argv is a list or tuple of strings and env is a dictionary
3337 like posix.environ. */
3338
Martin v. Löwis011e8422009-05-05 04:43:17 +00003339 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3340 PyUnicode_FSConverter,
3341 &opath, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003342 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003343 path = PyBytes_AsString(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003344 if (PyList_Check(argv)) {
3345 argc = PyList_Size(argv);
3346 getitem = PyList_GetItem;
3347 }
3348 else if (PyTuple_Check(argv)) {
3349 argc = PyTuple_Size(argv);
3350 getitem = PyTuple_GetItem;
3351 }
3352 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003353 PyErr_SetString(PyExc_TypeError,
3354 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003355 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003356 }
3357 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003358 PyErr_SetString(PyExc_TypeError,
3359 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003360 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003361 }
3362
3363 argvlist = PyMem_NEW(char *, argc+1);
3364 if (argvlist == NULL) {
3365 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003366 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003367 }
3368 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003369 if (!fsconvert_strdup((*getitem)(argv, i),
3370 &argvlist[i]))
Guido van Rossuma1065681999-01-25 23:20:23 +00003371 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003372 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003373 goto fail_1;
3374 }
3375 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003376 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003377 argvlist[argc] = NULL;
3378
Victor Stinner13bb71c2010-04-23 21:41:56 +00003379 envlist = parse_envlist(env, &envc);
3380 if (envlist == NULL)
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003381 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003382
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003383#if defined(PYOS_OS2) && defined(PYCC_GCC)
3384 Py_BEGIN_ALLOW_THREADS
3385 spawnval = spawnve(mode, path, argvlist, envlist);
3386 Py_END_ALLOW_THREADS
3387#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003388 if (mode == _OLD_P_OVERLAY)
3389 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003390
3391 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003392 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003393 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003394#endif
Tim Peters25059d32001-12-07 20:35:43 +00003395
Fred Drake699f3522000-06-29 21:12:41 +00003396 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003397 (void) posix_error();
3398 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003399#if SIZEOF_LONG == SIZEOF_VOID_P
3400 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003401#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003402 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003403#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003404
Guido van Rossuma1065681999-01-25 23:20:23 +00003405 while (--envc >= 0)
3406 PyMem_DEL(envlist[envc]);
3407 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003408 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003409 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003410 fail_0:
Victor Stinnerdcb24032010-04-22 12:08:36 +00003411 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003412 return res;
3413}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003414
3415/* OS/2 supports spawnvp & spawnvpe natively */
3416#if defined(PYOS_OS2)
3417PyDoc_STRVAR(posix_spawnvp__doc__,
3418"spawnvp(mode, file, args)\n\n\
3419Execute the program 'file' in a new process, using the environment\n\
3420search path to find the file.\n\
3421\n\
3422 mode: mode of process creation\n\
3423 file: executable file name\n\
3424 args: tuple or list of strings");
3425
3426static PyObject *
3427posix_spawnvp(PyObject *self, PyObject *args)
3428{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003429 PyObject *opath;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003430 char *path;
3431 PyObject *argv;
3432 char **argvlist;
3433 int mode, i, argc;
3434 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003435 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003436
3437 /* spawnvp has three arguments: (mode, path, argv), where
3438 argv is a list or tuple of strings. */
3439
Martin v. Löwis011e8422009-05-05 04:43:17 +00003440 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3441 PyUnicode_FSConverter,
3442 &opath, &argv))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003443 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003444 path = PyBytes_AsString(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003445 if (PyList_Check(argv)) {
3446 argc = PyList_Size(argv);
3447 getitem = PyList_GetItem;
3448 }
3449 else if (PyTuple_Check(argv)) {
3450 argc = PyTuple_Size(argv);
3451 getitem = PyTuple_GetItem;
3452 }
3453 else {
3454 PyErr_SetString(PyExc_TypeError,
3455 "spawnvp() arg 2 must be a tuple or list");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003456 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003457 return NULL;
3458 }
3459
3460 argvlist = PyMem_NEW(char *, argc+1);
3461 if (argvlist == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00003462 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003463 return PyErr_NoMemory();
3464 }
3465 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003466 if (!fsconvert_strdup((*getitem)(argv, i),
3467 &argvlist[i])) {
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003468 free_string_array(argvlist, i);
3469 PyErr_SetString(
3470 PyExc_TypeError,
3471 "spawnvp() arg 2 must contain only strings");
Victor Stinnerdcb24032010-04-22 12:08:36 +00003472 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003473 return NULL;
3474 }
3475 }
3476 argvlist[argc] = NULL;
3477
3478 Py_BEGIN_ALLOW_THREADS
3479#if defined(PYCC_GCC)
3480 spawnval = spawnvp(mode, path, argvlist);
3481#else
3482 spawnval = _spawnvp(mode, path, argvlist);
3483#endif
3484 Py_END_ALLOW_THREADS
3485
3486 free_string_array(argvlist, argc);
Victor Stinnerdcb24032010-04-22 12:08:36 +00003487 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003488
3489 if (spawnval == -1)
3490 return posix_error();
3491 else
3492 return Py_BuildValue("l", (long) spawnval);
3493}
3494
3495
3496PyDoc_STRVAR(posix_spawnvpe__doc__,
3497"spawnvpe(mode, file, args, env)\n\n\
3498Execute the program 'file' in a new process, using the environment\n\
3499search path to find the file.\n\
3500\n\
3501 mode: mode of process creation\n\
3502 file: executable file name\n\
3503 args: tuple or list of arguments\n\
3504 env: dictionary of strings mapping to strings");
3505
3506static PyObject *
3507posix_spawnvpe(PyObject *self, PyObject *args)
3508{
Martin v. Löwis011e8422009-05-05 04:43:17 +00003509 PyObject *opath
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003510 char *path;
3511 PyObject *argv, *env;
3512 char **argvlist;
3513 char **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003514 PyObject *res=NULL;
3515 int mode, i, argc, envc;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003516 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003517 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003518 int lastarg = 0;
3519
3520 /* spawnvpe has four arguments: (mode, path, argv, env), where
3521 argv is a list or tuple of strings and env is a dictionary
3522 like posix.environ. */
3523
3524 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
Martin v. Löwis011e8422009-05-05 04:43:17 +00003525 PyUnicode_FSConverter,
3526 &opath, &argv, &env))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003527 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003528 path = PyBytes_AsString(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003529 if (PyList_Check(argv)) {
3530 argc = PyList_Size(argv);
3531 getitem = PyList_GetItem;
3532 }
3533 else if (PyTuple_Check(argv)) {
3534 argc = PyTuple_Size(argv);
3535 getitem = PyTuple_GetItem;
3536 }
3537 else {
3538 PyErr_SetString(PyExc_TypeError,
3539 "spawnvpe() arg 2 must be a tuple or list");
3540 goto fail_0;
3541 }
3542 if (!PyMapping_Check(env)) {
3543 PyErr_SetString(PyExc_TypeError,
3544 "spawnvpe() arg 3 must be a mapping object");
3545 goto fail_0;
3546 }
3547
3548 argvlist = PyMem_NEW(char *, argc+1);
3549 if (argvlist == NULL) {
3550 PyErr_NoMemory();
3551 goto fail_0;
3552 }
3553 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +00003554 if (!fsconvert_strdup((*getitem)(argv, i),
3555 &argvlist[i]))
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003556 {
3557 lastarg = i;
3558 goto fail_1;
3559 }
3560 }
3561 lastarg = argc;
3562 argvlist[argc] = NULL;
3563
Victor Stinner13bb71c2010-04-23 21:41:56 +00003564 envlist = parse_envlist(env, &envc);
3565 if (envlist == NULL)
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003566 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003567
3568 Py_BEGIN_ALLOW_THREADS
3569#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003570 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003571#else
Christian Heimes292d3512008-02-03 16:51:08 +00003572 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003573#endif
3574 Py_END_ALLOW_THREADS
3575
3576 if (spawnval == -1)
3577 (void) posix_error();
3578 else
3579 res = Py_BuildValue("l", (long) spawnval);
3580
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003581 while (--envc >= 0)
3582 PyMem_DEL(envlist[envc]);
3583 PyMem_DEL(envlist);
3584 fail_1:
3585 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003586 fail_0:
Victor Stinnerdcb24032010-04-22 12:08:36 +00003587 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003588 return res;
3589}
3590#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003591#endif /* HAVE_SPAWNV */
3592
3593
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003594#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003595PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003596"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003597Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3598\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003599Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003600
3601static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003602posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003603{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003604 pid_t pid;
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003605 int result = 0;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003606 _PyImport_AcquireLock();
3607 pid = fork1();
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003608 if (pid == 0) {
3609 /* child: this clobbers and resets the import lock. */
3610 PyOS_AfterFork();
3611 } else {
3612 /* parent: release the import lock. */
3613 result = _PyImport_ReleaseLock();
3614 }
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003615 if (pid == -1)
3616 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003617 if (result < 0) {
3618 /* Don't clobber the OSError if the fork failed. */
3619 PyErr_SetString(PyExc_RuntimeError,
3620 "not holding the import lock");
3621 return NULL;
3622 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003623 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003624}
3625#endif
3626
3627
Guido van Rossumad0ee831995-03-01 10:34:45 +00003628#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003629PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003630"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003631Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003632Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003633
Barry Warsaw53699e91996-12-10 23:23:01 +00003634static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003635posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003636{
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003637 pid_t pid;
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003638 int result = 0;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003639 _PyImport_AcquireLock();
3640 pid = fork();
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003641 if (pid == 0) {
3642 /* child: this clobbers and resets the import lock. */
3643 PyOS_AfterFork();
3644 } else {
3645 /* parent: release the import lock. */
3646 result = _PyImport_ReleaseLock();
3647 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003648 if (pid == -1)
3649 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003650 if (result < 0) {
3651 /* Don't clobber the OSError if the fork failed. */
3652 PyErr_SetString(PyExc_RuntimeError,
3653 "not holding the import lock");
3654 return NULL;
3655 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003656 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003657}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003658#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003659
Neal Norwitzb59798b2003-03-21 01:43:31 +00003660/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003661/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3662#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003663#define DEV_PTY_FILE "/dev/ptc"
3664#define HAVE_DEV_PTMX
3665#else
3666#define DEV_PTY_FILE "/dev/ptmx"
3667#endif
3668
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003669#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003670#ifdef HAVE_PTY_H
3671#include <pty.h>
3672#else
3673#ifdef HAVE_LIBUTIL_H
3674#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003675#else
3676#ifdef HAVE_UTIL_H
3677#include <util.h>
3678#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003679#endif /* HAVE_LIBUTIL_H */
3680#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003681#ifdef HAVE_STROPTS_H
3682#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003683#endif
3684#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003685
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003686#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003687PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003688"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003689Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003690
3691static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003692posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003693{
3694 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003695#ifndef HAVE_OPENPTY
3696 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003697#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003698#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003699 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003700#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003701 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003702#endif
3703#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003704
Thomas Wouters70c21a12000-07-14 14:28:33 +00003705#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003706 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3707 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003708#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003709 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3710 if (slave_name == NULL)
3711 return posix_error();
3712
3713 slave_fd = open(slave_name, O_RDWR);
3714 if (slave_fd < 0)
3715 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003716#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003717 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003718 if (master_fd < 0)
3719 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003720 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003721 /* change permission of slave */
3722 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003723 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003724 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003725 }
3726 /* unlock slave */
3727 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003728 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003729 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003730 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003731 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003732 slave_name = ptsname(master_fd); /* get name of slave */
3733 if (slave_name == NULL)
3734 return posix_error();
3735 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3736 if (slave_fd < 0)
3737 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003738#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003739 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3740 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003741#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003742 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003743#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003744#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003745#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003746
Fred Drake8cef4cf2000-06-28 16:40:38 +00003747 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003748
Fred Drake8cef4cf2000-06-28 16:40:38 +00003749}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003750#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003751
3752#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003753PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003754"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003755Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3756Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003757To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003758
3759static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003760posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003761{
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003762 int master_fd = -1, result = 0;
Christian Heimes400adb02008-02-01 08:12:03 +00003763 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003764
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003765 _PyImport_AcquireLock();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003766 pid = forkpty(&master_fd, NULL, NULL, NULL);
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003767 if (pid == 0) {
3768 /* child: this clobbers and resets the import lock. */
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00003769 PyOS_AfterFork();
Gregory P. Smith2a1c0272010-03-01 17:04:45 +00003770 } else {
3771 /* parent: release the import lock. */
3772 result = _PyImport_ReleaseLock();
3773 }
Fred Drake8cef4cf2000-06-28 16:40:38 +00003774 if (pid == -1)
3775 return posix_error();
Benjamin Peterson0df35a92009-10-04 20:32:25 +00003776 if (result < 0) {
3777 /* Don't clobber the OSError if the fork failed. */
3778 PyErr_SetString(PyExc_RuntimeError,
3779 "not holding the import lock");
3780 return NULL;
3781 }
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00003782 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003783}
3784#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003785
Guido van Rossumad0ee831995-03-01 10:34:45 +00003786#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003790
Barry Warsaw53699e91996-12-10 23:23:01 +00003791static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003792posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003793{
Christian Heimes217cfd12007-12-02 14:31:20 +00003794 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003795}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003796#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003797
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003798
Guido van Rossumad0ee831995-03-01 10:34:45 +00003799#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003800PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003801"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003802Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003803
Barry Warsaw53699e91996-12-10 23:23:01 +00003804static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003805posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003806{
Christian Heimes217cfd12007-12-02 14:31:20 +00003807 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003808}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003809#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003810
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003811
Guido van Rossumad0ee831995-03-01 10:34:45 +00003812#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003813PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003814"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003815Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003816
Barry Warsaw53699e91996-12-10 23:23:01 +00003817static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003818posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003819{
Christian Heimes217cfd12007-12-02 14:31:20 +00003820 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003821}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003822#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003825PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003826"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003827Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003828
Barry Warsaw53699e91996-12-10 23:23:01 +00003829static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003830posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003831{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003832 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003833}
3834
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003835
Fred Drakec9680921999-12-13 16:37:25 +00003836#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003837PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003838"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003839Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003840
3841static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003842posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003843{
3844 PyObject *result = NULL;
3845
Fred Drakec9680921999-12-13 16:37:25 +00003846#ifdef NGROUPS_MAX
3847#define MAX_GROUPS NGROUPS_MAX
3848#else
3849 /* defined to be 16 on Solaris7, so this should be a small number */
3850#define MAX_GROUPS 64
3851#endif
3852 gid_t grouplist[MAX_GROUPS];
3853 int n;
3854
3855 n = getgroups(MAX_GROUPS, grouplist);
3856 if (n < 0)
3857 posix_error();
3858 else {
3859 result = PyList_New(n);
3860 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003861 int i;
3862 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003863 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003864 if (o == NULL) {
3865 Py_DECREF(result);
3866 result = NULL;
3867 break;
3868 }
3869 PyList_SET_ITEM(result, i, o);
3870 }
3871 }
3872 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003873
Fred Drakec9680921999-12-13 16:37:25 +00003874 return result;
3875}
3876#endif
3877
Antoine Pitroub7572f02009-12-02 20:46:48 +00003878#ifdef HAVE_INITGROUPS
3879PyDoc_STRVAR(posix_initgroups__doc__,
3880"initgroups(username, gid) -> None\n\n\
3881Call the system initgroups() to initialize the group access list with all of\n\
3882the groups of which the specified username is a member, plus the specified\n\
3883group id.");
3884
3885static PyObject *
3886posix_initgroups(PyObject *self, PyObject *args)
3887{
3888 char *username;
3889 long gid;
3890
3891 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
3892 return NULL;
3893
3894 if (initgroups(username, (gid_t) gid) == -1)
3895 return PyErr_SetFromErrno(PyExc_OSError);
3896
3897 Py_INCREF(Py_None);
3898 return Py_None;
3899}
3900#endif
3901
Martin v. Löwis606edc12002-06-13 21:09:11 +00003902#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003903PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003904"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003905Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003906
3907static PyObject *
3908posix_getpgid(PyObject *self, PyObject *args)
3909{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003910 pid_t pid, pgid;
Gregory P. Smith84508572010-03-14 18:56:11 +00003911 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
Martin v. Löwis606edc12002-06-13 21:09:11 +00003912 return NULL;
3913 pgid = getpgid(pid);
3914 if (pgid < 0)
3915 return posix_error();
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003916 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003917}
3918#endif /* HAVE_GETPGID */
3919
3920
Guido van Rossumb6775db1994-08-01 11:34:53 +00003921#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003922PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003923"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003924Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003925
Barry Warsaw53699e91996-12-10 23:23:01 +00003926static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003927posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003928{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003929#ifdef GETPGRP_HAVE_ARG
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003930 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003931#else /* GETPGRP_HAVE_ARG */
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003932 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003933#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003934}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003935#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003936
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003937
Guido van Rossumb6775db1994-08-01 11:34:53 +00003938#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003939PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003940"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003941Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003942
Barry Warsaw53699e91996-12-10 23:23:01 +00003943static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003944posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003945{
Guido van Rossum64933891994-10-20 21:56:42 +00003946#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003947 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003948#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003949 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003950#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003951 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003952 Py_INCREF(Py_None);
3953 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003954}
3955
Guido van Rossumb6775db1994-08-01 11:34:53 +00003956#endif /* HAVE_SETPGRP */
3957
Guido van Rossumad0ee831995-03-01 10:34:45 +00003958#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003959PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003960"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003961Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003962
Barry Warsaw53699e91996-12-10 23:23:01 +00003963static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003964posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003965{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00003966 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003967}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003968#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003969
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003970
Fred Drake12c6e2d1999-12-14 21:25:03 +00003971#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003972PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003973"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003974Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003975
3976static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003977posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003978{
Neal Norwitze241ce82003-02-17 18:17:05 +00003979 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003980 char *name;
3981 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003982
Fred Drakea30680b2000-12-06 21:24:28 +00003983 errno = 0;
3984 name = getlogin();
3985 if (name == NULL) {
3986 if (errno)
3987 posix_error();
3988 else
3989 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003990 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003991 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003992 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003993 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003994 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003995
Fred Drake12c6e2d1999-12-14 21:25:03 +00003996 return result;
3997}
3998#endif
3999
Guido van Rossumad0ee831995-03-01 10:34:45 +00004000#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004001PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004002"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004003Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004004
Barry Warsaw53699e91996-12-10 23:23:01 +00004005static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004006posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004007{
Christian Heimes217cfd12007-12-02 14:31:20 +00004008 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004009}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004010#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004012
Guido van Rossumad0ee831995-03-01 10:34:45 +00004013#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004014PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004015"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004016Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004017
Barry Warsaw53699e91996-12-10 23:23:01 +00004018static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004019posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004020{
Christian Heimes292d3512008-02-03 16:51:08 +00004021 pid_t pid;
4022 int sig;
Gregory P. Smith84508572010-03-14 18:56:11 +00004023 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00004024 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004025#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004026 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4027 APIRET rc;
4028 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004029 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004030
4031 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4032 APIRET rc;
4033 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004034 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004035
4036 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004037 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004038#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004039 if (kill(pid, sig) == -1)
4040 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004041#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004042 Py_INCREF(Py_None);
4043 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004044}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004045#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004046
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004047#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004048PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004049"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004050Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004051
4052static PyObject *
4053posix_killpg(PyObject *self, PyObject *args)
4054{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004055 int sig;
4056 pid_t pgid;
4057 /* XXX some man pages make the `pgid` parameter an int, others
4058 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4059 take the same type. Moreover, pid_t is always at least as wide as
4060 int (else compilation of this module fails), which is safe. */
Gregory P. Smith84508572010-03-14 18:56:11 +00004061 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004062 return NULL;
4063 if (killpg(pgid, sig) == -1)
4064 return posix_error();
4065 Py_INCREF(Py_None);
4066 return Py_None;
4067}
4068#endif
4069
Brian Curtineb24d742010-04-12 17:16:38 +00004070#ifdef MS_WINDOWS
4071PyDoc_STRVAR(win32_kill__doc__,
4072"kill(pid, sig)\n\n\
4073Kill a process with a signal.");
4074
4075static PyObject *
4076win32_kill(PyObject *self, PyObject *args)
4077{
4078 PyObject *result, handle_obj;
4079 DWORD pid, sig, err;
4080 HANDLE handle;
4081
4082 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4083 return NULL;
4084
4085 /* Console processes which share a common console can be sent CTRL+C or
4086 CTRL+BREAK events, provided they handle said events. */
4087 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4088 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4089 err = GetLastError();
4090 PyErr_SetFromWindowsErr(err);
4091 }
4092 else
4093 Py_RETURN_NONE;
4094 }
4095
4096 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4097 attempt to open and terminate the process. */
4098 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4099 if (handle == NULL) {
4100 err = GetLastError();
4101 return PyErr_SetFromWindowsErr(err);
4102 }
4103
4104 if (TerminateProcess(handle, sig) == 0) {
4105 err = GetLastError();
4106 result = PyErr_SetFromWindowsErr(err);
4107 } else {
4108 Py_INCREF(Py_None);
4109 result = Py_None;
4110 }
4111
4112 CloseHandle(handle);
4113 return result;
4114}
4115#endif /* MS_WINDOWS */
4116
Guido van Rossumc0125471996-06-28 18:55:32 +00004117#ifdef HAVE_PLOCK
4118
4119#ifdef HAVE_SYS_LOCK_H
4120#include <sys/lock.h>
4121#endif
4122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004123PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004124"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004125Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004126
Barry Warsaw53699e91996-12-10 23:23:01 +00004127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004128posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004129{
4130 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004131 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004132 return NULL;
4133 if (plock(op) == -1)
4134 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004135 Py_INCREF(Py_None);
4136 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004137}
4138#endif
4139
Guido van Rossumb6775db1994-08-01 11:34:53 +00004140#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004141PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004142"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004143Set the current process's user id.");
4144
Barry Warsaw53699e91996-12-10 23:23:01 +00004145static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004146posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004147{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004148 long uid_arg;
4149 uid_t uid;
4150 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004151 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004152 uid = uid_arg;
4153 if (uid != uid_arg) {
4154 PyErr_SetString(PyExc_OverflowError, "user id too big");
4155 return NULL;
4156 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004157 if (setuid(uid) < 0)
4158 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004159 Py_INCREF(Py_None);
4160 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004161}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004162#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004163
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004164
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004165#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004166PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004167"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004168Set the current process's effective user id.");
4169
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004170static PyObject *
4171posix_seteuid (PyObject *self, PyObject *args)
4172{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004173 long euid_arg;
4174 uid_t euid;
4175 if (!PyArg_ParseTuple(args, "l", &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004176 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004177 euid = euid_arg;
4178 if (euid != euid_arg) {
4179 PyErr_SetString(PyExc_OverflowError, "user id too big");
4180 return NULL;
4181 }
4182 if (seteuid(euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004183 return posix_error();
4184 } else {
4185 Py_INCREF(Py_None);
4186 return Py_None;
4187 }
4188}
4189#endif /* HAVE_SETEUID */
4190
4191#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004192PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004193"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004194Set the current process's effective group id.");
4195
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004196static PyObject *
4197posix_setegid (PyObject *self, PyObject *args)
4198{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004199 long egid_arg;
4200 gid_t egid;
4201 if (!PyArg_ParseTuple(args, "l", &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004202 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004203 egid = egid_arg;
4204 if (egid != egid_arg) {
4205 PyErr_SetString(PyExc_OverflowError, "group id too big");
4206 return NULL;
4207 }
4208 if (setegid(egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004209 return posix_error();
4210 } else {
4211 Py_INCREF(Py_None);
4212 return Py_None;
4213 }
4214}
4215#endif /* HAVE_SETEGID */
4216
4217#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004218PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004219"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004220Set the current process's real and effective user ids.");
4221
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004222static PyObject *
4223posix_setreuid (PyObject *self, PyObject *args)
4224{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004225 long ruid_arg, euid_arg;
4226 uid_t ruid, euid;
4227 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004228 return NULL;
Gregory P. Smithc78d79c2010-03-01 05:54:14 +00004229 if (ruid_arg == -1)
4230 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4231 else
4232 ruid = ruid_arg; /* otherwise, assign from our long */
4233 if (euid_arg == -1)
4234 euid = (uid_t)-1;
4235 else
4236 euid = euid_arg;
4237 if ((euid_arg != -1 && euid != euid_arg) ||
4238 (ruid_arg != -1 && ruid != ruid_arg)) {
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004239 PyErr_SetString(PyExc_OverflowError, "user id too big");
4240 return NULL;
4241 }
4242 if (setreuid(ruid, euid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004243 return posix_error();
4244 } else {
4245 Py_INCREF(Py_None);
4246 return Py_None;
4247 }
4248}
4249#endif /* HAVE_SETREUID */
4250
4251#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004252PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004253"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004254Set the current process's real and effective group ids.");
4255
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004256static PyObject *
4257posix_setregid (PyObject *self, PyObject *args)
4258{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004259 long rgid_arg, egid_arg;
4260 gid_t rgid, egid;
4261 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004262 return NULL;
Gregory P. Smithc78d79c2010-03-01 05:54:14 +00004263 if (rgid_arg == -1)
4264 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4265 else
4266 rgid = rgid_arg; /* otherwise, assign from our long */
4267 if (egid_arg == -1)
4268 egid = (gid_t)-1;
4269 else
4270 egid = egid_arg;
4271 if ((egid_arg != -1 && egid != egid_arg) ||
4272 (rgid_arg != -1 && rgid != rgid_arg)) {
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004273 PyErr_SetString(PyExc_OverflowError, "group id too big");
4274 return NULL;
4275 }
4276 if (setregid(rgid, egid) < 0) {
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004277 return posix_error();
4278 } else {
4279 Py_INCREF(Py_None);
4280 return Py_None;
4281 }
4282}
4283#endif /* HAVE_SETREGID */
4284
Guido van Rossumb6775db1994-08-01 11:34:53 +00004285#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004286PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004287"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004288Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004289
Barry Warsaw53699e91996-12-10 23:23:01 +00004290static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004291posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004292{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004293 long gid_arg;
4294 gid_t gid;
4295 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004296 return NULL;
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00004297 gid = gid_arg;
4298 if (gid != gid_arg) {
4299 PyErr_SetString(PyExc_OverflowError, "group id too big");
4300 return NULL;
4301 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004302 if (setgid(gid) < 0)
4303 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004304 Py_INCREF(Py_None);
4305 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004306}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004307#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004308
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004309#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004310PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004311"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004312Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004313
4314static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004315posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004316{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004317 int i, len;
4318 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004319
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004320 if (!PySequence_Check(groups)) {
4321 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4322 return NULL;
4323 }
4324 len = PySequence_Size(groups);
4325 if (len > MAX_GROUPS) {
4326 PyErr_SetString(PyExc_ValueError, "too many groups");
4327 return NULL;
4328 }
4329 for(i = 0; i < len; i++) {
4330 PyObject *elem;
4331 elem = PySequence_GetItem(groups, i);
4332 if (!elem)
4333 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004334 if (!PyLong_Check(elem)) {
4335 PyErr_SetString(PyExc_TypeError,
4336 "groups must be integers");
4337 Py_DECREF(elem);
4338 return NULL;
4339 } else {
4340 unsigned long x = PyLong_AsUnsignedLong(elem);
4341 if (PyErr_Occurred()) {
4342 PyErr_SetString(PyExc_TypeError,
4343 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004344 Py_DECREF(elem);
4345 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004346 }
Georg Brandla13c2442005-11-22 19:30:31 +00004347 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004348 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004349 if (grouplist[i] != x) {
4350 PyErr_SetString(PyExc_TypeError,
4351 "group id too big");
4352 Py_DECREF(elem);
4353 return NULL;
4354 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004355 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004356 Py_DECREF(elem);
4357 }
4358
4359 if (setgroups(len, grouplist) < 0)
4360 return posix_error();
4361 Py_INCREF(Py_None);
4362 return Py_None;
4363}
4364#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004365
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004366#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4367static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004368wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004369{
4370 PyObject *result;
4371 static PyObject *struct_rusage;
4372
4373 if (pid == -1)
4374 return posix_error();
4375
4376 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004377 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004378 if (m == NULL)
4379 return NULL;
4380 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4381 Py_DECREF(m);
4382 if (struct_rusage == NULL)
4383 return NULL;
4384 }
4385
4386 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4387 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4388 if (!result)
4389 return NULL;
4390
4391#ifndef doubletime
4392#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4393#endif
4394
4395 PyStructSequence_SET_ITEM(result, 0,
4396 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4397 PyStructSequence_SET_ITEM(result, 1,
4398 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4399#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004400 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004401 SET_INT(result, 2, ru->ru_maxrss);
4402 SET_INT(result, 3, ru->ru_ixrss);
4403 SET_INT(result, 4, ru->ru_idrss);
4404 SET_INT(result, 5, ru->ru_isrss);
4405 SET_INT(result, 6, ru->ru_minflt);
4406 SET_INT(result, 7, ru->ru_majflt);
4407 SET_INT(result, 8, ru->ru_nswap);
4408 SET_INT(result, 9, ru->ru_inblock);
4409 SET_INT(result, 10, ru->ru_oublock);
4410 SET_INT(result, 11, ru->ru_msgsnd);
4411 SET_INT(result, 12, ru->ru_msgrcv);
4412 SET_INT(result, 13, ru->ru_nsignals);
4413 SET_INT(result, 14, ru->ru_nvcsw);
4414 SET_INT(result, 15, ru->ru_nivcsw);
4415#undef SET_INT
4416
4417 if (PyErr_Occurred()) {
4418 Py_DECREF(result);
4419 return NULL;
4420 }
4421
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004422 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004423}
4424#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4425
4426#ifdef HAVE_WAIT3
4427PyDoc_STRVAR(posix_wait3__doc__,
4428"wait3(options) -> (pid, status, rusage)\n\n\
4429Wait for completion of a child process.");
4430
4431static PyObject *
4432posix_wait3(PyObject *self, PyObject *args)
4433{
Christian Heimes292d3512008-02-03 16:51:08 +00004434 pid_t pid;
4435 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004436 struct rusage ru;
4437 WAIT_TYPE status;
4438 WAIT_STATUS_INT(status) = 0;
4439
4440 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4441 return NULL;
4442
4443 Py_BEGIN_ALLOW_THREADS
4444 pid = wait3(&status, options, &ru);
4445 Py_END_ALLOW_THREADS
4446
4447 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4448}
4449#endif /* HAVE_WAIT3 */
4450
4451#ifdef HAVE_WAIT4
4452PyDoc_STRVAR(posix_wait4__doc__,
4453"wait4(pid, options) -> (pid, status, rusage)\n\n\
4454Wait for completion of a given child process.");
4455
4456static PyObject *
4457posix_wait4(PyObject *self, PyObject *args)
4458{
Christian Heimes292d3512008-02-03 16:51:08 +00004459 pid_t pid;
4460 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004461 struct rusage ru;
4462 WAIT_TYPE status;
4463 WAIT_STATUS_INT(status) = 0;
4464
Gregory P. Smith84508572010-03-14 18:56:11 +00004465 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004466 return NULL;
4467
4468 Py_BEGIN_ALLOW_THREADS
4469 pid = wait4(pid, &status, options, &ru);
4470 Py_END_ALLOW_THREADS
4471
4472 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4473}
4474#endif /* HAVE_WAIT4 */
4475
Guido van Rossumb6775db1994-08-01 11:34:53 +00004476#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004477PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004478"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004479Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004480
Barry Warsaw53699e91996-12-10 23:23:01 +00004481static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004482posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004483{
Christian Heimes292d3512008-02-03 16:51:08 +00004484 pid_t pid;
4485 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004486 WAIT_TYPE status;
4487 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004488
Gregory P. Smith84508572010-03-14 18:56:11 +00004489 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004490 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004491 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004492 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004493 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004494 if (pid == -1)
4495 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004496
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004497 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004498}
4499
Tim Petersab034fa2002-02-01 11:27:43 +00004500#elif defined(HAVE_CWAIT)
4501
4502/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004503PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004504"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004505"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004506
4507static PyObject *
4508posix_waitpid(PyObject *self, PyObject *args)
4509{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004510 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004511 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004512
Gregory P. Smith84508572010-03-14 18:56:11 +00004513 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
Tim Petersab034fa2002-02-01 11:27:43 +00004514 return NULL;
4515 Py_BEGIN_ALLOW_THREADS
4516 pid = _cwait(&status, pid, options);
4517 Py_END_ALLOW_THREADS
4518 if (pid == -1)
4519 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004520
4521 /* shift the status left a byte so this is more like the POSIX waitpid */
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004522 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004523}
4524#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004525
Guido van Rossumad0ee831995-03-01 10:34:45 +00004526#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004527PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004528"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004529Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004530
Barry Warsaw53699e91996-12-10 23:23:01 +00004531static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004532posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004533{
Christian Heimes292d3512008-02-03 16:51:08 +00004534 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004535 WAIT_TYPE status;
4536 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004537
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004538 Py_BEGIN_ALLOW_THREADS
4539 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004540 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004541 if (pid == -1)
4542 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004543
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004544 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004545}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004546#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004547
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004549PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004550"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004551Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004552
Barry Warsaw53699e91996-12-10 23:23:01 +00004553static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004554posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004555{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004556#ifdef HAVE_LSTAT
Martin v. Löwis011e8422009-05-05 04:43:17 +00004557 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004558#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004559#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00004560 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004561#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00004562 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004563#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004564#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004565}
4566
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004567
Guido van Rossumb6775db1994-08-01 11:34:53 +00004568#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004569PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004570"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004571Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004572
Barry Warsaw53699e91996-12-10 23:23:01 +00004573static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004574posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004575{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004576 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004577 char buf[MAXPATHLEN];
Martin v. Löwis011e8422009-05-05 04:43:17 +00004578 PyObject *opath;
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004579 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004580 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004581 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004582
Martin v. Löwis011e8422009-05-05 04:43:17 +00004583 if (!PyArg_ParseTuple(args, "O&:readlink",
4584 PyUnicode_FSConverter, &opath))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004585 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00004586 path = PyBytes_AsString(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004587 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004588 if (v == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00004589 Py_DECREF(opath);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004590 return NULL;
4591 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004592
4593 if (PyUnicode_Check(v)) {
4594 arg_is_unicode = 1;
4595 }
4596 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004597
Barry Warsaw53699e91996-12-10 23:23:01 +00004598 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004599 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004600 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004601 if (n < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004602 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004603
Victor Stinnerdcb24032010-04-22 12:08:36 +00004604 Py_DECREF(opath);
Christian Heimes72b710a2008-05-26 13:28:38 +00004605 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004606 if (arg_is_unicode) {
4607 PyObject *w;
4608
4609 w = PyUnicode_FromEncodedObject(v,
4610 Py_FileSystemDefaultEncoding,
Martin v. Löwis43c57782009-05-10 08:15:24 +00004611 "surrogateescape");
Thomas Wouters89f507f2006-12-13 04:49:30 +00004612 if (w != NULL) {
4613 Py_DECREF(v);
4614 v = w;
4615 }
4616 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004617 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004618 }
4619 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004620 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004621}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004622#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Guido van Rossumb6775db1994-08-01 11:34:53 +00004625#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004626PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004627"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004628Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004629
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004630static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004631posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004632{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004633 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004634}
4635#endif /* HAVE_SYMLINK */
4636
4637
4638#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004639#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4640static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004641system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004642{
4643 ULONG value = 0;
4644
4645 Py_BEGIN_ALLOW_THREADS
4646 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4647 Py_END_ALLOW_THREADS
4648
4649 return value;
4650}
4651
4652static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004653posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004654{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004655 /* Currently Only Uptime is Provided -- Others Later */
4656 return Py_BuildValue("ddddd",
4657 (double)0 /* t.tms_utime / HZ */,
4658 (double)0 /* t.tms_stime / HZ */,
4659 (double)0 /* t.tms_cutime / HZ */,
4660 (double)0 /* t.tms_cstime / HZ */,
4661 (double)system_uptime() / 1000);
4662}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004663#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004664#define NEED_TICKS_PER_SECOND
4665static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004666static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004667posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004668{
4669 struct tms t;
4670 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004671 errno = 0;
4672 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004673 if (c == (clock_t) -1)
4674 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004675 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004676 (double)t.tms_utime / ticks_per_second,
4677 (double)t.tms_stime / ticks_per_second,
4678 (double)t.tms_cutime / ticks_per_second,
4679 (double)t.tms_cstime / ticks_per_second,
4680 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004681}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004682#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004683#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004684
4685
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004686#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004687#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004688static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004689posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004690{
4691 FILETIME create, exit, kernel, user;
4692 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004693 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004694 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4695 /* The fields of a FILETIME structure are the hi and lo part
4696 of a 64-bit value expressed in 100 nanosecond units.
4697 1e7 is one second in such units; 1e-7 the inverse.
4698 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4699 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004700 return Py_BuildValue(
4701 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004702 (double)(user.dwHighDateTime*429.4967296 +
4703 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004704 (double)(kernel.dwHighDateTime*429.4967296 +
4705 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004706 (double)0,
4707 (double)0,
4708 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004709}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004710#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004711
4712#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004713PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004714"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004715Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004716#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004717
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004718
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004719#ifdef HAVE_GETSID
4720PyDoc_STRVAR(posix_getsid__doc__,
4721"getsid(pid) -> sid\n\n\
4722Call the system call getsid().");
4723
4724static PyObject *
4725posix_getsid(PyObject *self, PyObject *args)
4726{
Christian Heimes292d3512008-02-03 16:51:08 +00004727 pid_t pid;
4728 int sid;
Gregory P. Smith84508572010-03-14 18:56:11 +00004729 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004730 return NULL;
4731 sid = getsid(pid);
4732 if (sid < 0)
4733 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004734 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004735}
4736#endif /* HAVE_GETSID */
4737
4738
Guido van Rossumb6775db1994-08-01 11:34:53 +00004739#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004740PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004741"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004742Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004743
Barry Warsaw53699e91996-12-10 23:23:01 +00004744static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004745posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004746{
Guido van Rossum687dd131993-05-17 08:34:16 +00004747 if (setsid() < 0)
4748 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004749 Py_INCREF(Py_None);
4750 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004751}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004752#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004753
Guido van Rossumb6775db1994-08-01 11:34:53 +00004754#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004755PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004756"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004757Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004758
Barry Warsaw53699e91996-12-10 23:23:01 +00004759static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004760posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004761{
Christian Heimes292d3512008-02-03 16:51:08 +00004762 pid_t pid;
4763 int pgrp;
Gregory P. Smith84508572010-03-14 18:56:11 +00004764 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004765 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004766 if (setpgid(pid, pgrp) < 0)
4767 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004768 Py_INCREF(Py_None);
4769 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004770}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004771#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004772
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004773
Guido van Rossumb6775db1994-08-01 11:34:53 +00004774#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004775PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004776"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004777Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004778
Barry Warsaw53699e91996-12-10 23:23:01 +00004779static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004780posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004781{
Christian Heimes15ebc882008-02-04 18:48:49 +00004782 int fd;
4783 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004784 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004785 return NULL;
4786 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004787 if (pgid < 0)
4788 return posix_error();
Antoine Pitrouc3ee1662009-05-23 16:02:33 +00004789 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004790}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004791#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004792
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004793
Guido van Rossumb6775db1994-08-01 11:34:53 +00004794#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004795PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004796"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004797Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004798
Barry Warsaw53699e91996-12-10 23:23:01 +00004799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004800posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004801{
Antoine Pitrou971e51b2009-05-23 16:14:27 +00004802 int fd;
4803 pid_t pgid;
Gregory P. Smith84508572010-03-14 18:56:11 +00004804 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004805 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004806 if (tcsetpgrp(fd, pgid) < 0)
4807 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004808 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004809 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004810}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004811#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004812
Guido van Rossum687dd131993-05-17 08:34:16 +00004813/* Functions acting on file descriptors */
4814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004815PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004816"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004817Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004818
Barry Warsaw53699e91996-12-10 23:23:01 +00004819static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004820posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004821{
Martin v. Löwis011e8422009-05-05 04:43:17 +00004822 PyObject *ofile;
4823 char *file;
Guido van Rossum687dd131993-05-17 08:34:16 +00004824 int flag;
4825 int mode = 0777;
4826 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004827
4828#ifdef MS_WINDOWS
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004829 PyUnicodeObject *po;
4830 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4831 Py_BEGIN_ALLOW_THREADS
4832 /* PyUnicode_AS_UNICODE OK without thread
4833 lock as it is a simple dereference. */
4834 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4835 Py_END_ALLOW_THREADS
4836 if (fd < 0)
4837 return posix_error();
4838 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004839 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004840 /* Drop the argument parsing error as narrow strings
4841 are also valid. */
4842 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004843#endif
4844
Martin v. Löwis011e8422009-05-05 04:43:17 +00004845 if (!PyArg_ParseTuple(args, "O&i|i",
4846 PyUnicode_FSConverter, &ofile,
Mark Hammondef8b6542001-05-13 08:04:26 +00004847 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004848 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00004849 file = PyBytes_AsString(ofile);
Barry Warsaw53699e91996-12-10 23:23:01 +00004850 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004851 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004852 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004853 if (fd < 0)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004854 return posix_error_with_allocated_filename(ofile);
Victor Stinnerdcb24032010-04-22 12:08:36 +00004855 Py_DECREF(ofile);
Christian Heimes217cfd12007-12-02 14:31:20 +00004856 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004857}
4858
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004859
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004860PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004861"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004862Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004863
Barry Warsaw53699e91996-12-10 23:23:01 +00004864static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004865posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004866{
4867 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004868 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004869 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004870 if (!_PyVerify_fd(fd))
4871 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004872 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004873 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004874 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004875 if (res < 0)
4876 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004877 Py_INCREF(Py_None);
4878 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004879}
4880
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004881
Christian Heimesfdab48e2008-01-20 09:06:41 +00004882PyDoc_STRVAR(posix_closerange__doc__,
4883"closerange(fd_low, fd_high)\n\n\
4884Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4885
4886static PyObject *
4887posix_closerange(PyObject *self, PyObject *args)
4888{
4889 int fd_from, fd_to, i;
4890 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4891 return NULL;
4892 Py_BEGIN_ALLOW_THREADS
4893 for (i = fd_from; i < fd_to; i++)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004894 if (_PyVerify_fd(i))
4895 close(i);
Christian Heimesfdab48e2008-01-20 09:06:41 +00004896 Py_END_ALLOW_THREADS
4897 Py_RETURN_NONE;
4898}
4899
4900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004901PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004902"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004903Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004904
Barry Warsaw53699e91996-12-10 23:23:01 +00004905static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004906posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004907{
4908 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004909 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004910 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004911 if (!_PyVerify_fd(fd))
4912 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004913 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004914 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004915 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004916 if (fd < 0)
4917 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004918 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004919}
4920
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004921
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004922PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004923"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004924Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004925
Barry Warsaw53699e91996-12-10 23:23:01 +00004926static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004927posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004928{
4929 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004930 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004931 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004932 if (!_PyVerify_fd_dup2(fd, fd2))
4933 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004934 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004935 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004936 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004937 if (res < 0)
4938 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004939 Py_INCREF(Py_None);
4940 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004941}
4942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004943
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004944PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004945"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004946Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004947
Barry Warsaw53699e91996-12-10 23:23:01 +00004948static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004949posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004950{
4951 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004952#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004953 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004954#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004955 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004956#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004957 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004958 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004959 return NULL;
4960#ifdef SEEK_SET
4961 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4962 switch (how) {
4963 case 0: how = SEEK_SET; break;
4964 case 1: how = SEEK_CUR; break;
4965 case 2: how = SEEK_END; break;
4966 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004967#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004968
4969#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004970 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004971#else
4972 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00004973 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004974#endif
4975 if (PyErr_Occurred())
4976 return NULL;
4977
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00004978 if (!_PyVerify_fd(fd))
4979 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004980 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004981#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004982 res = _lseeki64(fd, pos, how);
4983#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004984 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004985#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004986 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004987 if (res < 0)
4988 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004989
4990#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004991 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004992#else
4993 return PyLong_FromLongLong(res);
4994#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004995}
4996
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004998PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004999"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005000Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005001
Barry Warsaw53699e91996-12-10 23:23:01 +00005002static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005003posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005004{
Guido van Rossum572dbf82007-04-27 23:53:51 +00005005 int fd, size;
5006 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005007 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005008 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005009 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005010 if (size < 0) {
5011 errno = EINVAL;
5012 return posix_error();
5013 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005014 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005015 if (buffer == NULL)
5016 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005017 if (!_PyVerify_fd(fd))
5018 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005019 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00005020 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005021 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005022 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005023 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005024 return posix_error();
5025 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005026 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00005027 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005028 return buffer;
5029}
5030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005032PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005033"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005034Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005035
Barry Warsaw53699e91996-12-10 23:23:01 +00005036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005037posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005038{
Martin v. Löwis423be952008-08-13 15:53:07 +00005039 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005040 int fd;
5041 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005042
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00005043 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00005044 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005045 if (!_PyVerify_fd(fd))
5046 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005047 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005048 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00005049 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00005050 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00005051 if (size < 0)
5052 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00005053 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005054}
5055
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005057PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005058"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005059Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005060
Barry Warsaw53699e91996-12-10 23:23:01 +00005061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005062posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005063{
5064 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005065 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005066 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005067 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005068 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005069#ifdef __VMS
5070 /* on OpenVMS we must ensure that all bytes are written to the file */
5071 fsync(fd);
5072#endif
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005073 if (!_PyVerify_fd(fd))
5074 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005075 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005076 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005077 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005078 if (res != 0) {
5079#ifdef MS_WINDOWS
5080 return win32_error("fstat", NULL);
5081#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005082 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005083#endif
5084 }
Tim Peters5aa91602002-01-30 05:46:57 +00005085
Martin v. Löwis14694662006-02-03 12:54:16 +00005086 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005087}
5088
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005089PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005090"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005091Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005092connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005093
5094static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005095posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005096{
5097 int fd;
5098 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5099 return NULL;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00005100 if (!_PyVerify_fd(fd))
5101 return PyBool_FromLong(0);
Fred Drake106c1a02002-04-23 15:58:02 +00005102 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005103}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005104
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005105#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005106PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005107"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005108Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005109
Barry Warsaw53699e91996-12-10 23:23:01 +00005110static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005111posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005112{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005113#if defined(PYOS_OS2)
5114 HFILE read, write;
5115 APIRET rc;
5116
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005117 Py_BEGIN_ALLOW_THREADS
5118 rc = DosCreatePipe( &read, &write, 4096);
5119 Py_END_ALLOW_THREADS
5120 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005121 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005122
5123 return Py_BuildValue("(ii)", read, write);
5124#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005125#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005126 int fds[2];
5127 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005128 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005129 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005130 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005131 if (res != 0)
5132 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005133 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005134#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005135 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005136 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005137 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005138 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005139 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005140 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005141 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005142 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005143 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5144 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005145 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005146#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005147#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005148}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005149#endif /* HAVE_PIPE */
5150
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005151
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005152#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005153PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005154"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005155Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005156
Barry Warsaw53699e91996-12-10 23:23:01 +00005157static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005158posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005159{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005160 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005161 int mode = 0666;
5162 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005163 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005164 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005165 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005166 res = mkfifo(filename, mode);
5167 Py_END_ALLOW_THREADS
5168 if (res < 0)
5169 return posix_error();
5170 Py_INCREF(Py_None);
5171 return Py_None;
5172}
5173#endif
5174
5175
Neal Norwitz11690112002-07-30 01:08:28 +00005176#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005177PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005178"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005179Create a filesystem node (file, device special file or named pipe)\n\
5180named filename. mode specifies both the permissions to use and the\n\
5181type of node to be created, being combined (bitwise OR) with one of\n\
5182S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005183device defines the newly created device special file (probably using\n\
5184os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005185
5186
5187static PyObject *
5188posix_mknod(PyObject *self, PyObject *args)
5189{
5190 char *filename;
5191 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005192 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005193 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005194 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005195 return NULL;
5196 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005197 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005198 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005199 if (res < 0)
5200 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005201 Py_INCREF(Py_None);
5202 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005203}
5204#endif
5205
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005206#ifdef HAVE_DEVICE_MACROS
5207PyDoc_STRVAR(posix_major__doc__,
5208"major(device) -> major number\n\
5209Extracts a device major number from a raw device number.");
5210
5211static PyObject *
5212posix_major(PyObject *self, PyObject *args)
5213{
5214 int device;
5215 if (!PyArg_ParseTuple(args, "i:major", &device))
5216 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005217 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005218}
5219
5220PyDoc_STRVAR(posix_minor__doc__,
5221"minor(device) -> minor number\n\
5222Extracts a device minor number from a raw device number.");
5223
5224static PyObject *
5225posix_minor(PyObject *self, PyObject *args)
5226{
5227 int device;
5228 if (!PyArg_ParseTuple(args, "i:minor", &device))
5229 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005230 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005231}
5232
5233PyDoc_STRVAR(posix_makedev__doc__,
5234"makedev(major, minor) -> device number\n\
5235Composes a raw device number from the major and minor device numbers.");
5236
5237static PyObject *
5238posix_makedev(PyObject *self, PyObject *args)
5239{
5240 int major, minor;
5241 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5242 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005243 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005244}
5245#endif /* device macros */
5246
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005247
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005248#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005249PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005250"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005251Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005252
Barry Warsaw53699e91996-12-10 23:23:01 +00005253static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005254posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005255{
5256 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005257 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005258 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005259 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005260
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005261 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005262 return NULL;
5263
5264#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005265 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005266#else
5267 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005268 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005269#endif
5270 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005271 return NULL;
5272
Barry Warsaw53699e91996-12-10 23:23:01 +00005273 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005274 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005275 Py_END_ALLOW_THREADS
Benjamin Peterson9053d752009-01-19 17:53:36 +00005276 if (res < 0)
5277 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005278 Py_INCREF(Py_None);
5279 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005280}
5281#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005282
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005283#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005284PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005285"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005286Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005287
Fred Drake762e2061999-08-26 17:23:54 +00005288/* Save putenv() parameters as values here, so we can collect them when they
5289 * get re-set with another call for the same key. */
5290static PyObject *posix_putenv_garbage;
5291
Tim Peters5aa91602002-01-30 05:46:57 +00005292static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005293posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005294{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005295#ifdef MS_WINDOWS
5296 wchar_t *s1, *s2;
5297 wchar_t *newenv;
5298#else
Martin v. Löwis011e8422009-05-05 04:43:17 +00005299 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005300 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005301 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005302#endif
Fred Drake762e2061999-08-26 17:23:54 +00005303 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005304 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005305
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005306#ifdef MS_WINDOWS
Martin v. Löwis011e8422009-05-05 04:43:17 +00005307 if (!PyArg_ParseTuple(args,
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005308 "uu:putenv",
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005309 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005310 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005311#else
5312 if (!PyArg_ParseTuple(args,
5313 "O&O&:putenv",
5314 PyUnicode_FSConverter, &os1,
5315 PyUnicode_FSConverter, &os2))
5316 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00005317 s1 = PyBytes_AsString(os1);
5318 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005319#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005320
5321#if defined(PYOS_OS2)
5322 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5323 APIRET rc;
5324
Guido van Rossumd48f2521997-12-05 22:19:34 +00005325 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5326 if (rc != NO_ERROR)
5327 return os2_error(rc);
5328
5329 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5330 APIRET rc;
5331
Guido van Rossumd48f2521997-12-05 22:19:34 +00005332 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5333 if (rc != NO_ERROR)
5334 return os2_error(rc);
5335 } else {
5336#endif
Fred Drake762e2061999-08-26 17:23:54 +00005337 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005338 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005339 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005340#ifdef MS_WINDOWS
5341 len = wcslen(s1) + wcslen(s2) + 2;
5342 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5343#else
5344 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005345 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005346#endif
Fred Drake762e2061999-08-26 17:23:54 +00005347 if (newstr == NULL)
5348 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005349#ifdef MS_WINDOWS
5350 newenv = PyUnicode_AsUnicode(newstr);
5351 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5352 if (_wputenv(newenv)) {
5353 Py_DECREF(newstr);
5354 posix_error();
5355 return NULL;
5356 }
5357#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005358 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005359 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5360 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005361 Py_DECREF(newstr);
Victor Stinnerdcb24032010-04-22 12:08:36 +00005362 Py_DECREF(os1);
5363 Py_DECREF(os2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005364 posix_error();
5365 return NULL;
5366 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005367#endif
Fred Drake762e2061999-08-26 17:23:54 +00005368 /* Install the first arg and newstr in posix_putenv_garbage;
5369 * this will cause previous value to be collected. This has to
5370 * happen after the real putenv() call because the old value
5371 * was still accessible until then. */
5372 if (PyDict_SetItem(posix_putenv_garbage,
5373 PyTuple_GET_ITEM(args, 0), newstr)) {
5374 /* really not much we can do; just leak */
5375 PyErr_Clear();
5376 }
5377 else {
5378 Py_DECREF(newstr);
5379 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005380
5381#if defined(PYOS_OS2)
5382 }
5383#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005384#ifndef MS_WINDOWS
Victor Stinnerdcb24032010-04-22 12:08:36 +00005385 Py_DECREF(os1);
5386 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005387#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005388 Py_INCREF(Py_None);
5389 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005390}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005391#endif /* putenv */
5392
Guido van Rossumc524d952001-10-19 01:31:59 +00005393#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005394PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005395"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005396Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005397
5398static PyObject *
5399posix_unsetenv(PyObject *self, PyObject *args)
5400{
5401 char *s1;
5402
5403 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5404 return NULL;
5405
5406 unsetenv(s1);
5407
5408 /* Remove the key from posix_putenv_garbage;
5409 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005410 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005411 * old value was still accessible until then.
5412 */
5413 if (PyDict_DelItem(posix_putenv_garbage,
5414 PyTuple_GET_ITEM(args, 0))) {
5415 /* really not much we can do; just leak */
5416 PyErr_Clear();
5417 }
5418
5419 Py_INCREF(Py_None);
5420 return Py_None;
5421}
5422#endif /* unsetenv */
5423
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005424PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005425"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005426Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005427
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005428static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005429posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005430{
5431 int code;
5432 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005433 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005434 return NULL;
5435 message = strerror(code);
5436 if (message == NULL) {
5437 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005438 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005439 return NULL;
5440 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005441 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005442}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005443
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005444
Guido van Rossumc9641791998-08-04 15:26:23 +00005445#ifdef HAVE_SYS_WAIT_H
5446
Fred Drake106c1a02002-04-23 15:58:02 +00005447#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005448PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005449"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005450Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005451
5452static PyObject *
5453posix_WCOREDUMP(PyObject *self, PyObject *args)
5454{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005455 WAIT_TYPE status;
5456 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005457
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005458 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005459 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005460
5461 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005462}
5463#endif /* WCOREDUMP */
5464
5465#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005466PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005467"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005468Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005469job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005470
5471static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005472posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005473{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005474 WAIT_TYPE status;
5475 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005476
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005477 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005478 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005479
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005480 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005481}
5482#endif /* WIFCONTINUED */
5483
Guido van Rossumc9641791998-08-04 15:26:23 +00005484#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005486"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005487Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005488
5489static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005490posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005491{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005492 WAIT_TYPE status;
5493 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005494
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005495 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005496 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005497
Fred Drake106c1a02002-04-23 15:58:02 +00005498 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005499}
5500#endif /* WIFSTOPPED */
5501
5502#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005504"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005505Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005506
5507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005508posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005509{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005510 WAIT_TYPE status;
5511 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005512
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005513 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005514 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005515
Fred Drake106c1a02002-04-23 15:58:02 +00005516 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005517}
5518#endif /* WIFSIGNALED */
5519
5520#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005521PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005522"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005523Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005525
5526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005527posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005528{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005529 WAIT_TYPE status;
5530 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005531
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005532 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005533 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005534
Fred Drake106c1a02002-04-23 15:58:02 +00005535 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005536}
5537#endif /* WIFEXITED */
5538
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005539#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005540PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005541"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005542Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005543
5544static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005545posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005546{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005547 WAIT_TYPE status;
5548 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005549
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005550 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005551 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005552
Guido van Rossumc9641791998-08-04 15:26:23 +00005553 return Py_BuildValue("i", WEXITSTATUS(status));
5554}
5555#endif /* WEXITSTATUS */
5556
5557#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005559"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005560Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005561value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005562
5563static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005564posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005565{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005566 WAIT_TYPE status;
5567 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005568
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005569 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005570 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005571
Guido van Rossumc9641791998-08-04 15:26:23 +00005572 return Py_BuildValue("i", WTERMSIG(status));
5573}
5574#endif /* WTERMSIG */
5575
5576#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005577PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005578"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005579Return the signal that stopped the process that provided\n\
5580the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005581
5582static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005583posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005584{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005585 WAIT_TYPE status;
5586 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005587
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005588 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005589 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005590
Guido van Rossumc9641791998-08-04 15:26:23 +00005591 return Py_BuildValue("i", WSTOPSIG(status));
5592}
5593#endif /* WSTOPSIG */
5594
5595#endif /* HAVE_SYS_WAIT_H */
5596
5597
Thomas Wouters477c8d52006-05-27 19:21:47 +00005598#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005599#ifdef _SCO_DS
5600/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5601 needed definitions in sys/statvfs.h */
5602#define _SVID3
5603#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005604#include <sys/statvfs.h>
5605
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005606static PyObject*
5607_pystatvfs_fromstructstatvfs(struct statvfs st) {
5608 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5609 if (v == NULL)
5610 return NULL;
5611
5612#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005613 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5614 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5615 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5616 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5617 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5618 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5619 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5620 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5621 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5622 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005623#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005624 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5625 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005626 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005627 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005628 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005629 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005630 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005631 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005632 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005633 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005634 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005635 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005636 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005637 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005638 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5639 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005640#endif
5641
5642 return v;
5643}
5644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005645PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005646"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005647Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005648
5649static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005650posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005651{
5652 int fd, res;
5653 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005654
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005655 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005656 return NULL;
5657 Py_BEGIN_ALLOW_THREADS
5658 res = fstatvfs(fd, &st);
5659 Py_END_ALLOW_THREADS
5660 if (res != 0)
5661 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005662
5663 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005664}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005665#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005666
5667
Thomas Wouters477c8d52006-05-27 19:21:47 +00005668#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005669#include <sys/statvfs.h>
5670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005671PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005672"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005673Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005674
5675static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005676posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005677{
5678 char *path;
5679 int res;
5680 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005681 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005682 return NULL;
5683 Py_BEGIN_ALLOW_THREADS
5684 res = statvfs(path, &st);
5685 Py_END_ALLOW_THREADS
5686 if (res != 0)
5687 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005688
5689 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005690}
5691#endif /* HAVE_STATVFS */
5692
Fred Drakec9680921999-12-13 16:37:25 +00005693/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5694 * It maps strings representing configuration variable names to
5695 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005696 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005697 * rarely-used constants. There are three separate tables that use
5698 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005699 *
5700 * This code is always included, even if none of the interfaces that
5701 * need it are included. The #if hackery needed to avoid it would be
5702 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005703 */
5704struct constdef {
5705 char *name;
5706 long value;
5707};
5708
Fred Drake12c6e2d1999-12-14 21:25:03 +00005709static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005710conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005711 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005712{
Christian Heimes217cfd12007-12-02 14:31:20 +00005713 if (PyLong_Check(arg)) {
5714 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005715 return 1;
5716 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005717 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005718 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005719 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005720 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005721 size_t hi = tablesize;
5722 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005723 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005724 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005725 PyErr_SetString(PyExc_TypeError,
5726 "configuration names must be strings or integers");
5727 return 0;
5728 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005729 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005730 if (confname == NULL)
5731 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005732 while (lo < hi) {
5733 mid = (lo + hi) / 2;
5734 cmp = strcmp(confname, table[mid].name);
5735 if (cmp < 0)
5736 hi = mid;
5737 else if (cmp > 0)
5738 lo = mid + 1;
5739 else {
5740 *valuep = table[mid].value;
5741 return 1;
5742 }
5743 }
5744 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005745 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005746 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005747}
5748
5749
5750#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5751static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005752#ifdef _PC_ABI_AIO_XFER_MAX
5753 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5754#endif
5755#ifdef _PC_ABI_ASYNC_IO
5756 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5757#endif
Fred Drakec9680921999-12-13 16:37:25 +00005758#ifdef _PC_ASYNC_IO
5759 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5760#endif
5761#ifdef _PC_CHOWN_RESTRICTED
5762 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5763#endif
5764#ifdef _PC_FILESIZEBITS
5765 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5766#endif
5767#ifdef _PC_LAST
5768 {"PC_LAST", _PC_LAST},
5769#endif
5770#ifdef _PC_LINK_MAX
5771 {"PC_LINK_MAX", _PC_LINK_MAX},
5772#endif
5773#ifdef _PC_MAX_CANON
5774 {"PC_MAX_CANON", _PC_MAX_CANON},
5775#endif
5776#ifdef _PC_MAX_INPUT
5777 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5778#endif
5779#ifdef _PC_NAME_MAX
5780 {"PC_NAME_MAX", _PC_NAME_MAX},
5781#endif
5782#ifdef _PC_NO_TRUNC
5783 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5784#endif
5785#ifdef _PC_PATH_MAX
5786 {"PC_PATH_MAX", _PC_PATH_MAX},
5787#endif
5788#ifdef _PC_PIPE_BUF
5789 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5790#endif
5791#ifdef _PC_PRIO_IO
5792 {"PC_PRIO_IO", _PC_PRIO_IO},
5793#endif
5794#ifdef _PC_SOCK_MAXBUF
5795 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5796#endif
5797#ifdef _PC_SYNC_IO
5798 {"PC_SYNC_IO", _PC_SYNC_IO},
5799#endif
5800#ifdef _PC_VDISABLE
5801 {"PC_VDISABLE", _PC_VDISABLE},
5802#endif
5803};
5804
Fred Drakec9680921999-12-13 16:37:25 +00005805static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005806conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005807{
5808 return conv_confname(arg, valuep, posix_constants_pathconf,
5809 sizeof(posix_constants_pathconf)
5810 / sizeof(struct constdef));
5811}
5812#endif
5813
5814#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005815PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005816"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005817Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005818If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005819
5820static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005821posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005822{
5823 PyObject *result = NULL;
5824 int name, fd;
5825
Fred Drake12c6e2d1999-12-14 21:25:03 +00005826 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5827 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005828 long limit;
5829
5830 errno = 0;
5831 limit = fpathconf(fd, name);
5832 if (limit == -1 && errno != 0)
5833 posix_error();
5834 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005835 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005836 }
5837 return result;
5838}
5839#endif
5840
5841
5842#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005843PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005844"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005845Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005846If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005847
5848static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005849posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005850{
5851 PyObject *result = NULL;
5852 int name;
5853 char *path;
5854
5855 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5856 conv_path_confname, &name)) {
5857 long limit;
5858
5859 errno = 0;
5860 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005861 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005862 if (errno == EINVAL)
5863 /* could be a path or name problem */
5864 posix_error();
5865 else
5866 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005867 }
Fred Drakec9680921999-12-13 16:37:25 +00005868 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005869 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005870 }
5871 return result;
5872}
5873#endif
5874
5875#ifdef HAVE_CONFSTR
5876static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005877#ifdef _CS_ARCHITECTURE
5878 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5879#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00005880#ifdef _CS_GNU_LIBC_VERSION
5881 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
5882#endif
5883#ifdef _CS_GNU_LIBPTHREAD_VERSION
5884 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
5885#endif
Fred Draked86ed291999-12-15 15:34:33 +00005886#ifdef _CS_HOSTNAME
5887 {"CS_HOSTNAME", _CS_HOSTNAME},
5888#endif
5889#ifdef _CS_HW_PROVIDER
5890 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5891#endif
5892#ifdef _CS_HW_SERIAL
5893 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5894#endif
5895#ifdef _CS_INITTAB_NAME
5896 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5897#endif
Fred Drakec9680921999-12-13 16:37:25 +00005898#ifdef _CS_LFS64_CFLAGS
5899 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5900#endif
5901#ifdef _CS_LFS64_LDFLAGS
5902 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5903#endif
5904#ifdef _CS_LFS64_LIBS
5905 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5906#endif
5907#ifdef _CS_LFS64_LINTFLAGS
5908 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5909#endif
5910#ifdef _CS_LFS_CFLAGS
5911 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5912#endif
5913#ifdef _CS_LFS_LDFLAGS
5914 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5915#endif
5916#ifdef _CS_LFS_LIBS
5917 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5918#endif
5919#ifdef _CS_LFS_LINTFLAGS
5920 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5921#endif
Fred Draked86ed291999-12-15 15:34:33 +00005922#ifdef _CS_MACHINE
5923 {"CS_MACHINE", _CS_MACHINE},
5924#endif
Fred Drakec9680921999-12-13 16:37:25 +00005925#ifdef _CS_PATH
5926 {"CS_PATH", _CS_PATH},
5927#endif
Fred Draked86ed291999-12-15 15:34:33 +00005928#ifdef _CS_RELEASE
5929 {"CS_RELEASE", _CS_RELEASE},
5930#endif
5931#ifdef _CS_SRPC_DOMAIN
5932 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5933#endif
5934#ifdef _CS_SYSNAME
5935 {"CS_SYSNAME", _CS_SYSNAME},
5936#endif
5937#ifdef _CS_VERSION
5938 {"CS_VERSION", _CS_VERSION},
5939#endif
Fred Drakec9680921999-12-13 16:37:25 +00005940#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5941 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5942#endif
5943#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5944 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5945#endif
5946#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5947 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5948#endif
5949#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5950 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5951#endif
5952#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5953 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5954#endif
5955#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5956 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5957#endif
5958#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5959 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5960#endif
5961#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5962 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5963#endif
5964#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5965 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5966#endif
5967#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5968 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5969#endif
5970#ifdef _CS_XBS5_LP64_OFF64_LIBS
5971 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5972#endif
5973#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5974 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5975#endif
5976#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5977 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5978#endif
5979#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5980 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5981#endif
5982#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5983 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5984#endif
5985#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5986 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5987#endif
Fred Draked86ed291999-12-15 15:34:33 +00005988#ifdef _MIPS_CS_AVAIL_PROCESSORS
5989 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5990#endif
5991#ifdef _MIPS_CS_BASE
5992 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5993#endif
5994#ifdef _MIPS_CS_HOSTID
5995 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5996#endif
5997#ifdef _MIPS_CS_HW_NAME
5998 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5999#endif
6000#ifdef _MIPS_CS_NUM_PROCESSORS
6001 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6002#endif
6003#ifdef _MIPS_CS_OSREL_MAJ
6004 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6005#endif
6006#ifdef _MIPS_CS_OSREL_MIN
6007 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6008#endif
6009#ifdef _MIPS_CS_OSREL_PATCH
6010 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6011#endif
6012#ifdef _MIPS_CS_OS_NAME
6013 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6014#endif
6015#ifdef _MIPS_CS_OS_PROVIDER
6016 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6017#endif
6018#ifdef _MIPS_CS_PROCESSORS
6019 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6020#endif
6021#ifdef _MIPS_CS_SERIAL
6022 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6023#endif
6024#ifdef _MIPS_CS_VENDOR
6025 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6026#endif
Fred Drakec9680921999-12-13 16:37:25 +00006027};
6028
6029static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006030conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006031{
6032 return conv_confname(arg, valuep, posix_constants_confstr,
6033 sizeof(posix_constants_confstr)
6034 / sizeof(struct constdef));
6035}
6036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006037PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006038"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006039Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006040
6041static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006042posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006043{
6044 PyObject *result = NULL;
6045 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006046 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006047
6048 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006049 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006050
Fred Drakec9680921999-12-13 16:37:25 +00006051 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006052 len = confstr(name, buffer, sizeof(buffer));
6053 if (len == 0) {
6054 if (errno) {
6055 posix_error();
6056 }
6057 else {
6058 result = Py_None;
6059 Py_INCREF(Py_None);
6060 }
Fred Drakec9680921999-12-13 16:37:25 +00006061 }
6062 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006063 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006064 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006065 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006066 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006067 }
6068 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006069 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006070 }
6071 }
6072 return result;
6073}
6074#endif
6075
6076
6077#ifdef HAVE_SYSCONF
6078static struct constdef posix_constants_sysconf[] = {
6079#ifdef _SC_2_CHAR_TERM
6080 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6081#endif
6082#ifdef _SC_2_C_BIND
6083 {"SC_2_C_BIND", _SC_2_C_BIND},
6084#endif
6085#ifdef _SC_2_C_DEV
6086 {"SC_2_C_DEV", _SC_2_C_DEV},
6087#endif
6088#ifdef _SC_2_C_VERSION
6089 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6090#endif
6091#ifdef _SC_2_FORT_DEV
6092 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6093#endif
6094#ifdef _SC_2_FORT_RUN
6095 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6096#endif
6097#ifdef _SC_2_LOCALEDEF
6098 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6099#endif
6100#ifdef _SC_2_SW_DEV
6101 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6102#endif
6103#ifdef _SC_2_UPE
6104 {"SC_2_UPE", _SC_2_UPE},
6105#endif
6106#ifdef _SC_2_VERSION
6107 {"SC_2_VERSION", _SC_2_VERSION},
6108#endif
Fred Draked86ed291999-12-15 15:34:33 +00006109#ifdef _SC_ABI_ASYNCHRONOUS_IO
6110 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6111#endif
6112#ifdef _SC_ACL
6113 {"SC_ACL", _SC_ACL},
6114#endif
Fred Drakec9680921999-12-13 16:37:25 +00006115#ifdef _SC_AIO_LISTIO_MAX
6116 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6117#endif
Fred Drakec9680921999-12-13 16:37:25 +00006118#ifdef _SC_AIO_MAX
6119 {"SC_AIO_MAX", _SC_AIO_MAX},
6120#endif
6121#ifdef _SC_AIO_PRIO_DELTA_MAX
6122 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6123#endif
6124#ifdef _SC_ARG_MAX
6125 {"SC_ARG_MAX", _SC_ARG_MAX},
6126#endif
6127#ifdef _SC_ASYNCHRONOUS_IO
6128 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6129#endif
6130#ifdef _SC_ATEXIT_MAX
6131 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6132#endif
Fred Draked86ed291999-12-15 15:34:33 +00006133#ifdef _SC_AUDIT
6134 {"SC_AUDIT", _SC_AUDIT},
6135#endif
Fred Drakec9680921999-12-13 16:37:25 +00006136#ifdef _SC_AVPHYS_PAGES
6137 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6138#endif
6139#ifdef _SC_BC_BASE_MAX
6140 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6141#endif
6142#ifdef _SC_BC_DIM_MAX
6143 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6144#endif
6145#ifdef _SC_BC_SCALE_MAX
6146 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6147#endif
6148#ifdef _SC_BC_STRING_MAX
6149 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6150#endif
Fred Draked86ed291999-12-15 15:34:33 +00006151#ifdef _SC_CAP
6152 {"SC_CAP", _SC_CAP},
6153#endif
Fred Drakec9680921999-12-13 16:37:25 +00006154#ifdef _SC_CHARCLASS_NAME_MAX
6155 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6156#endif
6157#ifdef _SC_CHAR_BIT
6158 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6159#endif
6160#ifdef _SC_CHAR_MAX
6161 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6162#endif
6163#ifdef _SC_CHAR_MIN
6164 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6165#endif
6166#ifdef _SC_CHILD_MAX
6167 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6168#endif
6169#ifdef _SC_CLK_TCK
6170 {"SC_CLK_TCK", _SC_CLK_TCK},
6171#endif
6172#ifdef _SC_COHER_BLKSZ
6173 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6174#endif
6175#ifdef _SC_COLL_WEIGHTS_MAX
6176 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6177#endif
6178#ifdef _SC_DCACHE_ASSOC
6179 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6180#endif
6181#ifdef _SC_DCACHE_BLKSZ
6182 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6183#endif
6184#ifdef _SC_DCACHE_LINESZ
6185 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6186#endif
6187#ifdef _SC_DCACHE_SZ
6188 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6189#endif
6190#ifdef _SC_DCACHE_TBLKSZ
6191 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6192#endif
6193#ifdef _SC_DELAYTIMER_MAX
6194 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6195#endif
6196#ifdef _SC_EQUIV_CLASS_MAX
6197 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6198#endif
6199#ifdef _SC_EXPR_NEST_MAX
6200 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6201#endif
6202#ifdef _SC_FSYNC
6203 {"SC_FSYNC", _SC_FSYNC},
6204#endif
6205#ifdef _SC_GETGR_R_SIZE_MAX
6206 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6207#endif
6208#ifdef _SC_GETPW_R_SIZE_MAX
6209 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6210#endif
6211#ifdef _SC_ICACHE_ASSOC
6212 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6213#endif
6214#ifdef _SC_ICACHE_BLKSZ
6215 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6216#endif
6217#ifdef _SC_ICACHE_LINESZ
6218 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6219#endif
6220#ifdef _SC_ICACHE_SZ
6221 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6222#endif
Fred Draked86ed291999-12-15 15:34:33 +00006223#ifdef _SC_INF
6224 {"SC_INF", _SC_INF},
6225#endif
Fred Drakec9680921999-12-13 16:37:25 +00006226#ifdef _SC_INT_MAX
6227 {"SC_INT_MAX", _SC_INT_MAX},
6228#endif
6229#ifdef _SC_INT_MIN
6230 {"SC_INT_MIN", _SC_INT_MIN},
6231#endif
6232#ifdef _SC_IOV_MAX
6233 {"SC_IOV_MAX", _SC_IOV_MAX},
6234#endif
Fred Draked86ed291999-12-15 15:34:33 +00006235#ifdef _SC_IP_SECOPTS
6236 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6237#endif
Fred Drakec9680921999-12-13 16:37:25 +00006238#ifdef _SC_JOB_CONTROL
6239 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6240#endif
Fred Draked86ed291999-12-15 15:34:33 +00006241#ifdef _SC_KERN_POINTERS
6242 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6243#endif
6244#ifdef _SC_KERN_SIM
6245 {"SC_KERN_SIM", _SC_KERN_SIM},
6246#endif
Fred Drakec9680921999-12-13 16:37:25 +00006247#ifdef _SC_LINE_MAX
6248 {"SC_LINE_MAX", _SC_LINE_MAX},
6249#endif
6250#ifdef _SC_LOGIN_NAME_MAX
6251 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6252#endif
6253#ifdef _SC_LOGNAME_MAX
6254 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6255#endif
6256#ifdef _SC_LONG_BIT
6257 {"SC_LONG_BIT", _SC_LONG_BIT},
6258#endif
Fred Draked86ed291999-12-15 15:34:33 +00006259#ifdef _SC_MAC
6260 {"SC_MAC", _SC_MAC},
6261#endif
Fred Drakec9680921999-12-13 16:37:25 +00006262#ifdef _SC_MAPPED_FILES
6263 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6264#endif
6265#ifdef _SC_MAXPID
6266 {"SC_MAXPID", _SC_MAXPID},
6267#endif
6268#ifdef _SC_MB_LEN_MAX
6269 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6270#endif
6271#ifdef _SC_MEMLOCK
6272 {"SC_MEMLOCK", _SC_MEMLOCK},
6273#endif
6274#ifdef _SC_MEMLOCK_RANGE
6275 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6276#endif
6277#ifdef _SC_MEMORY_PROTECTION
6278 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6279#endif
6280#ifdef _SC_MESSAGE_PASSING
6281 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6282#endif
Fred Draked86ed291999-12-15 15:34:33 +00006283#ifdef _SC_MMAP_FIXED_ALIGNMENT
6284 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6285#endif
Fred Drakec9680921999-12-13 16:37:25 +00006286#ifdef _SC_MQ_OPEN_MAX
6287 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6288#endif
6289#ifdef _SC_MQ_PRIO_MAX
6290 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6291#endif
Fred Draked86ed291999-12-15 15:34:33 +00006292#ifdef _SC_NACLS_MAX
6293 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6294#endif
Fred Drakec9680921999-12-13 16:37:25 +00006295#ifdef _SC_NGROUPS_MAX
6296 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6297#endif
6298#ifdef _SC_NL_ARGMAX
6299 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6300#endif
6301#ifdef _SC_NL_LANGMAX
6302 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6303#endif
6304#ifdef _SC_NL_MSGMAX
6305 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6306#endif
6307#ifdef _SC_NL_NMAX
6308 {"SC_NL_NMAX", _SC_NL_NMAX},
6309#endif
6310#ifdef _SC_NL_SETMAX
6311 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6312#endif
6313#ifdef _SC_NL_TEXTMAX
6314 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6315#endif
6316#ifdef _SC_NPROCESSORS_CONF
6317 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6318#endif
6319#ifdef _SC_NPROCESSORS_ONLN
6320 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6321#endif
Fred Draked86ed291999-12-15 15:34:33 +00006322#ifdef _SC_NPROC_CONF
6323 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6324#endif
6325#ifdef _SC_NPROC_ONLN
6326 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6327#endif
Fred Drakec9680921999-12-13 16:37:25 +00006328#ifdef _SC_NZERO
6329 {"SC_NZERO", _SC_NZERO},
6330#endif
6331#ifdef _SC_OPEN_MAX
6332 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6333#endif
6334#ifdef _SC_PAGESIZE
6335 {"SC_PAGESIZE", _SC_PAGESIZE},
6336#endif
6337#ifdef _SC_PAGE_SIZE
6338 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6339#endif
6340#ifdef _SC_PASS_MAX
6341 {"SC_PASS_MAX", _SC_PASS_MAX},
6342#endif
6343#ifdef _SC_PHYS_PAGES
6344 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6345#endif
6346#ifdef _SC_PII
6347 {"SC_PII", _SC_PII},
6348#endif
6349#ifdef _SC_PII_INTERNET
6350 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6351#endif
6352#ifdef _SC_PII_INTERNET_DGRAM
6353 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6354#endif
6355#ifdef _SC_PII_INTERNET_STREAM
6356 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6357#endif
6358#ifdef _SC_PII_OSI
6359 {"SC_PII_OSI", _SC_PII_OSI},
6360#endif
6361#ifdef _SC_PII_OSI_CLTS
6362 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6363#endif
6364#ifdef _SC_PII_OSI_COTS
6365 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6366#endif
6367#ifdef _SC_PII_OSI_M
6368 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6369#endif
6370#ifdef _SC_PII_SOCKET
6371 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6372#endif
6373#ifdef _SC_PII_XTI
6374 {"SC_PII_XTI", _SC_PII_XTI},
6375#endif
6376#ifdef _SC_POLL
6377 {"SC_POLL", _SC_POLL},
6378#endif
6379#ifdef _SC_PRIORITIZED_IO
6380 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6381#endif
6382#ifdef _SC_PRIORITY_SCHEDULING
6383 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6384#endif
6385#ifdef _SC_REALTIME_SIGNALS
6386 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6387#endif
6388#ifdef _SC_RE_DUP_MAX
6389 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6390#endif
6391#ifdef _SC_RTSIG_MAX
6392 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6393#endif
6394#ifdef _SC_SAVED_IDS
6395 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6396#endif
6397#ifdef _SC_SCHAR_MAX
6398 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6399#endif
6400#ifdef _SC_SCHAR_MIN
6401 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6402#endif
6403#ifdef _SC_SELECT
6404 {"SC_SELECT", _SC_SELECT},
6405#endif
6406#ifdef _SC_SEMAPHORES
6407 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6408#endif
6409#ifdef _SC_SEM_NSEMS_MAX
6410 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6411#endif
6412#ifdef _SC_SEM_VALUE_MAX
6413 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6414#endif
6415#ifdef _SC_SHARED_MEMORY_OBJECTS
6416 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6417#endif
6418#ifdef _SC_SHRT_MAX
6419 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6420#endif
6421#ifdef _SC_SHRT_MIN
6422 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6423#endif
6424#ifdef _SC_SIGQUEUE_MAX
6425 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6426#endif
6427#ifdef _SC_SIGRT_MAX
6428 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6429#endif
6430#ifdef _SC_SIGRT_MIN
6431 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6432#endif
Fred Draked86ed291999-12-15 15:34:33 +00006433#ifdef _SC_SOFTPOWER
6434 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6435#endif
Fred Drakec9680921999-12-13 16:37:25 +00006436#ifdef _SC_SPLIT_CACHE
6437 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6438#endif
6439#ifdef _SC_SSIZE_MAX
6440 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6441#endif
6442#ifdef _SC_STACK_PROT
6443 {"SC_STACK_PROT", _SC_STACK_PROT},
6444#endif
6445#ifdef _SC_STREAM_MAX
6446 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6447#endif
6448#ifdef _SC_SYNCHRONIZED_IO
6449 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6450#endif
6451#ifdef _SC_THREADS
6452 {"SC_THREADS", _SC_THREADS},
6453#endif
6454#ifdef _SC_THREAD_ATTR_STACKADDR
6455 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6456#endif
6457#ifdef _SC_THREAD_ATTR_STACKSIZE
6458 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6459#endif
6460#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6461 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6462#endif
6463#ifdef _SC_THREAD_KEYS_MAX
6464 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6465#endif
6466#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6467 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6468#endif
6469#ifdef _SC_THREAD_PRIO_INHERIT
6470 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6471#endif
6472#ifdef _SC_THREAD_PRIO_PROTECT
6473 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6474#endif
6475#ifdef _SC_THREAD_PROCESS_SHARED
6476 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6477#endif
6478#ifdef _SC_THREAD_SAFE_FUNCTIONS
6479 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6480#endif
6481#ifdef _SC_THREAD_STACK_MIN
6482 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6483#endif
6484#ifdef _SC_THREAD_THREADS_MAX
6485 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6486#endif
6487#ifdef _SC_TIMERS
6488 {"SC_TIMERS", _SC_TIMERS},
6489#endif
6490#ifdef _SC_TIMER_MAX
6491 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6492#endif
6493#ifdef _SC_TTY_NAME_MAX
6494 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6495#endif
6496#ifdef _SC_TZNAME_MAX
6497 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6498#endif
6499#ifdef _SC_T_IOV_MAX
6500 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6501#endif
6502#ifdef _SC_UCHAR_MAX
6503 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6504#endif
6505#ifdef _SC_UINT_MAX
6506 {"SC_UINT_MAX", _SC_UINT_MAX},
6507#endif
6508#ifdef _SC_UIO_MAXIOV
6509 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6510#endif
6511#ifdef _SC_ULONG_MAX
6512 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6513#endif
6514#ifdef _SC_USHRT_MAX
6515 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6516#endif
6517#ifdef _SC_VERSION
6518 {"SC_VERSION", _SC_VERSION},
6519#endif
6520#ifdef _SC_WORD_BIT
6521 {"SC_WORD_BIT", _SC_WORD_BIT},
6522#endif
6523#ifdef _SC_XBS5_ILP32_OFF32
6524 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6525#endif
6526#ifdef _SC_XBS5_ILP32_OFFBIG
6527 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6528#endif
6529#ifdef _SC_XBS5_LP64_OFF64
6530 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6531#endif
6532#ifdef _SC_XBS5_LPBIG_OFFBIG
6533 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6534#endif
6535#ifdef _SC_XOPEN_CRYPT
6536 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6537#endif
6538#ifdef _SC_XOPEN_ENH_I18N
6539 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6540#endif
6541#ifdef _SC_XOPEN_LEGACY
6542 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6543#endif
6544#ifdef _SC_XOPEN_REALTIME
6545 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6546#endif
6547#ifdef _SC_XOPEN_REALTIME_THREADS
6548 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6549#endif
6550#ifdef _SC_XOPEN_SHM
6551 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6552#endif
6553#ifdef _SC_XOPEN_UNIX
6554 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6555#endif
6556#ifdef _SC_XOPEN_VERSION
6557 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6558#endif
6559#ifdef _SC_XOPEN_XCU_VERSION
6560 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6561#endif
6562#ifdef _SC_XOPEN_XPG2
6563 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6564#endif
6565#ifdef _SC_XOPEN_XPG3
6566 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6567#endif
6568#ifdef _SC_XOPEN_XPG4
6569 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6570#endif
6571};
6572
6573static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006574conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006575{
6576 return conv_confname(arg, valuep, posix_constants_sysconf,
6577 sizeof(posix_constants_sysconf)
6578 / sizeof(struct constdef));
6579}
6580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006581PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006582"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006583Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006584
6585static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006586posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006587{
6588 PyObject *result = NULL;
6589 int name;
6590
6591 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6592 int value;
6593
6594 errno = 0;
6595 value = sysconf(name);
6596 if (value == -1 && errno != 0)
6597 posix_error();
6598 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006599 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006600 }
6601 return result;
6602}
6603#endif
6604
6605
Fred Drakebec628d1999-12-15 18:31:10 +00006606/* This code is used to ensure that the tables of configuration value names
6607 * are in sorted order as required by conv_confname(), and also to build the
6608 * the exported dictionaries that are used to publish information about the
6609 * names available on the host platform.
6610 *
6611 * Sorting the table at runtime ensures that the table is properly ordered
6612 * when used, even for platforms we're not able to test on. It also makes
6613 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006614 */
Fred Drakebec628d1999-12-15 18:31:10 +00006615
6616static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006617cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006618{
6619 const struct constdef *c1 =
6620 (const struct constdef *) v1;
6621 const struct constdef *c2 =
6622 (const struct constdef *) v2;
6623
6624 return strcmp(c1->name, c2->name);
6625}
6626
6627static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006628setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006629 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006630{
Fred Drakebec628d1999-12-15 18:31:10 +00006631 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006632 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006633
6634 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6635 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006636 if (d == NULL)
6637 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006638
Barry Warsaw3155db32000-04-13 15:20:40 +00006639 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006640 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006641 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6642 Py_XDECREF(o);
6643 Py_DECREF(d);
6644 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006645 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006646 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006647 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006648 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006649}
6650
Fred Drakebec628d1999-12-15 18:31:10 +00006651/* Return -1 on failure, 0 on success. */
6652static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006653setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006654{
6655#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006656 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006657 sizeof(posix_constants_pathconf)
6658 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006659 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006660 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006661#endif
6662#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006663 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006664 sizeof(posix_constants_confstr)
6665 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006666 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006667 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006668#endif
6669#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006670 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006671 sizeof(posix_constants_sysconf)
6672 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006673 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006674 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006675#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006676 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006677}
Fred Draked86ed291999-12-15 15:34:33 +00006678
6679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006680PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006681"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006682Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006683in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006684
6685static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006686posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006687{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006688 abort();
6689 /*NOTREACHED*/
6690 Py_FatalError("abort() called from Python code didn't abort!");
6691 return NULL;
6692}
Fred Drakebec628d1999-12-15 18:31:10 +00006693
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006694#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006695PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006696"startfile(filepath [, operation]) - Start a file with its associated\n\
6697application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006698\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006699When \"operation\" is not specified or \"open\", this acts like\n\
6700double-clicking the file in Explorer, or giving the file name as an\n\
6701argument to the DOS \"start\" command: the file is opened with whatever\n\
6702application (if any) its extension is associated.\n\
6703When another \"operation\" is given, it specifies what should be done with\n\
6704the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006705\n\
6706startfile returns as soon as the associated application is launched.\n\
6707There is no option to wait for the application to close, and no way\n\
6708to retrieve the application's exit status.\n\
6709\n\
6710The filepath is relative to the current directory. If you want to use\n\
6711an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006712the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006713
6714static PyObject *
6715win32_startfile(PyObject *self, PyObject *args)
6716{
Martin v. Löwis011e8422009-05-05 04:43:17 +00006717 PyObject *ofilepath;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006718 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006719 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006720 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006721
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006722 PyObject *unipath, *woperation = NULL;
6723 if (!PyArg_ParseTuple(args, "U|s:startfile",
6724 &unipath, &operation)) {
6725 PyErr_Clear();
6726 goto normal;
6727 }
6728
6729 if (operation) {
6730 woperation = PyUnicode_DecodeASCII(operation,
6731 strlen(operation), NULL);
6732 if (!woperation) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006733 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006734 operation = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006735 goto normal;
6736 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006737 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006738
6739 Py_BEGIN_ALLOW_THREADS
6740 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6741 PyUnicode_AS_UNICODE(unipath),
6742 NULL, NULL, SW_SHOWNORMAL);
6743 Py_END_ALLOW_THREADS
6744
6745 Py_XDECREF(woperation);
6746 if (rc <= (HINSTANCE)32) {
6747 PyObject *errval = win32_error_unicode("startfile",
6748 PyUnicode_AS_UNICODE(unipath));
6749 return errval;
6750 }
6751 Py_INCREF(Py_None);
6752 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006753
6754normal:
Martin v. Löwis011e8422009-05-05 04:43:17 +00006755 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6756 PyUnicode_FSConverter, &ofilepath,
Georg Brandlf4f44152006-02-18 22:29:33 +00006757 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006758 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00006759 filepath = PyBytes_AsString(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006760 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006761 rc = ShellExecute((HWND)0, operation, filepath,
6762 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006763 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006764 if (rc <= (HINSTANCE)32) {
6765 PyObject *errval = win32_error("startfile", filepath);
Victor Stinnerdcb24032010-04-22 12:08:36 +00006766 Py_DECREF(ofilepath);
Georg Brandle9f8ec92005-09-25 06:16:40 +00006767 return errval;
6768 }
Victor Stinnerdcb24032010-04-22 12:08:36 +00006769 Py_DECREF(ofilepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006770 Py_INCREF(Py_None);
6771 return Py_None;
6772}
6773#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006774
Martin v. Löwis438b5342002-12-27 10:16:42 +00006775#ifdef HAVE_GETLOADAVG
6776PyDoc_STRVAR(posix_getloadavg__doc__,
6777"getloadavg() -> (float, float, float)\n\n\
6778Return the number of processes in the system run queue averaged over\n\
6779the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6780was unobtainable");
6781
6782static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006783posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006784{
6785 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006786 if (getloadavg(loadavg, 3)!=3) {
6787 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6788 return NULL;
6789 } else
6790 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6791}
6792#endif
6793
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006794#ifdef MS_WINDOWS
6795
6796PyDoc_STRVAR(win32_urandom__doc__,
6797"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006798Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006799
6800typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6801 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6802 DWORD dwFlags );
6803typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6804 BYTE *pbBuffer );
6805
6806static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006807/* This handle is never explicitly released. Instead, the operating
6808 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006809static HCRYPTPROV hCryptProv = 0;
6810
Tim Peters4ad82172004-08-30 17:02:04 +00006811static PyObject*
6812win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006813{
Tim Petersd3115382004-08-30 17:36:46 +00006814 int howMany;
6815 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006816
Tim Peters4ad82172004-08-30 17:02:04 +00006817 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006818 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006819 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006820 if (howMany < 0)
6821 return PyErr_Format(PyExc_ValueError,
6822 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006823
Tim Peters4ad82172004-08-30 17:02:04 +00006824 if (hCryptProv == 0) {
6825 HINSTANCE hAdvAPI32 = NULL;
6826 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006827
Tim Peters4ad82172004-08-30 17:02:04 +00006828 /* Obtain handle to the DLL containing CryptoAPI
6829 This should not fail */
6830 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6831 if(hAdvAPI32 == NULL)
6832 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006833
Tim Peters4ad82172004-08-30 17:02:04 +00006834 /* Obtain pointers to the CryptoAPI functions
6835 This will fail on some early versions of Win95 */
6836 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6837 hAdvAPI32,
6838 "CryptAcquireContextA");
6839 if (pCryptAcquireContext == NULL)
6840 return PyErr_Format(PyExc_NotImplementedError,
6841 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006842
Tim Peters4ad82172004-08-30 17:02:04 +00006843 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6844 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006845 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006846 return PyErr_Format(PyExc_NotImplementedError,
6847 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006848
Tim Peters4ad82172004-08-30 17:02:04 +00006849 /* Acquire context */
6850 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6851 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6852 return win32_error("CryptAcquireContext", NULL);
6853 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006854
Tim Peters4ad82172004-08-30 17:02:04 +00006855 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006856 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006857 if (result != NULL) {
6858 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006859 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006860 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006861 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006862 Py_DECREF(result);
6863 return win32_error("CryptGenRandom", NULL);
6864 }
Tim Peters4ad82172004-08-30 17:02:04 +00006865 }
Tim Petersd3115382004-08-30 17:36:46 +00006866 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006867}
6868#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006869
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006870PyDoc_STRVAR(device_encoding__doc__,
6871"device_encoding(fd) -> str\n\n\
6872Return a string describing the encoding of the device\n\
6873if the output is a terminal; else return None.");
6874
6875static PyObject *
6876device_encoding(PyObject *self, PyObject *args)
6877{
6878 int fd;
6879 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6880 return NULL;
Kristján Valur Jónsson649170b2009-03-24 14:15:49 +00006881 if (!_PyVerify_fd(fd) || !isatty(fd)) {
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006882 Py_INCREF(Py_None);
6883 return Py_None;
6884 }
6885#if defined(MS_WINDOWS) || defined(MS_WIN64)
6886 if (fd == 0) {
6887 char buf[100];
6888 sprintf(buf, "cp%d", GetConsoleCP());
6889 return PyUnicode_FromString(buf);
6890 }
6891 if (fd == 1 || fd == 2) {
6892 char buf[100];
6893 sprintf(buf, "cp%d", GetConsoleOutputCP());
6894 return PyUnicode_FromString(buf);
6895 }
6896#elif defined(CODESET)
6897 {
6898 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006899 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006900 return PyUnicode_FromString(codeset);
6901 }
6902#endif
6903 Py_INCREF(Py_None);
6904 return Py_None;
6905}
6906
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006907#ifdef __VMS
6908/* Use openssl random routine */
6909#include <openssl/rand.h>
6910PyDoc_STRVAR(vms_urandom__doc__,
6911"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006912Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006913
6914static PyObject*
6915vms_urandom(PyObject *self, PyObject *args)
6916{
6917 int howMany;
6918 PyObject* result;
6919
6920 /* Read arguments */
6921 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6922 return NULL;
6923 if (howMany < 0)
6924 return PyErr_Format(PyExc_ValueError,
6925 "negative argument not allowed");
6926
6927 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006928 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006929 if (result != NULL) {
6930 /* Get random data */
6931 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006932 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006933 howMany) < 0) {
6934 Py_DECREF(result);
6935 return PyErr_Format(PyExc_ValueError,
6936 "RAND_pseudo_bytes");
6937 }
6938 }
6939 return result;
6940}
6941#endif
6942
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006943#ifdef HAVE_SETRESUID
6944PyDoc_STRVAR(posix_setresuid__doc__,
6945"setresuid(ruid, euid, suid)\n\n\
6946Set the current process's real, effective, and saved user ids.");
6947
6948static PyObject*
6949posix_setresuid (PyObject *self, PyObject *args)
6950{
6951 /* We assume uid_t is no larger than a long. */
6952 long ruid, euid, suid;
6953 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
6954 return NULL;
6955 if (setresuid(ruid, euid, suid) < 0)
6956 return posix_error();
6957 Py_RETURN_NONE;
6958}
6959#endif
6960
6961#ifdef HAVE_SETRESGID
6962PyDoc_STRVAR(posix_setresgid__doc__,
6963"setresgid(rgid, egid, sgid)\n\n\
6964Set the current process's real, effective, and saved group ids.");
6965
6966static PyObject*
6967posix_setresgid (PyObject *self, PyObject *args)
6968{
6969 /* We assume uid_t is no larger than a long. */
6970 long rgid, egid, sgid;
6971 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
6972 return NULL;
6973 if (setresgid(rgid, egid, sgid) < 0)
6974 return posix_error();
6975 Py_RETURN_NONE;
6976}
6977#endif
6978
6979#ifdef HAVE_GETRESUID
6980PyDoc_STRVAR(posix_getresuid__doc__,
6981"getresuid() -> (ruid, euid, suid)\n\n\
6982Get tuple of the current process's real, effective, and saved user ids.");
6983
6984static PyObject*
6985posix_getresuid (PyObject *self, PyObject *noargs)
6986{
6987 uid_t ruid, euid, suid;
6988 long l_ruid, l_euid, l_suid;
6989 if (getresuid(&ruid, &euid, &suid) < 0)
6990 return posix_error();
6991 /* Force the values into long's as we don't know the size of uid_t. */
6992 l_ruid = ruid;
6993 l_euid = euid;
6994 l_suid = suid;
6995 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
6996}
6997#endif
6998
6999#ifdef HAVE_GETRESGID
7000PyDoc_STRVAR(posix_getresgid__doc__,
7001"getresgid() -> (rgid, egid, sgid)\n\n\
7002Get tuple of the current process's real, effective, and saved user ids.");
7003
7004static PyObject*
7005posix_getresgid (PyObject *self, PyObject *noargs)
7006{
7007 uid_t rgid, egid, sgid;
7008 long l_rgid, l_egid, l_sgid;
7009 if (getresgid(&rgid, &egid, &sgid) < 0)
7010 return posix_error();
7011 /* Force the values into long's as we don't know the size of uid_t. */
7012 l_rgid = rgid;
7013 l_egid = egid;
7014 l_sgid = sgid;
7015 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
7016}
7017#endif
7018
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007019static PyMethodDef posix_methods[] = {
7020 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7021#ifdef HAVE_TTYNAME
7022 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7023#endif
7024 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007025#ifdef HAVE_CHFLAGS
7026 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7027#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007028 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007029#ifdef HAVE_FCHMOD
7030 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
7031#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007032#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007033 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007034#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007035#ifdef HAVE_LCHMOD
7036 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
7037#endif /* HAVE_LCHMOD */
7038#ifdef HAVE_FCHOWN
7039 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
7040#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007041#ifdef HAVE_LCHFLAGS
7042 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
7043#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007044#ifdef HAVE_LCHOWN
7045 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7046#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007047#ifdef HAVE_CHROOT
7048 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7049#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007050#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007051 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007052#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007053#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00007054 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7055 METH_NOARGS, posix_getcwd__doc__},
7056 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7057 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007058#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007059#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007060 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007061#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007062 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7063 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7064 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007065#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007066 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007067#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007068#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007069 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007070#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007071 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7072 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7073 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007074 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007075#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007076 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007077#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007078#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007079 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007080#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007081 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007082#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007083 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007084#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007085 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7086 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7087 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007088#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007089 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007090#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007091 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007092#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007093 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7094 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007095#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007096#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007097 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7098 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007099#if defined(PYOS_OS2)
7100 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7101 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7102#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007103#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007104#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007105 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007106#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007107#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007108 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007109#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007110#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007111 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007112#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007113#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007114 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007115#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007116#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007117 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007118#endif /* HAVE_GETEGID */
7119#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007120 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007121#endif /* HAVE_GETEUID */
7122#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007123 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007124#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007125#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007126 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007127#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007128 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007129#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007130 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007131#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007132#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007133 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007134#endif /* HAVE_GETPPID */
7135#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007136 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007137#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007138#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007139 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007140#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007141#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007142 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007143#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007144#ifdef HAVE_KILLPG
7145 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7146#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007147#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007148 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007149#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007150#ifdef MS_WINDOWS
7151 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Brian Curtineb24d742010-04-12 17:16:38 +00007152 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007153#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007154#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007155 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007156#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007157#ifdef HAVE_SETEUID
7158 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7159#endif /* HAVE_SETEUID */
7160#ifdef HAVE_SETEGID
7161 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7162#endif /* HAVE_SETEGID */
7163#ifdef HAVE_SETREUID
7164 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7165#endif /* HAVE_SETREUID */
7166#ifdef HAVE_SETREGID
7167 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7168#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007169#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007170 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007171#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007172#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00007173 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007174#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007175#ifdef HAVE_INITGROUPS
7176 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
7177#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007178#ifdef HAVE_GETPGID
7179 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7180#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007181#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007182 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007183#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007184#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007185 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007186#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007187#ifdef HAVE_WAIT3
7188 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7189#endif /* HAVE_WAIT3 */
7190#ifdef HAVE_WAIT4
7191 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7192#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007193#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007194 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007195#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007196#ifdef HAVE_GETSID
7197 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7198#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007199#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007200 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007201#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007202#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007203 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007204#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007205#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007206 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007207#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007208#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007209 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007210#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007211 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7212 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00007213 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007214 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007215 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7216 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7217 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7218 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7219 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7220 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007221 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007222#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007223 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007224#endif
7225#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007226 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007227#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007228#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007229 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7230#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007231#ifdef HAVE_DEVICE_MACROS
7232 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7233 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7234 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7235#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007236#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007237 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007238#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007239#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007240 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007241#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007242#ifdef HAVE_UNSETENV
7243 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7244#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007245 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007246#ifdef HAVE_FCHDIR
7247 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7248#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007249#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007250 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007251#endif
7252#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007253 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007254#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007255#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007256#ifdef WCOREDUMP
7257 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7258#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007259#ifdef WIFCONTINUED
7260 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7261#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007262#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007263 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007264#endif /* WIFSTOPPED */
7265#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007266 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007267#endif /* WIFSIGNALED */
7268#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007269 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007270#endif /* WIFEXITED */
7271#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007272 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007273#endif /* WEXITSTATUS */
7274#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007275 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007276#endif /* WTERMSIG */
7277#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007278 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007279#endif /* WSTOPSIG */
7280#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007281#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007282 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007283#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007284#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007285 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007286#endif
Fred Drakec9680921999-12-13 16:37:25 +00007287#ifdef HAVE_CONFSTR
7288 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7289#endif
7290#ifdef HAVE_SYSCONF
7291 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7292#endif
7293#ifdef HAVE_FPATHCONF
7294 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7295#endif
7296#ifdef HAVE_PATHCONF
7297 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7298#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007299 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007300#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007301 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7302#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007303#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007304 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007305#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007306 #ifdef MS_WINDOWS
7307 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7308 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007309 #ifdef __VMS
7310 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7311 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007312#ifdef HAVE_SETRESUID
7313 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
7314#endif
7315#ifdef HAVE_SETRESGID
7316 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
7317#endif
7318#ifdef HAVE_GETRESUID
7319 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
7320#endif
7321#ifdef HAVE_GETRESGID
7322 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
7323#endif
7324
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007325 {NULL, NULL} /* Sentinel */
7326};
7327
7328
Barry Warsaw4a342091996-12-19 23:50:02 +00007329static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007330ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007331{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007332 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007333}
7334
Guido van Rossumd48f2521997-12-05 22:19:34 +00007335#if defined(PYOS_OS2)
7336/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007337static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007338{
7339 APIRET rc;
7340 ULONG values[QSV_MAX+1];
7341 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007342 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007343
7344 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007345 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007346 Py_END_ALLOW_THREADS
7347
7348 if (rc != NO_ERROR) {
7349 os2_error(rc);
7350 return -1;
7351 }
7352
Fred Drake4d1e64b2002-04-15 19:40:07 +00007353 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7354 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7355 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7356 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7357 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7358 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7359 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007360
7361 switch (values[QSV_VERSION_MINOR]) {
7362 case 0: ver = "2.00"; break;
7363 case 10: ver = "2.10"; break;
7364 case 11: ver = "2.11"; break;
7365 case 30: ver = "3.00"; break;
7366 case 40: ver = "4.00"; break;
7367 case 50: ver = "5.00"; break;
7368 default:
Tim Peters885d4572001-11-28 20:27:42 +00007369 PyOS_snprintf(tmp, sizeof(tmp),
7370 "%d-%d", values[QSV_VERSION_MAJOR],
7371 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007372 ver = &tmp[0];
7373 }
7374
7375 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007376 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007377 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007378
7379 /* Add Indicator of Which Drive was Used to Boot the System */
7380 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7381 tmp[1] = ':';
7382 tmp[2] = '\0';
7383
Fred Drake4d1e64b2002-04-15 19:40:07 +00007384 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007385}
7386#endif
7387
Barry Warsaw4a342091996-12-19 23:50:02 +00007388static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007389all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007390{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007391#ifdef F_OK
7392 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007393#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007394#ifdef R_OK
7395 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007396#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007397#ifdef W_OK
7398 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007399#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007400#ifdef X_OK
7401 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007402#endif
Fred Drakec9680921999-12-13 16:37:25 +00007403#ifdef NGROUPS_MAX
7404 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7405#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007406#ifdef TMP_MAX
7407 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7408#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007409#ifdef WCONTINUED
7410 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7411#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007412#ifdef WNOHANG
7413 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007414#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007415#ifdef WUNTRACED
7416 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7417#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007418#ifdef O_RDONLY
7419 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7420#endif
7421#ifdef O_WRONLY
7422 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7423#endif
7424#ifdef O_RDWR
7425 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7426#endif
7427#ifdef O_NDELAY
7428 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7429#endif
7430#ifdef O_NONBLOCK
7431 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7432#endif
7433#ifdef O_APPEND
7434 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7435#endif
7436#ifdef O_DSYNC
7437 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7438#endif
7439#ifdef O_RSYNC
7440 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7441#endif
7442#ifdef O_SYNC
7443 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7444#endif
7445#ifdef O_NOCTTY
7446 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7447#endif
7448#ifdef O_CREAT
7449 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7450#endif
7451#ifdef O_EXCL
7452 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7453#endif
7454#ifdef O_TRUNC
7455 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7456#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007457#ifdef O_BINARY
7458 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7459#endif
7460#ifdef O_TEXT
7461 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7462#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007463#ifdef O_LARGEFILE
7464 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7465#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007466#ifdef O_SHLOCK
7467 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7468#endif
7469#ifdef O_EXLOCK
7470 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7471#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007472
Tim Peters5aa91602002-01-30 05:46:57 +00007473/* MS Windows */
7474#ifdef O_NOINHERIT
7475 /* Don't inherit in child processes. */
7476 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7477#endif
7478#ifdef _O_SHORT_LIVED
7479 /* Optimize for short life (keep in memory). */
7480 /* MS forgot to define this one with a non-underscore form too. */
7481 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7482#endif
7483#ifdef O_TEMPORARY
7484 /* Automatically delete when last handle is closed. */
7485 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7486#endif
7487#ifdef O_RANDOM
7488 /* Optimize for random access. */
7489 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7490#endif
7491#ifdef O_SEQUENTIAL
7492 /* Optimize for sequential access. */
7493 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7494#endif
7495
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007496/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007497#ifdef O_ASYNC
7498 /* Send a SIGIO signal whenever input or output
7499 becomes available on file descriptor */
7500 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7501#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007502#ifdef O_DIRECT
7503 /* Direct disk access. */
7504 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7505#endif
7506#ifdef O_DIRECTORY
7507 /* Must be a directory. */
7508 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7509#endif
7510#ifdef O_NOFOLLOW
7511 /* Do not follow links. */
7512 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7513#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007514#ifdef O_NOATIME
7515 /* Do not update the access time. */
7516 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7517#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007518
Barry Warsaw5676bd12003-01-07 20:57:09 +00007519 /* These come from sysexits.h */
7520#ifdef EX_OK
7521 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007522#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007523#ifdef EX_USAGE
7524 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007525#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007526#ifdef EX_DATAERR
7527 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007528#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007529#ifdef EX_NOINPUT
7530 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007531#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007532#ifdef EX_NOUSER
7533 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007534#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007535#ifdef EX_NOHOST
7536 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007537#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007538#ifdef EX_UNAVAILABLE
7539 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007540#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007541#ifdef EX_SOFTWARE
7542 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007543#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007544#ifdef EX_OSERR
7545 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007546#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007547#ifdef EX_OSFILE
7548 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007549#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007550#ifdef EX_CANTCREAT
7551 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007552#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007553#ifdef EX_IOERR
7554 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007555#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007556#ifdef EX_TEMPFAIL
7557 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007558#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007559#ifdef EX_PROTOCOL
7560 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007561#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007562#ifdef EX_NOPERM
7563 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007564#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007565#ifdef EX_CONFIG
7566 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007567#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007568#ifdef EX_NOTFOUND
7569 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007570#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007571
Guido van Rossum246bc171999-02-01 23:54:31 +00007572#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007573#if defined(PYOS_OS2) && defined(PYCC_GCC)
7574 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7575 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7576 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7577 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7578 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7579 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7580 if (ins(d, "P_PM", (long)P_PM)) return -1;
7581 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7582 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7583 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7584 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7585 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7586 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7587 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7588 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7589 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7590 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7591 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7592 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7593 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7594#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007595 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7596 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7597 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7598 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7599 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007600#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007601#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007602
Guido van Rossumd48f2521997-12-05 22:19:34 +00007603#if defined(PYOS_OS2)
7604 if (insertvalues(d)) return -1;
7605#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007606 return 0;
7607}
7608
7609
Tim Peters5aa91602002-01-30 05:46:57 +00007610#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007611#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007612#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007613
7614#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007615#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007616#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007617
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007618#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007619#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007620#define MODNAME "posix"
7621#endif
7622
Martin v. Löwis1a214512008-06-11 05:26:20 +00007623static struct PyModuleDef posixmodule = {
7624 PyModuleDef_HEAD_INIT,
7625 MODNAME,
7626 posix__doc__,
7627 -1,
7628 posix_methods,
7629 NULL,
7630 NULL,
7631 NULL,
7632 NULL
7633};
7634
7635
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007636PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007637INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007638{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007639 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007640
Martin v. Löwis1a214512008-06-11 05:26:20 +00007641 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007642 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007643 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007644
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007645 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007646 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007647 Py_XINCREF(v);
7648 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007649 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007650 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007651
Fred Drake4d1e64b2002-04-15 19:40:07 +00007652 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007653 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007654
Fred Drake4d1e64b2002-04-15 19:40:07 +00007655 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007656 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007657
Fred Drake4d1e64b2002-04-15 19:40:07 +00007658 Py_INCREF(PyExc_OSError);
7659 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007660
Guido van Rossumb3d39562000-01-31 18:41:26 +00007661#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007662 if (posix_putenv_garbage == NULL)
7663 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007664#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007665
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007666 if (!initialized) {
7667 stat_result_desc.name = MODNAME ".stat_result";
7668 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7669 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7670 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7671 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7672 structseq_new = StatResultType.tp_new;
7673 StatResultType.tp_new = statresult_new;
7674
7675 statvfs_result_desc.name = MODNAME ".statvfs_result";
7676 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007677#ifdef NEED_TICKS_PER_SECOND
7678# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7679 ticks_per_second = sysconf(_SC_CLK_TCK);
7680# elif defined(HZ)
7681 ticks_per_second = HZ;
7682# else
7683 ticks_per_second = 60; /* magic fallback value; may be bogus */
7684# endif
7685#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007686 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007687 Py_INCREF((PyObject*) &StatResultType);
7688 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007689 Py_INCREF((PyObject*) &StatVFSResultType);
7690 PyModule_AddObject(m, "statvfs_result",
7691 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007692 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007693
7694#ifdef __APPLE__
7695 /*
7696 * Step 2 of weak-linking support on Mac OS X.
7697 *
7698 * The code below removes functions that are not available on the
7699 * currently active platform.
7700 *
7701 * This block allow one to use a python binary that was build on
7702 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7703 * OSX 10.4.
7704 */
7705#ifdef HAVE_FSTATVFS
7706 if (fstatvfs == NULL) {
7707 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007708 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007709 }
7710 }
7711#endif /* HAVE_FSTATVFS */
7712
7713#ifdef HAVE_STATVFS
7714 if (statvfs == NULL) {
7715 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007716 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007717 }
7718 }
7719#endif /* HAVE_STATVFS */
7720
7721# ifdef HAVE_LCHOWN
7722 if (lchown == NULL) {
7723 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007724 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007725 }
7726 }
7727#endif /* HAVE_LCHOWN */
7728
7729
7730#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007731 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007732
Guido van Rossumb6775db1994-08-01 11:34:53 +00007733}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007734
7735#ifdef __cplusplus
7736}
7737#endif