blob: dda758f44579b0def471d033c69e0fa036df8785 [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 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * 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
Victor Stinner8c62be82010-05-06 00:08:46 +000072#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000073#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
Victor Stinner8c62be82010-05-06 00:08:46 +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
Victor Stinner8c62be82010-05-06 00:08:46 +0000107#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#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
Victor Stinner8c62be82010-05-06 00:08:46 +0000114#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#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
Victor Stinner8c62be82010-05-06 00:08:46 +0000119#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120#define HAVE_WAIT 1
121#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000127#define HAVE_SYSTEM 1
128#define HAVE_CWAIT 1
129#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000130#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 */
Victor Stinner8c62be82010-05-06 00:08:46 +0000134#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000139#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
Victor Stinner8c62be82010-05-06 00:08:46 +0000150#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +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
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000265#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000266#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000267#endif
268#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000269#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000270#endif
271#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000272#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000273#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000274#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000275#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000277#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000278#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279
Guido van Rossumd48f2521997-12-05 22:19:34 +0000280#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000282#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283
Tim Petersbc2e10e2002-03-03 23:17:02 +0000284#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000285#if defined(PATH_MAX) && PATH_MAX > 1024
286#define MAXPATHLEN PATH_MAX
287#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000289#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000290#endif /* MAXPATHLEN */
291
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000292#ifdef UNION_WAIT
293/* Emulate some macros on systems that have a union instead of macros */
294
295#ifndef WIFEXITED
296#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
297#endif
298
299#ifndef WEXITSTATUS
300#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
301#endif
302
303#ifndef WTERMSIG
304#define WTERMSIG(u_wait) ((u_wait).w_termsig)
305#endif
306
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307#define WAIT_TYPE union wait
308#define WAIT_STATUS_INT(s) (s.w_status)
309
310#else /* !UNION_WAIT */
311#define WAIT_TYPE int
312#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000313#endif /* UNION_WAIT */
314
Greg Wardb48bc172000-03-01 21:51:56 +0000315/* Don't use the "_r" form if we don't need it (also, won't have a
316 prototype for it, at least on Solaris -- maybe others as well?). */
317#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
318#define USE_CTERMID_R
319#endif
320
Fred Drake699f3522000-06-29 21:12:41 +0000321/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000322#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000323#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000324# define STAT win32_stat
325# define FSTAT win32_fstat
326# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000327#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000328# define STAT stat
329# define FSTAT fstat
330# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000331#endif
332
Tim Peters11b23062003-04-23 02:39:17 +0000333#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000334#include <sys/mkdev.h>
335#else
336#if defined(MAJOR_IN_SYSMACROS)
337#include <sys/sysmacros.h>
338#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000339#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
340#include <sys/mkdev.h>
341#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000342#endif
Fred Drake699f3522000-06-29 21:12:41 +0000343
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000344#if defined _MSC_VER && _MSC_VER >= 1400
345/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
346 * valid and throw an assertion if it isn't.
347 * Normally, an invalid fd is likely to be a C program error and therefore
348 * an assertion can be useful, but it does contradict the POSIX standard
349 * which for write(2) states:
350 * "Otherwise, -1 shall be returned and errno set to indicate the error."
351 * "[EBADF] The fildes argument is not a valid file descriptor open for
352 * writing."
353 * Furthermore, python allows the user to enter any old integer
354 * as a fd and should merely raise a python exception on error.
355 * The Microsoft CRT doesn't provide an official way to check for the
356 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000357 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000358 * internal structures involved.
359 * The structures below must be updated for each version of visual studio
360 * according to the file internal.h in the CRT source, until MS comes
361 * up with a less hacky way to do this.
362 * (all of this is to avoid globally modifying the CRT behaviour using
363 * _set_invalid_parameter_handler() and _CrtSetReportMode())
364 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000365/* The actual size of the structure is determined at runtime.
366 * Only the first items must be present.
367 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000368typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000369 intptr_t osfhnd;
370 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000371} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000372
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000373extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000374#define IOINFO_L2E 5
375#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
376#define IOINFO_ARRAYS 64
377#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
378#define FOPEN 0x01
379#define _NO_CONSOLE_FILENO (intptr_t)-2
380
381/* This function emulates what the windows CRT does to validate file handles */
382int
383_PyVerify_fd(int fd)
384{
Victor Stinner8c62be82010-05-06 00:08:46 +0000385 const int i1 = fd >> IOINFO_L2E;
386 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000387
Antoine Pitrou22e41552010-08-15 18:07:50 +0000388 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000389
Victor Stinner8c62be82010-05-06 00:08:46 +0000390 /* Determine the actual size of the ioinfo structure,
391 * as used by the CRT loaded in memory
392 */
393 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
394 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
395 }
396 if (sizeof_ioinfo == 0) {
397 /* This should not happen... */
398 goto fail;
399 }
400
401 /* See that it isn't a special CLEAR fileno */
402 if (fd != _NO_CONSOLE_FILENO) {
403 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
404 * we check pointer validity and other info
405 */
406 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
407 /* finally, check that the file is open */
408 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
409 if (info->osfile & FOPEN) {
410 return 1;
411 }
412 }
413 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000414 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000415 errno = EBADF;
416 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000417}
418
419/* the special case of checking dup2. The target fd must be in a sensible range */
420static int
421_PyVerify_fd_dup2(int fd1, int fd2)
422{
Victor Stinner8c62be82010-05-06 00:08:46 +0000423 if (!_PyVerify_fd(fd1))
424 return 0;
425 if (fd2 == _NO_CONSOLE_FILENO)
426 return 0;
427 if ((unsigned)fd2 < _NHANDLE_)
428 return 1;
429 else
430 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000431}
432#else
433/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
434#define _PyVerify_fd_dup2(A, B) (1)
435#endif
436
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000438#ifdef WITH_NEXT_FRAMEWORK
439/* On Darwin/MacOSX a shared library or framework has no access to
440** environ directly, we must obtain it with _NSGetEnviron().
441*/
442#include <crt_externs.h>
443static char **environ;
444#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000446#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447
Barry Warsaw53699e91996-12-10 23:23:01 +0000448static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000449convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450{
Victor Stinner8c62be82010-05-06 00:08:46 +0000451 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000452#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000453 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000454#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000455 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000456#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000457#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000458 APIRET rc;
459 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
460#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000461
Victor Stinner8c62be82010-05-06 00:08:46 +0000462 d = PyDict_New();
463 if (d == NULL)
464 return NULL;
465#ifdef WITH_NEXT_FRAMEWORK
466 if (environ == NULL)
467 environ = *_NSGetEnviron();
468#endif
469#ifdef MS_WINDOWS
470 /* _wenviron must be initialized in this way if the program is started
471 through main() instead of wmain(). */
472 _wgetenv(L"");
473 if (_wenviron == NULL)
474 return d;
475 /* This part ignores errors */
476 for (e = _wenviron; *e != NULL; e++) {
477 PyObject *k;
478 PyObject *v;
479 wchar_t *p = wcschr(*e, L'=');
480 if (p == NULL)
481 continue;
482 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
483 if (k == NULL) {
484 PyErr_Clear();
485 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000486 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000487 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
488 if (v == NULL) {
489 PyErr_Clear();
490 Py_DECREF(k);
491 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000492 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000493 if (PyDict_GetItem(d, k) == NULL) {
494 if (PyDict_SetItem(d, k, v) != 0)
495 PyErr_Clear();
496 }
497 Py_DECREF(k);
498 Py_DECREF(v);
499 }
500#else
501 if (environ == NULL)
502 return d;
503 /* This part ignores errors */
504 for (e = environ; *e != NULL; e++) {
505 PyObject *k;
506 PyObject *v;
507 char *p = strchr(*e, '=');
508 if (p == NULL)
509 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000510 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000511 if (k == NULL) {
512 PyErr_Clear();
513 continue;
514 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000515 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000516 if (v == NULL) {
517 PyErr_Clear();
518 Py_DECREF(k);
519 continue;
520 }
521 if (PyDict_GetItem(d, k) == NULL) {
522 if (PyDict_SetItem(d, k, v) != 0)
523 PyErr_Clear();
524 }
525 Py_DECREF(k);
526 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000527 }
528#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000529#if defined(PYOS_OS2)
530 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
531 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
532 PyObject *v = PyBytes_FromString(buffer);
533 PyDict_SetItemString(d, "BEGINLIBPATH", v);
534 Py_DECREF(v);
535 }
536 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
537 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
538 PyObject *v = PyBytes_FromString(buffer);
539 PyDict_SetItemString(d, "ENDLIBPATH", v);
540 Py_DECREF(v);
541 }
542#endif
543 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544}
545
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546/* Set a POSIX-specific error from errno, and return NULL */
547
Barry Warsawd58d7641998-07-23 16:14:40 +0000548static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000549posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000550{
Victor Stinner8c62be82010-05-06 00:08:46 +0000551 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552}
Barry Warsawd58d7641998-07-23 16:14:40 +0000553static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000554posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000555{
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000557}
558
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000559#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000560static PyObject *
561posix_error_with_unicode_filename(Py_UNICODE* name)
562{
Victor Stinner8c62be82010-05-06 00:08:46 +0000563 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000564}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000565#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000566
567
Mark Hammondef8b6542001-05-13 08:04:26 +0000568static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000569posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000570{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000571 PyObject *name_str, *rc;
572 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
573 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000574 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000575 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
576 name_str);
577 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000578 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000579}
580
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000581#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000582static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000583win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000584{
Victor Stinner8c62be82010-05-06 00:08:46 +0000585 /* XXX We should pass the function name along in the future.
586 (winreg.c also wants to pass the function name.)
587 This would however require an additional param to the
588 Windows error object, which is non-trivial.
589 */
590 errno = GetLastError();
591 if (filename)
592 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
593 else
594 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000595}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000596
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000597static PyObject *
598win32_error_unicode(char* function, Py_UNICODE* filename)
599{
Victor Stinner8c62be82010-05-06 00:08:46 +0000600 /* XXX - see win32_error for comments on 'function' */
601 errno = GetLastError();
602 if (filename)
603 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
604 else
605 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000606}
607
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000609convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000610{
Victor Stinner8c62be82010-05-06 00:08:46 +0000611 if (PyUnicode_CheckExact(*param))
612 Py_INCREF(*param);
613 else if (PyUnicode_Check(*param))
614 /* For a Unicode subtype that's not a Unicode object,
615 return a true Unicode object with the same data. */
616 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
617 PyUnicode_GET_SIZE(*param));
618 else
619 *param = PyUnicode_FromEncodedObject(*param,
620 Py_FileSystemDefaultEncoding,
621 "strict");
622 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623}
624
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000625#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626
Guido van Rossumd48f2521997-12-05 22:19:34 +0000627#if defined(PYOS_OS2)
628/**********************************************************************
629 * Helper Function to Trim and Format OS/2 Messages
630 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000631static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000632os2_formatmsg(char *msgbuf, int msglen, char *reason)
633{
634 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
635
636 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
637 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
638
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000639 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000640 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
641 }
642
643 /* Add Optional Reason Text */
644 if (reason) {
645 strcat(msgbuf, " : ");
646 strcat(msgbuf, reason);
647 }
648}
649
650/**********************************************************************
651 * Decode an OS/2 Operating System Error Code
652 *
653 * A convenience function to lookup an OS/2 error code and return a
654 * text message we can use to raise a Python exception.
655 *
656 * Notes:
657 * The messages for errors returned from the OS/2 kernel reside in
658 * the file OSO001.MSG in the \OS2 directory hierarchy.
659 *
660 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000661static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000662os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
663{
664 APIRET rc;
665 ULONG msglen;
666
667 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
668 Py_BEGIN_ALLOW_THREADS
669 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
670 errorcode, "oso001.msg", &msglen);
671 Py_END_ALLOW_THREADS
672
673 if (rc == NO_ERROR)
674 os2_formatmsg(msgbuf, msglen, reason);
675 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000676 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000677 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000678
679 return msgbuf;
680}
681
682/* Set an OS/2-specific error and return NULL. OS/2 kernel
683 errors are not in a global variable e.g. 'errno' nor are
684 they congruent with posix error numbers. */
685
Victor Stinner8c62be82010-05-06 00:08:46 +0000686static PyObject *
687os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000688{
689 char text[1024];
690 PyObject *v;
691
692 os2_strerror(text, sizeof(text), code, "");
693
694 v = Py_BuildValue("(is)", code, text);
695 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000696 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000697 Py_DECREF(v);
698 }
699 return NULL; /* Signal to Python that an Exception is Pending */
700}
701
702#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703
704/* POSIX generic methods */
705
Barry Warsaw53699e91996-12-10 23:23:01 +0000706static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000707posix_fildes(PyObject *fdobj, int (*func)(int))
708{
Victor Stinner8c62be82010-05-06 00:08:46 +0000709 int fd;
710 int res;
711 fd = PyObject_AsFileDescriptor(fdobj);
712 if (fd < 0)
713 return NULL;
714 if (!_PyVerify_fd(fd))
715 return posix_error();
716 Py_BEGIN_ALLOW_THREADS
717 res = (*func)(fd);
718 Py_END_ALLOW_THREADS
719 if (res < 0)
720 return posix_error();
721 Py_INCREF(Py_None);
722 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000723}
Guido van Rossum21142a01999-01-08 21:05:37 +0000724
725static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000726posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727{
Victor Stinner8c62be82010-05-06 00:08:46 +0000728 PyObject *opath1 = NULL;
729 char *path1;
730 int res;
731 if (!PyArg_ParseTuple(args, format,
732 PyUnicode_FSConverter, &opath1))
733 return NULL;
734 path1 = PyBytes_AsString(opath1);
735 Py_BEGIN_ALLOW_THREADS
736 res = (*func)(path1);
737 Py_END_ALLOW_THREADS
738 if (res < 0)
739 return posix_error_with_allocated_filename(opath1);
740 Py_DECREF(opath1);
741 Py_INCREF(Py_None);
742 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000743}
744
Barry Warsaw53699e91996-12-10 23:23:01 +0000745static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000746posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000747 char *format,
748 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000749{
Victor Stinner8c62be82010-05-06 00:08:46 +0000750 PyObject *opath1 = NULL, *opath2 = NULL;
751 char *path1, *path2;
752 int res;
753 if (!PyArg_ParseTuple(args, format,
754 PyUnicode_FSConverter, &opath1,
755 PyUnicode_FSConverter, &opath2)) {
756 return NULL;
757 }
758 path1 = PyBytes_AsString(opath1);
759 path2 = PyBytes_AsString(opath2);
760 Py_BEGIN_ALLOW_THREADS
761 res = (*func)(path1, path2);
762 Py_END_ALLOW_THREADS
763 Py_DECREF(opath1);
764 Py_DECREF(opath2);
765 if (res != 0)
766 /* XXX how to report both path1 and path2??? */
767 return posix_error();
768 Py_INCREF(Py_None);
769 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770}
771
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000772#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000773static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000774win32_1str(PyObject* args, char* func,
775 char* format, BOOL (__stdcall *funcA)(LPCSTR),
776 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777{
Victor Stinner8c62be82010-05-06 00:08:46 +0000778 PyObject *uni;
779 char *ansi;
780 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000781
Victor Stinner8c62be82010-05-06 00:08:46 +0000782 if (!PyArg_ParseTuple(args, wformat, &uni))
783 PyErr_Clear();
784 else {
785 Py_BEGIN_ALLOW_THREADS
786 result = funcW(PyUnicode_AsUnicode(uni));
787 Py_END_ALLOW_THREADS
788 if (!result)
789 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
790 Py_INCREF(Py_None);
791 return Py_None;
792 }
793 if (!PyArg_ParseTuple(args, format, &ansi))
794 return NULL;
795 Py_BEGIN_ALLOW_THREADS
796 result = funcA(ansi);
797 Py_END_ALLOW_THREADS
798 if (!result)
799 return win32_error(func, ansi);
800 Py_INCREF(Py_None);
801 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802
803}
804
805/* This is a reimplementation of the C library's chdir function,
806 but one that produces Win32 errors instead of DOS error codes.
807 chdir is essentially a wrapper around SetCurrentDirectory; however,
808 it also needs to set "magic" environment variables indicating
809 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000810static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811win32_chdir(LPCSTR path)
812{
Victor Stinner8c62be82010-05-06 00:08:46 +0000813 char new_path[MAX_PATH+1];
814 int result;
815 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000816
Victor Stinner8c62be82010-05-06 00:08:46 +0000817 if(!SetCurrentDirectoryA(path))
818 return FALSE;
819 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
820 if (!result)
821 return FALSE;
822 /* In the ANSI API, there should not be any paths longer
823 than MAX_PATH. */
824 assert(result <= MAX_PATH+1);
825 if (strncmp(new_path, "\\\\", 2) == 0 ||
826 strncmp(new_path, "//", 2) == 0)
827 /* UNC path, nothing to do. */
828 return TRUE;
829 env[1] = new_path[0];
830 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831}
832
833/* The Unicode version differs from the ANSI version
834 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000835static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000836win32_wchdir(LPCWSTR path)
837{
Victor Stinner8c62be82010-05-06 00:08:46 +0000838 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
839 int result;
840 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841
Victor Stinner8c62be82010-05-06 00:08:46 +0000842 if(!SetCurrentDirectoryW(path))
843 return FALSE;
844 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
845 if (!result)
846 return FALSE;
847 if (result > MAX_PATH+1) {
848 new_path = malloc(result * sizeof(wchar_t));
849 if (!new_path) {
850 SetLastError(ERROR_OUTOFMEMORY);
851 return FALSE;
852 }
853 result = GetCurrentDirectoryW(result, new_path);
854 if (!result) {
855 free(new_path);
856 return FALSE;
857 }
858 }
859 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
860 wcsncmp(new_path, L"//", 2) == 0)
861 /* UNC path, nothing to do. */
862 return TRUE;
863 env[1] = new_path[0];
864 result = SetEnvironmentVariableW(env, new_path);
865 if (new_path != _new_path)
866 free(new_path);
867 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000868}
869#endif
870
Martin v. Löwis14694662006-02-03 12:54:16 +0000871#ifdef MS_WINDOWS
872/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
873 - time stamps are restricted to second resolution
874 - file modification times suffer from forth-and-back conversions between
875 UTC and local time
876 Therefore, we implement our own stat, based on the Win32 API directly.
877*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000878#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000879
880struct win32_stat{
881 int st_dev;
882 __int64 st_ino;
883 unsigned short st_mode;
884 int st_nlink;
885 int st_uid;
886 int st_gid;
887 int st_rdev;
888 __int64 st_size;
889 int st_atime;
890 int st_atime_nsec;
891 int st_mtime;
892 int st_mtime_nsec;
893 int st_ctime;
894 int st_ctime_nsec;
895};
896
897static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
898
899static void
900FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
901{
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
903 /* Cannot simply cast and dereference in_ptr,
904 since it might not be aligned properly */
905 __int64 in;
906 memcpy(&in, in_ptr, sizeof(in));
907 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
908 /* XXX Win32 supports time stamps past 2038; we currently don't */
909 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000910}
911
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912static void
913time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
914{
Victor Stinner8c62be82010-05-06 00:08:46 +0000915 /* XXX endianness */
916 __int64 out;
917 out = time_in + secs_between_epochs;
918 out = out * 10000000 + nsec_in / 100;
919 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920}
921
Martin v. Löwis14694662006-02-03 12:54:16 +0000922/* Below, we *know* that ugo+r is 0444 */
923#if _S_IREAD != 0400
924#error Unsupported C library
925#endif
926static int
927attributes_to_mode(DWORD attr)
928{
Victor Stinner8c62be82010-05-06 00:08:46 +0000929 int m = 0;
930 if (attr & FILE_ATTRIBUTE_DIRECTORY)
931 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
932 else
933 m |= _S_IFREG;
934 if (attr & FILE_ATTRIBUTE_READONLY)
935 m |= 0444;
936 else
937 m |= 0666;
938 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000939}
940
941static int
942attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
943{
Victor Stinner8c62be82010-05-06 00:08:46 +0000944 memset(result, 0, sizeof(*result));
945 result->st_mode = attributes_to_mode(info->dwFileAttributes);
946 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
947 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
948 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
949 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000950
Victor Stinner8c62be82010-05-06 00:08:46 +0000951 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000952}
953
Guido van Rossumd8faa362007-04-27 19:54:29 +0000954static BOOL
955attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
956{
Victor Stinner8c62be82010-05-06 00:08:46 +0000957 HANDLE hFindFile;
958 WIN32_FIND_DATAA FileData;
959 hFindFile = FindFirstFileA(pszFile, &FileData);
960 if (hFindFile == INVALID_HANDLE_VALUE)
961 return FALSE;
962 FindClose(hFindFile);
963 pfad->dwFileAttributes = FileData.dwFileAttributes;
964 pfad->ftCreationTime = FileData.ftCreationTime;
965 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
966 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
967 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
968 pfad->nFileSizeLow = FileData.nFileSizeLow;
969 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000970}
971
972static BOOL
973attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
974{
Victor Stinner8c62be82010-05-06 00:08:46 +0000975 HANDLE hFindFile;
976 WIN32_FIND_DATAW FileData;
977 hFindFile = FindFirstFileW(pszFile, &FileData);
978 if (hFindFile == INVALID_HANDLE_VALUE)
979 return FALSE;
980 FindClose(hFindFile);
981 pfad->dwFileAttributes = FileData.dwFileAttributes;
982 pfad->ftCreationTime = FileData.ftCreationTime;
983 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
984 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
985 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
986 pfad->nFileSizeLow = FileData.nFileSizeLow;
987 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000988}
989
Brian Curtind40e6f72010-07-08 21:39:08 +0000990/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
991 win32_stat_w
992
993 In Posix, stat automatically traverses symlinks and returns the stat
994 structure for the target. In Windows, the equivalent GetFileAttributes by
995 default does not traverse symlinks and instead returns attributes for
996 the symlink.
997
998 Therefore, win32_lstat will get the attributes traditionally, and
999 win32_stat will first explicitly resolve the symlink target and then will
1000 call win32_lstat on that result.
1001
1002 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1003
1004static int
1005win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001006{
Victor Stinner8c62be82010-05-06 00:08:46 +00001007 WIN32_FILE_ATTRIBUTE_DATA info;
1008 int code;
1009 char *dot;
Brian Curtind40e6f72010-07-08 21:39:08 +00001010 WIN32_FIND_DATAA find_data;
1011 HANDLE find_data_handle;
Victor Stinner8c62be82010-05-06 00:08:46 +00001012 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
1013 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1014 /* Protocol violation: we explicitly clear errno, instead of
1015 setting it to a POSIX error. Callers should use GetLastError. */
1016 errno = 0;
1017 return -1;
1018 } else {
1019 /* Could not get attributes on open file. Fall back to
1020 reading the directory. */
1021 if (!attributes_from_dir(path, &info)) {
1022 /* Very strange. This should not fail now */
1023 errno = 0;
1024 return -1;
1025 }
1026 }
1027 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001028
Victor Stinner8c62be82010-05-06 00:08:46 +00001029 code = attribute_data_to_stat(&info, result);
1030 if (code != 0)
1031 return code;
Brian Curtind40e6f72010-07-08 21:39:08 +00001032
1033 /* Get WIN32_FIND_DATA structure for the path to determine if
1034 it is a symlink */
1035 if(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1036 find_data_handle = FindFirstFileA(path, &find_data);
1037 if(find_data_handle != INVALID_HANDLE_VALUE) {
1038 if(find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
1039 /* first clear the S_IFMT bits */
1040 result->st_mode ^= (result->st_mode & 0170000);
1041 /* now set the bits that make this a symlink */
1042 result->st_mode |= 0120000;
1043 }
1044 FindClose(find_data_handle);
1045 }
1046 }
1047
Victor Stinner8c62be82010-05-06 00:08:46 +00001048 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1049 dot = strrchr(path, '.');
1050 if (dot) {
Brian Curtind40e6f72010-07-08 21:39:08 +00001051 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1052 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00001053 result->st_mode |= 0111;
1054 }
1055 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001056}
1057
Victor Stinner8c62be82010-05-06 00:08:46 +00001058static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001059win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001060{
Victor Stinner8c62be82010-05-06 00:08:46 +00001061 int code;
1062 const wchar_t *dot;
1063 WIN32_FILE_ATTRIBUTE_DATA info;
Brian Curtind40e6f72010-07-08 21:39:08 +00001064 WIN32_FIND_DATAW find_data;
1065 HANDLE find_data_handle;
Victor Stinner8c62be82010-05-06 00:08:46 +00001066 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1067 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1068 /* Protocol violation: we explicitly clear errno, instead of
1069 setting it to a POSIX error. Callers should use GetLastError. */
1070 errno = 0;
1071 return -1;
1072 } else {
Brian Curtind40e6f72010-07-08 21:39:08 +00001073 /* Could not get attributes on open file. Fall back to reading
1074 the directory. */
1075 if (!attributes_from_dir_w(path, &info)) {
1076 /* Very strange. This should not fail now */
1077 errno = 0;
1078 return -1;
1079 }
1080 }
1081 }
1082 code = attribute_data_to_stat(&info, result);
1083 if (code < 0)
1084 return code;
1085
1086 /* Get WIN32_FIND_DATA structure for the path to determine if
1087 it is a symlink */
1088 if(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1089 find_data_handle = FindFirstFileW(path, &find_data);
1090 if(find_data_handle != INVALID_HANDLE_VALUE) {
1091 if(find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
1092 /* first clear the S_IFMT bits */
1093 result->st_mode ^= (result->st_mode & 0170000);
1094 /* now set the bits that make this a symlink */
1095 result->st_mode |= 0120000;
1096 }
1097 FindClose(find_data_handle);
1098 }
1099 }
1100
1101 /* Set IFEXEC if it is an .exe, .bat, ... */
1102 dot = wcsrchr(path, '.');
1103 if (dot) {
1104 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1105 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1106 result->st_mode |= 0111;
1107 }
1108 return code;
1109}
1110
1111/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1112static int has_GetFinalPathNameByHandle = 0;
1113static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1114 DWORD);
1115static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1116 DWORD);
1117static int
1118check_GetFinalPathNameByHandle()
1119{
1120 HINSTANCE hKernel32;
1121 /* only recheck */
1122 if (!has_GetFinalPathNameByHandle)
1123 {
1124 hKernel32 = GetModuleHandle("KERNEL32");
1125 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1126 "GetFinalPathNameByHandleA");
1127 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1128 "GetFinalPathNameByHandleW");
Brian Curtin74e45612010-07-09 15:58:59 +00001129 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1130 Py_GetFinalPathNameByHandleW;
Brian Curtind40e6f72010-07-08 21:39:08 +00001131 }
1132 return has_GetFinalPathNameByHandle;
1133}
1134
1135static int
1136win32_stat(const char* path, struct win32_stat *result)
1137{
1138 /* Traverse the symlink to the target using
1139 GetFinalPathNameByHandle()
1140 */
1141 int code;
1142 HANDLE hFile;
1143 int buf_size;
1144 char *target_path;
1145 int result_length;
1146 WIN32_FILE_ATTRIBUTE_DATA info;
1147
1148 if(!check_GetFinalPathNameByHandle()) {
1149 /* if the OS doesn't have GetFinalPathNameByHandle, it doesn't
1150 have symlinks, so just fall back to the traditional behavior
1151 found in lstat. */
1152 return win32_lstat(path, result);
1153 }
1154
1155 hFile = CreateFileA(
1156 path,
1157 0, /* desired access */
1158 0, /* share mode */
1159 NULL, /* security attributes */
1160 OPEN_EXISTING,
1161 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1162 FILE_FLAG_BACKUP_SEMANTICS,
1163 NULL);
1164
1165 if(hFile == INVALID_HANDLE_VALUE) {
1166 /* Either the target doesn't exist, or we don't have access to
1167 get a handle to it. If the former, we need to return an error.
1168 If the latter, we can use attributes_from_dir. */
1169 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1170 /* Protocol violation: we explicitly clear errno, instead of
1171 setting it to a POSIX error. Callers should use GetLastError. */
1172 errno = 0;
1173 return -1;
1174 } else {
1175 /* Could not get attributes on open file. Fall back to
1176 reading the directory. */
1177 if (!attributes_from_dir(path, &info)) {
1178 /* Very strange. This should not fail now */
1179 errno = 0;
1180 return -1;
1181 }
1182 }
1183 code = attribute_data_to_stat(&info, result);
1184 }
1185
1186 buf_size = Py_GetFinalPathNameByHandleA(hFile, 0, 0, VOLUME_NAME_DOS);
1187 if(!buf_size) return -1;
1188 target_path = (char *)malloc((buf_size+1)*sizeof(char));
1189 result_length = Py_GetFinalPathNameByHandleA(hFile, target_path,
1190 buf_size, VOLUME_NAME_DOS);
1191
1192 if(!result_length)
1193 return -1;
1194
1195 if(!CloseHandle(hFile))
1196 return -1;
1197
1198 target_path[result_length] = 0;
1199 code = win32_lstat(target_path, result);
1200 free(target_path);
1201
1202 return code;
1203}
1204
1205static int
1206win32_stat_w(const wchar_t* path, struct win32_stat *result)
1207{
1208 /* Traverse the symlink to the target using GetFinalPathNameByHandle() */
1209 int code;
1210 HANDLE hFile;
1211 int buf_size;
1212 wchar_t *target_path;
1213 int result_length;
1214 WIN32_FILE_ATTRIBUTE_DATA info;
1215
1216 if(!check_GetFinalPathNameByHandle()) {
1217 /* If the OS doesn't have GetFinalPathNameByHandle, it doesn't have
1218 symlinks, so just fall back to the traditional behavior found
1219 in lstat. */
1220 return win32_lstat_w(path, result);
1221 }
1222
1223 hFile = CreateFileW(
1224 path,
1225 0, /* desired access */
1226 0, /* share mode */
1227 NULL, /* security attributes */
1228 OPEN_EXISTING,
1229 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1230 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1231 NULL);
Antoine Pitroub73caab2010-08-09 23:39:31 +00001232
Brian Curtind40e6f72010-07-08 21:39:08 +00001233 if(hFile == INVALID_HANDLE_VALUE) {
1234 /* Either the target doesn't exist, or we don't have access to
1235 get a handle to it. If the former, we need to return an error.
1236 If the latter, we can use attributes_from_dir. */
1237 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1238 /* Protocol violation: we explicitly clear errno, instead of
1239 setting it to a POSIX error. Callers should use GetLastError. */
1240 errno = 0;
1241 return -1;
1242 } else {
Victor Stinner8c62be82010-05-06 00:08:46 +00001243 /* Could not get attributes on open file. Fall back to
1244 reading the directory. */
1245 if (!attributes_from_dir_w(path, &info)) {
1246 /* Very strange. This should not fail now */
1247 errno = 0;
1248 return -1;
1249 }
1250 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001251 code = attribute_data_to_stat(&info, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001252 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001253 else {
1254 /* We have a good handle to the target, use it to determine the target
1255 path name (then we'll call lstat on it). */
1256 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_DOS);
1257 if(!buf_size)
1258 return -1;
1259
1260 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
1261 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
1262 buf_size, VOLUME_NAME_DOS);
1263
1264 if(!result_length)
1265 return -1;
1266
1267 if(!CloseHandle(hFile))
1268 return -1;
1269
1270 target_path[result_length] = 0;
1271 code = win32_lstat_w(target_path, result);
1272 free(target_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001273 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001274
Victor Stinner8c62be82010-05-06 00:08:46 +00001275 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001276}
1277
1278static int
1279win32_fstat(int file_number, struct win32_stat *result)
1280{
Victor Stinner8c62be82010-05-06 00:08:46 +00001281 BY_HANDLE_FILE_INFORMATION info;
1282 HANDLE h;
1283 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001284
Victor Stinner8c62be82010-05-06 00:08:46 +00001285 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001286
Victor Stinner8c62be82010-05-06 00:08:46 +00001287 /* Protocol violation: we explicitly clear errno, instead of
1288 setting it to a POSIX error. Callers should use GetLastError. */
1289 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001290
Victor Stinner8c62be82010-05-06 00:08:46 +00001291 if (h == INVALID_HANDLE_VALUE) {
1292 /* This is really a C library error (invalid file handle).
1293 We set the Win32 error to the closes one matching. */
1294 SetLastError(ERROR_INVALID_HANDLE);
1295 return -1;
1296 }
1297 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001298
Victor Stinner8c62be82010-05-06 00:08:46 +00001299 type = GetFileType(h);
1300 if (type == FILE_TYPE_UNKNOWN) {
1301 DWORD error = GetLastError();
1302 if (error != 0) {
1303 return -1;
1304 }
1305 /* else: valid but unknown file */
1306 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001307
Victor Stinner8c62be82010-05-06 00:08:46 +00001308 if (type != FILE_TYPE_DISK) {
1309 if (type == FILE_TYPE_CHAR)
1310 result->st_mode = _S_IFCHR;
1311 else if (type == FILE_TYPE_PIPE)
1312 result->st_mode = _S_IFIFO;
1313 return 0;
1314 }
1315
1316 if (!GetFileInformationByHandle(h, &info)) {
1317 return -1;
1318 }
1319
1320 /* similar to stat() */
1321 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1322 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
Brian Curtin74e45612010-07-09 15:58:59 +00001323 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime,
1324 &result->st_ctime_nsec);
1325 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime,
1326 &result->st_mtime_nsec);
1327 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime,
1328 &result->st_atime_nsec);
Victor Stinner8c62be82010-05-06 00:08:46 +00001329 /* specific to fstat() */
1330 result->st_nlink = info.nNumberOfLinks;
1331 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1332 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001333}
1334
1335#endif /* MS_WINDOWS */
1336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001338"stat_result: Result from stat or lstat.\n\n\
1339This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001340 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001341or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1342\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001343Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1344or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001345\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001346See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001347
1348static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001349 {"st_mode", "protection bits"},
1350 {"st_ino", "inode"},
1351 {"st_dev", "device"},
1352 {"st_nlink", "number of hard links"},
1353 {"st_uid", "user ID of owner"},
1354 {"st_gid", "group ID of owner"},
1355 {"st_size", "total size, in bytes"},
1356 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1357 {NULL, "integer time of last access"},
1358 {NULL, "integer time of last modification"},
1359 {NULL, "integer time of last change"},
1360 {"st_atime", "time of last access"},
1361 {"st_mtime", "time of last modification"},
1362 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001363#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001364 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001365#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001366#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001367 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001368#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001369#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001370 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001371#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001372#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001373 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001374#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001375#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001376 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001377#endif
1378#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001379 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001380#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001381 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001382};
1383
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001384#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001385#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001386#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001387#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001388#endif
1389
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001390#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001391#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1392#else
1393#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1394#endif
1395
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001396#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001397#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1398#else
1399#define ST_RDEV_IDX ST_BLOCKS_IDX
1400#endif
1401
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001402#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1403#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1404#else
1405#define ST_FLAGS_IDX ST_RDEV_IDX
1406#endif
1407
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001408#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001409#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001410#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001411#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001412#endif
1413
1414#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1415#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1416#else
1417#define ST_BIRTHTIME_IDX ST_GEN_IDX
1418#endif
1419
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001420static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001421 "stat_result", /* name */
1422 stat_result__doc__, /* doc */
1423 stat_result_fields,
1424 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001425};
1426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001427PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001428"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1429This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001430 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001431or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001432\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001433See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001434
1435static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 {"f_bsize", },
1437 {"f_frsize", },
1438 {"f_blocks", },
1439 {"f_bfree", },
1440 {"f_bavail", },
1441 {"f_files", },
1442 {"f_ffree", },
1443 {"f_favail", },
1444 {"f_flag", },
1445 {"f_namemax",},
1446 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001447};
1448
1449static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001450 "statvfs_result", /* name */
1451 statvfs_result__doc__, /* doc */
1452 statvfs_result_fields,
1453 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001454};
1455
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001456static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001457static PyTypeObject StatResultType;
1458static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001459static newfunc structseq_new;
1460
1461static PyObject *
1462statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1463{
Victor Stinner8c62be82010-05-06 00:08:46 +00001464 PyStructSequence *result;
1465 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001466
Victor Stinner8c62be82010-05-06 00:08:46 +00001467 result = (PyStructSequence*)structseq_new(type, args, kwds);
1468 if (!result)
1469 return NULL;
1470 /* If we have been initialized from a tuple,
1471 st_?time might be set to None. Initialize it
1472 from the int slots. */
1473 for (i = 7; i <= 9; i++) {
1474 if (result->ob_item[i+3] == Py_None) {
1475 Py_DECREF(Py_None);
1476 Py_INCREF(result->ob_item[i]);
1477 result->ob_item[i+3] = result->ob_item[i];
1478 }
1479 }
1480 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001481}
1482
1483
1484
1485/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001486static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001487
1488PyDoc_STRVAR(stat_float_times__doc__,
1489"stat_float_times([newval]) -> oldval\n\n\
1490Determine whether os.[lf]stat represents time stamps as float objects.\n\
1491If newval is True, future calls to stat() return floats, if it is False,\n\
1492future calls return ints. \n\
1493If newval is omitted, return the current setting.\n");
1494
1495static PyObject*
1496stat_float_times(PyObject* self, PyObject *args)
1497{
Victor Stinner8c62be82010-05-06 00:08:46 +00001498 int newval = -1;
1499 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1500 return NULL;
1501 if (newval == -1)
1502 /* Return old value */
1503 return PyBool_FromLong(_stat_float_times);
1504 _stat_float_times = newval;
1505 Py_INCREF(Py_None);
1506 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001507}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001508
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001509static void
1510fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1511{
Victor Stinner8c62be82010-05-06 00:08:46 +00001512 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001513#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001514 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001515#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001516 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001517#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001518 if (!ival)
1519 return;
1520 if (_stat_float_times) {
1521 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1522 } else {
1523 fval = ival;
1524 Py_INCREF(fval);
1525 }
1526 PyStructSequence_SET_ITEM(v, index, ival);
1527 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001528}
1529
Tim Peters5aa91602002-01-30 05:46:57 +00001530/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001531 (used by posix_stat() and posix_fstat()) */
1532static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001533_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001534{
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 unsigned long ansec, mnsec, cnsec;
1536 PyObject *v = PyStructSequence_New(&StatResultType);
1537 if (v == NULL)
1538 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001539
Victor Stinner8c62be82010-05-06 00:08:46 +00001540 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001541#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001542 PyStructSequence_SET_ITEM(v, 1,
1543 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001544#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001545 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001546#endif
1547#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001548 PyStructSequence_SET_ITEM(v, 2,
1549 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001550#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001551 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001552#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001553 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1554 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1555 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001556#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001557 PyStructSequence_SET_ITEM(v, 6,
1558 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001559#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001560 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001561#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001562
Martin v. Löwis14694662006-02-03 12:54:16 +00001563#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001564 ansec = st->st_atim.tv_nsec;
1565 mnsec = st->st_mtim.tv_nsec;
1566 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001567#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001568 ansec = st->st_atimespec.tv_nsec;
1569 mnsec = st->st_mtimespec.tv_nsec;
1570 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001571#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001572 ansec = st->st_atime_nsec;
1573 mnsec = st->st_mtime_nsec;
1574 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001575#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001576 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001577#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 fill_time(v, 7, st->st_atime, ansec);
1579 fill_time(v, 8, st->st_mtime, mnsec);
1580 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001581
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001582#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001583 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1584 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001585#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001586#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001587 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1588 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001589#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001590#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1592 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001593#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001594#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1596 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001597#endif
1598#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 {
1600 PyObject *val;
1601 unsigned long bsec,bnsec;
1602 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001603#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001605#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001606 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001607#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 if (_stat_float_times) {
1609 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1610 } else {
1611 val = PyLong_FromLong((long)bsec);
1612 }
1613 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1614 val);
1615 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001616#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001617#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1619 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001620#endif
Fred Drake699f3522000-06-29 21:12:41 +00001621
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 if (PyErr_Occurred()) {
1623 Py_DECREF(v);
1624 return NULL;
1625 }
Fred Drake699f3522000-06-29 21:12:41 +00001626
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001628}
1629
Martin v. Löwisd8948722004-06-02 09:57:56 +00001630#ifdef MS_WINDOWS
1631
1632/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1633 where / can be used in place of \ and the trailing slash is optional.
1634 Both SERVER and SHARE must have at least one character.
1635*/
1636
1637#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1638#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001639#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001640#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001641#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001642
Tim Peters4ad82172004-08-30 17:02:04 +00001643static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001644IsUNCRootA(char *path, int pathlen)
1645{
Victor Stinner8c62be82010-05-06 00:08:46 +00001646 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001647
Victor Stinner8c62be82010-05-06 00:08:46 +00001648 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001649
Victor Stinner8c62be82010-05-06 00:08:46 +00001650 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1651 /* minimum UNCRoot is \\x\y */
1652 return FALSE;
1653 for (i = 2; i < pathlen ; i++)
1654 if (ISSLASH(path[i])) break;
1655 if (i == 2 || i == pathlen)
1656 /* do not allow \\\SHARE or \\SERVER */
1657 return FALSE;
1658 share = i+1;
1659 for (i = share; i < pathlen; i++)
1660 if (ISSLASH(path[i])) break;
1661 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001662
Victor Stinner8c62be82010-05-06 00:08:46 +00001663 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001664}
1665
Tim Peters4ad82172004-08-30 17:02:04 +00001666static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001667IsUNCRootW(Py_UNICODE *path, int pathlen)
1668{
Victor Stinner8c62be82010-05-06 00:08:46 +00001669 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001670
Victor Stinner8c62be82010-05-06 00:08:46 +00001671 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001672
Victor Stinner8c62be82010-05-06 00:08:46 +00001673 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1674 /* minimum UNCRoot is \\x\y */
1675 return FALSE;
1676 for (i = 2; i < pathlen ; i++)
1677 if (ISSLASH(path[i])) break;
1678 if (i == 2 || i == pathlen)
1679 /* do not allow \\\SHARE or \\SERVER */
1680 return FALSE;
1681 share = i+1;
1682 for (i = share; i < pathlen; i++)
1683 if (ISSLASH(path[i])) break;
1684 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001685
Victor Stinner8c62be82010-05-06 00:08:46 +00001686 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001687}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001688#endif /* MS_WINDOWS */
1689
Barry Warsaw53699e91996-12-10 23:23:01 +00001690static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001691posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001692 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001693#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001695#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001697#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 char *wformat,
1699 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001700{
Victor Stinner8c62be82010-05-06 00:08:46 +00001701 STRUCT_STAT st;
1702 PyObject *opath;
1703 char *path;
1704 int res;
1705 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001706
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001707#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001708 PyUnicodeObject *po;
1709 if (PyArg_ParseTuple(args, wformat, &po)) {
1710 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001711
Victor Stinner8c62be82010-05-06 00:08:46 +00001712 Py_BEGIN_ALLOW_THREADS
1713 /* PyUnicode_AS_UNICODE result OK without
1714 thread lock as it is a simple dereference. */
1715 res = wstatfunc(wpath, &st);
1716 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001717
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 if (res != 0)
1719 return win32_error_unicode("stat", wpath);
1720 return _pystat_fromstructstat(&st);
1721 }
1722 /* Drop the argument parsing error as narrow strings
1723 are also valid. */
1724 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001725#endif
1726
Victor Stinner8c62be82010-05-06 00:08:46 +00001727 if (!PyArg_ParseTuple(args, format,
1728 PyUnicode_FSConverter, &opath))
1729 return NULL;
1730 path = PyBytes_AsString(opath);
1731 Py_BEGIN_ALLOW_THREADS
1732 res = (*statfunc)(path, &st);
1733 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001734
Victor Stinner8c62be82010-05-06 00:08:46 +00001735 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001736#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001737 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001738#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001739 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001740#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 }
1742 else
1743 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001744
Victor Stinner8c62be82010-05-06 00:08:46 +00001745 Py_DECREF(opath);
1746 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001747}
1748
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001749/* POSIX methods */
1750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001751PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001752"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001753Use the real uid/gid to test for access to a path. Note that most\n\
1754operations will use the effective uid/gid, therefore this routine can\n\
1755be used in a suid/sgid environment to test if the invoking user has the\n\
1756specified access to the path. The mode argument can be F_OK to test\n\
1757existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001758
1759static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001760posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001761{
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 PyObject *opath;
1763 char *path;
1764 int mode;
1765
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001766#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001767 DWORD attr;
1768 PyUnicodeObject *po;
1769 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1770 Py_BEGIN_ALLOW_THREADS
1771 /* PyUnicode_AS_UNICODE OK without thread lock as
1772 it is a simple dereference. */
1773 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1774 Py_END_ALLOW_THREADS
1775 goto finish;
1776 }
1777 /* Drop the argument parsing error as narrow strings
1778 are also valid. */
1779 PyErr_Clear();
1780 if (!PyArg_ParseTuple(args, "O&i:access",
1781 PyUnicode_FSConverter, &opath, &mode))
1782 return NULL;
1783 path = PyBytes_AsString(opath);
1784 Py_BEGIN_ALLOW_THREADS
1785 attr = GetFileAttributesA(path);
1786 Py_END_ALLOW_THREADS
1787 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001788finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001789 if (attr == 0xFFFFFFFF)
1790 /* File does not exist, or cannot read attributes */
1791 return PyBool_FromLong(0);
1792 /* Access is possible if either write access wasn't requested, or
1793 the file isn't read-only, or if it's a directory, as there are
1794 no read-only directories on Windows. */
1795 return PyBool_FromLong(!(mode & 2)
1796 || !(attr & FILE_ATTRIBUTE_READONLY)
1797 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001798#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 int res;
1800 if (!PyArg_ParseTuple(args, "O&i:access",
1801 PyUnicode_FSConverter, &opath, &mode))
1802 return NULL;
1803 path = PyBytes_AsString(opath);
1804 Py_BEGIN_ALLOW_THREADS
1805 res = access(path, mode);
1806 Py_END_ALLOW_THREADS
1807 Py_DECREF(opath);
1808 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001809#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001810}
1811
Guido van Rossumd371ff11999-01-25 16:12:23 +00001812#ifndef F_OK
1813#define F_OK 0
1814#endif
1815#ifndef R_OK
1816#define R_OK 4
1817#endif
1818#ifndef W_OK
1819#define W_OK 2
1820#endif
1821#ifndef X_OK
1822#define X_OK 1
1823#endif
1824
1825#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001826PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001827"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001828Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001829
1830static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001831posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001832{
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 int id;
1834 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001835
Victor Stinner8c62be82010-05-06 00:08:46 +00001836 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1837 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001838
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001839#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001840 /* file descriptor 0 only, the default input device (stdin) */
1841 if (id == 0) {
1842 ret = ttyname();
1843 }
1844 else {
1845 ret = NULL;
1846 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001847#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001848 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001849#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 if (ret == NULL)
1851 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001852 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001853}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001854#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001855
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001856#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001857PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001858"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001859Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001860
1861static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001862posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001863{
Victor Stinner8c62be82010-05-06 00:08:46 +00001864 char *ret;
1865 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001866
Greg Wardb48bc172000-03-01 21:51:56 +00001867#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001869#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001870 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001871#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001872 if (ret == NULL)
1873 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001874 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001875}
1876#endif
1877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001880Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001881
Barry Warsaw53699e91996-12-10 23:23:01 +00001882static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001883posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001884{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001885#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001886 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001887#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001888 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001889#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001890 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001891#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001892 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001893#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001894}
1895
Fred Drake4d1e64b2002-04-15 19:40:07 +00001896#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001898"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001899Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001900opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001901
1902static PyObject *
1903posix_fchdir(PyObject *self, PyObject *fdobj)
1904{
Victor Stinner8c62be82010-05-06 00:08:46 +00001905 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001906}
1907#endif /* HAVE_FCHDIR */
1908
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001911"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001913
Barry Warsaw53699e91996-12-10 23:23:01 +00001914static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001915posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001916{
Victor Stinner8c62be82010-05-06 00:08:46 +00001917 PyObject *opath = NULL;
1918 char *path = NULL;
1919 int i;
1920 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001921#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001922 DWORD attr;
1923 PyUnicodeObject *po;
1924 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1925 Py_BEGIN_ALLOW_THREADS
1926 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1927 if (attr != 0xFFFFFFFF) {
1928 if (i & _S_IWRITE)
1929 attr &= ~FILE_ATTRIBUTE_READONLY;
1930 else
1931 attr |= FILE_ATTRIBUTE_READONLY;
1932 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1933 }
1934 else
1935 res = 0;
1936 Py_END_ALLOW_THREADS
1937 if (!res)
1938 return win32_error_unicode("chmod",
1939 PyUnicode_AS_UNICODE(po));
1940 Py_INCREF(Py_None);
1941 return Py_None;
1942 }
1943 /* Drop the argument parsing error as narrow strings
1944 are also valid. */
1945 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001946
Victor Stinner8c62be82010-05-06 00:08:46 +00001947 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1948 &opath, &i))
1949 return NULL;
1950 path = PyBytes_AsString(opath);
1951 Py_BEGIN_ALLOW_THREADS
1952 attr = GetFileAttributesA(path);
1953 if (attr != 0xFFFFFFFF) {
1954 if (i & _S_IWRITE)
1955 attr &= ~FILE_ATTRIBUTE_READONLY;
1956 else
1957 attr |= FILE_ATTRIBUTE_READONLY;
1958 res = SetFileAttributesA(path, attr);
1959 }
1960 else
1961 res = 0;
1962 Py_END_ALLOW_THREADS
1963 if (!res) {
1964 win32_error("chmod", path);
1965 Py_DECREF(opath);
1966 return NULL;
1967 }
1968 Py_DECREF(opath);
1969 Py_INCREF(Py_None);
1970 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001971#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001972 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1973 &opath, &i))
1974 return NULL;
1975 path = PyBytes_AsString(opath);
1976 Py_BEGIN_ALLOW_THREADS
1977 res = chmod(path, i);
1978 Py_END_ALLOW_THREADS
1979 if (res < 0)
1980 return posix_error_with_allocated_filename(opath);
1981 Py_DECREF(opath);
1982 Py_INCREF(Py_None);
1983 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001984#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001985}
1986
Christian Heimes4e30a842007-11-30 22:12:06 +00001987#ifdef HAVE_FCHMOD
1988PyDoc_STRVAR(posix_fchmod__doc__,
1989"fchmod(fd, mode)\n\n\
1990Change the access permissions of the file given by file\n\
1991descriptor fd.");
1992
1993static PyObject *
1994posix_fchmod(PyObject *self, PyObject *args)
1995{
Victor Stinner8c62be82010-05-06 00:08:46 +00001996 int fd, mode, res;
1997 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1998 return NULL;
1999 Py_BEGIN_ALLOW_THREADS
2000 res = fchmod(fd, mode);
2001 Py_END_ALLOW_THREADS
2002 if (res < 0)
2003 return posix_error();
2004 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002005}
2006#endif /* HAVE_FCHMOD */
2007
2008#ifdef HAVE_LCHMOD
2009PyDoc_STRVAR(posix_lchmod__doc__,
2010"lchmod(path, mode)\n\n\
2011Change the access permissions of a file. If path is a symlink, this\n\
2012affects the link itself rather than the target.");
2013
2014static PyObject *
2015posix_lchmod(PyObject *self, PyObject *args)
2016{
Victor Stinner8c62be82010-05-06 00:08:46 +00002017 PyObject *opath;
2018 char *path;
2019 int i;
2020 int res;
2021 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2022 &opath, &i))
2023 return NULL;
2024 path = PyBytes_AsString(opath);
2025 Py_BEGIN_ALLOW_THREADS
2026 res = lchmod(path, i);
2027 Py_END_ALLOW_THREADS
2028 if (res < 0)
2029 return posix_error_with_allocated_filename(opath);
2030 Py_DECREF(opath);
2031 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002032}
2033#endif /* HAVE_LCHMOD */
2034
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002035
Thomas Wouterscf297e42007-02-23 15:07:44 +00002036#ifdef HAVE_CHFLAGS
2037PyDoc_STRVAR(posix_chflags__doc__,
2038"chflags(path, flags)\n\n\
2039Set file flags.");
2040
2041static PyObject *
2042posix_chflags(PyObject *self, PyObject *args)
2043{
Victor Stinner8c62be82010-05-06 00:08:46 +00002044 PyObject *opath;
2045 char *path;
2046 unsigned long flags;
2047 int res;
2048 if (!PyArg_ParseTuple(args, "O&k:chflags",
2049 PyUnicode_FSConverter, &opath, &flags))
2050 return NULL;
2051 path = PyBytes_AsString(opath);
2052 Py_BEGIN_ALLOW_THREADS
2053 res = chflags(path, flags);
2054 Py_END_ALLOW_THREADS
2055 if (res < 0)
2056 return posix_error_with_allocated_filename(opath);
2057 Py_DECREF(opath);
2058 Py_INCREF(Py_None);
2059 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002060}
2061#endif /* HAVE_CHFLAGS */
2062
2063#ifdef HAVE_LCHFLAGS
2064PyDoc_STRVAR(posix_lchflags__doc__,
2065"lchflags(path, flags)\n\n\
2066Set file flags.\n\
2067This function will not follow symbolic links.");
2068
2069static PyObject *
2070posix_lchflags(PyObject *self, PyObject *args)
2071{
Victor Stinner8c62be82010-05-06 00:08:46 +00002072 PyObject *opath;
2073 char *path;
2074 unsigned long flags;
2075 int res;
2076 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2077 PyUnicode_FSConverter, &opath, &flags))
2078 return NULL;
2079 path = PyBytes_AsString(opath);
2080 Py_BEGIN_ALLOW_THREADS
2081 res = lchflags(path, flags);
2082 Py_END_ALLOW_THREADS
2083 if (res < 0)
2084 return posix_error_with_allocated_filename(opath);
2085 Py_DECREF(opath);
2086 Py_INCREF(Py_None);
2087 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002088}
2089#endif /* HAVE_LCHFLAGS */
2090
Martin v. Löwis244edc82001-10-04 22:44:26 +00002091#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002092PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002093"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002094Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002095
2096static PyObject *
2097posix_chroot(PyObject *self, PyObject *args)
2098{
Victor Stinner8c62be82010-05-06 00:08:46 +00002099 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002100}
2101#endif
2102
Guido van Rossum21142a01999-01-08 21:05:37 +00002103#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002104PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002105"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002107
2108static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002109posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002110{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002111 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002112}
2113#endif /* HAVE_FSYNC */
2114
2115#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002116
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002117#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002118extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2119#endif
2120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002121PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002122"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002123force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002124 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002125
2126static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002127posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002128{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002129 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002130}
2131#endif /* HAVE_FDATASYNC */
2132
2133
Fredrik Lundh10723342000-07-10 16:38:09 +00002134#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002135PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002136"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002137Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002138
Barry Warsaw53699e91996-12-10 23:23:01 +00002139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002140posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002141{
Victor Stinner8c62be82010-05-06 00:08:46 +00002142 PyObject *opath;
2143 char *path;
2144 long uid, gid;
2145 int res;
2146 if (!PyArg_ParseTuple(args, "O&ll:chown",
2147 PyUnicode_FSConverter, &opath,
2148 &uid, &gid))
2149 return NULL;
2150 path = PyBytes_AsString(opath);
2151 Py_BEGIN_ALLOW_THREADS
2152 res = chown(path, (uid_t) uid, (gid_t) gid);
2153 Py_END_ALLOW_THREADS
2154 if (res < 0)
2155 return posix_error_with_allocated_filename(opath);
2156 Py_DECREF(opath);
2157 Py_INCREF(Py_None);
2158 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002159}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002160#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002161
Christian Heimes4e30a842007-11-30 22:12:06 +00002162#ifdef HAVE_FCHOWN
2163PyDoc_STRVAR(posix_fchown__doc__,
2164"fchown(fd, uid, gid)\n\n\
2165Change the owner and group id of the file given by file descriptor\n\
2166fd to the numeric uid and gid.");
2167
2168static PyObject *
2169posix_fchown(PyObject *self, PyObject *args)
2170{
Victor Stinner8c62be82010-05-06 00:08:46 +00002171 int fd;
2172 long uid, gid;
2173 int res;
2174 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2175 return NULL;
2176 Py_BEGIN_ALLOW_THREADS
2177 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2178 Py_END_ALLOW_THREADS
2179 if (res < 0)
2180 return posix_error();
2181 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002182}
2183#endif /* HAVE_FCHOWN */
2184
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002185#ifdef HAVE_LCHOWN
2186PyDoc_STRVAR(posix_lchown__doc__,
2187"lchown(path, uid, gid)\n\n\
2188Change the owner and group id of path to the numeric uid and gid.\n\
2189This function will not follow symbolic links.");
2190
2191static PyObject *
2192posix_lchown(PyObject *self, PyObject *args)
2193{
Victor Stinner8c62be82010-05-06 00:08:46 +00002194 PyObject *opath;
2195 char *path;
2196 long uid, gid;
2197 int res;
2198 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2199 PyUnicode_FSConverter, &opath,
2200 &uid, &gid))
2201 return NULL;
2202 path = PyBytes_AsString(opath);
2203 Py_BEGIN_ALLOW_THREADS
2204 res = lchown(path, (uid_t) uid, (gid_t) gid);
2205 Py_END_ALLOW_THREADS
2206 if (res < 0)
2207 return posix_error_with_allocated_filename(opath);
2208 Py_DECREF(opath);
2209 Py_INCREF(Py_None);
2210 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002211}
2212#endif /* HAVE_LCHOWN */
2213
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002214
Guido van Rossum36bc6801995-06-14 22:54:23 +00002215#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002216static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002217posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002218{
Victor Stinner8c62be82010-05-06 00:08:46 +00002219 char buf[1026];
2220 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002221
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002222#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002223 if (!use_bytes) {
2224 wchar_t wbuf[1026];
2225 wchar_t *wbuf2 = wbuf;
2226 PyObject *resobj;
2227 DWORD len;
2228 Py_BEGIN_ALLOW_THREADS
2229 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2230 /* If the buffer is large enough, len does not include the
2231 terminating \0. If the buffer is too small, len includes
2232 the space needed for the terminator. */
2233 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2234 wbuf2 = malloc(len * sizeof(wchar_t));
2235 if (wbuf2)
2236 len = GetCurrentDirectoryW(len, wbuf2);
2237 }
2238 Py_END_ALLOW_THREADS
2239 if (!wbuf2) {
2240 PyErr_NoMemory();
2241 return NULL;
2242 }
2243 if (!len) {
2244 if (wbuf2 != wbuf) free(wbuf2);
2245 return win32_error("getcwdu", NULL);
2246 }
2247 resobj = PyUnicode_FromWideChar(wbuf2, len);
2248 if (wbuf2 != wbuf) free(wbuf2);
2249 return resobj;
2250 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002251#endif
2252
Victor Stinner8c62be82010-05-06 00:08:46 +00002253 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002254#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002256#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002257 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002258#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002259 Py_END_ALLOW_THREADS
2260 if (res == NULL)
2261 return posix_error();
2262 if (use_bytes)
2263 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002264 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002265}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002266
2267PyDoc_STRVAR(posix_getcwd__doc__,
2268"getcwd() -> path\n\n\
2269Return a unicode string representing the current working directory.");
2270
2271static PyObject *
2272posix_getcwd_unicode(PyObject *self)
2273{
2274 return posix_getcwd(0);
2275}
2276
2277PyDoc_STRVAR(posix_getcwdb__doc__,
2278"getcwdb() -> path\n\n\
2279Return a bytes string representing the current working directory.");
2280
2281static PyObject *
2282posix_getcwd_bytes(PyObject *self)
2283{
2284 return posix_getcwd(1);
2285}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002286#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002287
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002288
Guido van Rossumb6775db1994-08-01 11:34:53 +00002289#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002290PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002291"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002292Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002293
Barry Warsaw53699e91996-12-10 23:23:01 +00002294static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002295posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002296{
Victor Stinner8c62be82010-05-06 00:08:46 +00002297 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002298}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002299#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002300
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002302PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002303"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002304Return a list containing the names of the entries in the directory.\n\
2305\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002306 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002307\n\
2308The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002309entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002310
Barry Warsaw53699e91996-12-10 23:23:01 +00002311static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002312posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002313{
Victor Stinner8c62be82010-05-06 00:08:46 +00002314 /* XXX Should redo this putting the (now four) versions of opendir
2315 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002316#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002317
Victor Stinner8c62be82010-05-06 00:08:46 +00002318 PyObject *d, *v;
2319 HANDLE hFindFile;
2320 BOOL result;
2321 WIN32_FIND_DATA FileData;
2322 PyObject *opath;
2323 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2324 char *bufptr = namebuf;
2325 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002326
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002327 PyObject *po = NULL;
2328 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002329 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002330 Py_UNICODE *wnamebuf, *po_wchars;
2331
2332 if (po == NULL) { // Default arg: "."
2333 po_wchars = L".";
2334 len = 1;
2335 } else {
2336 po_wchars = PyUnicode_AS_UNICODE(po);
2337 len = PyUnicode_GET_SIZE(po);
2338 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002339 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002340 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2341 if (!wnamebuf) {
2342 PyErr_NoMemory();
2343 return NULL;
2344 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002345 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002346 if (len > 0) {
2347 Py_UNICODE wch = wnamebuf[len-1];
2348 if (wch != L'/' && wch != L'\\' && wch != L':')
2349 wnamebuf[len++] = L'\\';
2350 wcscpy(wnamebuf + len, L"*.*");
2351 }
2352 if ((d = PyList_New(0)) == NULL) {
2353 free(wnamebuf);
2354 return NULL;
2355 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002356 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002357 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002358 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002359 if (hFindFile == INVALID_HANDLE_VALUE) {
2360 int error = GetLastError();
2361 if (error == ERROR_FILE_NOT_FOUND) {
2362 free(wnamebuf);
2363 return d;
2364 }
2365 Py_DECREF(d);
2366 win32_error_unicode("FindFirstFileW", wnamebuf);
2367 free(wnamebuf);
2368 return NULL;
2369 }
2370 do {
2371 /* Skip over . and .. */
2372 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2373 wcscmp(wFileData.cFileName, L"..") != 0) {
2374 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2375 if (v == NULL) {
2376 Py_DECREF(d);
2377 d = NULL;
2378 break;
2379 }
2380 if (PyList_Append(d, v) != 0) {
2381 Py_DECREF(v);
2382 Py_DECREF(d);
2383 d = NULL;
2384 break;
2385 }
2386 Py_DECREF(v);
2387 }
2388 Py_BEGIN_ALLOW_THREADS
2389 result = FindNextFileW(hFindFile, &wFileData);
2390 Py_END_ALLOW_THREADS
2391 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2392 it got to the end of the directory. */
2393 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2394 Py_DECREF(d);
2395 win32_error_unicode("FindNextFileW", wnamebuf);
2396 FindClose(hFindFile);
2397 free(wnamebuf);
2398 return NULL;
2399 }
2400 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002401
Victor Stinner8c62be82010-05-06 00:08:46 +00002402 if (FindClose(hFindFile) == FALSE) {
2403 Py_DECREF(d);
2404 win32_error_unicode("FindClose", wnamebuf);
2405 free(wnamebuf);
2406 return NULL;
2407 }
2408 free(wnamebuf);
2409 return d;
2410 }
2411 /* Drop the argument parsing error as narrow strings
2412 are also valid. */
2413 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002414
Victor Stinner8c62be82010-05-06 00:08:46 +00002415 if (!PyArg_ParseTuple(args, "O&:listdir",
2416 PyUnicode_FSConverter, &opath))
2417 return NULL;
2418 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2419 PyErr_SetString(PyExc_ValueError, "path too long");
2420 Py_DECREF(opath);
2421 return NULL;
2422 }
2423 strcpy(namebuf, PyBytes_AsString(opath));
2424 len = PyObject_Size(opath);
2425 if (len > 0) {
2426 char ch = namebuf[len-1];
2427 if (ch != SEP && ch != ALTSEP && ch != ':')
2428 namebuf[len++] = '/';
2429 strcpy(namebuf + len, "*.*");
2430 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002431
Victor Stinner8c62be82010-05-06 00:08:46 +00002432 if ((d = PyList_New(0)) == NULL)
2433 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002434
Antoine Pitroub73caab2010-08-09 23:39:31 +00002435 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002436 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002437 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002438 if (hFindFile == INVALID_HANDLE_VALUE) {
2439 int error = GetLastError();
2440 if (error == ERROR_FILE_NOT_FOUND)
2441 return d;
2442 Py_DECREF(d);
2443 return win32_error("FindFirstFile", namebuf);
2444 }
2445 do {
2446 /* Skip over . and .. */
2447 if (strcmp(FileData.cFileName, ".") != 0 &&
2448 strcmp(FileData.cFileName, "..") != 0) {
2449 v = PyBytes_FromString(FileData.cFileName);
2450 if (v == NULL) {
2451 Py_DECREF(d);
2452 d = NULL;
2453 break;
2454 }
2455 if (PyList_Append(d, v) != 0) {
2456 Py_DECREF(v);
2457 Py_DECREF(d);
2458 d = NULL;
2459 break;
2460 }
2461 Py_DECREF(v);
2462 }
2463 Py_BEGIN_ALLOW_THREADS
2464 result = FindNextFile(hFindFile, &FileData);
2465 Py_END_ALLOW_THREADS
2466 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2467 it got to the end of the directory. */
2468 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2469 Py_DECREF(d);
2470 win32_error("FindNextFile", namebuf);
2471 FindClose(hFindFile);
2472 return NULL;
2473 }
2474 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002475
Victor Stinner8c62be82010-05-06 00:08:46 +00002476 if (FindClose(hFindFile) == FALSE) {
2477 Py_DECREF(d);
2478 return win32_error("FindClose", namebuf);
2479 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002480
Victor Stinner8c62be82010-05-06 00:08:46 +00002481 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002482
Tim Peters0bb44a42000-09-15 07:44:49 +00002483#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002484
2485#ifndef MAX_PATH
2486#define MAX_PATH CCHMAXPATH
2487#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002488 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002489 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002490 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002491 PyObject *d, *v;
2492 char namebuf[MAX_PATH+5];
2493 HDIR hdir = 1;
2494 ULONG srchcnt = 1;
2495 FILEFINDBUF3 ep;
2496 APIRET rc;
2497
Victor Stinner8c62be82010-05-06 00:08:46 +00002498 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002499 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002500 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002501 name = PyBytes_AsString(oname);
2502 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002503 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002504 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002505 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002506 return NULL;
2507 }
2508 strcpy(namebuf, name);
2509 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002510 if (*pt == ALTSEP)
2511 *pt = SEP;
2512 if (namebuf[len-1] != SEP)
2513 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002514 strcpy(namebuf + len, "*.*");
2515
Neal Norwitz6c913782007-10-14 03:23:09 +00002516 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002517 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002518 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002519 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002520
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002521 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2522 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002523 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002524 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2525 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2526 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002527
2528 if (rc != NO_ERROR) {
2529 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002530 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002531 }
2532
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002533 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002534 do {
2535 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002536 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002537 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002538
2539 strcpy(namebuf, ep.achName);
2540
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002541 /* Leave Case of Name Alone -- In Native Form */
2542 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002543
Christian Heimes72b710a2008-05-26 13:28:38 +00002544 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002545 if (v == NULL) {
2546 Py_DECREF(d);
2547 d = NULL;
2548 break;
2549 }
2550 if (PyList_Append(d, v) != 0) {
2551 Py_DECREF(v);
2552 Py_DECREF(d);
2553 d = NULL;
2554 break;
2555 }
2556 Py_DECREF(v);
2557 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2558 }
2559
Victor Stinnerdcb24032010-04-22 12:08:36 +00002560 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002561 return d;
2562#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002563 PyObject *oname;
2564 char *name;
2565 PyObject *d, *v;
2566 DIR *dirp;
2567 struct dirent *ep;
2568 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002569
Victor Stinner8c62be82010-05-06 00:08:46 +00002570 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002571 /* v is never read, so it does not need to be initialized yet. */
2572 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002573 arg_is_unicode = 0;
2574 PyErr_Clear();
2575 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002576 oname = NULL;
2577 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002578 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002579 if (oname == NULL) { // Default arg: "."
2580 oname = PyBytes_FromString(".");
2581 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002582 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002583 Py_BEGIN_ALLOW_THREADS
2584 dirp = opendir(name);
2585 Py_END_ALLOW_THREADS
2586 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002587 return posix_error_with_allocated_filename(oname);
2588 }
2589 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002590 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002591 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002592 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002593 Py_DECREF(oname);
2594 return NULL;
2595 }
2596 for (;;) {
2597 errno = 0;
2598 Py_BEGIN_ALLOW_THREADS
2599 ep = readdir(dirp);
2600 Py_END_ALLOW_THREADS
2601 if (ep == NULL) {
2602 if (errno == 0) {
2603 break;
2604 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002605 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002606 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002607 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002608 Py_DECREF(d);
2609 return posix_error_with_allocated_filename(oname);
2610 }
2611 }
2612 if (ep->d_name[0] == '.' &&
2613 (NAMLEN(ep) == 1 ||
2614 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2615 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002616 if (arg_is_unicode)
2617 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2618 else
2619 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002620 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002621 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002622 break;
2623 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002624 if (PyList_Append(d, v) != 0) {
2625 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002626 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002627 break;
2628 }
2629 Py_DECREF(v);
2630 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002631 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002633 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002635
Victor Stinner8c62be82010-05-06 00:08:46 +00002636 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002637
Tim Peters0bb44a42000-09-15 07:44:49 +00002638#endif /* which OS */
2639} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002640
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002641#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002642/* A helper function for abspath on win32 */
2643static PyObject *
2644posix__getfullpathname(PyObject *self, PyObject *args)
2645{
Victor Stinner8c62be82010-05-06 00:08:46 +00002646 PyObject *opath;
2647 char *path;
2648 char outbuf[MAX_PATH*2];
2649 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002650#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002651 PyUnicodeObject *po;
2652 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2653 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2654 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2655 Py_UNICODE *wtemp;
2656 DWORD result;
2657 PyObject *v;
2658 result = GetFullPathNameW(wpath,
2659 sizeof(woutbuf)/sizeof(woutbuf[0]),
2660 woutbuf, &wtemp);
2661 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2662 woutbufp = malloc(result * sizeof(Py_UNICODE));
2663 if (!woutbufp)
2664 return PyErr_NoMemory();
2665 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2666 }
2667 if (result)
2668 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2669 else
2670 v = win32_error_unicode("GetFullPathNameW", wpath);
2671 if (woutbufp != woutbuf)
2672 free(woutbufp);
2673 return v;
2674 }
2675 /* Drop the argument parsing error as narrow strings
2676 are also valid. */
2677 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002678
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002679#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002680 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2681 PyUnicode_FSConverter, &opath))
2682 return NULL;
2683 path = PyBytes_AsString(opath);
2684 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2685 outbuf, &temp)) {
2686 win32_error("GetFullPathName", path);
2687 Py_DECREF(opath);
2688 return NULL;
2689 }
2690 Py_DECREF(opath);
2691 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2692 return PyUnicode_Decode(outbuf, strlen(outbuf),
2693 Py_FileSystemDefaultEncoding, NULL);
2694 }
2695 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002696} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002697
2698/* A helper function for samepath on windows */
2699static PyObject *
2700posix__getfinalpathname(PyObject *self, PyObject *args)
2701{
2702 HANDLE hFile;
2703 int buf_size;
2704 wchar_t *target_path;
2705 int result_length;
2706 PyObject *result;
2707 wchar_t *path;
2708
2709 if (!PyArg_ParseTuple(args, "u|:_getfullpathname", &path)) {
2710 return NULL;
2711 }
2712
2713 if(!check_GetFinalPathNameByHandle()) {
2714 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2715 NotImplementedError. */
2716 return PyErr_Format(PyExc_NotImplementedError,
2717 "GetFinalPathNameByHandle not available on this platform");
2718 }
2719
2720 hFile = CreateFileW(
2721 path,
2722 0, /* desired access */
2723 0, /* share mode */
2724 NULL, /* security attributes */
2725 OPEN_EXISTING,
2726 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2727 FILE_FLAG_BACKUP_SEMANTICS,
2728 NULL);
2729
2730 if(hFile == INVALID_HANDLE_VALUE) {
2731 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002732 return PyErr_Format(PyExc_RuntimeError,
2733 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002734 }
2735
2736 /* We have a good handle to the target, use it to determine the
2737 target path name. */
2738 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2739
2740 if(!buf_size)
2741 return win32_error_unicode("GetFinalPathNameByHandle", path);
2742
2743 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2744 if(!target_path)
2745 return PyErr_NoMemory();
2746
2747 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2748 buf_size, VOLUME_NAME_DOS);
2749 if(!result_length)
2750 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2751
2752 if(!CloseHandle(hFile))
2753 return win32_error_unicode("GetFinalPathNameByHandle", path);
2754
2755 target_path[result_length] = 0;
2756 result = PyUnicode_FromUnicode(target_path, result_length);
2757 free(target_path);
2758 return result;
2759
2760} /* end of posix__getfinalpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002761#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002763PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002764"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002765Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002766
Barry Warsaw53699e91996-12-10 23:23:01 +00002767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002768posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002769{
Victor Stinner8c62be82010-05-06 00:08:46 +00002770 int res;
2771 PyObject *opath;
2772 char *path;
2773 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002774
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002775#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002776 PyUnicodeObject *po;
2777 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2778 Py_BEGIN_ALLOW_THREADS
2779 /* PyUnicode_AS_UNICODE OK without thread lock as
2780 it is a simple dereference. */
2781 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2782 Py_END_ALLOW_THREADS
2783 if (!res)
2784 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2785 Py_INCREF(Py_None);
2786 return Py_None;
2787 }
2788 /* Drop the argument parsing error as narrow strings
2789 are also valid. */
2790 PyErr_Clear();
2791 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2792 PyUnicode_FSConverter, &opath, &mode))
2793 return NULL;
2794 path = PyBytes_AsString(opath);
2795 Py_BEGIN_ALLOW_THREADS
2796 /* PyUnicode_AS_UNICODE OK without thread lock as
2797 it is a simple dereference. */
2798 res = CreateDirectoryA(path, NULL);
2799 Py_END_ALLOW_THREADS
2800 if (!res) {
2801 win32_error("mkdir", path);
2802 Py_DECREF(opath);
2803 return NULL;
2804 }
2805 Py_DECREF(opath);
2806 Py_INCREF(Py_None);
2807 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002808#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002809
Victor Stinner8c62be82010-05-06 00:08:46 +00002810 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2811 PyUnicode_FSConverter, &opath, &mode))
2812 return NULL;
2813 path = PyBytes_AsString(opath);
2814 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002815#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002816 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002817#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002818 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002819#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002820 Py_END_ALLOW_THREADS
2821 if (res < 0)
2822 return posix_error_with_allocated_filename(opath);
2823 Py_DECREF(opath);
2824 Py_INCREF(Py_None);
2825 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002826#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002827}
2828
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002829
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002830/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2831#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002832#include <sys/resource.h>
2833#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002834
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002835
2836#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002837PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002838"nice(inc) -> new_priority\n\n\
2839Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002840
Barry Warsaw53699e91996-12-10 23:23:01 +00002841static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002842posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002843{
Victor Stinner8c62be82010-05-06 00:08:46 +00002844 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002845
Victor Stinner8c62be82010-05-06 00:08:46 +00002846 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2847 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002848
Victor Stinner8c62be82010-05-06 00:08:46 +00002849 /* There are two flavours of 'nice': one that returns the new
2850 priority (as required by almost all standards out there) and the
2851 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2852 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002853
Victor Stinner8c62be82010-05-06 00:08:46 +00002854 If we are of the nice family that returns the new priority, we
2855 need to clear errno before the call, and check if errno is filled
2856 before calling posix_error() on a returnvalue of -1, because the
2857 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002858
Victor Stinner8c62be82010-05-06 00:08:46 +00002859 errno = 0;
2860 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002861#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002862 if (value == 0)
2863 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002864#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002865 if (value == -1 && errno != 0)
2866 /* either nice() or getpriority() returned an error */
2867 return posix_error();
2868 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002869}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002870#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002872PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002873"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002874Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002875
Barry Warsaw53699e91996-12-10 23:23:01 +00002876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002877posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002878{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002879#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002880 PyObject *o1, *o2;
2881 char *p1, *p2;
2882 BOOL result;
2883 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2884 goto error;
2885 if (!convert_to_unicode(&o1))
2886 goto error;
2887 if (!convert_to_unicode(&o2)) {
2888 Py_DECREF(o1);
2889 goto error;
2890 }
2891 Py_BEGIN_ALLOW_THREADS
2892 result = MoveFileW(PyUnicode_AsUnicode(o1),
2893 PyUnicode_AsUnicode(o2));
2894 Py_END_ALLOW_THREADS
2895 Py_DECREF(o1);
2896 Py_DECREF(o2);
2897 if (!result)
2898 return win32_error("rename", NULL);
2899 Py_INCREF(Py_None);
2900 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002901error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002902 PyErr_Clear();
2903 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2904 return NULL;
2905 Py_BEGIN_ALLOW_THREADS
2906 result = MoveFileA(p1, p2);
2907 Py_END_ALLOW_THREADS
2908 if (!result)
2909 return win32_error("rename", NULL);
2910 Py_INCREF(Py_None);
2911 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002912#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002913 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002914#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002915}
2916
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002918PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002919"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002920Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002921
Barry Warsaw53699e91996-12-10 23:23:01 +00002922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002923posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002924{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002925#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002926 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002927#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002928 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002929#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002930}
2931
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002932
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002933PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002934"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002935Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002936
Barry Warsaw53699e91996-12-10 23:23:01 +00002937static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002938posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002939{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002940#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00002941 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002942#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002943 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002944#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002945}
2946
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002947
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002948#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002949PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002950"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002951Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002952
Barry Warsaw53699e91996-12-10 23:23:01 +00002953static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002954posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002955{
Victor Stinner8c62be82010-05-06 00:08:46 +00002956 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002957#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002958 wchar_t *command;
2959 if (!PyArg_ParseTuple(args, "u:system", &command))
2960 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002961
Victor Stinner8c62be82010-05-06 00:08:46 +00002962 Py_BEGIN_ALLOW_THREADS
2963 sts = _wsystem(command);
2964 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002965#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002966 PyObject *command_obj;
2967 char *command;
2968 if (!PyArg_ParseTuple(args, "O&:system",
2969 PyUnicode_FSConverter, &command_obj))
2970 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002971
Victor Stinner8c62be82010-05-06 00:08:46 +00002972 command = PyBytes_AsString(command_obj);
2973 Py_BEGIN_ALLOW_THREADS
2974 sts = system(command);
2975 Py_END_ALLOW_THREADS
2976 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002977#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002978 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002979}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002980#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002981
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002983PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002984"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002985Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002986
Barry Warsaw53699e91996-12-10 23:23:01 +00002987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002988posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002989{
Victor Stinner8c62be82010-05-06 00:08:46 +00002990 int i;
2991 if (!PyArg_ParseTuple(args, "i:umask", &i))
2992 return NULL;
2993 i = (int)umask(i);
2994 if (i < 0)
2995 return posix_error();
2996 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002997}
2998
Brian Curtind40e6f72010-07-08 21:39:08 +00002999#ifdef MS_WINDOWS
3000
3001/* override the default DeleteFileW behavior so that directory
3002symlinks can be removed with this function, the same as with
3003Unix symlinks */
3004BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3005{
3006 WIN32_FILE_ATTRIBUTE_DATA info;
3007 WIN32_FIND_DATAW find_data;
3008 HANDLE find_data_handle;
3009 int is_directory = 0;
3010 int is_link = 0;
3011
3012 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3013 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3014
3015 /* Get WIN32_FIND_DATA structure for the path to determine if
3016 it is a symlink */
3017 if(is_directory &&
3018 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3019 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3020
3021 if(find_data_handle != INVALID_HANDLE_VALUE) {
3022 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3023 FindClose(find_data_handle);
3024 }
3025 }
3026 }
3027
3028 if (is_directory && is_link)
3029 return RemoveDirectoryW(lpFileName);
3030
3031 return DeleteFileW(lpFileName);
3032}
3033#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003035PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003036"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003037Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003039PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003040"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003041Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003042
Barry Warsaw53699e91996-12-10 23:23:01 +00003043static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003044posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003045{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003046#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003047 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3048 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003049#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003050 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003051#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003052}
3053
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003054
Guido van Rossumb6775db1994-08-01 11:34:53 +00003055#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003056PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003057"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003058Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003059
Barry Warsaw53699e91996-12-10 23:23:01 +00003060static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003061posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003062{
Victor Stinner8c62be82010-05-06 00:08:46 +00003063 struct utsname u;
3064 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003065
Victor Stinner8c62be82010-05-06 00:08:46 +00003066 Py_BEGIN_ALLOW_THREADS
3067 res = uname(&u);
3068 Py_END_ALLOW_THREADS
3069 if (res < 0)
3070 return posix_error();
3071 return Py_BuildValue("(sssss)",
3072 u.sysname,
3073 u.nodename,
3074 u.release,
3075 u.version,
3076 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003077}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003078#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003079
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003080static int
3081extract_time(PyObject *t, long* sec, long* usec)
3082{
Victor Stinner8c62be82010-05-06 00:08:46 +00003083 long intval;
3084 if (PyFloat_Check(t)) {
3085 double tval = PyFloat_AsDouble(t);
3086 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
3087 if (!intobj)
3088 return -1;
3089 intval = PyLong_AsLong(intobj);
3090 Py_DECREF(intobj);
3091 if (intval == -1 && PyErr_Occurred())
3092 return -1;
3093 *sec = intval;
3094 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3095 if (*usec < 0)
3096 /* If rounding gave us a negative number,
3097 truncate. */
3098 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003099 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003100 }
3101 intval = PyLong_AsLong(t);
3102 if (intval == -1 && PyErr_Occurred())
3103 return -1;
3104 *sec = intval;
3105 *usec = 0;
3106 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003107}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003109PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003110"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003111utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003112Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003113second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003114
Barry Warsaw53699e91996-12-10 23:23:01 +00003115static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003116posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003117{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003118#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003119 PyObject *arg;
3120 PyUnicodeObject *obwpath;
3121 wchar_t *wpath = NULL;
3122 PyObject *oapath;
3123 char *apath;
3124 HANDLE hFile;
3125 long atimesec, mtimesec, ausec, musec;
3126 FILETIME atime, mtime;
3127 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003128
Victor Stinner8c62be82010-05-06 00:08:46 +00003129 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3130 wpath = PyUnicode_AS_UNICODE(obwpath);
3131 Py_BEGIN_ALLOW_THREADS
3132 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3133 NULL, OPEN_EXISTING,
3134 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3135 Py_END_ALLOW_THREADS
3136 if (hFile == INVALID_HANDLE_VALUE)
3137 return win32_error_unicode("utime", wpath);
3138 } else
3139 /* Drop the argument parsing error as narrow strings
3140 are also valid. */
3141 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003142
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 if (!wpath) {
3144 if (!PyArg_ParseTuple(args, "O&O:utime",
3145 PyUnicode_FSConverter, &oapath, &arg))
3146 return NULL;
3147 apath = PyBytes_AsString(oapath);
3148 Py_BEGIN_ALLOW_THREADS
3149 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3150 NULL, OPEN_EXISTING,
3151 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3152 Py_END_ALLOW_THREADS
3153 if (hFile == INVALID_HANDLE_VALUE) {
3154 win32_error("utime", apath);
3155 Py_DECREF(oapath);
3156 return NULL;
3157 }
3158 Py_DECREF(oapath);
3159 }
3160
3161 if (arg == Py_None) {
3162 SYSTEMTIME now;
3163 GetSystemTime(&now);
3164 if (!SystemTimeToFileTime(&now, &mtime) ||
3165 !SystemTimeToFileTime(&now, &atime)) {
3166 win32_error("utime", NULL);
3167 goto done;
3168 }
3169 }
3170 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3171 PyErr_SetString(PyExc_TypeError,
3172 "utime() arg 2 must be a tuple (atime, mtime)");
3173 goto done;
3174 }
3175 else {
3176 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3177 &atimesec, &ausec) == -1)
3178 goto done;
3179 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3180 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3181 &mtimesec, &musec) == -1)
3182 goto done;
3183 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3184 }
3185 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3186 /* Avoid putting the file name into the error here,
3187 as that may confuse the user into believing that
3188 something is wrong with the file, when it also
3189 could be the time stamp that gives a problem. */
3190 win32_error("utime", NULL);
3191 }
3192 Py_INCREF(Py_None);
3193 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003194done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003195 CloseHandle(hFile);
3196 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003197#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003198
Victor Stinner8c62be82010-05-06 00:08:46 +00003199 PyObject *opath;
3200 char *path;
3201 long atime, mtime, ausec, musec;
3202 int res;
3203 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003204
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003205#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003206 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003207#define ATIME buf[0].tv_sec
3208#define MTIME buf[1].tv_sec
3209#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003210/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003212#define ATIME buf.actime
3213#define MTIME buf.modtime
3214#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003215#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003217#define ATIME buf[0]
3218#define MTIME buf[1]
3219#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003220#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003221
Mark Hammond817c9292003-12-03 01:22:38 +00003222
Victor Stinner8c62be82010-05-06 00:08:46 +00003223 if (!PyArg_ParseTuple(args, "O&O:utime",
3224 PyUnicode_FSConverter, &opath, &arg))
3225 return NULL;
3226 path = PyBytes_AsString(opath);
3227 if (arg == Py_None) {
3228 /* optional time values not given */
3229 Py_BEGIN_ALLOW_THREADS
3230 res = utime(path, NULL);
3231 Py_END_ALLOW_THREADS
3232 }
3233 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3234 PyErr_SetString(PyExc_TypeError,
3235 "utime() arg 2 must be a tuple (atime, mtime)");
3236 Py_DECREF(opath);
3237 return NULL;
3238 }
3239 else {
3240 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3241 &atime, &ausec) == -1) {
3242 Py_DECREF(opath);
3243 return NULL;
3244 }
3245 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3246 &mtime, &musec) == -1) {
3247 Py_DECREF(opath);
3248 return NULL;
3249 }
3250 ATIME = atime;
3251 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003252#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003253 buf[0].tv_usec = ausec;
3254 buf[1].tv_usec = musec;
3255 Py_BEGIN_ALLOW_THREADS
3256 res = utimes(path, buf);
3257 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003258#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003259 Py_BEGIN_ALLOW_THREADS
3260 res = utime(path, UTIME_ARG);
3261 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003262#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003263 }
3264 if (res < 0) {
3265 return posix_error_with_allocated_filename(opath);
3266 }
3267 Py_DECREF(opath);
3268 Py_INCREF(Py_None);
3269 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003270#undef UTIME_ARG
3271#undef ATIME
3272#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003273#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003274}
3275
Guido van Rossum85e3b011991-06-03 12:42:10 +00003276
Guido van Rossum3b066191991-06-04 19:40:25 +00003277/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003279PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003280"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003281Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003282
Barry Warsaw53699e91996-12-10 23:23:01 +00003283static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003284posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003285{
Victor Stinner8c62be82010-05-06 00:08:46 +00003286 int sts;
3287 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3288 return NULL;
3289 _exit(sts);
3290 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003291}
3292
Martin v. Löwis114619e2002-10-07 06:44:21 +00003293#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3294static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003295free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003296{
Victor Stinner8c62be82010-05-06 00:08:46 +00003297 Py_ssize_t i;
3298 for (i = 0; i < count; i++)
3299 PyMem_Free(array[i]);
3300 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003301}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003302
Antoine Pitrou69f71142009-05-24 21:25:49 +00003303static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003304int fsconvert_strdup(PyObject *o, char**out)
3305{
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 PyObject *bytes;
3307 Py_ssize_t size;
3308 if (!PyUnicode_FSConverter(o, &bytes))
3309 return 0;
3310 size = PyBytes_GET_SIZE(bytes);
3311 *out = PyMem_Malloc(size+1);
3312 if (!*out)
3313 return 0;
3314 memcpy(*out, PyBytes_AsString(bytes), size+1);
3315 Py_DECREF(bytes);
3316 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003317}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003318#endif
3319
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003320
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003321#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003322PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003323"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003324Execute an executable path with arguments, replacing current process.\n\
3325\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003326 path: path of executable file\n\
3327 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003328
Barry Warsaw53699e91996-12-10 23:23:01 +00003329static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003330posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003331{
Victor Stinner8c62be82010-05-06 00:08:46 +00003332 PyObject *opath;
3333 char *path;
3334 PyObject *argv;
3335 char **argvlist;
3336 Py_ssize_t i, argc;
3337 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003338
Victor Stinner8c62be82010-05-06 00:08:46 +00003339 /* execv has two arguments: (path, argv), where
3340 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003341
Victor Stinner8c62be82010-05-06 00:08:46 +00003342 if (!PyArg_ParseTuple(args, "O&O:execv",
3343 PyUnicode_FSConverter,
3344 &opath, &argv))
3345 return NULL;
3346 path = PyBytes_AsString(opath);
3347 if (PyList_Check(argv)) {
3348 argc = PyList_Size(argv);
3349 getitem = PyList_GetItem;
3350 }
3351 else if (PyTuple_Check(argv)) {
3352 argc = PyTuple_Size(argv);
3353 getitem = PyTuple_GetItem;
3354 }
3355 else {
3356 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3357 Py_DECREF(opath);
3358 return NULL;
3359 }
3360 if (argc < 1) {
3361 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3362 Py_DECREF(opath);
3363 return NULL;
3364 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003365
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 argvlist = PyMem_NEW(char *, argc+1);
3367 if (argvlist == NULL) {
3368 Py_DECREF(opath);
3369 return PyErr_NoMemory();
3370 }
3371 for (i = 0; i < argc; i++) {
3372 if (!fsconvert_strdup((*getitem)(argv, i),
3373 &argvlist[i])) {
3374 free_string_array(argvlist, i);
3375 PyErr_SetString(PyExc_TypeError,
3376 "execv() arg 2 must contain only strings");
3377 Py_DECREF(opath);
3378 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003379
Victor Stinner8c62be82010-05-06 00:08:46 +00003380 }
3381 }
3382 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003383
Victor Stinner8c62be82010-05-06 00:08:46 +00003384 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003385
Victor Stinner8c62be82010-05-06 00:08:46 +00003386 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003387
Victor Stinner8c62be82010-05-06 00:08:46 +00003388 free_string_array(argvlist, argc);
3389 Py_DECREF(opath);
3390 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003391}
3392
Victor Stinner13bb71c2010-04-23 21:41:56 +00003393static char**
3394parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3395{
Victor Stinner8c62be82010-05-06 00:08:46 +00003396 char **envlist;
3397 Py_ssize_t i, pos, envc;
3398 PyObject *keys=NULL, *vals=NULL;
3399 PyObject *key, *val, *key2, *val2;
3400 char *p, *k, *v;
3401 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003402
Victor Stinner8c62be82010-05-06 00:08:46 +00003403 i = PyMapping_Size(env);
3404 if (i < 0)
3405 return NULL;
3406 envlist = PyMem_NEW(char *, i + 1);
3407 if (envlist == NULL) {
3408 PyErr_NoMemory();
3409 return NULL;
3410 }
3411 envc = 0;
3412 keys = PyMapping_Keys(env);
3413 vals = PyMapping_Values(env);
3414 if (!keys || !vals)
3415 goto error;
3416 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3417 PyErr_Format(PyExc_TypeError,
3418 "env.keys() or env.values() is not a list");
3419 goto error;
3420 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003421
Victor Stinner8c62be82010-05-06 00:08:46 +00003422 for (pos = 0; pos < i; pos++) {
3423 key = PyList_GetItem(keys, pos);
3424 val = PyList_GetItem(vals, pos);
3425 if (!key || !val)
3426 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003427
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 if (PyUnicode_FSConverter(key, &key2) == 0)
3429 goto error;
3430 if (PyUnicode_FSConverter(val, &val2) == 0) {
3431 Py_DECREF(key2);
3432 goto error;
3433 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003434
3435#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003436 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3437 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003438#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003439 k = PyBytes_AsString(key2);
3440 v = PyBytes_AsString(val2);
3441 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003442
Victor Stinner8c62be82010-05-06 00:08:46 +00003443 p = PyMem_NEW(char, len);
3444 if (p == NULL) {
3445 PyErr_NoMemory();
3446 Py_DECREF(key2);
3447 Py_DECREF(val2);
3448 goto error;
3449 }
3450 PyOS_snprintf(p, len, "%s=%s", k, v);
3451 envlist[envc++] = p;
3452 Py_DECREF(key2);
3453 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003454#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003455 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003456#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003457 }
3458 Py_DECREF(vals);
3459 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003460
Victor Stinner8c62be82010-05-06 00:08:46 +00003461 envlist[envc] = 0;
3462 *envc_ptr = envc;
3463 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003464
3465error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003466 Py_XDECREF(keys);
3467 Py_XDECREF(vals);
3468 while (--envc >= 0)
3469 PyMem_DEL(envlist[envc]);
3470 PyMem_DEL(envlist);
3471 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003472}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003473
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003474PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003475"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003476Execute a path with arguments and environment, replacing current process.\n\
3477\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003478 path: path of executable file\n\
3479 args: tuple or list of arguments\n\
3480 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003481
Barry Warsaw53699e91996-12-10 23:23:01 +00003482static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003483posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003484{
Victor Stinner8c62be82010-05-06 00:08:46 +00003485 PyObject *opath;
3486 char *path;
3487 PyObject *argv, *env;
3488 char **argvlist;
3489 char **envlist;
3490 Py_ssize_t i, argc, envc;
3491 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3492 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003493
Victor Stinner8c62be82010-05-06 00:08:46 +00003494 /* execve has three arguments: (path, argv, env), where
3495 argv is a list or tuple of strings and env is a dictionary
3496 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003497
Victor Stinner8c62be82010-05-06 00:08:46 +00003498 if (!PyArg_ParseTuple(args, "O&OO:execve",
3499 PyUnicode_FSConverter,
3500 &opath, &argv, &env))
3501 return NULL;
3502 path = PyBytes_AsString(opath);
3503 if (PyList_Check(argv)) {
3504 argc = PyList_Size(argv);
3505 getitem = PyList_GetItem;
3506 }
3507 else if (PyTuple_Check(argv)) {
3508 argc = PyTuple_Size(argv);
3509 getitem = PyTuple_GetItem;
3510 }
3511 else {
3512 PyErr_SetString(PyExc_TypeError,
3513 "execve() arg 2 must be a tuple or list");
3514 goto fail_0;
3515 }
3516 if (!PyMapping_Check(env)) {
3517 PyErr_SetString(PyExc_TypeError,
3518 "execve() arg 3 must be a mapping object");
3519 goto fail_0;
3520 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003521
Victor Stinner8c62be82010-05-06 00:08:46 +00003522 argvlist = PyMem_NEW(char *, argc+1);
3523 if (argvlist == NULL) {
3524 PyErr_NoMemory();
3525 goto fail_0;
3526 }
3527 for (i = 0; i < argc; i++) {
3528 if (!fsconvert_strdup((*getitem)(argv, i),
3529 &argvlist[i]))
3530 {
3531 lastarg = i;
3532 goto fail_1;
3533 }
3534 }
3535 lastarg = argc;
3536 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003537
Victor Stinner8c62be82010-05-06 00:08:46 +00003538 envlist = parse_envlist(env, &envc);
3539 if (envlist == NULL)
3540 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003541
Victor Stinner8c62be82010-05-06 00:08:46 +00003542 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003543
Victor Stinner8c62be82010-05-06 00:08:46 +00003544 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003545
Victor Stinner8c62be82010-05-06 00:08:46 +00003546 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003547
Victor Stinner8c62be82010-05-06 00:08:46 +00003548 while (--envc >= 0)
3549 PyMem_DEL(envlist[envc]);
3550 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003551 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003552 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003553 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 Py_DECREF(opath);
3555 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003556}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003557#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003558
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003559
Guido van Rossuma1065681999-01-25 23:20:23 +00003560#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003561PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003562"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003563Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003564\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003565 mode: mode of process creation\n\
3566 path: path of executable file\n\
3567 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003568
3569static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003570posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003571{
Victor Stinner8c62be82010-05-06 00:08:46 +00003572 PyObject *opath;
3573 char *path;
3574 PyObject *argv;
3575 char **argvlist;
3576 int mode, i;
3577 Py_ssize_t argc;
3578 Py_intptr_t spawnval;
3579 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003580
Victor Stinner8c62be82010-05-06 00:08:46 +00003581 /* spawnv has three arguments: (mode, path, argv), where
3582 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003583
Victor Stinner8c62be82010-05-06 00:08:46 +00003584 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3585 PyUnicode_FSConverter,
3586 &opath, &argv))
3587 return NULL;
3588 path = PyBytes_AsString(opath);
3589 if (PyList_Check(argv)) {
3590 argc = PyList_Size(argv);
3591 getitem = PyList_GetItem;
3592 }
3593 else if (PyTuple_Check(argv)) {
3594 argc = PyTuple_Size(argv);
3595 getitem = PyTuple_GetItem;
3596 }
3597 else {
3598 PyErr_SetString(PyExc_TypeError,
3599 "spawnv() arg 2 must be a tuple or list");
3600 Py_DECREF(opath);
3601 return NULL;
3602 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003603
Victor Stinner8c62be82010-05-06 00:08:46 +00003604 argvlist = PyMem_NEW(char *, argc+1);
3605 if (argvlist == NULL) {
3606 Py_DECREF(opath);
3607 return PyErr_NoMemory();
3608 }
3609 for (i = 0; i < argc; i++) {
3610 if (!fsconvert_strdup((*getitem)(argv, i),
3611 &argvlist[i])) {
3612 free_string_array(argvlist, i);
3613 PyErr_SetString(
3614 PyExc_TypeError,
3615 "spawnv() arg 2 must contain only strings");
3616 Py_DECREF(opath);
3617 return NULL;
3618 }
3619 }
3620 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003621
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003622#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003623 Py_BEGIN_ALLOW_THREADS
3624 spawnval = spawnv(mode, path, argvlist);
3625 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003626#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003627 if (mode == _OLD_P_OVERLAY)
3628 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003629
Victor Stinner8c62be82010-05-06 00:08:46 +00003630 Py_BEGIN_ALLOW_THREADS
3631 spawnval = _spawnv(mode, path, argvlist);
3632 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003633#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003634
Victor Stinner8c62be82010-05-06 00:08:46 +00003635 free_string_array(argvlist, argc);
3636 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003637
Victor Stinner8c62be82010-05-06 00:08:46 +00003638 if (spawnval == -1)
3639 return posix_error();
3640 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003641#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003642 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003643#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003645#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003646}
3647
3648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003649PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003650"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003651Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003652\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003653 mode: mode of process creation\n\
3654 path: path of executable file\n\
3655 args: tuple or list of arguments\n\
3656 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003657
3658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003659posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003660{
Victor Stinner8c62be82010-05-06 00:08:46 +00003661 PyObject *opath;
3662 char *path;
3663 PyObject *argv, *env;
3664 char **argvlist;
3665 char **envlist;
3666 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003667 int mode;
3668 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003669 Py_intptr_t spawnval;
3670 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3671 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003672
Victor Stinner8c62be82010-05-06 00:08:46 +00003673 /* spawnve has four arguments: (mode, path, argv, env), where
3674 argv is a list or tuple of strings and env is a dictionary
3675 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003676
Victor Stinner8c62be82010-05-06 00:08:46 +00003677 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3678 PyUnicode_FSConverter,
3679 &opath, &argv, &env))
3680 return NULL;
3681 path = PyBytes_AsString(opath);
3682 if (PyList_Check(argv)) {
3683 argc = PyList_Size(argv);
3684 getitem = PyList_GetItem;
3685 }
3686 else if (PyTuple_Check(argv)) {
3687 argc = PyTuple_Size(argv);
3688 getitem = PyTuple_GetItem;
3689 }
3690 else {
3691 PyErr_SetString(PyExc_TypeError,
3692 "spawnve() arg 2 must be a tuple or list");
3693 goto fail_0;
3694 }
3695 if (!PyMapping_Check(env)) {
3696 PyErr_SetString(PyExc_TypeError,
3697 "spawnve() arg 3 must be a mapping object");
3698 goto fail_0;
3699 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003700
Victor Stinner8c62be82010-05-06 00:08:46 +00003701 argvlist = PyMem_NEW(char *, argc+1);
3702 if (argvlist == NULL) {
3703 PyErr_NoMemory();
3704 goto fail_0;
3705 }
3706 for (i = 0; i < argc; i++) {
3707 if (!fsconvert_strdup((*getitem)(argv, i),
3708 &argvlist[i]))
3709 {
3710 lastarg = i;
3711 goto fail_1;
3712 }
3713 }
3714 lastarg = argc;
3715 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003716
Victor Stinner8c62be82010-05-06 00:08:46 +00003717 envlist = parse_envlist(env, &envc);
3718 if (envlist == NULL)
3719 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003720
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003721#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003722 Py_BEGIN_ALLOW_THREADS
3723 spawnval = spawnve(mode, path, argvlist, envlist);
3724 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003725#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003726 if (mode == _OLD_P_OVERLAY)
3727 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003728
Victor Stinner8c62be82010-05-06 00:08:46 +00003729 Py_BEGIN_ALLOW_THREADS
3730 spawnval = _spawnve(mode, path, argvlist, envlist);
3731 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003732#endif
Tim Peters25059d32001-12-07 20:35:43 +00003733
Victor Stinner8c62be82010-05-06 00:08:46 +00003734 if (spawnval == -1)
3735 (void) posix_error();
3736 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003737#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003739#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003740 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003741#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003742
Victor Stinner8c62be82010-05-06 00:08:46 +00003743 while (--envc >= 0)
3744 PyMem_DEL(envlist[envc]);
3745 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003746 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003747 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003748 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003749 Py_DECREF(opath);
3750 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003751}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003752
3753/* OS/2 supports spawnvp & spawnvpe natively */
3754#if defined(PYOS_OS2)
3755PyDoc_STRVAR(posix_spawnvp__doc__,
3756"spawnvp(mode, file, args)\n\n\
3757Execute the program 'file' in a new process, using the environment\n\
3758search path to find the file.\n\
3759\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003760 mode: mode of process creation\n\
3761 file: executable file name\n\
3762 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003763
3764static PyObject *
3765posix_spawnvp(PyObject *self, PyObject *args)
3766{
Victor Stinner8c62be82010-05-06 00:08:46 +00003767 PyObject *opath;
3768 char *path;
3769 PyObject *argv;
3770 char **argvlist;
3771 int mode, i, argc;
3772 Py_intptr_t spawnval;
3773 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003774
Victor Stinner8c62be82010-05-06 00:08:46 +00003775 /* spawnvp has three arguments: (mode, path, argv), where
3776 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003777
Victor Stinner8c62be82010-05-06 00:08:46 +00003778 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3779 PyUnicode_FSConverter,
3780 &opath, &argv))
3781 return NULL;
3782 path = PyBytes_AsString(opath);
3783 if (PyList_Check(argv)) {
3784 argc = PyList_Size(argv);
3785 getitem = PyList_GetItem;
3786 }
3787 else if (PyTuple_Check(argv)) {
3788 argc = PyTuple_Size(argv);
3789 getitem = PyTuple_GetItem;
3790 }
3791 else {
3792 PyErr_SetString(PyExc_TypeError,
3793 "spawnvp() arg 2 must be a tuple or list");
3794 Py_DECREF(opath);
3795 return NULL;
3796 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003797
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 argvlist = PyMem_NEW(char *, argc+1);
3799 if (argvlist == NULL) {
3800 Py_DECREF(opath);
3801 return PyErr_NoMemory();
3802 }
3803 for (i = 0; i < argc; i++) {
3804 if (!fsconvert_strdup((*getitem)(argv, i),
3805 &argvlist[i])) {
3806 free_string_array(argvlist, i);
3807 PyErr_SetString(
3808 PyExc_TypeError,
3809 "spawnvp() arg 2 must contain only strings");
3810 Py_DECREF(opath);
3811 return NULL;
3812 }
3813 }
3814 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003815
Victor Stinner8c62be82010-05-06 00:08:46 +00003816 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003817#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003818 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003819#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003820 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003821#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003822 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003823
Victor Stinner8c62be82010-05-06 00:08:46 +00003824 free_string_array(argvlist, argc);
3825 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003826
Victor Stinner8c62be82010-05-06 00:08:46 +00003827 if (spawnval == -1)
3828 return posix_error();
3829 else
3830 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003831}
3832
3833
3834PyDoc_STRVAR(posix_spawnvpe__doc__,
3835"spawnvpe(mode, file, args, env)\n\n\
3836Execute the program 'file' in a new process, using the environment\n\
3837search path to find the file.\n\
3838\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 mode: mode of process creation\n\
3840 file: executable file name\n\
3841 args: tuple or list of arguments\n\
3842 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003843
3844static PyObject *
3845posix_spawnvpe(PyObject *self, PyObject *args)
3846{
Victor Stinner8c62be82010-05-06 00:08:46 +00003847 PyObject *opath
3848 char *path;
3849 PyObject *argv, *env;
3850 char **argvlist;
3851 char **envlist;
3852 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003853 int mode;
3854 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003855 Py_intptr_t spawnval;
3856 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3857 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003858
Victor Stinner8c62be82010-05-06 00:08:46 +00003859 /* spawnvpe has four arguments: (mode, path, argv, env), where
3860 argv is a list or tuple of strings and env is a dictionary
3861 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003862
Victor Stinner8c62be82010-05-06 00:08:46 +00003863 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3864 PyUnicode_FSConverter,
3865 &opath, &argv, &env))
3866 return NULL;
3867 path = PyBytes_AsString(opath);
3868 if (PyList_Check(argv)) {
3869 argc = PyList_Size(argv);
3870 getitem = PyList_GetItem;
3871 }
3872 else if (PyTuple_Check(argv)) {
3873 argc = PyTuple_Size(argv);
3874 getitem = PyTuple_GetItem;
3875 }
3876 else {
3877 PyErr_SetString(PyExc_TypeError,
3878 "spawnvpe() arg 2 must be a tuple or list");
3879 goto fail_0;
3880 }
3881 if (!PyMapping_Check(env)) {
3882 PyErr_SetString(PyExc_TypeError,
3883 "spawnvpe() arg 3 must be a mapping object");
3884 goto fail_0;
3885 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003886
Victor Stinner8c62be82010-05-06 00:08:46 +00003887 argvlist = PyMem_NEW(char *, argc+1);
3888 if (argvlist == NULL) {
3889 PyErr_NoMemory();
3890 goto fail_0;
3891 }
3892 for (i = 0; i < argc; i++) {
3893 if (!fsconvert_strdup((*getitem)(argv, i),
3894 &argvlist[i]))
3895 {
3896 lastarg = i;
3897 goto fail_1;
3898 }
3899 }
3900 lastarg = argc;
3901 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003902
Victor Stinner8c62be82010-05-06 00:08:46 +00003903 envlist = parse_envlist(env, &envc);
3904 if (envlist == NULL)
3905 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003906
Victor Stinner8c62be82010-05-06 00:08:46 +00003907 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003908#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003909 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003910#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003911 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003912#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003913 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003914
Victor Stinner8c62be82010-05-06 00:08:46 +00003915 if (spawnval == -1)
3916 (void) posix_error();
3917 else
3918 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003919
Victor Stinner8c62be82010-05-06 00:08:46 +00003920 while (--envc >= 0)
3921 PyMem_DEL(envlist[envc]);
3922 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003923 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003924 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003925 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003926 Py_DECREF(opath);
3927 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003928}
3929#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003930#endif /* HAVE_SPAWNV */
3931
3932
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003933#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003934PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003935"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003936Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3937\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003938Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003939
3940static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003941posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003942{
Victor Stinner8c62be82010-05-06 00:08:46 +00003943 pid_t pid;
3944 int result = 0;
3945 _PyImport_AcquireLock();
3946 pid = fork1();
3947 if (pid == 0) {
3948 /* child: this clobbers and resets the import lock. */
3949 PyOS_AfterFork();
3950 } else {
3951 /* parent: release the import lock. */
3952 result = _PyImport_ReleaseLock();
3953 }
3954 if (pid == -1)
3955 return posix_error();
3956 if (result < 0) {
3957 /* Don't clobber the OSError if the fork failed. */
3958 PyErr_SetString(PyExc_RuntimeError,
3959 "not holding the import lock");
3960 return NULL;
3961 }
3962 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003963}
3964#endif
3965
3966
Guido van Rossumad0ee831995-03-01 10:34:45 +00003967#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003968PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003969"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003970Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003971Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003972
Barry Warsaw53699e91996-12-10 23:23:01 +00003973static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003974posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003975{
Victor Stinner8c62be82010-05-06 00:08:46 +00003976 pid_t pid;
3977 int result = 0;
3978 _PyImport_AcquireLock();
3979 pid = fork();
3980 if (pid == 0) {
3981 /* child: this clobbers and resets the import lock. */
3982 PyOS_AfterFork();
3983 } else {
3984 /* parent: release the import lock. */
3985 result = _PyImport_ReleaseLock();
3986 }
3987 if (pid == -1)
3988 return posix_error();
3989 if (result < 0) {
3990 /* Don't clobber the OSError if the fork failed. */
3991 PyErr_SetString(PyExc_RuntimeError,
3992 "not holding the import lock");
3993 return NULL;
3994 }
3995 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003996}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003997#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003998
Neal Norwitzb59798b2003-03-21 01:43:31 +00003999/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004000/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4001#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004002#define DEV_PTY_FILE "/dev/ptc"
4003#define HAVE_DEV_PTMX
4004#else
4005#define DEV_PTY_FILE "/dev/ptmx"
4006#endif
4007
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004008#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004009#ifdef HAVE_PTY_H
4010#include <pty.h>
4011#else
4012#ifdef HAVE_LIBUTIL_H
4013#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004014#else
4015#ifdef HAVE_UTIL_H
4016#include <util.h>
4017#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004018#endif /* HAVE_LIBUTIL_H */
4019#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004020#ifdef HAVE_STROPTS_H
4021#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004022#endif
4023#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004024
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004025#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004026PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004027"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004028Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004029
4030static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004031posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004032{
Victor Stinner8c62be82010-05-06 00:08:46 +00004033 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004034#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004036#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004037#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004038 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004039#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004040 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004041#endif
4042#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004043
Thomas Wouters70c21a12000-07-14 14:28:33 +00004044#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4046 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004047#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004048 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4049 if (slave_name == NULL)
4050 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004051
Victor Stinner8c62be82010-05-06 00:08:46 +00004052 slave_fd = open(slave_name, O_RDWR);
4053 if (slave_fd < 0)
4054 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004055#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004056 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4057 if (master_fd < 0)
4058 return posix_error();
4059 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4060 /* change permission of slave */
4061 if (grantpt(master_fd) < 0) {
4062 PyOS_setsig(SIGCHLD, sig_saved);
4063 return posix_error();
4064 }
4065 /* unlock slave */
4066 if (unlockpt(master_fd) < 0) {
4067 PyOS_setsig(SIGCHLD, sig_saved);
4068 return posix_error();
4069 }
4070 PyOS_setsig(SIGCHLD, sig_saved);
4071 slave_name = ptsname(master_fd); /* get name of slave */
4072 if (slave_name == NULL)
4073 return posix_error();
4074 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4075 if (slave_fd < 0)
4076 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004077#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004078 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4079 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004080#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004081 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004082#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004083#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004084#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004085
Victor Stinner8c62be82010-05-06 00:08:46 +00004086 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004087
Fred Drake8cef4cf2000-06-28 16:40:38 +00004088}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004089#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004090
4091#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004092PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004093"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004094Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4095Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004096To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004097
4098static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004099posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004100{
Victor Stinner8c62be82010-05-06 00:08:46 +00004101 int master_fd = -1, result = 0;
4102 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004103
Victor Stinner8c62be82010-05-06 00:08:46 +00004104 _PyImport_AcquireLock();
4105 pid = forkpty(&master_fd, NULL, NULL, NULL);
4106 if (pid == 0) {
4107 /* child: this clobbers and resets the import lock. */
4108 PyOS_AfterFork();
4109 } else {
4110 /* parent: release the import lock. */
4111 result = _PyImport_ReleaseLock();
4112 }
4113 if (pid == -1)
4114 return posix_error();
4115 if (result < 0) {
4116 /* Don't clobber the OSError if the fork failed. */
4117 PyErr_SetString(PyExc_RuntimeError,
4118 "not holding the import lock");
4119 return NULL;
4120 }
4121 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004122}
4123#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004124
Guido van Rossumad0ee831995-03-01 10:34:45 +00004125#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004126PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004127"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004128Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004129
Barry Warsaw53699e91996-12-10 23:23:01 +00004130static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004131posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004132{
Victor Stinner8c62be82010-05-06 00:08:46 +00004133 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004134}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004135#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004136
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004137
Guido van Rossumad0ee831995-03-01 10:34:45 +00004138#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004139PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004140"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004141Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004142
Barry Warsaw53699e91996-12-10 23:23:01 +00004143static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004144posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004145{
Victor Stinner8c62be82010-05-06 00:08:46 +00004146 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004147}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004148#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004149
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004150
Guido van Rossumad0ee831995-03-01 10:34:45 +00004151#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004152PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004153"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004154Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004155
Barry Warsaw53699e91996-12-10 23:23:01 +00004156static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004157posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004158{
Victor Stinner8c62be82010-05-06 00:08:46 +00004159 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004160}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004161#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004164PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004165"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004166Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004167
Barry Warsaw53699e91996-12-10 23:23:01 +00004168static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004169posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004170{
Victor Stinner8c62be82010-05-06 00:08:46 +00004171 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004172}
4173
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004174
Fred Drakec9680921999-12-13 16:37:25 +00004175#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004176PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004177"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004178Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004179
4180static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004181posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004182{
4183 PyObject *result = NULL;
4184
Fred Drakec9680921999-12-13 16:37:25 +00004185#ifdef NGROUPS_MAX
4186#define MAX_GROUPS NGROUPS_MAX
4187#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004188 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004189#define MAX_GROUPS 64
4190#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004191 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004192
4193 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4194 * This is a helper variable to store the intermediate result when
4195 * that happens.
4196 *
4197 * To keep the code readable the OSX behaviour is unconditional,
4198 * according to the POSIX spec this should be safe on all unix-y
4199 * systems.
4200 */
4201 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004202 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004203
Victor Stinner8c62be82010-05-06 00:08:46 +00004204 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004205 if (n < 0) {
4206 if (errno == EINVAL) {
4207 n = getgroups(0, NULL);
4208 if (n == -1) {
4209 return posix_error();
4210 }
4211 if (n == 0) {
4212 /* Avoid malloc(0) */
4213 alt_grouplist = grouplist;
4214 } else {
4215 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4216 if (alt_grouplist == NULL) {
4217 errno = EINVAL;
4218 return posix_error();
4219 }
4220 n = getgroups(n, alt_grouplist);
4221 if (n == -1) {
4222 PyMem_Free(alt_grouplist);
4223 return posix_error();
4224 }
4225 }
4226 } else {
4227 return posix_error();
4228 }
4229 }
4230 result = PyList_New(n);
4231 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004232 int i;
4233 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004234 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004235 if (o == NULL) {
4236 Py_DECREF(result);
4237 result = NULL;
4238 break;
Fred Drakec9680921999-12-13 16:37:25 +00004239 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004240 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004241 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004242 }
4243
4244 if (alt_grouplist != grouplist) {
4245 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004246 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004247
Fred Drakec9680921999-12-13 16:37:25 +00004248 return result;
4249}
4250#endif
4251
Antoine Pitroub7572f02009-12-02 20:46:48 +00004252#ifdef HAVE_INITGROUPS
4253PyDoc_STRVAR(posix_initgroups__doc__,
4254"initgroups(username, gid) -> None\n\n\
4255Call the system initgroups() to initialize the group access list with all of\n\
4256the groups of which the specified username is a member, plus the specified\n\
4257group id.");
4258
4259static PyObject *
4260posix_initgroups(PyObject *self, PyObject *args)
4261{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004262 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004263 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004264 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004265 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004266
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004267 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4268 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004269 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004270 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004271
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004272 res = initgroups(username, (gid_t) gid);
4273 Py_DECREF(oname);
4274 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004275 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004276
Victor Stinner8c62be82010-05-06 00:08:46 +00004277 Py_INCREF(Py_None);
4278 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004279}
4280#endif
4281
Martin v. Löwis606edc12002-06-13 21:09:11 +00004282#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004283PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004284"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004285Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004286
4287static PyObject *
4288posix_getpgid(PyObject *self, PyObject *args)
4289{
Victor Stinner8c62be82010-05-06 00:08:46 +00004290 pid_t pid, pgid;
4291 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4292 return NULL;
4293 pgid = getpgid(pid);
4294 if (pgid < 0)
4295 return posix_error();
4296 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004297}
4298#endif /* HAVE_GETPGID */
4299
4300
Guido van Rossumb6775db1994-08-01 11:34:53 +00004301#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004302PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004303"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004304Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004305
Barry Warsaw53699e91996-12-10 23:23:01 +00004306static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004307posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004308{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004309#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004310 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004311#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004312 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004313#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004314}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004315#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004316
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004317
Guido van Rossumb6775db1994-08-01 11:34:53 +00004318#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004319PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004320"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004321Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004322
Barry Warsaw53699e91996-12-10 23:23:01 +00004323static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004324posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004325{
Guido van Rossum64933891994-10-20 21:56:42 +00004326#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004327 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004328#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004329 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004330#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004331 return posix_error();
4332 Py_INCREF(Py_None);
4333 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004334}
4335
Guido van Rossumb6775db1994-08-01 11:34:53 +00004336#endif /* HAVE_SETPGRP */
4337
Guido van Rossumad0ee831995-03-01 10:34:45 +00004338#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004339PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004340"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004341Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004342
Barry Warsaw53699e91996-12-10 23:23:01 +00004343static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004344posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004345{
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004347}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004348#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004350
Fred Drake12c6e2d1999-12-14 21:25:03 +00004351#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004352PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004353"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004354Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004355
4356static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004357posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004358{
Victor Stinner8c62be82010-05-06 00:08:46 +00004359 PyObject *result = NULL;
4360 char *name;
4361 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004362
Victor Stinner8c62be82010-05-06 00:08:46 +00004363 errno = 0;
4364 name = getlogin();
4365 if (name == NULL) {
4366 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004367 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004368 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004369 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004370 }
4371 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004372 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004374
Fred Drake12c6e2d1999-12-14 21:25:03 +00004375 return result;
4376}
4377#endif
4378
Guido van Rossumad0ee831995-03-01 10:34:45 +00004379#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004380PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004381"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004382Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004383
Barry Warsaw53699e91996-12-10 23:23:01 +00004384static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004385posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004386{
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004388}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004389#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004390
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004391
Guido van Rossumad0ee831995-03-01 10:34:45 +00004392#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004393PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004394"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004395Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004396
Barry Warsaw53699e91996-12-10 23:23:01 +00004397static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004398posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004399{
Victor Stinner8c62be82010-05-06 00:08:46 +00004400 pid_t pid;
4401 int sig;
4402 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4403 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004404#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004405 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4406 APIRET rc;
4407 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004408 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004409
4410 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4411 APIRET rc;
4412 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004413 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004414
4415 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004416 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004417#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004418 if (kill(pid, sig) == -1)
4419 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004420#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004421 Py_INCREF(Py_None);
4422 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004423}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004424#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004425
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004426#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004427PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004428"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004429Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004430
4431static PyObject *
4432posix_killpg(PyObject *self, PyObject *args)
4433{
Victor Stinner8c62be82010-05-06 00:08:46 +00004434 int sig;
4435 pid_t pgid;
4436 /* XXX some man pages make the `pgid` parameter an int, others
4437 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4438 take the same type. Moreover, pid_t is always at least as wide as
4439 int (else compilation of this module fails), which is safe. */
4440 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4441 return NULL;
4442 if (killpg(pgid, sig) == -1)
4443 return posix_error();
4444 Py_INCREF(Py_None);
4445 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004446}
4447#endif
4448
Brian Curtineb24d742010-04-12 17:16:38 +00004449#ifdef MS_WINDOWS
4450PyDoc_STRVAR(win32_kill__doc__,
4451"kill(pid, sig)\n\n\
4452Kill a process with a signal.");
4453
4454static PyObject *
4455win32_kill(PyObject *self, PyObject *args)
4456{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004457 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004458 DWORD pid, sig, err;
4459 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004460
Victor Stinner8c62be82010-05-06 00:08:46 +00004461 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4462 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004463
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 /* Console processes which share a common console can be sent CTRL+C or
4465 CTRL+BREAK events, provided they handle said events. */
4466 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4467 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4468 err = GetLastError();
4469 PyErr_SetFromWindowsErr(err);
4470 }
4471 else
4472 Py_RETURN_NONE;
4473 }
Brian Curtineb24d742010-04-12 17:16:38 +00004474
Victor Stinner8c62be82010-05-06 00:08:46 +00004475 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4476 attempt to open and terminate the process. */
4477 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4478 if (handle == NULL) {
4479 err = GetLastError();
4480 return PyErr_SetFromWindowsErr(err);
4481 }
Brian Curtineb24d742010-04-12 17:16:38 +00004482
Victor Stinner8c62be82010-05-06 00:08:46 +00004483 if (TerminateProcess(handle, sig) == 0) {
4484 err = GetLastError();
4485 result = PyErr_SetFromWindowsErr(err);
4486 } else {
4487 Py_INCREF(Py_None);
4488 result = Py_None;
4489 }
Brian Curtineb24d742010-04-12 17:16:38 +00004490
Victor Stinner8c62be82010-05-06 00:08:46 +00004491 CloseHandle(handle);
4492 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004493}
4494#endif /* MS_WINDOWS */
4495
Guido van Rossumc0125471996-06-28 18:55:32 +00004496#ifdef HAVE_PLOCK
4497
4498#ifdef HAVE_SYS_LOCK_H
4499#include <sys/lock.h>
4500#endif
4501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004502PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004503"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004504Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004505
Barry Warsaw53699e91996-12-10 23:23:01 +00004506static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004507posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004508{
Victor Stinner8c62be82010-05-06 00:08:46 +00004509 int op;
4510 if (!PyArg_ParseTuple(args, "i:plock", &op))
4511 return NULL;
4512 if (plock(op) == -1)
4513 return posix_error();
4514 Py_INCREF(Py_None);
4515 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004516}
4517#endif
4518
Guido van Rossumb6775db1994-08-01 11:34:53 +00004519#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004520PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004521"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004522Set the current process's user id.");
4523
Barry Warsaw53699e91996-12-10 23:23:01 +00004524static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004525posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004526{
Victor Stinner8c62be82010-05-06 00:08:46 +00004527 long uid_arg;
4528 uid_t uid;
4529 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4530 return NULL;
4531 uid = uid_arg;
4532 if (uid != uid_arg) {
4533 PyErr_SetString(PyExc_OverflowError, "user id too big");
4534 return NULL;
4535 }
4536 if (setuid(uid) < 0)
4537 return posix_error();
4538 Py_INCREF(Py_None);
4539 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004540}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004541#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004542
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004543
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004544#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004545PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004546"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004547Set the current process's effective user id.");
4548
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004549static PyObject *
4550posix_seteuid (PyObject *self, PyObject *args)
4551{
Victor Stinner8c62be82010-05-06 00:08:46 +00004552 long euid_arg;
4553 uid_t euid;
4554 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4555 return NULL;
4556 euid = euid_arg;
4557 if (euid != euid_arg) {
4558 PyErr_SetString(PyExc_OverflowError, "user id too big");
4559 return NULL;
4560 }
4561 if (seteuid(euid) < 0) {
4562 return posix_error();
4563 } else {
4564 Py_INCREF(Py_None);
4565 return Py_None;
4566 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004567}
4568#endif /* HAVE_SETEUID */
4569
4570#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004571PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004572"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004573Set the current process's effective group id.");
4574
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004575static PyObject *
4576posix_setegid (PyObject *self, PyObject *args)
4577{
Victor Stinner8c62be82010-05-06 00:08:46 +00004578 long egid_arg;
4579 gid_t egid;
4580 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4581 return NULL;
4582 egid = egid_arg;
4583 if (egid != egid_arg) {
4584 PyErr_SetString(PyExc_OverflowError, "group id too big");
4585 return NULL;
4586 }
4587 if (setegid(egid) < 0) {
4588 return posix_error();
4589 } else {
4590 Py_INCREF(Py_None);
4591 return Py_None;
4592 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004593}
4594#endif /* HAVE_SETEGID */
4595
4596#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004597PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004598"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004599Set the current process's real and effective user ids.");
4600
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004601static PyObject *
4602posix_setreuid (PyObject *self, PyObject *args)
4603{
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 long ruid_arg, euid_arg;
4605 uid_t ruid, euid;
4606 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4607 return NULL;
4608 if (ruid_arg == -1)
4609 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4610 else
4611 ruid = ruid_arg; /* otherwise, assign from our long */
4612 if (euid_arg == -1)
4613 euid = (uid_t)-1;
4614 else
4615 euid = euid_arg;
4616 if ((euid_arg != -1 && euid != euid_arg) ||
4617 (ruid_arg != -1 && ruid != ruid_arg)) {
4618 PyErr_SetString(PyExc_OverflowError, "user id too big");
4619 return NULL;
4620 }
4621 if (setreuid(ruid, euid) < 0) {
4622 return posix_error();
4623 } else {
4624 Py_INCREF(Py_None);
4625 return Py_None;
4626 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004627}
4628#endif /* HAVE_SETREUID */
4629
4630#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004631PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004632"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004633Set the current process's real and effective group ids.");
4634
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004635static PyObject *
4636posix_setregid (PyObject *self, PyObject *args)
4637{
Victor Stinner8c62be82010-05-06 00:08:46 +00004638 long rgid_arg, egid_arg;
4639 gid_t rgid, egid;
4640 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4641 return NULL;
4642 if (rgid_arg == -1)
4643 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4644 else
4645 rgid = rgid_arg; /* otherwise, assign from our long */
4646 if (egid_arg == -1)
4647 egid = (gid_t)-1;
4648 else
4649 egid = egid_arg;
4650 if ((egid_arg != -1 && egid != egid_arg) ||
4651 (rgid_arg != -1 && rgid != rgid_arg)) {
4652 PyErr_SetString(PyExc_OverflowError, "group id too big");
4653 return NULL;
4654 }
4655 if (setregid(rgid, egid) < 0) {
4656 return posix_error();
4657 } else {
4658 Py_INCREF(Py_None);
4659 return Py_None;
4660 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004661}
4662#endif /* HAVE_SETREGID */
4663
Guido van Rossumb6775db1994-08-01 11:34:53 +00004664#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004665PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004666"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004667Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004668
Barry Warsaw53699e91996-12-10 23:23:01 +00004669static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004670posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004671{
Victor Stinner8c62be82010-05-06 00:08:46 +00004672 long gid_arg;
4673 gid_t gid;
4674 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4675 return NULL;
4676 gid = gid_arg;
4677 if (gid != gid_arg) {
4678 PyErr_SetString(PyExc_OverflowError, "group id too big");
4679 return NULL;
4680 }
4681 if (setgid(gid) < 0)
4682 return posix_error();
4683 Py_INCREF(Py_None);
4684 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004685}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004686#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004687
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004688#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004689PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004690"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004691Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004692
4693static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004694posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004695{
Victor Stinner8c62be82010-05-06 00:08:46 +00004696 int i, len;
4697 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004698
Victor Stinner8c62be82010-05-06 00:08:46 +00004699 if (!PySequence_Check(groups)) {
4700 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4701 return NULL;
4702 }
4703 len = PySequence_Size(groups);
4704 if (len > MAX_GROUPS) {
4705 PyErr_SetString(PyExc_ValueError, "too many groups");
4706 return NULL;
4707 }
4708 for(i = 0; i < len; i++) {
4709 PyObject *elem;
4710 elem = PySequence_GetItem(groups, i);
4711 if (!elem)
4712 return NULL;
4713 if (!PyLong_Check(elem)) {
4714 PyErr_SetString(PyExc_TypeError,
4715 "groups must be integers");
4716 Py_DECREF(elem);
4717 return NULL;
4718 } else {
4719 unsigned long x = PyLong_AsUnsignedLong(elem);
4720 if (PyErr_Occurred()) {
4721 PyErr_SetString(PyExc_TypeError,
4722 "group id too big");
4723 Py_DECREF(elem);
4724 return NULL;
4725 }
4726 grouplist[i] = x;
4727 /* read back the value to see if it fitted in gid_t */
4728 if (grouplist[i] != x) {
4729 PyErr_SetString(PyExc_TypeError,
4730 "group id too big");
4731 Py_DECREF(elem);
4732 return NULL;
4733 }
4734 }
4735 Py_DECREF(elem);
4736 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004737
Victor Stinner8c62be82010-05-06 00:08:46 +00004738 if (setgroups(len, grouplist) < 0)
4739 return posix_error();
4740 Py_INCREF(Py_None);
4741 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004742}
4743#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004744
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004745#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4746static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004747wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004748{
Victor Stinner8c62be82010-05-06 00:08:46 +00004749 PyObject *result;
4750 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004751
Victor Stinner8c62be82010-05-06 00:08:46 +00004752 if (pid == -1)
4753 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004754
Victor Stinner8c62be82010-05-06 00:08:46 +00004755 if (struct_rusage == NULL) {
4756 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4757 if (m == NULL)
4758 return NULL;
4759 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4760 Py_DECREF(m);
4761 if (struct_rusage == NULL)
4762 return NULL;
4763 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004764
Victor Stinner8c62be82010-05-06 00:08:46 +00004765 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4766 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4767 if (!result)
4768 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004769
4770#ifndef doubletime
4771#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4772#endif
4773
Victor Stinner8c62be82010-05-06 00:08:46 +00004774 PyStructSequence_SET_ITEM(result, 0,
4775 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4776 PyStructSequence_SET_ITEM(result, 1,
4777 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004778#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004779 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4780 SET_INT(result, 2, ru->ru_maxrss);
4781 SET_INT(result, 3, ru->ru_ixrss);
4782 SET_INT(result, 4, ru->ru_idrss);
4783 SET_INT(result, 5, ru->ru_isrss);
4784 SET_INT(result, 6, ru->ru_minflt);
4785 SET_INT(result, 7, ru->ru_majflt);
4786 SET_INT(result, 8, ru->ru_nswap);
4787 SET_INT(result, 9, ru->ru_inblock);
4788 SET_INT(result, 10, ru->ru_oublock);
4789 SET_INT(result, 11, ru->ru_msgsnd);
4790 SET_INT(result, 12, ru->ru_msgrcv);
4791 SET_INT(result, 13, ru->ru_nsignals);
4792 SET_INT(result, 14, ru->ru_nvcsw);
4793 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004794#undef SET_INT
4795
Victor Stinner8c62be82010-05-06 00:08:46 +00004796 if (PyErr_Occurred()) {
4797 Py_DECREF(result);
4798 return NULL;
4799 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004800
Victor Stinner8c62be82010-05-06 00:08:46 +00004801 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004802}
4803#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4804
4805#ifdef HAVE_WAIT3
4806PyDoc_STRVAR(posix_wait3__doc__,
4807"wait3(options) -> (pid, status, rusage)\n\n\
4808Wait for completion of a child process.");
4809
4810static PyObject *
4811posix_wait3(PyObject *self, PyObject *args)
4812{
Victor Stinner8c62be82010-05-06 00:08:46 +00004813 pid_t pid;
4814 int options;
4815 struct rusage ru;
4816 WAIT_TYPE status;
4817 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004818
Victor Stinner8c62be82010-05-06 00:08:46 +00004819 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4820 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004821
Victor Stinner8c62be82010-05-06 00:08:46 +00004822 Py_BEGIN_ALLOW_THREADS
4823 pid = wait3(&status, options, &ru);
4824 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004825
Victor Stinner8c62be82010-05-06 00:08:46 +00004826 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004827}
4828#endif /* HAVE_WAIT3 */
4829
4830#ifdef HAVE_WAIT4
4831PyDoc_STRVAR(posix_wait4__doc__,
4832"wait4(pid, options) -> (pid, status, rusage)\n\n\
4833Wait for completion of a given child process.");
4834
4835static PyObject *
4836posix_wait4(PyObject *self, PyObject *args)
4837{
Victor Stinner8c62be82010-05-06 00:08:46 +00004838 pid_t pid;
4839 int options;
4840 struct rusage ru;
4841 WAIT_TYPE status;
4842 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004843
Victor Stinner8c62be82010-05-06 00:08:46 +00004844 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4845 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004846
Victor Stinner8c62be82010-05-06 00:08:46 +00004847 Py_BEGIN_ALLOW_THREADS
4848 pid = wait4(pid, &status, options, &ru);
4849 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004850
Victor Stinner8c62be82010-05-06 00:08:46 +00004851 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004852}
4853#endif /* HAVE_WAIT4 */
4854
Guido van Rossumb6775db1994-08-01 11:34:53 +00004855#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004856PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004857"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004858Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004859
Barry Warsaw53699e91996-12-10 23:23:01 +00004860static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004861posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004862{
Victor Stinner8c62be82010-05-06 00:08:46 +00004863 pid_t pid;
4864 int options;
4865 WAIT_TYPE status;
4866 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004867
Victor Stinner8c62be82010-05-06 00:08:46 +00004868 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4869 return NULL;
4870 Py_BEGIN_ALLOW_THREADS
4871 pid = waitpid(pid, &status, options);
4872 Py_END_ALLOW_THREADS
4873 if (pid == -1)
4874 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004875
Victor Stinner8c62be82010-05-06 00:08:46 +00004876 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004877}
4878
Tim Petersab034fa2002-02-01 11:27:43 +00004879#elif defined(HAVE_CWAIT)
4880
4881/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004882PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004883"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004884"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004885
4886static PyObject *
4887posix_waitpid(PyObject *self, PyObject *args)
4888{
Victor Stinner8c62be82010-05-06 00:08:46 +00004889 Py_intptr_t pid;
4890 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004891
Victor Stinner8c62be82010-05-06 00:08:46 +00004892 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4893 return NULL;
4894 Py_BEGIN_ALLOW_THREADS
4895 pid = _cwait(&status, pid, options);
4896 Py_END_ALLOW_THREADS
4897 if (pid == -1)
4898 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004899
Victor Stinner8c62be82010-05-06 00:08:46 +00004900 /* shift the status left a byte so this is more like the POSIX waitpid */
4901 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004902}
4903#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004904
Guido van Rossumad0ee831995-03-01 10:34:45 +00004905#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004906PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004907"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004908Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004909
Barry Warsaw53699e91996-12-10 23:23:01 +00004910static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004911posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004912{
Victor Stinner8c62be82010-05-06 00:08:46 +00004913 pid_t pid;
4914 WAIT_TYPE status;
4915 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004916
Victor Stinner8c62be82010-05-06 00:08:46 +00004917 Py_BEGIN_ALLOW_THREADS
4918 pid = wait(&status);
4919 Py_END_ALLOW_THREADS
4920 if (pid == -1)
4921 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004922
Victor Stinner8c62be82010-05-06 00:08:46 +00004923 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004924}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004925#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004926
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004928PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004929"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004930Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004931
Barry Warsaw53699e91996-12-10 23:23:01 +00004932static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004933posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004934{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004935#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00004936 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004937#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004938#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00004939 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
4940 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004941#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004943#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004944#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004945}
4946
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004947
Guido van Rossumb6775db1994-08-01 11:34:53 +00004948#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004949PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004950"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004951Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004952
Barry Warsaw53699e91996-12-10 23:23:01 +00004953static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004954posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004955{
Victor Stinner8c62be82010-05-06 00:08:46 +00004956 PyObject* v;
4957 char buf[MAXPATHLEN];
4958 PyObject *opath;
4959 char *path;
4960 int n;
4961 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004962
Victor Stinner8c62be82010-05-06 00:08:46 +00004963 if (!PyArg_ParseTuple(args, "O&:readlink",
4964 PyUnicode_FSConverter, &opath))
4965 return NULL;
4966 path = PyBytes_AsString(opath);
4967 v = PySequence_GetItem(args, 0);
4968 if (v == NULL) {
4969 Py_DECREF(opath);
4970 return NULL;
4971 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004972
Victor Stinner8c62be82010-05-06 00:08:46 +00004973 if (PyUnicode_Check(v)) {
4974 arg_is_unicode = 1;
4975 }
4976 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004977
Victor Stinner8c62be82010-05-06 00:08:46 +00004978 Py_BEGIN_ALLOW_THREADS
4979 n = readlink(path, buf, (int) sizeof buf);
4980 Py_END_ALLOW_THREADS
4981 if (n < 0)
4982 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004983
Victor Stinner8c62be82010-05-06 00:08:46 +00004984 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00004985 if (arg_is_unicode)
4986 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
4987 else
4988 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004989}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004990#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004991
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004992
Guido van Rossumb6775db1994-08-01 11:34:53 +00004993#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004994PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004995"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004996Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004997
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004999posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005000{
Victor Stinner8c62be82010-05-06 00:08:46 +00005001 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005002}
5003#endif /* HAVE_SYMLINK */
5004
Brian Curtind40e6f72010-07-08 21:39:08 +00005005#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5006
5007PyDoc_STRVAR(win_readlink__doc__,
5008"readlink(path) -> path\n\n\
5009Return a string representing the path to which the symbolic link points.");
5010
5011/* The following structure was copied from
5012 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
5013 include doesn't seem to be present in the Windows SDK (at least as included
5014 with Visual Studio Express). */
5015typedef struct _REPARSE_DATA_BUFFER {
5016 ULONG ReparseTag;
5017 USHORT ReparseDataLength;
5018 USHORT Reserved;
5019 union {
5020 struct {
5021 USHORT SubstituteNameOffset;
5022 USHORT SubstituteNameLength;
5023 USHORT PrintNameOffset;
5024 USHORT PrintNameLength;
5025 ULONG Flags;
5026 WCHAR PathBuffer[1];
5027 } SymbolicLinkReparseBuffer;
5028
5029 struct {
5030 USHORT SubstituteNameOffset;
5031 USHORT SubstituteNameLength;
5032 USHORT PrintNameOffset;
5033 USHORT PrintNameLength;
5034 WCHAR PathBuffer[1];
5035 } MountPointReparseBuffer;
5036
5037 struct {
5038 UCHAR DataBuffer[1];
5039 } GenericReparseBuffer;
5040 };
5041} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
5042
Brian Curtin74e45612010-07-09 15:58:59 +00005043#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
5044 GenericReparseBuffer)
Brian Curtind40e6f72010-07-08 21:39:08 +00005045
5046#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
5047
5048/* Windows readlink implementation */
5049static PyObject *
5050win_readlink(PyObject *self, PyObject *args)
5051{
5052 wchar_t *path;
5053 DWORD n_bytes_returned;
5054 DWORD io_result;
5055 PyObject *result;
5056 HANDLE reparse_point_handle;
5057
5058 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5059 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5060 wchar_t *print_name;
5061
5062 if (!PyArg_ParseTuple(args,
5063 "u:readlink",
5064 &path))
5065 return NULL;
5066
5067 /* First get a handle to the reparse point */
5068 Py_BEGIN_ALLOW_THREADS
5069 reparse_point_handle = CreateFileW(
5070 path,
5071 0,
5072 0,
5073 0,
5074 OPEN_EXISTING,
5075 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5076 0);
5077 Py_END_ALLOW_THREADS
5078
5079 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5080 {
5081 return win32_error_unicode("readlink", path);
5082 }
5083
5084 Py_BEGIN_ALLOW_THREADS
5085 /* New call DeviceIoControl to read the reparse point */
5086 io_result = DeviceIoControl(
5087 reparse_point_handle,
5088 FSCTL_GET_REPARSE_POINT,
5089 0, 0, /* in buffer */
5090 target_buffer, sizeof(target_buffer),
5091 &n_bytes_returned,
5092 0 /* we're not using OVERLAPPED_IO */
5093 );
5094 CloseHandle(reparse_point_handle);
5095 Py_END_ALLOW_THREADS
5096
5097 if (io_result==0)
5098 {
5099 return win32_error_unicode("readlink", path);
5100 }
5101
5102 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5103 {
5104 PyErr_SetString(PyExc_ValueError,
5105 "not a symbolic link");
5106 return NULL;
5107 }
Brian Curtin74e45612010-07-09 15:58:59 +00005108 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5109 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5110
5111 result = PyUnicode_FromWideChar(print_name,
5112 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005113 return result;
5114}
5115
5116#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5117
5118#if !defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
5119
5120/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5121static int has_CreateSymbolicLinkW = 0;
5122static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5123static int
5124check_CreateSymbolicLinkW()
5125{
5126 HINSTANCE hKernel32;
5127 /* only recheck */
5128 if (has_CreateSymbolicLinkW)
5129 return has_CreateSymbolicLinkW;
5130 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005131 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5132 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005133 if (Py_CreateSymbolicLinkW)
5134 has_CreateSymbolicLinkW = 1;
5135 return has_CreateSymbolicLinkW;
5136}
5137
5138PyDoc_STRVAR(win_symlink__doc__,
5139"symlink(src, dst, target_is_directory=False)\n\n\
5140Create a symbolic link pointing to src named dst.\n\
5141target_is_directory is required if the target is to be interpreted as\n\
5142a directory.\n\
5143This function requires Windows 6.0 or greater, and raises a\n\
5144NotImplementedError otherwise.");
5145
5146static PyObject *
5147win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5148{
5149 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5150 PyObject *src, *dest;
5151 int target_is_directory = 0;
5152 DWORD res;
5153 WIN32_FILE_ATTRIBUTE_DATA src_info;
5154
5155 if (!check_CreateSymbolicLinkW())
5156 {
5157 /* raise NotImplementedError */
5158 return PyErr_Format(PyExc_NotImplementedError,
5159 "CreateSymbolicLinkW not found");
5160 }
5161 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5162 kwlist, &src, &dest, &target_is_directory))
5163 return NULL;
5164 if (!convert_to_unicode(&src)) { return NULL; }
5165 if (!convert_to_unicode(&dest)) {
5166 Py_DECREF(src);
5167 return NULL;
5168 }
5169
5170 /* if src is a directory, ensure target_is_directory==1 */
5171 if(
5172 GetFileAttributesExW(
5173 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5174 ))
5175 {
5176 target_is_directory = target_is_directory ||
5177 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5178 }
5179
5180 Py_BEGIN_ALLOW_THREADS
5181 res = Py_CreateSymbolicLinkW(
5182 PyUnicode_AsUnicode(dest),
5183 PyUnicode_AsUnicode(src),
5184 target_is_directory);
5185 Py_END_ALLOW_THREADS
5186 Py_DECREF(src);
5187 Py_DECREF(dest);
5188 if (!res)
5189 {
5190 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5191 }
5192
5193 Py_INCREF(Py_None);
5194 return Py_None;
5195}
5196#endif /* !defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005197
5198#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005199#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5200static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005201system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005202{
5203 ULONG value = 0;
5204
5205 Py_BEGIN_ALLOW_THREADS
5206 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5207 Py_END_ALLOW_THREADS
5208
5209 return value;
5210}
5211
5212static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005213posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005214{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005215 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005216 return Py_BuildValue("ddddd",
5217 (double)0 /* t.tms_utime / HZ */,
5218 (double)0 /* t.tms_stime / HZ */,
5219 (double)0 /* t.tms_cutime / HZ */,
5220 (double)0 /* t.tms_cstime / HZ */,
5221 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005222}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005223#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005224#define NEED_TICKS_PER_SECOND
5225static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005226static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005227posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005228{
Victor Stinner8c62be82010-05-06 00:08:46 +00005229 struct tms t;
5230 clock_t c;
5231 errno = 0;
5232 c = times(&t);
5233 if (c == (clock_t) -1)
5234 return posix_error();
5235 return Py_BuildValue("ddddd",
5236 (double)t.tms_utime / ticks_per_second,
5237 (double)t.tms_stime / ticks_per_second,
5238 (double)t.tms_cutime / ticks_per_second,
5239 (double)t.tms_cstime / ticks_per_second,
5240 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005241}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005242#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005243#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005244
5245
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005246#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005247#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005248static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005249posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005250{
Victor Stinner8c62be82010-05-06 00:08:46 +00005251 FILETIME create, exit, kernel, user;
5252 HANDLE hProc;
5253 hProc = GetCurrentProcess();
5254 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5255 /* The fields of a FILETIME structure are the hi and lo part
5256 of a 64-bit value expressed in 100 nanosecond units.
5257 1e7 is one second in such units; 1e-7 the inverse.
5258 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5259 */
5260 return Py_BuildValue(
5261 "ddddd",
5262 (double)(user.dwHighDateTime*429.4967296 +
5263 user.dwLowDateTime*1e-7),
5264 (double)(kernel.dwHighDateTime*429.4967296 +
5265 kernel.dwLowDateTime*1e-7),
5266 (double)0,
5267 (double)0,
5268 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005269}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005270#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005271
5272#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005273PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005274"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005275Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005276#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005277
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005278
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005279#ifdef HAVE_GETSID
5280PyDoc_STRVAR(posix_getsid__doc__,
5281"getsid(pid) -> sid\n\n\
5282Call the system call getsid().");
5283
5284static PyObject *
5285posix_getsid(PyObject *self, PyObject *args)
5286{
Victor Stinner8c62be82010-05-06 00:08:46 +00005287 pid_t pid;
5288 int sid;
5289 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5290 return NULL;
5291 sid = getsid(pid);
5292 if (sid < 0)
5293 return posix_error();
5294 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005295}
5296#endif /* HAVE_GETSID */
5297
5298
Guido van Rossumb6775db1994-08-01 11:34:53 +00005299#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005300PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005301"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005302Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005303
Barry Warsaw53699e91996-12-10 23:23:01 +00005304static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005305posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005306{
Victor Stinner8c62be82010-05-06 00:08:46 +00005307 if (setsid() < 0)
5308 return posix_error();
5309 Py_INCREF(Py_None);
5310 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005311}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005312#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005313
Guido van Rossumb6775db1994-08-01 11:34:53 +00005314#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005315PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005316"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005317Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005318
Barry Warsaw53699e91996-12-10 23:23:01 +00005319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005320posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005321{
Victor Stinner8c62be82010-05-06 00:08:46 +00005322 pid_t pid;
5323 int pgrp;
5324 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5325 return NULL;
5326 if (setpgid(pid, pgrp) < 0)
5327 return posix_error();
5328 Py_INCREF(Py_None);
5329 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005330}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005331#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005332
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005333
Guido van Rossumb6775db1994-08-01 11:34:53 +00005334#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005335PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005336"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005337Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005338
Barry Warsaw53699e91996-12-10 23:23:01 +00005339static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005340posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005341{
Victor Stinner8c62be82010-05-06 00:08:46 +00005342 int fd;
5343 pid_t pgid;
5344 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5345 return NULL;
5346 pgid = tcgetpgrp(fd);
5347 if (pgid < 0)
5348 return posix_error();
5349 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005350}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005351#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005352
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005353
Guido van Rossumb6775db1994-08-01 11:34:53 +00005354#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005355PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005356"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005357Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005358
Barry Warsaw53699e91996-12-10 23:23:01 +00005359static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005360posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005361{
Victor Stinner8c62be82010-05-06 00:08:46 +00005362 int fd;
5363 pid_t pgid;
5364 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5365 return NULL;
5366 if (tcsetpgrp(fd, pgid) < 0)
5367 return posix_error();
5368 Py_INCREF(Py_None);
5369 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005370}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005371#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005372
Guido van Rossum687dd131993-05-17 08:34:16 +00005373/* Functions acting on file descriptors */
5374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005375PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005376"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005377Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005378
Barry Warsaw53699e91996-12-10 23:23:01 +00005379static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005380posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005381{
Victor Stinner8c62be82010-05-06 00:08:46 +00005382 PyObject *ofile;
5383 char *file;
5384 int flag;
5385 int mode = 0777;
5386 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005387
5388#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005389 PyUnicodeObject *po;
5390 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5391 Py_BEGIN_ALLOW_THREADS
5392 /* PyUnicode_AS_UNICODE OK without thread
5393 lock as it is a simple dereference. */
5394 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5395 Py_END_ALLOW_THREADS
5396 if (fd < 0)
5397 return posix_error();
5398 return PyLong_FromLong((long)fd);
5399 }
5400 /* Drop the argument parsing error as narrow strings
5401 are also valid. */
5402 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005403#endif
5404
Victor Stinner8c62be82010-05-06 00:08:46 +00005405 if (!PyArg_ParseTuple(args, "O&i|i",
5406 PyUnicode_FSConverter, &ofile,
5407 &flag, &mode))
5408 return NULL;
5409 file = PyBytes_AsString(ofile);
5410 Py_BEGIN_ALLOW_THREADS
5411 fd = open(file, flag, mode);
5412 Py_END_ALLOW_THREADS
5413 if (fd < 0)
5414 return posix_error_with_allocated_filename(ofile);
5415 Py_DECREF(ofile);
5416 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005417}
5418
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005419
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005420PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005421"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005422Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005423
Barry Warsaw53699e91996-12-10 23:23:01 +00005424static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005425posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005426{
Victor Stinner8c62be82010-05-06 00:08:46 +00005427 int fd, res;
5428 if (!PyArg_ParseTuple(args, "i:close", &fd))
5429 return NULL;
5430 if (!_PyVerify_fd(fd))
5431 return posix_error();
5432 Py_BEGIN_ALLOW_THREADS
5433 res = close(fd);
5434 Py_END_ALLOW_THREADS
5435 if (res < 0)
5436 return posix_error();
5437 Py_INCREF(Py_None);
5438 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005439}
5440
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005441
Victor Stinner8c62be82010-05-06 00:08:46 +00005442PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005443"closerange(fd_low, fd_high)\n\n\
5444Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5445
5446static PyObject *
5447posix_closerange(PyObject *self, PyObject *args)
5448{
Victor Stinner8c62be82010-05-06 00:08:46 +00005449 int fd_from, fd_to, i;
5450 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5451 return NULL;
5452 Py_BEGIN_ALLOW_THREADS
5453 for (i = fd_from; i < fd_to; i++)
5454 if (_PyVerify_fd(i))
5455 close(i);
5456 Py_END_ALLOW_THREADS
5457 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005458}
5459
5460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005461PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005462"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005463Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005464
Barry Warsaw53699e91996-12-10 23:23:01 +00005465static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005466posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005467{
Victor Stinner8c62be82010-05-06 00:08:46 +00005468 int fd;
5469 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5470 return NULL;
5471 if (!_PyVerify_fd(fd))
5472 return posix_error();
5473 Py_BEGIN_ALLOW_THREADS
5474 fd = dup(fd);
5475 Py_END_ALLOW_THREADS
5476 if (fd < 0)
5477 return posix_error();
5478 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005479}
5480
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005482PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005483"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005484Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005485
Barry Warsaw53699e91996-12-10 23:23:01 +00005486static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005487posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005488{
Victor Stinner8c62be82010-05-06 00:08:46 +00005489 int fd, fd2, res;
5490 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5491 return NULL;
5492 if (!_PyVerify_fd_dup2(fd, fd2))
5493 return posix_error();
5494 Py_BEGIN_ALLOW_THREADS
5495 res = dup2(fd, fd2);
5496 Py_END_ALLOW_THREADS
5497 if (res < 0)
5498 return posix_error();
5499 Py_INCREF(Py_None);
5500 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005501}
5502
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005505"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005506Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005507
Barry Warsaw53699e91996-12-10 23:23:01 +00005508static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005509posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005510{
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005512#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005513 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005514#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005515 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005516#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005517 PyObject *posobj;
5518 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5519 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005520#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005521 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5522 switch (how) {
5523 case 0: how = SEEK_SET; break;
5524 case 1: how = SEEK_CUR; break;
5525 case 2: how = SEEK_END; break;
5526 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005527#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005528
5529#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005530 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005531#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005532 pos = PyLong_Check(posobj) ?
5533 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005534#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005535 if (PyErr_Occurred())
5536 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005537
Victor Stinner8c62be82010-05-06 00:08:46 +00005538 if (!_PyVerify_fd(fd))
5539 return posix_error();
5540 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005541#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005542 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005543#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005544 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005545#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005546 Py_END_ALLOW_THREADS
5547 if (res < 0)
5548 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005549
5550#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005551 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005552#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005553 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005554#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005555}
5556
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005559"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005561
Barry Warsaw53699e91996-12-10 23:23:01 +00005562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005563posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005564{
Victor Stinner8c62be82010-05-06 00:08:46 +00005565 int fd, size;
5566 Py_ssize_t n;
5567 PyObject *buffer;
5568 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5569 return NULL;
5570 if (size < 0) {
5571 errno = EINVAL;
5572 return posix_error();
5573 }
5574 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5575 if (buffer == NULL)
5576 return NULL;
5577 if (!_PyVerify_fd(fd))
5578 return posix_error();
5579 Py_BEGIN_ALLOW_THREADS
5580 n = read(fd, PyBytes_AS_STRING(buffer), size);
5581 Py_END_ALLOW_THREADS
5582 if (n < 0) {
5583 Py_DECREF(buffer);
5584 return posix_error();
5585 }
5586 if (n != size)
5587 _PyBytes_Resize(&buffer, n);
5588 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005589}
5590
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005591
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005592PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005593"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005594Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005595
Barry Warsaw53699e91996-12-10 23:23:01 +00005596static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005597posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005598{
Victor Stinner8c62be82010-05-06 00:08:46 +00005599 Py_buffer pbuf;
5600 int fd;
5601 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005602
Victor Stinner8c62be82010-05-06 00:08:46 +00005603 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5604 return NULL;
5605 if (!_PyVerify_fd(fd))
5606 return posix_error();
5607 Py_BEGIN_ALLOW_THREADS
5608 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5609 Py_END_ALLOW_THREADS
5610 PyBuffer_Release(&pbuf);
5611 if (size < 0)
5612 return posix_error();
5613 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005614}
5615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005617PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005618"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005619Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005620
Barry Warsaw53699e91996-12-10 23:23:01 +00005621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005622posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005623{
Victor Stinner8c62be82010-05-06 00:08:46 +00005624 int fd;
5625 STRUCT_STAT st;
5626 int res;
5627 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5628 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005629#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005630 /* on OpenVMS we must ensure that all bytes are written to the file */
5631 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005632#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005633 if (!_PyVerify_fd(fd))
5634 return posix_error();
5635 Py_BEGIN_ALLOW_THREADS
5636 res = FSTAT(fd, &st);
5637 Py_END_ALLOW_THREADS
5638 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005639#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005640 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005641#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005642 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005643#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005644 }
Tim Peters5aa91602002-01-30 05:46:57 +00005645
Victor Stinner8c62be82010-05-06 00:08:46 +00005646 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005647}
5648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005649PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005650"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005651Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005652connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005653
5654static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005655posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005656{
Victor Stinner8c62be82010-05-06 00:08:46 +00005657 int fd;
5658 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5659 return NULL;
5660 if (!_PyVerify_fd(fd))
5661 return PyBool_FromLong(0);
5662 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005663}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005664
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005665#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005666PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005667"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005668Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005669
Barry Warsaw53699e91996-12-10 23:23:01 +00005670static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005671posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005672{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005673#if defined(PYOS_OS2)
5674 HFILE read, write;
5675 APIRET rc;
5676
Victor Stinner8c62be82010-05-06 00:08:46 +00005677 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005678 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005679 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005680 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005681 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005682
5683 return Py_BuildValue("(ii)", read, write);
5684#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005685#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005686 int fds[2];
5687 int res;
5688 Py_BEGIN_ALLOW_THREADS
5689 res = pipe(fds);
5690 Py_END_ALLOW_THREADS
5691 if (res != 0)
5692 return posix_error();
5693 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005694#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005695 HANDLE read, write;
5696 int read_fd, write_fd;
5697 BOOL ok;
5698 Py_BEGIN_ALLOW_THREADS
5699 ok = CreatePipe(&read, &write, NULL, 0);
5700 Py_END_ALLOW_THREADS
5701 if (!ok)
5702 return win32_error("CreatePipe", NULL);
5703 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5704 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5705 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005706#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005707#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005708}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005709#endif /* HAVE_PIPE */
5710
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005711
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005712#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005713PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005714"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005715Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005716
Barry Warsaw53699e91996-12-10 23:23:01 +00005717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005718posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005719{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005720 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005721 char *filename;
5722 int mode = 0666;
5723 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005724 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5725 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005726 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005727 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005728 Py_BEGIN_ALLOW_THREADS
5729 res = mkfifo(filename, mode);
5730 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005731 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005732 if (res < 0)
5733 return posix_error();
5734 Py_INCREF(Py_None);
5735 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005736}
5737#endif
5738
5739
Neal Norwitz11690112002-07-30 01:08:28 +00005740#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005741PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005742"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005743Create a filesystem node (file, device special file or named pipe)\n\
5744named filename. mode specifies both the permissions to use and the\n\
5745type of node to be created, being combined (bitwise OR) with one of\n\
5746S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005747device defines the newly created device special file (probably using\n\
5748os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005749
5750
5751static PyObject *
5752posix_mknod(PyObject *self, PyObject *args)
5753{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005754 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005755 char *filename;
5756 int mode = 0600;
5757 int device = 0;
5758 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005759 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5760 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005761 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005762 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005763 Py_BEGIN_ALLOW_THREADS
5764 res = mknod(filename, mode, device);
5765 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005766 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005767 if (res < 0)
5768 return posix_error();
5769 Py_INCREF(Py_None);
5770 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005771}
5772#endif
5773
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005774#ifdef HAVE_DEVICE_MACROS
5775PyDoc_STRVAR(posix_major__doc__,
5776"major(device) -> major number\n\
5777Extracts a device major number from a raw device number.");
5778
5779static PyObject *
5780posix_major(PyObject *self, PyObject *args)
5781{
Victor Stinner8c62be82010-05-06 00:08:46 +00005782 int device;
5783 if (!PyArg_ParseTuple(args, "i:major", &device))
5784 return NULL;
5785 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005786}
5787
5788PyDoc_STRVAR(posix_minor__doc__,
5789"minor(device) -> minor number\n\
5790Extracts a device minor number from a raw device number.");
5791
5792static PyObject *
5793posix_minor(PyObject *self, PyObject *args)
5794{
Victor Stinner8c62be82010-05-06 00:08:46 +00005795 int device;
5796 if (!PyArg_ParseTuple(args, "i:minor", &device))
5797 return NULL;
5798 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005799}
5800
5801PyDoc_STRVAR(posix_makedev__doc__,
5802"makedev(major, minor) -> device number\n\
5803Composes a raw device number from the major and minor device numbers.");
5804
5805static PyObject *
5806posix_makedev(PyObject *self, PyObject *args)
5807{
Victor Stinner8c62be82010-05-06 00:08:46 +00005808 int major, minor;
5809 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5810 return NULL;
5811 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005812}
5813#endif /* device macros */
5814
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005815
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005816#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005817PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005818"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005819Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005820
Barry Warsaw53699e91996-12-10 23:23:01 +00005821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005822posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005823{
Victor Stinner8c62be82010-05-06 00:08:46 +00005824 int fd;
5825 off_t length;
5826 int res;
5827 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005828
Victor Stinner8c62be82010-05-06 00:08:46 +00005829 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5830 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005831
5832#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005833 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005834#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005835 length = PyLong_Check(lenobj) ?
5836 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005837#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005838 if (PyErr_Occurred())
5839 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005840
Victor Stinner8c62be82010-05-06 00:08:46 +00005841 Py_BEGIN_ALLOW_THREADS
5842 res = ftruncate(fd, length);
5843 Py_END_ALLOW_THREADS
5844 if (res < 0)
5845 return posix_error();
5846 Py_INCREF(Py_None);
5847 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005848}
5849#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005850
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005851#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005852PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005853"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005854Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005855
Fred Drake762e2061999-08-26 17:23:54 +00005856/* Save putenv() parameters as values here, so we can collect them when they
5857 * get re-set with another call for the same key. */
5858static PyObject *posix_putenv_garbage;
5859
Tim Peters5aa91602002-01-30 05:46:57 +00005860static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005861posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005862{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005863#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005864 wchar_t *s1, *s2;
5865 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005866#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005867 PyObject *os1, *os2;
5868 char *s1, *s2;
5869 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005870#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005871 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005872 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005873
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005874#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005875 if (!PyArg_ParseTuple(args,
5876 "uu:putenv",
5877 &s1, &s2))
5878 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005879#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005880 if (!PyArg_ParseTuple(args,
5881 "O&O&:putenv",
5882 PyUnicode_FSConverter, &os1,
5883 PyUnicode_FSConverter, &os2))
5884 return NULL;
5885 s1 = PyBytes_AsString(os1);
5886 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005887#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005888
5889#if defined(PYOS_OS2)
5890 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5891 APIRET rc;
5892
Guido van Rossumd48f2521997-12-05 22:19:34 +00005893 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005894 if (rc != NO_ERROR) {
5895 os2_error(rc);
5896 goto error;
5897 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005898
5899 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5900 APIRET rc;
5901
Guido van Rossumd48f2521997-12-05 22:19:34 +00005902 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005903 if (rc != NO_ERROR) {
5904 os2_error(rc);
5905 goto error;
5906 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005907 } else {
5908#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005909 /* XXX This can leak memory -- not easy to fix :-( */
5910 /* len includes space for a trailing \0; the size arg to
5911 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005912#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 len = wcslen(s1) + wcslen(s2) + 2;
5914 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005915#else
Victor Stinner84ae1182010-05-06 22:05:07 +00005916 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00005917 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005918#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005919 if (newstr == NULL) {
5920 PyErr_NoMemory();
5921 goto error;
5922 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005923#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005924 newenv = PyUnicode_AsUnicode(newstr);
5925 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5926 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005927 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005928 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005930#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005931 newenv = PyBytes_AS_STRING(newstr);
5932 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5933 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005935 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005936 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005937#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005938
Victor Stinner8c62be82010-05-06 00:08:46 +00005939 /* Install the first arg and newstr in posix_putenv_garbage;
5940 * this will cause previous value to be collected. This has to
5941 * happen after the real putenv() call because the old value
5942 * was still accessible until then. */
5943 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005944#ifdef MS_WINDOWS
5945 PyTuple_GET_ITEM(args, 0),
5946#else
5947 os1,
5948#endif
5949 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005950 /* really not much we can do; just leak */
5951 PyErr_Clear();
5952 }
5953 else {
5954 Py_DECREF(newstr);
5955 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005956
5957#if defined(PYOS_OS2)
5958 }
5959#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005960
Martin v. Löwis011e8422009-05-05 04:43:17 +00005961#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005962 Py_DECREF(os1);
5963 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005964#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005965 Py_RETURN_NONE;
5966
5967error:
5968#ifndef MS_WINDOWS
5969 Py_DECREF(os1);
5970 Py_DECREF(os2);
5971#endif
5972 Py_XDECREF(newstr);
5973 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005974}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005975#endif /* putenv */
5976
Guido van Rossumc524d952001-10-19 01:31:59 +00005977#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005978PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005979"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005980Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005981
5982static PyObject *
5983posix_unsetenv(PyObject *self, PyObject *args)
5984{
Victor Stinner84ae1182010-05-06 22:05:07 +00005985#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005986 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005987
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5989 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00005990#else
5991 PyObject *os1;
5992 char *s1;
5993
5994 if (!PyArg_ParseTuple(args, "O&:unsetenv",
5995 PyUnicode_FSConverter, &os1))
5996 return NULL;
5997 s1 = PyBytes_AsString(os1);
5998#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00005999
Victor Stinner8c62be82010-05-06 00:08:46 +00006000 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006001
Victor Stinner8c62be82010-05-06 00:08:46 +00006002 /* Remove the key from posix_putenv_garbage;
6003 * this will cause it to be collected. This has to
6004 * happen after the real unsetenv() call because the
6005 * old value was still accessible until then.
6006 */
6007 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006008#ifdef MS_WINDOWS
6009 PyTuple_GET_ITEM(args, 0)
6010#else
6011 os1
6012#endif
6013 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006014 /* really not much we can do; just leak */
6015 PyErr_Clear();
6016 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006017
Victor Stinner84ae1182010-05-06 22:05:07 +00006018#ifndef MS_WINDOWS
6019 Py_DECREF(os1);
6020#endif
6021 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006022}
6023#endif /* unsetenv */
6024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006025PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006026"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006027Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006028
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006030posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006031{
Victor Stinner8c62be82010-05-06 00:08:46 +00006032 int code;
6033 char *message;
6034 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6035 return NULL;
6036 message = strerror(code);
6037 if (message == NULL) {
6038 PyErr_SetString(PyExc_ValueError,
6039 "strerror() argument out of range");
6040 return NULL;
6041 }
6042 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006043}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006044
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006045
Guido van Rossumc9641791998-08-04 15:26:23 +00006046#ifdef HAVE_SYS_WAIT_H
6047
Fred Drake106c1a02002-04-23 15:58:02 +00006048#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006049PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006050"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006051Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006052
6053static PyObject *
6054posix_WCOREDUMP(PyObject *self, PyObject *args)
6055{
Victor Stinner8c62be82010-05-06 00:08:46 +00006056 WAIT_TYPE status;
6057 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006058
Victor Stinner8c62be82010-05-06 00:08:46 +00006059 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6060 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006061
Victor Stinner8c62be82010-05-06 00:08:46 +00006062 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006063}
6064#endif /* WCOREDUMP */
6065
6066#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006067PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006068"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006069Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006070job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006071
6072static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006073posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006074{
Victor Stinner8c62be82010-05-06 00:08:46 +00006075 WAIT_TYPE status;
6076 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006077
Victor Stinner8c62be82010-05-06 00:08:46 +00006078 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6079 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006080
Victor Stinner8c62be82010-05-06 00:08:46 +00006081 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006082}
6083#endif /* WIFCONTINUED */
6084
Guido van Rossumc9641791998-08-04 15:26:23 +00006085#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006086PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006087"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006088Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006089
6090static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006091posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006092{
Victor Stinner8c62be82010-05-06 00:08:46 +00006093 WAIT_TYPE status;
6094 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006095
Victor Stinner8c62be82010-05-06 00:08:46 +00006096 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6097 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006098
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006100}
6101#endif /* WIFSTOPPED */
6102
6103#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006104PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006105"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006106Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006107
6108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006109posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006110{
Victor Stinner8c62be82010-05-06 00:08:46 +00006111 WAIT_TYPE status;
6112 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006113
Victor Stinner8c62be82010-05-06 00:08:46 +00006114 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6115 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006116
Victor Stinner8c62be82010-05-06 00:08:46 +00006117 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006118}
6119#endif /* WIFSIGNALED */
6120
6121#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006122PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006123"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006124Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006125system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006126
6127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006128posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006129{
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 WAIT_TYPE status;
6131 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006132
Victor Stinner8c62be82010-05-06 00:08:46 +00006133 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6134 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006135
Victor Stinner8c62be82010-05-06 00:08:46 +00006136 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006137}
6138#endif /* WIFEXITED */
6139
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006140#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006141PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006142"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006143Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006144
6145static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006146posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006147{
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 WAIT_TYPE status;
6149 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006150
Victor Stinner8c62be82010-05-06 00:08:46 +00006151 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6152 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006153
Victor Stinner8c62be82010-05-06 00:08:46 +00006154 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006155}
6156#endif /* WEXITSTATUS */
6157
6158#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006159PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006160"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006161Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006162value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006163
6164static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006165posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006166{
Victor Stinner8c62be82010-05-06 00:08:46 +00006167 WAIT_TYPE status;
6168 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006169
Victor Stinner8c62be82010-05-06 00:08:46 +00006170 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6171 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006172
Victor Stinner8c62be82010-05-06 00:08:46 +00006173 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006174}
6175#endif /* WTERMSIG */
6176
6177#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006178PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006179"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006180Return the signal that stopped the process that provided\n\
6181the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006182
6183static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006184posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006185{
Victor Stinner8c62be82010-05-06 00:08:46 +00006186 WAIT_TYPE status;
6187 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006188
Victor Stinner8c62be82010-05-06 00:08:46 +00006189 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6190 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006191
Victor Stinner8c62be82010-05-06 00:08:46 +00006192 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006193}
6194#endif /* WSTOPSIG */
6195
6196#endif /* HAVE_SYS_WAIT_H */
6197
6198
Thomas Wouters477c8d52006-05-27 19:21:47 +00006199#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006200#ifdef _SCO_DS
6201/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6202 needed definitions in sys/statvfs.h */
6203#define _SVID3
6204#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006205#include <sys/statvfs.h>
6206
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006207static PyObject*
6208_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6210 if (v == NULL)
6211 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006212
6213#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006214 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6215 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6216 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6217 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6218 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6219 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6220 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6221 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6222 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6223 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006224#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6226 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6227 PyStructSequence_SET_ITEM(v, 2,
6228 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6229 PyStructSequence_SET_ITEM(v, 3,
6230 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6231 PyStructSequence_SET_ITEM(v, 4,
6232 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6233 PyStructSequence_SET_ITEM(v, 5,
6234 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6235 PyStructSequence_SET_ITEM(v, 6,
6236 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6237 PyStructSequence_SET_ITEM(v, 7,
6238 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6239 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6240 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006241#endif
6242
Victor Stinner8c62be82010-05-06 00:08:46 +00006243 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006244}
6245
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006246PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006247"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006248Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006249
6250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006251posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006252{
Victor Stinner8c62be82010-05-06 00:08:46 +00006253 int fd, res;
6254 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006255
Victor Stinner8c62be82010-05-06 00:08:46 +00006256 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6257 return NULL;
6258 Py_BEGIN_ALLOW_THREADS
6259 res = fstatvfs(fd, &st);
6260 Py_END_ALLOW_THREADS
6261 if (res != 0)
6262 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006263
Victor Stinner8c62be82010-05-06 00:08:46 +00006264 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006265}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006266#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006267
6268
Thomas Wouters477c8d52006-05-27 19:21:47 +00006269#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006270#include <sys/statvfs.h>
6271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006272PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006273"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006274Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006275
6276static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006277posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006278{
Victor Stinner8c62be82010-05-06 00:08:46 +00006279 char *path;
6280 int res;
6281 struct statvfs st;
6282 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6283 return NULL;
6284 Py_BEGIN_ALLOW_THREADS
6285 res = statvfs(path, &st);
6286 Py_END_ALLOW_THREADS
6287 if (res != 0)
6288 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006289
Victor Stinner8c62be82010-05-06 00:08:46 +00006290 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006291}
6292#endif /* HAVE_STATVFS */
6293
Fred Drakec9680921999-12-13 16:37:25 +00006294/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6295 * It maps strings representing configuration variable names to
6296 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006297 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006298 * rarely-used constants. There are three separate tables that use
6299 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006300 *
6301 * This code is always included, even if none of the interfaces that
6302 * need it are included. The #if hackery needed to avoid it would be
6303 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006304 */
6305struct constdef {
6306 char *name;
6307 long value;
6308};
6309
Fred Drake12c6e2d1999-12-14 21:25:03 +00006310static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006311conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006312 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006313{
Christian Heimes217cfd12007-12-02 14:31:20 +00006314 if (PyLong_Check(arg)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006315 *valuep = PyLong_AS_LONG(arg);
6316 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006317 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006318 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006319 /* look up the value in the table using a binary search */
6320 size_t lo = 0;
6321 size_t mid;
6322 size_t hi = tablesize;
6323 int cmp;
6324 const char *confname;
6325 if (!PyUnicode_Check(arg)) {
6326 PyErr_SetString(PyExc_TypeError,
6327 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00006328 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006329 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006330 confname = _PyUnicode_AsString(arg);
6331 if (confname == NULL)
6332 return 0;
6333 while (lo < hi) {
6334 mid = (lo + hi) / 2;
6335 cmp = strcmp(confname, table[mid].name);
6336 if (cmp < 0)
6337 hi = mid;
6338 else if (cmp > 0)
6339 lo = mid + 1;
6340 else {
6341 *valuep = table[mid].value;
6342 return 1;
6343 }
6344 }
6345 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6346 return 0;
6347 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006348}
6349
6350
6351#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6352static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006353#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006354 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006355#endif
6356#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006357 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006358#endif
Fred Drakec9680921999-12-13 16:37:25 +00006359#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006360 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006361#endif
6362#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006364#endif
6365#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006366 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006367#endif
6368#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006369 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006370#endif
6371#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006372 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006373#endif
6374#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006375 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006376#endif
6377#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006378 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006379#endif
6380#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006381 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006382#endif
6383#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006384 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006385#endif
6386#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006387 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006388#endif
6389#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006390 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006391#endif
6392#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006393 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006394#endif
6395#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006396 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006397#endif
6398#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006399 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006400#endif
6401#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006402 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006403#endif
6404};
6405
Fred Drakec9680921999-12-13 16:37:25 +00006406static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006407conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006408{
6409 return conv_confname(arg, valuep, posix_constants_pathconf,
6410 sizeof(posix_constants_pathconf)
6411 / sizeof(struct constdef));
6412}
6413#endif
6414
6415#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006416PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006417"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006418Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006419If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006420
6421static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006422posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006423{
6424 PyObject *result = NULL;
6425 int name, fd;
6426
Fred Drake12c6e2d1999-12-14 21:25:03 +00006427 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6428 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006429 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006430
Victor Stinner8c62be82010-05-06 00:08:46 +00006431 errno = 0;
6432 limit = fpathconf(fd, name);
6433 if (limit == -1 && errno != 0)
6434 posix_error();
6435 else
6436 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006437 }
6438 return result;
6439}
6440#endif
6441
6442
6443#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006444PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006445"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006446Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006447If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006448
6449static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006450posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006451{
6452 PyObject *result = NULL;
6453 int name;
6454 char *path;
6455
6456 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6457 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006458 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006459
Victor Stinner8c62be82010-05-06 00:08:46 +00006460 errno = 0;
6461 limit = pathconf(path, name);
6462 if (limit == -1 && errno != 0) {
6463 if (errno == EINVAL)
6464 /* could be a path or name problem */
6465 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006466 else
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 posix_error_with_filename(path);
6468 }
6469 else
6470 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006471 }
6472 return result;
6473}
6474#endif
6475
6476#ifdef HAVE_CONFSTR
6477static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006478#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006479 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006480#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006481#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006482 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006483#endif
6484#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006486#endif
Fred Draked86ed291999-12-15 15:34:33 +00006487#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006488 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006489#endif
6490#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006491 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006492#endif
6493#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006494 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006495#endif
6496#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006497 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006498#endif
Fred Drakec9680921999-12-13 16:37:25 +00006499#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006501#endif
6502#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006504#endif
6505#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006507#endif
6508#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006510#endif
6511#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006512 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006513#endif
6514#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006515 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006516#endif
6517#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006518 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006519#endif
6520#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006522#endif
Fred Draked86ed291999-12-15 15:34:33 +00006523#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006524 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006525#endif
Fred Drakec9680921999-12-13 16:37:25 +00006526#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006527 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006528#endif
Fred Draked86ed291999-12-15 15:34:33 +00006529#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006530 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006531#endif
6532#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006533 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006534#endif
6535#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006536 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006537#endif
6538#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006540#endif
Fred Drakec9680921999-12-13 16:37:25 +00006541#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006542 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006543#endif
6544#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006545 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006546#endif
6547#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006549#endif
6550#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006551 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006552#endif
6553#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006554 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006555#endif
6556#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006557 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006558#endif
6559#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006560 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006561#endif
6562#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006563 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006564#endif
6565#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006566 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006567#endif
6568#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006569 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006570#endif
6571#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006572 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006573#endif
6574#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006575 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006576#endif
6577#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006578 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006579#endif
6580#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006581 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006582#endif
6583#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006584 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006585#endif
6586#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006587 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006588#endif
Fred Draked86ed291999-12-15 15:34:33 +00006589#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006591#endif
6592#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006593 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006594#endif
6595#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006596 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006597#endif
6598#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006599 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006600#endif
6601#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006602 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006603#endif
6604#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006605 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006606#endif
6607#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006608 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006609#endif
6610#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006611 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006612#endif
6613#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006614 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006615#endif
6616#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006617 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006618#endif
6619#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006620 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006621#endif
6622#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006623 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006624#endif
6625#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006626 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006627#endif
Fred Drakec9680921999-12-13 16:37:25 +00006628};
6629
6630static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006631conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006632{
6633 return conv_confname(arg, valuep, posix_constants_confstr,
6634 sizeof(posix_constants_confstr)
6635 / sizeof(struct constdef));
6636}
6637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006638PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006639"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006640Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006641
6642static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006643posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006644{
6645 PyObject *result = NULL;
6646 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006647 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006648
6649 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006650 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006651
Fred Drakec9680921999-12-13 16:37:25 +00006652 errno = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006653 len = confstr(name, buffer, sizeof(buffer));
6654 if (len == 0) {
6655 if (errno) {
6656 posix_error();
6657 }
6658 else {
6659 result = Py_None;
6660 Py_INCREF(Py_None);
6661 }
Fred Drakec9680921999-12-13 16:37:25 +00006662 }
6663 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006664 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006665 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006666 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006667 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006668 }
6669 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006670 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006671 }
6672 }
6673 return result;
6674}
6675#endif
6676
6677
6678#ifdef HAVE_SYSCONF
6679static struct constdef posix_constants_sysconf[] = {
6680#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006681 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006682#endif
6683#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006684 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006685#endif
6686#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006687 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006688#endif
6689#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006690 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006691#endif
6692#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006693 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006694#endif
6695#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006696 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006697#endif
6698#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006700#endif
6701#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006702 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006703#endif
6704#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006705 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006706#endif
6707#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006709#endif
Fred Draked86ed291999-12-15 15:34:33 +00006710#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006711 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006712#endif
6713#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006714 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006715#endif
Fred Drakec9680921999-12-13 16:37:25 +00006716#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006718#endif
Fred Drakec9680921999-12-13 16:37:25 +00006719#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006721#endif
6722#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006724#endif
6725#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006727#endif
6728#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006729 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006730#endif
6731#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006732 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006733#endif
Fred Draked86ed291999-12-15 15:34:33 +00006734#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006735 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006736#endif
Fred Drakec9680921999-12-13 16:37:25 +00006737#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006738 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006739#endif
6740#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006741 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006742#endif
6743#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006744 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006745#endif
6746#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006748#endif
6749#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006750 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006751#endif
Fred Draked86ed291999-12-15 15:34:33 +00006752#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006753 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006754#endif
Fred Drakec9680921999-12-13 16:37:25 +00006755#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006756 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006757#endif
6758#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006759 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006760#endif
6761#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006762 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006763#endif
6764#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006765 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006766#endif
6767#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006768 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006769#endif
6770#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006771 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006772#endif
6773#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006774 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006775#endif
6776#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006777 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006778#endif
6779#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006780 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006781#endif
6782#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006783 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006784#endif
6785#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006786 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006787#endif
6788#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006789 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006790#endif
6791#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006793#endif
6794#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006795 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006796#endif
6797#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006799#endif
6800#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006801 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006802#endif
6803#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006805#endif
6806#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006807 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006808#endif
6809#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006810 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006811#endif
6812#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006813 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006814#endif
6815#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006816 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006817#endif
6818#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006820#endif
6821#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006823#endif
Fred Draked86ed291999-12-15 15:34:33 +00006824#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006825 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006826#endif
Fred Drakec9680921999-12-13 16:37:25 +00006827#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006829#endif
6830#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006831 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006832#endif
6833#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006834 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006835#endif
Fred Draked86ed291999-12-15 15:34:33 +00006836#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006838#endif
Fred Drakec9680921999-12-13 16:37:25 +00006839#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006840 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006841#endif
Fred Draked86ed291999-12-15 15:34:33 +00006842#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006844#endif
6845#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006847#endif
Fred Drakec9680921999-12-13 16:37:25 +00006848#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006849 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006850#endif
6851#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006852 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006853#endif
6854#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006856#endif
6857#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006858 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006859#endif
Fred Draked86ed291999-12-15 15:34:33 +00006860#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006861 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006862#endif
Fred Drakec9680921999-12-13 16:37:25 +00006863#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006865#endif
6866#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006868#endif
6869#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006871#endif
6872#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006873 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006874#endif
6875#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006876 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006877#endif
6878#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006880#endif
6881#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006882 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006883#endif
Fred Draked86ed291999-12-15 15:34:33 +00006884#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006885 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006886#endif
Fred Drakec9680921999-12-13 16:37:25 +00006887#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006888 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006889#endif
6890#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006891 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006892#endif
Fred Draked86ed291999-12-15 15:34:33 +00006893#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006894 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006895#endif
Fred Drakec9680921999-12-13 16:37:25 +00006896#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006897 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006898#endif
6899#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006900 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006901#endif
6902#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006903 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006904#endif
6905#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006906 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006907#endif
6908#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006909 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006910#endif
6911#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006913#endif
6914#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006916#endif
6917#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006919#endif
6920#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006922#endif
Fred Draked86ed291999-12-15 15:34:33 +00006923#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006924 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006925#endif
6926#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006928#endif
Fred Drakec9680921999-12-13 16:37:25 +00006929#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00006930 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006931#endif
6932#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006934#endif
6935#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006936 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006937#endif
6938#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006939 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006940#endif
6941#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006942 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006943#endif
6944#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006945 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006946#endif
6947#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00006948 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006949#endif
6950#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00006951 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006952#endif
6953#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006954 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006955#endif
6956#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006958#endif
6959#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006961#endif
6962#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006963 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006964#endif
6965#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006967#endif
6968#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00006969 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006970#endif
6971#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00006972 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006973#endif
6974#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00006975 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006976#endif
6977#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00006978 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006979#endif
6980#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006981 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006982#endif
6983#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006985#endif
6986#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006988#endif
6989#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006991#endif
6992#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006993 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006994#endif
6995#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00006996 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006997#endif
6998#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006999 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007000#endif
7001#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007002 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007003#endif
7004#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007006#endif
7007#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007009#endif
7010#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007011 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007012#endif
7013#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007014 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007015#endif
7016#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007017 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007018#endif
7019#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007020 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007021#endif
7022#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007024#endif
7025#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007026 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007027#endif
7028#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007030#endif
7031#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007033#endif
Fred Draked86ed291999-12-15 15:34:33 +00007034#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007035 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007036#endif
Fred Drakec9680921999-12-13 16:37:25 +00007037#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007038 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007039#endif
7040#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007042#endif
7043#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007044 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007045#endif
7046#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007047 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007048#endif
7049#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007050 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007051#endif
7052#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007053 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007054#endif
7055#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007057#endif
7058#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007060#endif
7061#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007063#endif
7064#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007065 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007066#endif
7067#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007069#endif
7070#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007071 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007072#endif
7073#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007075#endif
7076#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007078#endif
7079#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007081#endif
7082#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007084#endif
7085#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007087#endif
7088#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007090#endif
7091#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007093#endif
7094#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007095 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007096#endif
7097#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007099#endif
7100#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007102#endif
7103#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007104 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007105#endif
7106#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007107 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007108#endif
7109#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007111#endif
7112#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007114#endif
7115#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007116 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007117#endif
7118#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007119 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007120#endif
7121#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007123#endif
7124#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007126#endif
7127#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007128 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007129#endif
7130#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007131 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007132#endif
7133#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007134 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007135#endif
7136#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007137 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007138#endif
7139#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007140 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007141#endif
7142#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007143 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007144#endif
7145#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007147#endif
7148#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007149 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007150#endif
7151#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007153#endif
7154#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007155 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007156#endif
7157#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007158 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007159#endif
7160#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007161 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007162#endif
7163#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007164 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007165#endif
7166#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007168#endif
7169#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007171#endif
7172};
7173
7174static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007175conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007176{
7177 return conv_confname(arg, valuep, posix_constants_sysconf,
7178 sizeof(posix_constants_sysconf)
7179 / sizeof(struct constdef));
7180}
7181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007182PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007183"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007184Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007185
7186static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007187posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007188{
7189 PyObject *result = NULL;
7190 int name;
7191
7192 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7193 int value;
7194
7195 errno = 0;
7196 value = sysconf(name);
7197 if (value == -1 && errno != 0)
7198 posix_error();
7199 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007200 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007201 }
7202 return result;
7203}
7204#endif
7205
7206
Fred Drakebec628d1999-12-15 18:31:10 +00007207/* This code is used to ensure that the tables of configuration value names
7208 * are in sorted order as required by conv_confname(), and also to build the
7209 * the exported dictionaries that are used to publish information about the
7210 * names available on the host platform.
7211 *
7212 * Sorting the table at runtime ensures that the table is properly ordered
7213 * when used, even for platforms we're not able to test on. It also makes
7214 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007215 */
Fred Drakebec628d1999-12-15 18:31:10 +00007216
7217static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007218cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007219{
7220 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007221 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007222 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007223 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007224
7225 return strcmp(c1->name, c2->name);
7226}
7227
7228static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007229setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007230 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007231{
Fred Drakebec628d1999-12-15 18:31:10 +00007232 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007233 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007234
7235 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7236 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007237 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007239
Barry Warsaw3155db32000-04-13 15:20:40 +00007240 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 PyObject *o = PyLong_FromLong(table[i].value);
7242 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7243 Py_XDECREF(o);
7244 Py_DECREF(d);
7245 return -1;
7246 }
7247 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007248 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007249 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007250}
7251
Fred Drakebec628d1999-12-15 18:31:10 +00007252/* Return -1 on failure, 0 on success. */
7253static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007254setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007255{
7256#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007257 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007258 sizeof(posix_constants_pathconf)
7259 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007260 "pathconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007262#endif
7263#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007264 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007265 sizeof(posix_constants_confstr)
7266 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007267 "confstr_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007268 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007269#endif
7270#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007271 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007272 sizeof(posix_constants_sysconf)
7273 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007274 "sysconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007276#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007277 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007278}
Fred Draked86ed291999-12-15 15:34:33 +00007279
7280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007281PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007282"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007283Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007284in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007285
7286static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007287posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007288{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007289 abort();
7290 /*NOTREACHED*/
7291 Py_FatalError("abort() called from Python code didn't abort!");
7292 return NULL;
7293}
Fred Drakebec628d1999-12-15 18:31:10 +00007294
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007295#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007296PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007297"startfile(filepath [, operation]) - Start a file with its associated\n\
7298application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007299\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007300When \"operation\" is not specified or \"open\", this acts like\n\
7301double-clicking the file in Explorer, or giving the file name as an\n\
7302argument to the DOS \"start\" command: the file is opened with whatever\n\
7303application (if any) its extension is associated.\n\
7304When another \"operation\" is given, it specifies what should be done with\n\
7305the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007306\n\
7307startfile returns as soon as the associated application is launched.\n\
7308There is no option to wait for the application to close, and no way\n\
7309to retrieve the application's exit status.\n\
7310\n\
7311The filepath is relative to the current directory. If you want to use\n\
7312an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007313the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007314
7315static PyObject *
7316win32_startfile(PyObject *self, PyObject *args)
7317{
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 PyObject *ofilepath;
7319 char *filepath;
7320 char *operation = NULL;
7321 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007322
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 PyObject *unipath, *woperation = NULL;
7324 if (!PyArg_ParseTuple(args, "U|s:startfile",
7325 &unipath, &operation)) {
7326 PyErr_Clear();
7327 goto normal;
7328 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007329
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 if (operation) {
7331 woperation = PyUnicode_DecodeASCII(operation,
7332 strlen(operation), NULL);
7333 if (!woperation) {
7334 PyErr_Clear();
7335 operation = NULL;
7336 goto normal;
7337 }
7338 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007339
Victor Stinner8c62be82010-05-06 00:08:46 +00007340 Py_BEGIN_ALLOW_THREADS
7341 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7342 PyUnicode_AS_UNICODE(unipath),
7343 NULL, NULL, SW_SHOWNORMAL);
7344 Py_END_ALLOW_THREADS
7345
7346 Py_XDECREF(woperation);
7347 if (rc <= (HINSTANCE)32) {
7348 PyObject *errval = win32_error_unicode("startfile",
7349 PyUnicode_AS_UNICODE(unipath));
7350 return errval;
7351 }
7352 Py_INCREF(Py_None);
7353 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007354
7355normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7357 PyUnicode_FSConverter, &ofilepath,
7358 &operation))
7359 return NULL;
7360 filepath = PyBytes_AsString(ofilepath);
7361 Py_BEGIN_ALLOW_THREADS
7362 rc = ShellExecute((HWND)0, operation, filepath,
7363 NULL, NULL, SW_SHOWNORMAL);
7364 Py_END_ALLOW_THREADS
7365 if (rc <= (HINSTANCE)32) {
7366 PyObject *errval = win32_error("startfile", filepath);
7367 Py_DECREF(ofilepath);
7368 return errval;
7369 }
7370 Py_DECREF(ofilepath);
7371 Py_INCREF(Py_None);
7372 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007373}
7374#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007375
Martin v. Löwis438b5342002-12-27 10:16:42 +00007376#ifdef HAVE_GETLOADAVG
7377PyDoc_STRVAR(posix_getloadavg__doc__,
7378"getloadavg() -> (float, float, float)\n\n\
7379Return the number of processes in the system run queue averaged over\n\
7380the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7381was unobtainable");
7382
7383static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007384posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007385{
7386 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007387 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007388 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7389 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007390 } else
Victor Stinner8c62be82010-05-06 00:08:46 +00007391 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007392}
7393#endif
7394
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007395#ifdef MS_WINDOWS
7396
7397PyDoc_STRVAR(win32_urandom__doc__,
7398"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007399Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007400
7401typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7402 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7403 DWORD dwFlags );
7404typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7405 BYTE *pbBuffer );
7406
7407static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007408/* This handle is never explicitly released. Instead, the operating
7409 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007410static HCRYPTPROV hCryptProv = 0;
7411
Tim Peters4ad82172004-08-30 17:02:04 +00007412static PyObject*
7413win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007414{
Victor Stinner8c62be82010-05-06 00:08:46 +00007415 int howMany;
7416 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007417
Victor Stinner8c62be82010-05-06 00:08:46 +00007418 /* Read arguments */
7419 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7420 return NULL;
7421 if (howMany < 0)
7422 return PyErr_Format(PyExc_ValueError,
7423 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007424
Victor Stinner8c62be82010-05-06 00:08:46 +00007425 if (hCryptProv == 0) {
7426 HINSTANCE hAdvAPI32 = NULL;
7427 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007428
Victor Stinner8c62be82010-05-06 00:08:46 +00007429 /* Obtain handle to the DLL containing CryptoAPI
7430 This should not fail */
7431 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7432 if(hAdvAPI32 == NULL)
7433 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007434
Victor Stinner8c62be82010-05-06 00:08:46 +00007435 /* Obtain pointers to the CryptoAPI functions
7436 This will fail on some early versions of Win95 */
7437 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7438 hAdvAPI32,
7439 "CryptAcquireContextA");
7440 if (pCryptAcquireContext == NULL)
7441 return PyErr_Format(PyExc_NotImplementedError,
7442 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007443
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7445 hAdvAPI32, "CryptGenRandom");
7446 if (pCryptGenRandom == NULL)
7447 return PyErr_Format(PyExc_NotImplementedError,
7448 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007449
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 /* Acquire context */
7451 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7452 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7453 return win32_error("CryptAcquireContext", NULL);
7454 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007455
Victor Stinner8c62be82010-05-06 00:08:46 +00007456 /* Allocate bytes */
7457 result = PyBytes_FromStringAndSize(NULL, howMany);
7458 if (result != NULL) {
7459 /* Get random data */
7460 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7461 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7462 PyBytes_AS_STRING(result))) {
7463 Py_DECREF(result);
7464 return win32_error("CryptGenRandom", NULL);
7465 }
7466 }
7467 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007468}
7469#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007470
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007471PyDoc_STRVAR(device_encoding__doc__,
7472"device_encoding(fd) -> str\n\n\
7473Return a string describing the encoding of the device\n\
7474if the output is a terminal; else return None.");
7475
7476static PyObject *
7477device_encoding(PyObject *self, PyObject *args)
7478{
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 int fd;
7480 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7481 return NULL;
7482 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7483 Py_INCREF(Py_None);
7484 return Py_None;
7485 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007486#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007487 if (fd == 0) {
7488 char buf[100];
7489 sprintf(buf, "cp%d", GetConsoleCP());
7490 return PyUnicode_FromString(buf);
7491 }
7492 if (fd == 1 || fd == 2) {
7493 char buf[100];
7494 sprintf(buf, "cp%d", GetConsoleOutputCP());
7495 return PyUnicode_FromString(buf);
7496 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007497#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007498 {
7499 char *codeset = nl_langinfo(CODESET);
7500 if (codeset != NULL && codeset[0] != 0)
7501 return PyUnicode_FromString(codeset);
7502 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007503#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007504 Py_INCREF(Py_None);
7505 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007506}
7507
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007508#ifdef __VMS
7509/* Use openssl random routine */
7510#include <openssl/rand.h>
7511PyDoc_STRVAR(vms_urandom__doc__,
7512"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007513Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007514
7515static PyObject*
7516vms_urandom(PyObject *self, PyObject *args)
7517{
Victor Stinner8c62be82010-05-06 00:08:46 +00007518 int howMany;
7519 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007520
Victor Stinner8c62be82010-05-06 00:08:46 +00007521 /* Read arguments */
7522 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7523 return NULL;
7524 if (howMany < 0)
7525 return PyErr_Format(PyExc_ValueError,
7526 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007527
Victor Stinner8c62be82010-05-06 00:08:46 +00007528 /* Allocate bytes */
7529 result = PyBytes_FromStringAndSize(NULL, howMany);
7530 if (result != NULL) {
7531 /* Get random data */
7532 if (RAND_pseudo_bytes((unsigned char*)
7533 PyBytes_AS_STRING(result),
7534 howMany) < 0) {
7535 Py_DECREF(result);
7536 return PyErr_Format(PyExc_ValueError,
7537 "RAND_pseudo_bytes");
7538 }
7539 }
7540 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007541}
7542#endif
7543
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007544#ifdef HAVE_SETRESUID
7545PyDoc_STRVAR(posix_setresuid__doc__,
7546"setresuid(ruid, euid, suid)\n\n\
7547Set the current process's real, effective, and saved user ids.");
7548
7549static PyObject*
7550posix_setresuid (PyObject *self, PyObject *args)
7551{
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 /* We assume uid_t is no larger than a long. */
7553 long ruid, euid, suid;
7554 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7555 return NULL;
7556 if (setresuid(ruid, euid, suid) < 0)
7557 return posix_error();
7558 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007559}
7560#endif
7561
7562#ifdef HAVE_SETRESGID
7563PyDoc_STRVAR(posix_setresgid__doc__,
7564"setresgid(rgid, egid, sgid)\n\n\
7565Set the current process's real, effective, and saved group ids.");
7566
7567static PyObject*
7568posix_setresgid (PyObject *self, PyObject *args)
7569{
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 /* We assume uid_t is no larger than a long. */
7571 long rgid, egid, sgid;
7572 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7573 return NULL;
7574 if (setresgid(rgid, egid, sgid) < 0)
7575 return posix_error();
7576 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007577}
7578#endif
7579
7580#ifdef HAVE_GETRESUID
7581PyDoc_STRVAR(posix_getresuid__doc__,
7582"getresuid() -> (ruid, euid, suid)\n\n\
7583Get tuple of the current process's real, effective, and saved user ids.");
7584
7585static PyObject*
7586posix_getresuid (PyObject *self, PyObject *noargs)
7587{
Victor Stinner8c62be82010-05-06 00:08:46 +00007588 uid_t ruid, euid, suid;
7589 long l_ruid, l_euid, l_suid;
7590 if (getresuid(&ruid, &euid, &suid) < 0)
7591 return posix_error();
7592 /* Force the values into long's as we don't know the size of uid_t. */
7593 l_ruid = ruid;
7594 l_euid = euid;
7595 l_suid = suid;
7596 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007597}
7598#endif
7599
7600#ifdef HAVE_GETRESGID
7601PyDoc_STRVAR(posix_getresgid__doc__,
7602"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007603Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007604
7605static PyObject*
7606posix_getresgid (PyObject *self, PyObject *noargs)
7607{
Victor Stinner8c62be82010-05-06 00:08:46 +00007608 uid_t rgid, egid, sgid;
7609 long l_rgid, l_egid, l_sgid;
7610 if (getresgid(&rgid, &egid, &sgid) < 0)
7611 return posix_error();
7612 /* Force the values into long's as we don't know the size of uid_t. */
7613 l_rgid = rgid;
7614 l_egid = egid;
7615 l_sgid = sgid;
7616 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007617}
7618#endif
7619
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007620static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007621 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007622#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007623 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007624#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007626#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007628#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007629 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007630#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007631 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007632#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007633#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007635#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007636#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007637 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007638#endif /* HAVE_LCHMOD */
7639#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007641#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007642#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007643 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007644#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007645#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007646 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007647#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007648#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007650#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007651#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007652 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007653#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007654#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007655 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7656 METH_NOARGS, posix_getcwd__doc__},
7657 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7658 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007659#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007660#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007661 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007662#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7664 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7665 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007666#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007667 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007668#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007669#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007670 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007671#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007672#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007673 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007674#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007675 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7676 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7677 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007678 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007679#ifdef HAVE_SYMLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007680 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007681#endif /* HAVE_SYMLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007682#if !defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin74e45612010-07-09 15:58:59 +00007683 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
7684 win_symlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007685#endif /* !defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007686#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007688#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007689 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007690#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007691 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007692#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007693 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7694 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7695 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007696#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007697 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007698#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007699 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007700#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007701 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7702 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007703#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007704#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007705 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7706 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007707#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007708 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7709 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007710#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007711#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007712#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007713 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007714#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007715#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007716 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007717#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007718#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007720#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007721#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007722 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007723#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007724#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007725 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007726#endif /* HAVE_GETEGID */
7727#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007728 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007729#endif /* HAVE_GETEUID */
7730#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007732#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007733#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007735#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007736 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007737#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007738 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007739#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007740#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007741 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007742#endif /* HAVE_GETPPID */
7743#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007744 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007745#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007746#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007747 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007748#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007749#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007750 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007751#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007752#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007753 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007754#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007755#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007756 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007757#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007758#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007759 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7760 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007761#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007762#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007764#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007765#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007767#endif /* HAVE_SETEUID */
7768#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007770#endif /* HAVE_SETEGID */
7771#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007773#endif /* HAVE_SETREUID */
7774#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007775 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007776#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007777#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007779#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007780#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007782#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007783#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007785#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007786#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007788#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007789#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007791#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007792#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007794#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007795#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007796 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007797#endif /* HAVE_WAIT3 */
7798#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007799 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007800#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007801#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007803#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007804#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007806#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007807#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007809#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007810#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007811 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007812#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007813#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007815#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007816#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007818#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007819 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7820 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7821 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7822 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7823 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7824 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7825 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7826 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7827 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7828 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7829 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007830#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007832#endif
7833#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007835#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007836#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007837 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007838#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007839#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007840 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7841 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7842 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007843#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007844#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007846#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007847#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007849#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007850#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007852#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007854#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007856#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007857#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007859#endif
7860#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007862#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007863#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007864#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007866#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007867#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007869#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007870#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007872#endif /* WIFSTOPPED */
7873#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007875#endif /* WIFSIGNALED */
7876#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007878#endif /* WIFEXITED */
7879#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007881#endif /* WEXITSTATUS */
7882#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007884#endif /* WTERMSIG */
7885#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007887#endif /* WSTOPSIG */
7888#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007889#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007890 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007891#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007892#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007893 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007894#endif
Fred Drakec9680921999-12-13 16:37:25 +00007895#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00007896 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007897#endif
7898#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007900#endif
7901#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007903#endif
7904#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007905 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007906#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007907 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007908#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00007910 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007911#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007912#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00007913 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007914#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007915 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007917 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007918 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007919 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007920 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007921#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007923#endif
7924#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007926#endif
7927#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007929#endif
7930#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007932#endif
7933
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007935};
7936
7937
Barry Warsaw4a342091996-12-19 23:50:02 +00007938static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007939ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007940{
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007942}
7943
Guido van Rossumd48f2521997-12-05 22:19:34 +00007944#if defined(PYOS_OS2)
7945/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007946static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007947{
7948 APIRET rc;
7949 ULONG values[QSV_MAX+1];
7950 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007951 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007952
7953 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007954 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007955 Py_END_ALLOW_THREADS
7956
7957 if (rc != NO_ERROR) {
7958 os2_error(rc);
7959 return -1;
7960 }
7961
Fred Drake4d1e64b2002-04-15 19:40:07 +00007962 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7963 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7964 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7965 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7966 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7967 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7968 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007969
7970 switch (values[QSV_VERSION_MINOR]) {
7971 case 0: ver = "2.00"; break;
7972 case 10: ver = "2.10"; break;
7973 case 11: ver = "2.11"; break;
7974 case 30: ver = "3.00"; break;
7975 case 40: ver = "4.00"; break;
7976 case 50: ver = "5.00"; break;
7977 default:
Tim Peters885d4572001-11-28 20:27:42 +00007978 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007980 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007981 ver = &tmp[0];
7982 }
7983
7984 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007985 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007986 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007987
7988 /* Add Indicator of Which Drive was Used to Boot the System */
7989 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7990 tmp[1] = ':';
7991 tmp[2] = '\0';
7992
Fred Drake4d1e64b2002-04-15 19:40:07 +00007993 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007994}
7995#endif
7996
Barry Warsaw4a342091996-12-19 23:50:02 +00007997static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007998all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007999{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008000#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008002#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008003#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008005#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008006#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008008#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008009#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008011#endif
Fred Drakec9680921999-12-13 16:37:25 +00008012#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008014#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008015#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008016 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008017#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008018#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008019 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008020#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008021#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008023#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008024#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008025 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008026#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008027#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008028 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008029#endif
8030#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008031 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008032#endif
8033#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008034 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008035#endif
8036#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008037 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008038#endif
8039#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008041#endif
8042#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008044#endif
8045#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008046 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008047#endif
8048#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008049 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008050#endif
8051#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008052 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008053#endif
8054#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008055 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008056#endif
8057#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008058 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008059#endif
8060#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008061 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008062#endif
8063#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008064 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008065#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008066#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008067 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008068#endif
8069#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008070 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008071#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008072#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008073 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008074#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008075#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008076 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008077#endif
8078#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008079 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008080#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008081
Tim Peters5aa91602002-01-30 05:46:57 +00008082/* MS Windows */
8083#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 /* Don't inherit in child processes. */
8085 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008086#endif
8087#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008088 /* Optimize for short life (keep in memory). */
8089 /* MS forgot to define this one with a non-underscore form too. */
8090 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008091#endif
8092#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008093 /* Automatically delete when last handle is closed. */
8094 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008095#endif
8096#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 /* Optimize for random access. */
8098 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008099#endif
8100#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008101 /* Optimize for sequential access. */
8102 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008103#endif
8104
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008105/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008106#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008107 /* Send a SIGIO signal whenever input or output
8108 becomes available on file descriptor */
8109 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008110#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008111#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 /* Direct disk access. */
8113 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008114#endif
8115#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008116 /* Must be a directory. */
8117 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008118#endif
8119#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 /* Do not follow links. */
8121 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008122#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008123#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008124 /* Do not update the access time. */
8125 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008126#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008127
Victor Stinner8c62be82010-05-06 00:08:46 +00008128 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008129#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008130 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008131#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008132#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008134#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008135#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008136 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008137#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008138#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008139 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008140#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008141#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008143#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008144#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008145 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008146#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008147#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008148 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008149#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008150#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008151 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008152#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008153#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008154 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008155#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008156#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008157 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008158#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008159#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008160 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008161#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008162#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008163 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008164#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008165#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008166 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008167#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008168#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008169 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008170#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008171#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008172 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008173#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008174#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008175 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008176#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008177#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008178 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008179#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008180
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008181 /* statvfs */
8182#ifdef ST_RDONLY
8183 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
8184#endif /* ST_RDONLY */
8185#ifdef ST_NOSUID
8186 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
8187#endif /* ST_NOSUID */
8188
Guido van Rossum246bc171999-02-01 23:54:31 +00008189#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008190#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008191 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8192 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8193 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8194 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8195 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8196 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8197 if (ins(d, "P_PM", (long)P_PM)) return -1;
8198 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8199 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8200 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8201 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8202 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8203 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8204 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8205 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8206 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8207 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8208 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8209 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8210 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008211#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008212 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8213 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8214 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8215 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8216 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008217#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008218#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008219
Guido van Rossumd48f2521997-12-05 22:19:34 +00008220#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008221 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008222#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008224}
8225
8226
Tim Peters5aa91602002-01-30 05:46:57 +00008227#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008228#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008229#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008230
8231#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008232#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008233#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008234
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008235#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008236#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008237#define MODNAME "posix"
8238#endif
8239
Martin v. Löwis1a214512008-06-11 05:26:20 +00008240static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 PyModuleDef_HEAD_INIT,
8242 MODNAME,
8243 posix__doc__,
8244 -1,
8245 posix_methods,
8246 NULL,
8247 NULL,
8248 NULL,
8249 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008250};
8251
8252
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008253PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008254INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008255{
Victor Stinner8c62be82010-05-06 00:08:46 +00008256 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008257
Victor Stinner8c62be82010-05-06 00:08:46 +00008258 m = PyModule_Create(&posixmodule);
8259 if (m == NULL)
8260 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008261
Victor Stinner8c62be82010-05-06 00:08:46 +00008262 /* Initialize environ dictionary */
8263 v = convertenviron();
8264 Py_XINCREF(v);
8265 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8266 return NULL;
8267 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008268
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 if (all_ins(m))
8270 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008271
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 if (setup_confname_tables(m))
8273 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008274
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 Py_INCREF(PyExc_OSError);
8276 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008277
Guido van Rossumb3d39562000-01-31 18:41:26 +00008278#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008279 if (posix_putenv_garbage == NULL)
8280 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008281#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008282
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 if (!initialized) {
8284 stat_result_desc.name = MODNAME ".stat_result";
8285 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8286 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8287 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8288 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8289 structseq_new = StatResultType.tp_new;
8290 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008291
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 statvfs_result_desc.name = MODNAME ".statvfs_result";
8293 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008294#ifdef NEED_TICKS_PER_SECOND
8295# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008297# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008298 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008299# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008300 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008301# endif
8302#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008303 }
8304 Py_INCREF((PyObject*) &StatResultType);
8305 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8306 Py_INCREF((PyObject*) &StatVFSResultType);
8307 PyModule_AddObject(m, "statvfs_result",
8308 (PyObject*) &StatVFSResultType);
8309 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008310
8311#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008312 /*
8313 * Step 2 of weak-linking support on Mac OS X.
8314 *
8315 * The code below removes functions that are not available on the
8316 * currently active platform.
8317 *
8318 * This block allow one to use a python binary that was build on
8319 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8320 * OSX 10.4.
8321 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008322#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008323 if (fstatvfs == NULL) {
8324 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8325 return NULL;
8326 }
8327 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008328#endif /* HAVE_FSTATVFS */
8329
8330#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 if (statvfs == NULL) {
8332 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8333 return NULL;
8334 }
8335 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008336#endif /* HAVE_STATVFS */
8337
8338# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 if (lchown == NULL) {
8340 if (PyObject_DelAttrString(m, "lchown") == -1) {
8341 return NULL;
8342 }
8343 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008344#endif /* HAVE_LCHOWN */
8345
8346
8347#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008348 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008349
Guido van Rossumb6775db1994-08-01 11:34:53 +00008350}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008351
8352#ifdef __cplusplus
8353}
8354#endif