blob: 3b9fd056f544f907e840ad160fd590d81fb48a8d [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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#endif /* defined(__VMS) */
35
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#ifdef __cplusplus
37extern "C" {
38#endif
39
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000041"This module provides access to operating system functionality that is\n\
42standardized by the C Standard and the POSIX standard (a thinly\n\
43disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000046
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#if defined(PYOS_OS2)
48#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_TYPES_H */
65
66#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000069
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000071#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000072#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000073
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000075#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000077
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#ifdef HAVE_FCNTL_H
79#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000080#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000081
Guido van Rossuma6535fd2001-10-18 19:44:10 +000082#ifdef HAVE_GRP_H
83#include <grp.h>
84#endif
85
Barry Warsaw5676bd12003-01-07 20:57:09 +000086#ifdef HAVE_SYSEXITS_H
87#include <sysexits.h>
88#endif /* HAVE_SYSEXITS_H */
89
Anthony Baxter8a560de2004-10-13 15:30:56 +000090#ifdef HAVE_SYS_LOADAVG_H
91#include <sys/loadavg.h>
92#endif
93
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000094#ifdef HAVE_LANGINFO_H
95#include <langinfo.h>
96#endif
97
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000099/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000100#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#include <process.h>
102#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000103#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000104#define HAVE_GETCWD 1
105#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000106#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#if defined(__OS2__)
108#define HAVE_EXECV 1
109#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000110#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000111#include <process.h>
112#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000113#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_EXECV 1
115#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_OPENDIR 1
117#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000118#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_WAIT 1
120#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000121#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000122#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000123#define HAVE_GETPPID 1
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000124#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000128#define HAVE_SYSTEM 1
129#define HAVE_CWAIT 1
130#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000132#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000133#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
134/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136/* Unix functions that the configure script doesn't check for */
137#define HAVE_EXECV 1
138#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#define HAVE_FORK1 1
141#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_GETCWD 1
143#define HAVE_GETEGID 1
144#define HAVE_GETEUID 1
145#define HAVE_GETGID 1
146#define HAVE_GETPPID 1
147#define HAVE_GETUID 1
148#define HAVE_KILL 1
149#define HAVE_OPENDIR 1
150#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000154#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#endif /* _MSC_VER */
156#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000157#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000159
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000161
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000162#if defined(__sgi)&&_COMPILER_VERSION>=700
163/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 (default) */
165extern char *ctermid_r(char *);
166#endif
167
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000168#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000170extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000172#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000177#endif
178#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int chdir(char *);
180extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000182extern int chdir(const char *);
183extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000185#ifdef __BORLANDC__
186extern int chmod(const char *, int);
187#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000188extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000190/*#ifdef HAVE_FCHMOD
191extern int fchmod(int, mode_t);
192#endif*/
193/*#ifdef HAVE_LCHMOD
194extern int lchmod(const char *, mode_t);
195#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int chown(const char *, uid_t, gid_t);
197extern char *getcwd(char *, int);
198extern char *strerror(int);
199extern int link(const char *, const char *);
200extern int rename(const char *, const char *);
201extern int stat(const char *, struct stat *);
202extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000205#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000207extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000211#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#ifdef HAVE_UTIME_H
214#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000215#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000217#ifdef HAVE_SYS_UTIME_H
218#include <sys/utime.h>
219#define HAVE_UTIME_H /* pretend we do for the rest of this file */
220#endif /* HAVE_SYS_UTIME_H */
221
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#ifdef HAVE_SYS_TIMES_H
223#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000224#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
226#ifdef HAVE_SYS_PARAM_H
227#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000228#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229
230#ifdef HAVE_SYS_UTSNAME_H
231#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000232#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000234#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) strlen((dirent)->d_name)
237#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000238#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#include <direct.h>
240#define NAMLEN(dirent) strlen((dirent)->d_name)
241#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000247#endif
248#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#endif
251#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#endif
254#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259#endif
260#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262#endif
263#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000266#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000267#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000268#endif
269#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000270#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000271#endif
272#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000273#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000274#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000276#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000278#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000279#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000280#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
281#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000282static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000283#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285
Guido van Rossumd48f2521997-12-05 22:19:34 +0000286#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000288#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289
Tim Petersbc2e10e2002-03-03 23:17:02 +0000290#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000291#if defined(PATH_MAX) && PATH_MAX > 1024
292#define MAXPATHLEN PATH_MAX
293#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000294#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000295#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000296#endif /* MAXPATHLEN */
297
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000298#ifdef UNION_WAIT
299/* Emulate some macros on systems that have a union instead of macros */
300
301#ifndef WIFEXITED
302#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
303#endif
304
305#ifndef WEXITSTATUS
306#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
307#endif
308
309#ifndef WTERMSIG
310#define WTERMSIG(u_wait) ((u_wait).w_termsig)
311#endif
312
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313#define WAIT_TYPE union wait
314#define WAIT_STATUS_INT(s) (s.w_status)
315
316#else /* !UNION_WAIT */
317#define WAIT_TYPE int
318#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000319#endif /* UNION_WAIT */
320
Greg Wardb48bc172000-03-01 21:51:56 +0000321/* Don't use the "_r" form if we don't need it (also, won't have a
322 prototype for it, at least on Solaris -- maybe others as well?). */
323#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
324#define USE_CTERMID_R
325#endif
326
Fred Drake699f3522000-06-29 21:12:41 +0000327/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000328#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000329#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000330# define STAT win32_stat
331# define FSTAT win32_fstat
332# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000333#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000334# define STAT stat
335# define FSTAT fstat
336# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000337#endif
338
Tim Peters11b23062003-04-23 02:39:17 +0000339#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000340#include <sys/mkdev.h>
341#else
342#if defined(MAJOR_IN_SYSMACROS)
343#include <sys/sysmacros.h>
344#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000345#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
346#include <sys/mkdev.h>
347#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000348#endif
Fred Drake699f3522000-06-29 21:12:41 +0000349
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000350#if defined _MSC_VER && _MSC_VER >= 1400
351/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
352 * valid and throw an assertion if it isn't.
353 * Normally, an invalid fd is likely to be a C program error and therefore
354 * an assertion can be useful, but it does contradict the POSIX standard
355 * which for write(2) states:
356 * "Otherwise, -1 shall be returned and errno set to indicate the error."
357 * "[EBADF] The fildes argument is not a valid file descriptor open for
358 * writing."
359 * Furthermore, python allows the user to enter any old integer
360 * as a fd and should merely raise a python exception on error.
361 * The Microsoft CRT doesn't provide an official way to check for the
362 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000363 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000364 * internal structures involved.
365 * The structures below must be updated for each version of visual studio
366 * according to the file internal.h in the CRT source, until MS comes
367 * up with a less hacky way to do this.
368 * (all of this is to avoid globally modifying the CRT behaviour using
369 * _set_invalid_parameter_handler() and _CrtSetReportMode())
370 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000371/* The actual size of the structure is determined at runtime.
372 * Only the first items must be present.
373 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000374typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000375 intptr_t osfhnd;
376 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000377} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000378
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380#define IOINFO_L2E 5
381#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
382#define IOINFO_ARRAYS 64
383#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
384#define FOPEN 0x01
385#define _NO_CONSOLE_FILENO (intptr_t)-2
386
387/* This function emulates what the windows CRT does to validate file handles */
388int
389_PyVerify_fd(int fd)
390{
Victor Stinner8c62be82010-05-06 00:08:46 +0000391 const int i1 = fd >> IOINFO_L2E;
392 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000393
Antoine Pitrou22e41552010-08-15 18:07:50 +0000394 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000395
Victor Stinner8c62be82010-05-06 00:08:46 +0000396 /* Determine the actual size of the ioinfo structure,
397 * as used by the CRT loaded in memory
398 */
399 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
400 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
401 }
402 if (sizeof_ioinfo == 0) {
403 /* This should not happen... */
404 goto fail;
405 }
406
407 /* See that it isn't a special CLEAR fileno */
408 if (fd != _NO_CONSOLE_FILENO) {
409 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
410 * we check pointer validity and other info
411 */
412 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
413 /* finally, check that the file is open */
414 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
415 if (info->osfile & FOPEN) {
416 return 1;
417 }
418 }
419 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000420 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000421 errno = EBADF;
422 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000423}
424
425/* the special case of checking dup2. The target fd must be in a sensible range */
426static int
427_PyVerify_fd_dup2(int fd1, int fd2)
428{
Victor Stinner8c62be82010-05-06 00:08:46 +0000429 if (!_PyVerify_fd(fd1))
430 return 0;
431 if (fd2 == _NO_CONSOLE_FILENO)
432 return 0;
433 if ((unsigned)fd2 < _NHANDLE_)
434 return 1;
435 else
436 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000437}
438#else
439/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
440#define _PyVerify_fd_dup2(A, B) (1)
441#endif
442
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000443#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000444/* The following structure was copied from
445 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
446 include doesn't seem to be present in the Windows SDK (at least as included
447 with Visual Studio Express). */
448typedef struct _REPARSE_DATA_BUFFER {
449 ULONG ReparseTag;
450 USHORT ReparseDataLength;
451 USHORT Reserved;
452 union {
453 struct {
454 USHORT SubstituteNameOffset;
455 USHORT SubstituteNameLength;
456 USHORT PrintNameOffset;
457 USHORT PrintNameLength;
458 ULONG Flags;
459 WCHAR PathBuffer[1];
460 } SymbolicLinkReparseBuffer;
461
462 struct {
463 USHORT SubstituteNameOffset;
464 USHORT SubstituteNameLength;
465 USHORT PrintNameOffset;
466 USHORT PrintNameLength;
467 WCHAR PathBuffer[1];
468 } MountPointReparseBuffer;
469
470 struct {
471 UCHAR DataBuffer[1];
472 } GenericReparseBuffer;
473 };
474} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
475
476#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
477 GenericReparseBuffer)
478#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
479
480static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000481win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000482{
483 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
484 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
485 DWORD n_bytes_returned;
486 const wchar_t *ptr;
487 wchar_t *buf;
488 size_t len;
489
490 if (0 == DeviceIoControl(
491 reparse_point_handle,
492 FSCTL_GET_REPARSE_POINT,
493 NULL, 0, /* in buffer */
494 target_buffer, sizeof(target_buffer),
495 &n_bytes_returned,
496 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000497 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000498
499 if (reparse_tag)
500 *reparse_tag = rdb->ReparseTag;
501
502 if (target_path) {
503 switch (rdb->ReparseTag) {
504 case IO_REPARSE_TAG_SYMLINK:
505 /* XXX: Maybe should use SubstituteName? */
506 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
507 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
508 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
509 break;
510 case IO_REPARSE_TAG_MOUNT_POINT:
511 ptr = rdb->MountPointReparseBuffer.PathBuffer +
512 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
513 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
514 break;
515 default:
516 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000517 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000518 }
519 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
520 if (!buf) {
521 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000522 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000523 }
524 wcsncpy(buf, ptr, len);
525 buf[len] = L'\0';
526 if (wcsncmp(buf, L"\\??\\", 4) == 0)
527 buf[1] = L'\\';
528 *target_path = buf;
529 }
530
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000531 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000532}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000533#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000534
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000536#ifdef WITH_NEXT_FRAMEWORK
537/* On Darwin/MacOSX a shared library or framework has no access to
538** environ directly, we must obtain it with _NSGetEnviron().
539*/
540#include <crt_externs.h>
541static char **environ;
542#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000544#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545
Barry Warsaw53699e91996-12-10 23:23:01 +0000546static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000547convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548{
Victor Stinner8c62be82010-05-06 00:08:46 +0000549 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000550#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000551 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000552#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000553 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000554#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000555#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 APIRET rc;
557 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
558#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000559
Victor Stinner8c62be82010-05-06 00:08:46 +0000560 d = PyDict_New();
561 if (d == NULL)
562 return NULL;
563#ifdef WITH_NEXT_FRAMEWORK
564 if (environ == NULL)
565 environ = *_NSGetEnviron();
566#endif
567#ifdef MS_WINDOWS
568 /* _wenviron must be initialized in this way if the program is started
569 through main() instead of wmain(). */
570 _wgetenv(L"");
571 if (_wenviron == NULL)
572 return d;
573 /* This part ignores errors */
574 for (e = _wenviron; *e != NULL; e++) {
575 PyObject *k;
576 PyObject *v;
577 wchar_t *p = wcschr(*e, L'=');
578 if (p == NULL)
579 continue;
580 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
581 if (k == NULL) {
582 PyErr_Clear();
583 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000584 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000585 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
586 if (v == NULL) {
587 PyErr_Clear();
588 Py_DECREF(k);
589 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000590 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000591 if (PyDict_GetItem(d, k) == NULL) {
592 if (PyDict_SetItem(d, k, v) != 0)
593 PyErr_Clear();
594 }
595 Py_DECREF(k);
596 Py_DECREF(v);
597 }
598#else
599 if (environ == NULL)
600 return d;
601 /* This part ignores errors */
602 for (e = environ; *e != NULL; e++) {
603 PyObject *k;
604 PyObject *v;
605 char *p = strchr(*e, '=');
606 if (p == NULL)
607 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000608 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000609 if (k == NULL) {
610 PyErr_Clear();
611 continue;
612 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000613 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000614 if (v == NULL) {
615 PyErr_Clear();
616 Py_DECREF(k);
617 continue;
618 }
619 if (PyDict_GetItem(d, k) == NULL) {
620 if (PyDict_SetItem(d, k, v) != 0)
621 PyErr_Clear();
622 }
623 Py_DECREF(k);
624 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000625 }
626#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000627#if defined(PYOS_OS2)
628 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
629 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
630 PyObject *v = PyBytes_FromString(buffer);
631 PyDict_SetItemString(d, "BEGINLIBPATH", v);
632 Py_DECREF(v);
633 }
634 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
635 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
636 PyObject *v = PyBytes_FromString(buffer);
637 PyDict_SetItemString(d, "ENDLIBPATH", v);
638 Py_DECREF(v);
639 }
640#endif
641 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644/* Set a POSIX-specific error from errno, and return NULL */
645
Barry Warsawd58d7641998-07-23 16:14:40 +0000646static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000647posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000648{
Victor Stinner8c62be82010-05-06 00:08:46 +0000649 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650}
Barry Warsawd58d7641998-07-23 16:14:40 +0000651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000652posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000653{
Victor Stinner8c62be82010-05-06 00:08:46 +0000654 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000655}
656
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000657
Mark Hammondef8b6542001-05-13 08:04:26 +0000658static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000659posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000660{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000661 PyObject *name_str, *rc;
662 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
663 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000664 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000665 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
666 name_str);
667 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000668 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000669}
670
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000671#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000672static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000673win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000674{
Victor Stinner8c62be82010-05-06 00:08:46 +0000675 /* XXX We should pass the function name along in the future.
676 (winreg.c also wants to pass the function name.)
677 This would however require an additional param to the
678 Windows error object, which is non-trivial.
679 */
680 errno = GetLastError();
681 if (filename)
682 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
683 else
684 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000685}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000687static PyObject *
688win32_error_unicode(char* function, Py_UNICODE* filename)
689{
Victor Stinner8c62be82010-05-06 00:08:46 +0000690 /* XXX - see win32_error for comments on 'function' */
691 errno = GetLastError();
692 if (filename)
693 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
694 else
695 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000696}
697
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000699convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700{
Victor Stinner8c62be82010-05-06 00:08:46 +0000701 if (PyUnicode_CheckExact(*param))
702 Py_INCREF(*param);
703 else if (PyUnicode_Check(*param))
704 /* For a Unicode subtype that's not a Unicode object,
705 return a true Unicode object with the same data. */
706 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
707 PyUnicode_GET_SIZE(*param));
708 else
709 *param = PyUnicode_FromEncodedObject(*param,
710 Py_FileSystemDefaultEncoding,
711 "strict");
712 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000713}
714
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000715#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716
Guido van Rossumd48f2521997-12-05 22:19:34 +0000717#if defined(PYOS_OS2)
718/**********************************************************************
719 * Helper Function to Trim and Format OS/2 Messages
720 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000721static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000722os2_formatmsg(char *msgbuf, int msglen, char *reason)
723{
724 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
725
726 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
727 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
728
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000729 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000730 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
731 }
732
733 /* Add Optional Reason Text */
734 if (reason) {
735 strcat(msgbuf, " : ");
736 strcat(msgbuf, reason);
737 }
738}
739
740/**********************************************************************
741 * Decode an OS/2 Operating System Error Code
742 *
743 * A convenience function to lookup an OS/2 error code and return a
744 * text message we can use to raise a Python exception.
745 *
746 * Notes:
747 * The messages for errors returned from the OS/2 kernel reside in
748 * the file OSO001.MSG in the \OS2 directory hierarchy.
749 *
750 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000751static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000752os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
753{
754 APIRET rc;
755 ULONG msglen;
756
757 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
758 Py_BEGIN_ALLOW_THREADS
759 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
760 errorcode, "oso001.msg", &msglen);
761 Py_END_ALLOW_THREADS
762
763 if (rc == NO_ERROR)
764 os2_formatmsg(msgbuf, msglen, reason);
765 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000766 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000767 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000768
769 return msgbuf;
770}
771
772/* Set an OS/2-specific error and return NULL. OS/2 kernel
773 errors are not in a global variable e.g. 'errno' nor are
774 they congruent with posix error numbers. */
775
Victor Stinner8c62be82010-05-06 00:08:46 +0000776static PyObject *
777os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000778{
779 char text[1024];
780 PyObject *v;
781
782 os2_strerror(text, sizeof(text), code, "");
783
784 v = Py_BuildValue("(is)", code, text);
785 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000786 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000787 Py_DECREF(v);
788 }
789 return NULL; /* Signal to Python that an Exception is Pending */
790}
791
792#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000793
794/* POSIX generic methods */
795
Barry Warsaw53699e91996-12-10 23:23:01 +0000796static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000797posix_fildes(PyObject *fdobj, int (*func)(int))
798{
Victor Stinner8c62be82010-05-06 00:08:46 +0000799 int fd;
800 int res;
801 fd = PyObject_AsFileDescriptor(fdobj);
802 if (fd < 0)
803 return NULL;
804 if (!_PyVerify_fd(fd))
805 return posix_error();
806 Py_BEGIN_ALLOW_THREADS
807 res = (*func)(fd);
808 Py_END_ALLOW_THREADS
809 if (res < 0)
810 return posix_error();
811 Py_INCREF(Py_None);
812 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000813}
Guido van Rossum21142a01999-01-08 21:05:37 +0000814
815static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000816posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817{
Victor Stinner8c62be82010-05-06 00:08:46 +0000818 PyObject *opath1 = NULL;
819 char *path1;
820 int res;
821 if (!PyArg_ParseTuple(args, format,
822 PyUnicode_FSConverter, &opath1))
823 return NULL;
824 path1 = PyBytes_AsString(opath1);
825 Py_BEGIN_ALLOW_THREADS
826 res = (*func)(path1);
827 Py_END_ALLOW_THREADS
828 if (res < 0)
829 return posix_error_with_allocated_filename(opath1);
830 Py_DECREF(opath1);
831 Py_INCREF(Py_None);
832 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833}
834
Barry Warsaw53699e91996-12-10 23:23:01 +0000835static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000836posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000837 char *format,
838 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839{
Victor Stinner8c62be82010-05-06 00:08:46 +0000840 PyObject *opath1 = NULL, *opath2 = NULL;
841 char *path1, *path2;
842 int res;
843 if (!PyArg_ParseTuple(args, format,
844 PyUnicode_FSConverter, &opath1,
845 PyUnicode_FSConverter, &opath2)) {
846 return NULL;
847 }
848 path1 = PyBytes_AsString(opath1);
849 path2 = PyBytes_AsString(opath2);
850 Py_BEGIN_ALLOW_THREADS
851 res = (*func)(path1, path2);
852 Py_END_ALLOW_THREADS
853 Py_DECREF(opath1);
854 Py_DECREF(opath2);
855 if (res != 0)
856 /* XXX how to report both path1 and path2??? */
857 return posix_error();
858 Py_INCREF(Py_None);
859 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860}
861
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000862#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000864win32_1str(PyObject* args, char* func,
865 char* format, BOOL (__stdcall *funcA)(LPCSTR),
866 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867{
Victor Stinner8c62be82010-05-06 00:08:46 +0000868 PyObject *uni;
869 char *ansi;
870 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000871
Victor Stinner8c62be82010-05-06 00:08:46 +0000872 if (!PyArg_ParseTuple(args, wformat, &uni))
873 PyErr_Clear();
874 else {
875 Py_BEGIN_ALLOW_THREADS
876 result = funcW(PyUnicode_AsUnicode(uni));
877 Py_END_ALLOW_THREADS
878 if (!result)
879 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
880 Py_INCREF(Py_None);
881 return Py_None;
882 }
883 if (!PyArg_ParseTuple(args, format, &ansi))
884 return NULL;
885 Py_BEGIN_ALLOW_THREADS
886 result = funcA(ansi);
887 Py_END_ALLOW_THREADS
888 if (!result)
889 return win32_error(func, ansi);
890 Py_INCREF(Py_None);
891 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892
893}
894
895/* This is a reimplementation of the C library's chdir function,
896 but one that produces Win32 errors instead of DOS error codes.
897 chdir is essentially a wrapper around SetCurrentDirectory; however,
898 it also needs to set "magic" environment variables indicating
899 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000900static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000901win32_chdir(LPCSTR path)
902{
Victor Stinner8c62be82010-05-06 00:08:46 +0000903 char new_path[MAX_PATH+1];
904 int result;
905 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906
Victor Stinner8c62be82010-05-06 00:08:46 +0000907 if(!SetCurrentDirectoryA(path))
908 return FALSE;
909 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
910 if (!result)
911 return FALSE;
912 /* In the ANSI API, there should not be any paths longer
913 than MAX_PATH. */
914 assert(result <= MAX_PATH+1);
915 if (strncmp(new_path, "\\\\", 2) == 0 ||
916 strncmp(new_path, "//", 2) == 0)
917 /* UNC path, nothing to do. */
918 return TRUE;
919 env[1] = new_path[0];
920 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921}
922
923/* The Unicode version differs from the ANSI version
924 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000925static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926win32_wchdir(LPCWSTR path)
927{
Victor Stinner8c62be82010-05-06 00:08:46 +0000928 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
929 int result;
930 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000931
Victor Stinner8c62be82010-05-06 00:08:46 +0000932 if(!SetCurrentDirectoryW(path))
933 return FALSE;
934 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
935 if (!result)
936 return FALSE;
937 if (result > MAX_PATH+1) {
938 new_path = malloc(result * sizeof(wchar_t));
939 if (!new_path) {
940 SetLastError(ERROR_OUTOFMEMORY);
941 return FALSE;
942 }
943 result = GetCurrentDirectoryW(result, new_path);
944 if (!result) {
945 free(new_path);
946 return FALSE;
947 }
948 }
949 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
950 wcsncmp(new_path, L"//", 2) == 0)
951 /* UNC path, nothing to do. */
952 return TRUE;
953 env[1] = new_path[0];
954 result = SetEnvironmentVariableW(env, new_path);
955 if (new_path != _new_path)
956 free(new_path);
957 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958}
959#endif
960
Martin v. Löwis14694662006-02-03 12:54:16 +0000961#ifdef MS_WINDOWS
962/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
963 - time stamps are restricted to second resolution
964 - file modification times suffer from forth-and-back conversions between
965 UTC and local time
966 Therefore, we implement our own stat, based on the Win32 API directly.
967*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000968#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000969
970struct win32_stat{
971 int st_dev;
972 __int64 st_ino;
973 unsigned short st_mode;
974 int st_nlink;
975 int st_uid;
976 int st_gid;
977 int st_rdev;
978 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000979 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000980 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000981 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000982 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000983 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000984 int st_ctime_nsec;
985};
986
987static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
988
989static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000990FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000991{
Victor Stinner8c62be82010-05-06 00:08:46 +0000992 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
993 /* Cannot simply cast and dereference in_ptr,
994 since it might not be aligned properly */
995 __int64 in;
996 memcpy(&in, in_ptr, sizeof(in));
997 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000998 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +0000999}
1000
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001002time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001003{
Victor Stinner8c62be82010-05-06 00:08:46 +00001004 /* XXX endianness */
1005 __int64 out;
1006 out = time_in + secs_between_epochs;
1007 out = out * 10000000 + nsec_in / 100;
1008 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001009}
1010
Martin v. Löwis14694662006-02-03 12:54:16 +00001011/* Below, we *know* that ugo+r is 0444 */
1012#if _S_IREAD != 0400
1013#error Unsupported C library
1014#endif
1015static int
1016attributes_to_mode(DWORD attr)
1017{
Victor Stinner8c62be82010-05-06 00:08:46 +00001018 int m = 0;
1019 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1020 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1021 else
1022 m |= _S_IFREG;
1023 if (attr & FILE_ATTRIBUTE_READONLY)
1024 m |= 0444;
1025 else
1026 m |= 0666;
1027 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001028}
1029
1030static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001031attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001032{
Victor Stinner8c62be82010-05-06 00:08:46 +00001033 memset(result, 0, sizeof(*result));
1034 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1035 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1036 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1037 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1038 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001039 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001040 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001041 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1042 /* first clear the S_IFMT bits */
1043 result->st_mode ^= (result->st_mode & 0170000);
1044 /* now set the bits that make this a symlink */
1045 result->st_mode |= 0120000;
1046 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001047
Victor Stinner8c62be82010-05-06 00:08:46 +00001048 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001049}
1050
Guido van Rossumd8faa362007-04-27 19:54:29 +00001051static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001052attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001053{
Victor Stinner8c62be82010-05-06 00:08:46 +00001054 HANDLE hFindFile;
1055 WIN32_FIND_DATAA FileData;
1056 hFindFile = FindFirstFileA(pszFile, &FileData);
1057 if (hFindFile == INVALID_HANDLE_VALUE)
1058 return FALSE;
1059 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001060 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001061 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001062 info->dwFileAttributes = FileData.dwFileAttributes;
1063 info->ftCreationTime = FileData.ftCreationTime;
1064 info->ftLastAccessTime = FileData.ftLastAccessTime;
1065 info->ftLastWriteTime = FileData.ftLastWriteTime;
1066 info->nFileSizeHigh = FileData.nFileSizeHigh;
1067 info->nFileSizeLow = FileData.nFileSizeLow;
1068/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001069 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1070 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001071 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001072}
1073
1074static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001075attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001076{
Victor Stinner8c62be82010-05-06 00:08:46 +00001077 HANDLE hFindFile;
1078 WIN32_FIND_DATAW FileData;
1079 hFindFile = FindFirstFileW(pszFile, &FileData);
1080 if (hFindFile == INVALID_HANDLE_VALUE)
1081 return FALSE;
1082 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001083 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001084 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001085 info->dwFileAttributes = FileData.dwFileAttributes;
1086 info->ftCreationTime = FileData.ftCreationTime;
1087 info->ftLastAccessTime = FileData.ftLastAccessTime;
1088 info->ftLastWriteTime = FileData.ftLastWriteTime;
1089 info->nFileSizeHigh = FileData.nFileSizeHigh;
1090 info->nFileSizeLow = FileData.nFileSizeLow;
1091/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001092 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1093 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001094 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001095}
1096
Brian Curtinf5e76d02010-11-24 13:14:05 +00001097#ifndef SYMLOOP_MAX
1098#define SYMLOOP_MAX ( 88 )
1099#endif
1100
1101static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001102win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001103
1104static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001105win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001106{
1107 int code;
1108 HANDLE hFile;
1109 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001110 ULONG reparse_tag = 0;
Hirokazu Yamamoto74673512010-12-05 02:48:08 +00001111 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001112 const char *dot;
1113
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001114 if (depth > SYMLOOP_MAX) {
1115 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1116 return -1;
1117 }
1118
Brian Curtinf5e76d02010-11-24 13:14:05 +00001119 hFile = CreateFileA(
1120 path,
1121 0, /* desired access */
1122 0, /* share mode */
1123 NULL, /* security attributes */
1124 OPEN_EXISTING,
1125 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1126 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1127 NULL);
1128
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001129 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001130 /* Either the target doesn't exist, or we don't have access to
1131 get a handle to it. If the former, we need to return an error.
1132 If the latter, we can use attributes_from_dir. */
1133 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001134 return -1;
1135 /* Could not get attributes on open file. Fall back to
1136 reading the directory. */
1137 if (!attributes_from_dir(path, &info, &reparse_tag))
1138 /* Very strange. This should not fail now */
1139 return -1;
1140 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1141 if (traverse) {
1142 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001143 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001144 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001145 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001146 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001147 } else {
1148 if (!GetFileInformationByHandle(hFile, &info)) {
1149 CloseHandle(hFile);
1150 return -1;;
1151 }
1152 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1153 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1154 CloseHandle(hFile);
1155 if (code < 0)
1156 return code;
1157 if (traverse) {
1158 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1159 free(target_path);
1160 return code;
1161 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001162 } else
1163 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001164 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001165 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001166
1167 /* Set S_IEXEC if it is an .exe, .bat, ... */
1168 dot = strrchr(path, '.');
1169 if (dot) {
1170 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1171 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1172 result->st_mode |= 0111;
1173 }
1174 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001175}
1176
1177static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001178win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001179{
1180 int code;
1181 HANDLE hFile;
1182 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001183 ULONG reparse_tag = 0;
1184 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001185 const wchar_t *dot;
1186
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001187 if (depth > SYMLOOP_MAX) {
1188 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1189 return -1;
1190 }
1191
Brian Curtinf5e76d02010-11-24 13:14:05 +00001192 hFile = CreateFileW(
1193 path,
1194 0, /* desired access */
1195 0, /* share mode */
1196 NULL, /* security attributes */
1197 OPEN_EXISTING,
1198 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1199 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1200 NULL);
1201
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001202 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001203 /* Either the target doesn't exist, or we don't have access to
1204 get a handle to it. If the former, we need to return an error.
1205 If the latter, we can use attributes_from_dir. */
1206 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001207 return -1;
1208 /* Could not get attributes on open file. Fall back to
1209 reading the directory. */
1210 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1211 /* Very strange. This should not fail now */
1212 return -1;
1213 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1214 if (traverse) {
1215 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001216 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001217 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001218 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001219 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001220 } else {
1221 if (!GetFileInformationByHandle(hFile, &info)) {
1222 CloseHandle(hFile);
1223 return -1;;
1224 }
1225 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1226 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1227 CloseHandle(hFile);
1228 if (code < 0)
1229 return code;
1230 if (traverse) {
1231 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1232 free(target_path);
1233 return code;
1234 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001235 } else
1236 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001237 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001238 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001239
1240 /* Set S_IEXEC if it is an .exe, .bat, ... */
1241 dot = wcsrchr(path, '.');
1242 if (dot) {
1243 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1244 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1245 result->st_mode |= 0111;
1246 }
1247 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248}
1249
1250static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001251win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001252{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001253 /* Protocol violation: we explicitly clear errno, instead of
1254 setting it to a POSIX error. Callers should use GetLastError. */
1255 int code = win32_xstat_impl(path, result, traverse, 0);
1256 errno = 0;
1257 return code;
1258}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001259
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001260static int
1261win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1262{
1263 /* Protocol violation: we explicitly clear errno, instead of
1264 setting it to a POSIX error. Callers should use GetLastError. */
1265 int code = win32_xstat_impl_w(path, result, traverse, 0);
1266 errno = 0;
1267 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268}
1269
Brian Curtind40e6f72010-07-08 21:39:08 +00001270/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1271 win32_stat_w
1272
1273 In Posix, stat automatically traverses symlinks and returns the stat
1274 structure for the target. In Windows, the equivalent GetFileAttributes by
1275 default does not traverse symlinks and instead returns attributes for
1276 the symlink.
1277
1278 Therefore, win32_lstat will get the attributes traditionally, and
1279 win32_stat will first explicitly resolve the symlink target and then will
1280 call win32_lstat on that result.
1281
1282 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1283
1284static int
1285win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001286{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001287 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001288}
1289
Victor Stinner8c62be82010-05-06 00:08:46 +00001290static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001291win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001292{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001293 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001294}
1295
1296static int
1297win32_stat(const char* path, struct win32_stat *result)
1298{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001299 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001300}
1301
1302static int
1303win32_stat_w(const wchar_t* path, struct win32_stat *result)
1304{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001305 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001306}
1307
1308static int
1309win32_fstat(int file_number, struct win32_stat *result)
1310{
Victor Stinner8c62be82010-05-06 00:08:46 +00001311 BY_HANDLE_FILE_INFORMATION info;
1312 HANDLE h;
1313 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001314
Victor Stinner8c62be82010-05-06 00:08:46 +00001315 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001316
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 /* Protocol violation: we explicitly clear errno, instead of
1318 setting it to a POSIX error. Callers should use GetLastError. */
1319 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001320
Victor Stinner8c62be82010-05-06 00:08:46 +00001321 if (h == INVALID_HANDLE_VALUE) {
1322 /* This is really a C library error (invalid file handle).
1323 We set the Win32 error to the closes one matching. */
1324 SetLastError(ERROR_INVALID_HANDLE);
1325 return -1;
1326 }
1327 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001328
Victor Stinner8c62be82010-05-06 00:08:46 +00001329 type = GetFileType(h);
1330 if (type == FILE_TYPE_UNKNOWN) {
1331 DWORD error = GetLastError();
1332 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001333 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001334 }
1335 /* else: valid but unknown file */
1336 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001337
Victor Stinner8c62be82010-05-06 00:08:46 +00001338 if (type != FILE_TYPE_DISK) {
1339 if (type == FILE_TYPE_CHAR)
1340 result->st_mode = _S_IFCHR;
1341 else if (type == FILE_TYPE_PIPE)
1342 result->st_mode = _S_IFIFO;
1343 return 0;
1344 }
1345
1346 if (!GetFileInformationByHandle(h, &info)) {
1347 return -1;
1348 }
1349
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001350 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001351 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001352 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1353 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001354}
1355
1356#endif /* MS_WINDOWS */
1357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001358PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359"stat_result: Result from stat or lstat.\n\n\
1360This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001361 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001362or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1363\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001364Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1365or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001366\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001367See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001368
1369static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001370 {"st_mode", "protection bits"},
1371 {"st_ino", "inode"},
1372 {"st_dev", "device"},
1373 {"st_nlink", "number of hard links"},
1374 {"st_uid", "user ID of owner"},
1375 {"st_gid", "group ID of owner"},
1376 {"st_size", "total size, in bytes"},
1377 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1378 {NULL, "integer time of last access"},
1379 {NULL, "integer time of last modification"},
1380 {NULL, "integer time of last change"},
1381 {"st_atime", "time of last access"},
1382 {"st_mtime", "time of last modification"},
1383 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001384#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001385 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001386#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001387#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001388 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001389#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001390#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001391 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001392#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001393#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001394 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001395#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001396#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001397 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001398#endif
1399#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001400 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001401#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001402 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001403};
1404
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001405#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001406#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001407#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001408#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001409#endif
1410
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001411#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001412#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1413#else
1414#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1415#endif
1416
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001417#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001418#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1419#else
1420#define ST_RDEV_IDX ST_BLOCKS_IDX
1421#endif
1422
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001423#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1424#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1425#else
1426#define ST_FLAGS_IDX ST_RDEV_IDX
1427#endif
1428
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001429#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001430#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001431#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001432#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001433#endif
1434
1435#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1436#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1437#else
1438#define ST_BIRTHTIME_IDX ST_GEN_IDX
1439#endif
1440
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001441static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001442 "stat_result", /* name */
1443 stat_result__doc__, /* doc */
1444 stat_result_fields,
1445 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001446};
1447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001448PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001449"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1450This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001451 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001452or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001453\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001454See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001455
1456static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001457 {"f_bsize", },
1458 {"f_frsize", },
1459 {"f_blocks", },
1460 {"f_bfree", },
1461 {"f_bavail", },
1462 {"f_files", },
1463 {"f_ffree", },
1464 {"f_favail", },
1465 {"f_flag", },
1466 {"f_namemax",},
1467 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001468};
1469
1470static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 "statvfs_result", /* name */
1472 statvfs_result__doc__, /* doc */
1473 statvfs_result_fields,
1474 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001475};
1476
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001478static PyTypeObject StatResultType;
1479static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001480static newfunc structseq_new;
1481
1482static PyObject *
1483statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1484{
Victor Stinner8c62be82010-05-06 00:08:46 +00001485 PyStructSequence *result;
1486 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001487
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 result = (PyStructSequence*)structseq_new(type, args, kwds);
1489 if (!result)
1490 return NULL;
1491 /* If we have been initialized from a tuple,
1492 st_?time might be set to None. Initialize it
1493 from the int slots. */
1494 for (i = 7; i <= 9; i++) {
1495 if (result->ob_item[i+3] == Py_None) {
1496 Py_DECREF(Py_None);
1497 Py_INCREF(result->ob_item[i]);
1498 result->ob_item[i+3] = result->ob_item[i];
1499 }
1500 }
1501 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001502}
1503
1504
1505
1506/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001507static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001508
1509PyDoc_STRVAR(stat_float_times__doc__,
1510"stat_float_times([newval]) -> oldval\n\n\
1511Determine whether os.[lf]stat represents time stamps as float objects.\n\
1512If newval is True, future calls to stat() return floats, if it is False,\n\
1513future calls return ints. \n\
1514If newval is omitted, return the current setting.\n");
1515
1516static PyObject*
1517stat_float_times(PyObject* self, PyObject *args)
1518{
Victor Stinner8c62be82010-05-06 00:08:46 +00001519 int newval = -1;
1520 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1521 return NULL;
1522 if (newval == -1)
1523 /* Return old value */
1524 return PyBool_FromLong(_stat_float_times);
1525 _stat_float_times = newval;
1526 Py_INCREF(Py_None);
1527 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001528}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001529
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001530static void
1531fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1532{
Victor Stinner8c62be82010-05-06 00:08:46 +00001533 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001534#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001536#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001537 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001538#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001539 if (!ival)
1540 return;
1541 if (_stat_float_times) {
1542 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1543 } else {
1544 fval = ival;
1545 Py_INCREF(fval);
1546 }
1547 PyStructSequence_SET_ITEM(v, index, ival);
1548 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001549}
1550
Tim Peters5aa91602002-01-30 05:46:57 +00001551/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001552 (used by posix_stat() and posix_fstat()) */
1553static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001554_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001555{
Victor Stinner8c62be82010-05-06 00:08:46 +00001556 unsigned long ansec, mnsec, cnsec;
1557 PyObject *v = PyStructSequence_New(&StatResultType);
1558 if (v == NULL)
1559 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001560
Victor Stinner8c62be82010-05-06 00:08:46 +00001561 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001562#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 PyStructSequence_SET_ITEM(v, 1,
1564 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001565#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001566 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001567#endif
1568#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001569 PyStructSequence_SET_ITEM(v, 2,
1570 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001571#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001572 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001573#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001574 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1575 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1576 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001577#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 PyStructSequence_SET_ITEM(v, 6,
1579 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001580#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001581 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001582#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001583
Martin v. Löwis14694662006-02-03 12:54:16 +00001584#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001585 ansec = st->st_atim.tv_nsec;
1586 mnsec = st->st_mtim.tv_nsec;
1587 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001588#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 ansec = st->st_atimespec.tv_nsec;
1590 mnsec = st->st_mtimespec.tv_nsec;
1591 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001592#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 ansec = st->st_atime_nsec;
1594 mnsec = st->st_mtime_nsec;
1595 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001596#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001597 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001598#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 fill_time(v, 7, st->st_atime, ansec);
1600 fill_time(v, 8, st->st_mtime, mnsec);
1601 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001602
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001603#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1605 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001606#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001607#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1609 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001610#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001611#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001612 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1613 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001614#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001615#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001616 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1617 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001618#endif
1619#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001620 {
1621 PyObject *val;
1622 unsigned long bsec,bnsec;
1623 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001624#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001625 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001626#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001628#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001629 if (_stat_float_times) {
1630 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1631 } else {
1632 val = PyLong_FromLong((long)bsec);
1633 }
1634 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1635 val);
1636 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001637#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001638#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001639 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1640 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001641#endif
Fred Drake699f3522000-06-29 21:12:41 +00001642
Victor Stinner8c62be82010-05-06 00:08:46 +00001643 if (PyErr_Occurred()) {
1644 Py_DECREF(v);
1645 return NULL;
1646 }
Fred Drake699f3522000-06-29 21:12:41 +00001647
Victor Stinner8c62be82010-05-06 00:08:46 +00001648 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001649}
1650
Barry Warsaw53699e91996-12-10 23:23:01 +00001651static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001652posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001653 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001654#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001656#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001657 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001658#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001659 char *wformat,
1660 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001661{
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 STRUCT_STAT st;
1663 PyObject *opath;
1664 char *path;
1665 int res;
1666 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001667
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001668#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001669 PyUnicodeObject *po;
1670 if (PyArg_ParseTuple(args, wformat, &po)) {
1671 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001672
Victor Stinner8c62be82010-05-06 00:08:46 +00001673 Py_BEGIN_ALLOW_THREADS
1674 /* PyUnicode_AS_UNICODE result OK without
1675 thread lock as it is a simple dereference. */
1676 res = wstatfunc(wpath, &st);
1677 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001678
Victor Stinner8c62be82010-05-06 00:08:46 +00001679 if (res != 0)
1680 return win32_error_unicode("stat", wpath);
1681 return _pystat_fromstructstat(&st);
1682 }
1683 /* Drop the argument parsing error as narrow strings
1684 are also valid. */
1685 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001686#endif
1687
Victor Stinner8c62be82010-05-06 00:08:46 +00001688 if (!PyArg_ParseTuple(args, format,
1689 PyUnicode_FSConverter, &opath))
1690 return NULL;
1691 path = PyBytes_AsString(opath);
1692 Py_BEGIN_ALLOW_THREADS
1693 res = (*statfunc)(path, &st);
1694 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001695
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001697#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001699#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001701#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 }
1703 else
1704 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001705
Victor Stinner8c62be82010-05-06 00:08:46 +00001706 Py_DECREF(opath);
1707 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001708}
1709
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001710/* POSIX methods */
1711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001712PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001713"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001714Use the real uid/gid to test for access to a path. Note that most\n\
1715operations will use the effective uid/gid, therefore this routine can\n\
1716be used in a suid/sgid environment to test if the invoking user has the\n\
1717specified access to the path. The mode argument can be F_OK to test\n\
1718existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001719
1720static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001721posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001722{
Victor Stinner8c62be82010-05-06 00:08:46 +00001723 PyObject *opath;
1724 char *path;
1725 int mode;
1726
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001727#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 DWORD attr;
1729 PyUnicodeObject *po;
1730 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1731 Py_BEGIN_ALLOW_THREADS
1732 /* PyUnicode_AS_UNICODE OK without thread lock as
1733 it is a simple dereference. */
1734 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1735 Py_END_ALLOW_THREADS
1736 goto finish;
1737 }
1738 /* Drop the argument parsing error as narrow strings
1739 are also valid. */
1740 PyErr_Clear();
1741 if (!PyArg_ParseTuple(args, "O&i:access",
1742 PyUnicode_FSConverter, &opath, &mode))
1743 return NULL;
1744 path = PyBytes_AsString(opath);
1745 Py_BEGIN_ALLOW_THREADS
1746 attr = GetFileAttributesA(path);
1747 Py_END_ALLOW_THREADS
1748 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001749finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001750 if (attr == 0xFFFFFFFF)
1751 /* File does not exist, or cannot read attributes */
1752 return PyBool_FromLong(0);
1753 /* Access is possible if either write access wasn't requested, or
1754 the file isn't read-only, or if it's a directory, as there are
1755 no read-only directories on Windows. */
1756 return PyBool_FromLong(!(mode & 2)
1757 || !(attr & FILE_ATTRIBUTE_READONLY)
1758 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001759#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 int res;
1761 if (!PyArg_ParseTuple(args, "O&i:access",
1762 PyUnicode_FSConverter, &opath, &mode))
1763 return NULL;
1764 path = PyBytes_AsString(opath);
1765 Py_BEGIN_ALLOW_THREADS
1766 res = access(path, mode);
1767 Py_END_ALLOW_THREADS
1768 Py_DECREF(opath);
1769 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001770#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001771}
1772
Guido van Rossumd371ff11999-01-25 16:12:23 +00001773#ifndef F_OK
1774#define F_OK 0
1775#endif
1776#ifndef R_OK
1777#define R_OK 4
1778#endif
1779#ifndef W_OK
1780#define W_OK 2
1781#endif
1782#ifndef X_OK
1783#define X_OK 1
1784#endif
1785
1786#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001787PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001788"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001790
1791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001792posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001793{
Victor Stinner8c62be82010-05-06 00:08:46 +00001794 int id;
1795 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001796
Victor Stinner8c62be82010-05-06 00:08:46 +00001797 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1798 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001799
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001800#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001801 /* file descriptor 0 only, the default input device (stdin) */
1802 if (id == 0) {
1803 ret = ttyname();
1804 }
1805 else {
1806 ret = NULL;
1807 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001808#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001809 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001810#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001811 if (ret == NULL)
1812 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001813 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001814}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001815#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001816
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001817#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001819"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001821
1822static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001823posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001824{
Victor Stinner8c62be82010-05-06 00:08:46 +00001825 char *ret;
1826 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001827
Greg Wardb48bc172000-03-01 21:51:56 +00001828#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001829 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001830#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001832#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 if (ret == NULL)
1834 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001835 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001836}
1837#endif
1838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001839PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001840"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Barry Warsaw53699e91996-12-10 23:23:01 +00001843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001844posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001847 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001848#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001849 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001850#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001851 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001852#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001853 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001854#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001855}
1856
Fred Drake4d1e64b2002-04-15 19:40:07 +00001857#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001859"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001860Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001862
1863static PyObject *
1864posix_fchdir(PyObject *self, PyObject *fdobj)
1865{
Victor Stinner8c62be82010-05-06 00:08:46 +00001866 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001867}
1868#endif /* HAVE_FCHDIR */
1869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001872"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001874
Barry Warsaw53699e91996-12-10 23:23:01 +00001875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001876posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001877{
Victor Stinner8c62be82010-05-06 00:08:46 +00001878 PyObject *opath = NULL;
1879 char *path = NULL;
1880 int i;
1881 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001882#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 DWORD attr;
1884 PyUnicodeObject *po;
1885 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1886 Py_BEGIN_ALLOW_THREADS
1887 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1888 if (attr != 0xFFFFFFFF) {
1889 if (i & _S_IWRITE)
1890 attr &= ~FILE_ATTRIBUTE_READONLY;
1891 else
1892 attr |= FILE_ATTRIBUTE_READONLY;
1893 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1894 }
1895 else
1896 res = 0;
1897 Py_END_ALLOW_THREADS
1898 if (!res)
1899 return win32_error_unicode("chmod",
1900 PyUnicode_AS_UNICODE(po));
1901 Py_INCREF(Py_None);
1902 return Py_None;
1903 }
1904 /* Drop the argument parsing error as narrow strings
1905 are also valid. */
1906 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001907
Victor Stinner8c62be82010-05-06 00:08:46 +00001908 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1909 &opath, &i))
1910 return NULL;
1911 path = PyBytes_AsString(opath);
1912 Py_BEGIN_ALLOW_THREADS
1913 attr = GetFileAttributesA(path);
1914 if (attr != 0xFFFFFFFF) {
1915 if (i & _S_IWRITE)
1916 attr &= ~FILE_ATTRIBUTE_READONLY;
1917 else
1918 attr |= FILE_ATTRIBUTE_READONLY;
1919 res = SetFileAttributesA(path, attr);
1920 }
1921 else
1922 res = 0;
1923 Py_END_ALLOW_THREADS
1924 if (!res) {
1925 win32_error("chmod", path);
1926 Py_DECREF(opath);
1927 return NULL;
1928 }
1929 Py_DECREF(opath);
1930 Py_INCREF(Py_None);
1931 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001932#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001933 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1934 &opath, &i))
1935 return NULL;
1936 path = PyBytes_AsString(opath);
1937 Py_BEGIN_ALLOW_THREADS
1938 res = chmod(path, i);
1939 Py_END_ALLOW_THREADS
1940 if (res < 0)
1941 return posix_error_with_allocated_filename(opath);
1942 Py_DECREF(opath);
1943 Py_INCREF(Py_None);
1944 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001945#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001946}
1947
Christian Heimes4e30a842007-11-30 22:12:06 +00001948#ifdef HAVE_FCHMOD
1949PyDoc_STRVAR(posix_fchmod__doc__,
1950"fchmod(fd, mode)\n\n\
1951Change the access permissions of the file given by file\n\
1952descriptor fd.");
1953
1954static PyObject *
1955posix_fchmod(PyObject *self, PyObject *args)
1956{
Victor Stinner8c62be82010-05-06 00:08:46 +00001957 int fd, mode, res;
1958 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1959 return NULL;
1960 Py_BEGIN_ALLOW_THREADS
1961 res = fchmod(fd, mode);
1962 Py_END_ALLOW_THREADS
1963 if (res < 0)
1964 return posix_error();
1965 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001966}
1967#endif /* HAVE_FCHMOD */
1968
1969#ifdef HAVE_LCHMOD
1970PyDoc_STRVAR(posix_lchmod__doc__,
1971"lchmod(path, mode)\n\n\
1972Change the access permissions of a file. If path is a symlink, this\n\
1973affects the link itself rather than the target.");
1974
1975static PyObject *
1976posix_lchmod(PyObject *self, PyObject *args)
1977{
Victor Stinner8c62be82010-05-06 00:08:46 +00001978 PyObject *opath;
1979 char *path;
1980 int i;
1981 int res;
1982 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1983 &opath, &i))
1984 return NULL;
1985 path = PyBytes_AsString(opath);
1986 Py_BEGIN_ALLOW_THREADS
1987 res = lchmod(path, i);
1988 Py_END_ALLOW_THREADS
1989 if (res < 0)
1990 return posix_error_with_allocated_filename(opath);
1991 Py_DECREF(opath);
1992 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001993}
1994#endif /* HAVE_LCHMOD */
1995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001996
Thomas Wouterscf297e42007-02-23 15:07:44 +00001997#ifdef HAVE_CHFLAGS
1998PyDoc_STRVAR(posix_chflags__doc__,
1999"chflags(path, flags)\n\n\
2000Set file flags.");
2001
2002static PyObject *
2003posix_chflags(PyObject *self, PyObject *args)
2004{
Victor Stinner8c62be82010-05-06 00:08:46 +00002005 PyObject *opath;
2006 char *path;
2007 unsigned long flags;
2008 int res;
2009 if (!PyArg_ParseTuple(args, "O&k:chflags",
2010 PyUnicode_FSConverter, &opath, &flags))
2011 return NULL;
2012 path = PyBytes_AsString(opath);
2013 Py_BEGIN_ALLOW_THREADS
2014 res = chflags(path, flags);
2015 Py_END_ALLOW_THREADS
2016 if (res < 0)
2017 return posix_error_with_allocated_filename(opath);
2018 Py_DECREF(opath);
2019 Py_INCREF(Py_None);
2020 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002021}
2022#endif /* HAVE_CHFLAGS */
2023
2024#ifdef HAVE_LCHFLAGS
2025PyDoc_STRVAR(posix_lchflags__doc__,
2026"lchflags(path, flags)\n\n\
2027Set file flags.\n\
2028This function will not follow symbolic links.");
2029
2030static PyObject *
2031posix_lchflags(PyObject *self, PyObject *args)
2032{
Victor Stinner8c62be82010-05-06 00:08:46 +00002033 PyObject *opath;
2034 char *path;
2035 unsigned long flags;
2036 int res;
2037 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2038 PyUnicode_FSConverter, &opath, &flags))
2039 return NULL;
2040 path = PyBytes_AsString(opath);
2041 Py_BEGIN_ALLOW_THREADS
2042 res = lchflags(path, flags);
2043 Py_END_ALLOW_THREADS
2044 if (res < 0)
2045 return posix_error_with_allocated_filename(opath);
2046 Py_DECREF(opath);
2047 Py_INCREF(Py_None);
2048 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002049}
2050#endif /* HAVE_LCHFLAGS */
2051
Martin v. Löwis244edc82001-10-04 22:44:26 +00002052#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002053PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002054"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002056
2057static PyObject *
2058posix_chroot(PyObject *self, PyObject *args)
2059{
Victor Stinner8c62be82010-05-06 00:08:46 +00002060 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002061}
2062#endif
2063
Guido van Rossum21142a01999-01-08 21:05:37 +00002064#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002066"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002068
2069static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002070posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002071{
Stefan Krah0e803b32010-11-26 16:16:47 +00002072 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002073}
2074#endif /* HAVE_FSYNC */
2075
2076#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002077
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002078#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002079extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2080#endif
2081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002083"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002084force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002086
2087static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002088posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002089{
Stefan Krah0e803b32010-11-26 16:16:47 +00002090 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002091}
2092#endif /* HAVE_FDATASYNC */
2093
2094
Fredrik Lundh10723342000-07-10 16:38:09 +00002095#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002096PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002097"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002098Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002099
Barry Warsaw53699e91996-12-10 23:23:01 +00002100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002101posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002102{
Victor Stinner8c62be82010-05-06 00:08:46 +00002103 PyObject *opath;
2104 char *path;
2105 long uid, gid;
2106 int res;
2107 if (!PyArg_ParseTuple(args, "O&ll:chown",
2108 PyUnicode_FSConverter, &opath,
2109 &uid, &gid))
2110 return NULL;
2111 path = PyBytes_AsString(opath);
2112 Py_BEGIN_ALLOW_THREADS
2113 res = chown(path, (uid_t) uid, (gid_t) gid);
2114 Py_END_ALLOW_THREADS
2115 if (res < 0)
2116 return posix_error_with_allocated_filename(opath);
2117 Py_DECREF(opath);
2118 Py_INCREF(Py_None);
2119 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002120}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002121#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002122
Christian Heimes4e30a842007-11-30 22:12:06 +00002123#ifdef HAVE_FCHOWN
2124PyDoc_STRVAR(posix_fchown__doc__,
2125"fchown(fd, uid, gid)\n\n\
2126Change the owner and group id of the file given by file descriptor\n\
2127fd to the numeric uid and gid.");
2128
2129static PyObject *
2130posix_fchown(PyObject *self, PyObject *args)
2131{
Victor Stinner8c62be82010-05-06 00:08:46 +00002132 int fd;
2133 long uid, gid;
2134 int res;
2135 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2136 return NULL;
2137 Py_BEGIN_ALLOW_THREADS
2138 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2139 Py_END_ALLOW_THREADS
2140 if (res < 0)
2141 return posix_error();
2142 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002143}
2144#endif /* HAVE_FCHOWN */
2145
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002146#ifdef HAVE_LCHOWN
2147PyDoc_STRVAR(posix_lchown__doc__,
2148"lchown(path, uid, gid)\n\n\
2149Change the owner and group id of path to the numeric uid and gid.\n\
2150This function will not follow symbolic links.");
2151
2152static PyObject *
2153posix_lchown(PyObject *self, PyObject *args)
2154{
Victor Stinner8c62be82010-05-06 00:08:46 +00002155 PyObject *opath;
2156 char *path;
2157 long uid, gid;
2158 int res;
2159 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2160 PyUnicode_FSConverter, &opath,
2161 &uid, &gid))
2162 return NULL;
2163 path = PyBytes_AsString(opath);
2164 Py_BEGIN_ALLOW_THREADS
2165 res = lchown(path, (uid_t) uid, (gid_t) gid);
2166 Py_END_ALLOW_THREADS
2167 if (res < 0)
2168 return posix_error_with_allocated_filename(opath);
2169 Py_DECREF(opath);
2170 Py_INCREF(Py_None);
2171 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002172}
2173#endif /* HAVE_LCHOWN */
2174
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002175
Guido van Rossum36bc6801995-06-14 22:54:23 +00002176#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002177static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002178posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002179{
Victor Stinner8c62be82010-05-06 00:08:46 +00002180 char buf[1026];
2181 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002182
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002183#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002184 if (!use_bytes) {
2185 wchar_t wbuf[1026];
2186 wchar_t *wbuf2 = wbuf;
2187 PyObject *resobj;
2188 DWORD len;
2189 Py_BEGIN_ALLOW_THREADS
2190 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2191 /* If the buffer is large enough, len does not include the
2192 terminating \0. If the buffer is too small, len includes
2193 the space needed for the terminator. */
2194 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2195 wbuf2 = malloc(len * sizeof(wchar_t));
2196 if (wbuf2)
2197 len = GetCurrentDirectoryW(len, wbuf2);
2198 }
2199 Py_END_ALLOW_THREADS
2200 if (!wbuf2) {
2201 PyErr_NoMemory();
2202 return NULL;
2203 }
2204 if (!len) {
2205 if (wbuf2 != wbuf) free(wbuf2);
2206 return win32_error("getcwdu", NULL);
2207 }
2208 resobj = PyUnicode_FromWideChar(wbuf2, len);
2209 if (wbuf2 != wbuf) free(wbuf2);
2210 return resobj;
2211 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002212#endif
2213
Victor Stinner8c62be82010-05-06 00:08:46 +00002214 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002215#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002216 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002217#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002218 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002219#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002220 Py_END_ALLOW_THREADS
2221 if (res == NULL)
2222 return posix_error();
2223 if (use_bytes)
2224 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002225 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002226}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002227
2228PyDoc_STRVAR(posix_getcwd__doc__,
2229"getcwd() -> path\n\n\
2230Return a unicode string representing the current working directory.");
2231
2232static PyObject *
2233posix_getcwd_unicode(PyObject *self)
2234{
2235 return posix_getcwd(0);
2236}
2237
2238PyDoc_STRVAR(posix_getcwdb__doc__,
2239"getcwdb() -> path\n\n\
2240Return a bytes string representing the current working directory.");
2241
2242static PyObject *
2243posix_getcwd_bytes(PyObject *self)
2244{
2245 return posix_getcwd(1);
2246}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002247#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002248
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002249
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002251PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002252"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002253Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002254
Barry Warsaw53699e91996-12-10 23:23:01 +00002255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002256posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002257{
Victor Stinner8c62be82010-05-06 00:08:46 +00002258 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002259}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002260#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002261
Brian Curtin1b9df392010-11-24 20:24:31 +00002262#ifdef MS_WINDOWS
2263PyDoc_STRVAR(win32_link__doc__,
2264"link(src, dst)\n\n\
2265Create a hard link to a file.");
2266
2267static PyObject *
2268win32_link(PyObject *self, PyObject *args)
2269{
2270 PyObject *osrc, *odst;
2271 char *src, *dst;
2272 BOOL rslt;
2273
Brian Curtinfc889c42010-11-28 23:59:46 +00002274 PyUnicodeObject *usrc, *udst;
2275 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2276 Py_BEGIN_ALLOW_THREADS
2277 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2278 PyUnicode_AS_UNICODE(usrc), NULL);
2279 Py_END_ALLOW_THREADS
2280
2281 if (rslt == 0)
2282 return win32_error("link", NULL);
2283
2284 Py_RETURN_NONE;
2285 }
2286
2287 /* Narrow strings also valid. */
2288 PyErr_Clear();
2289
Brian Curtin1b9df392010-11-24 20:24:31 +00002290 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2291 PyUnicode_FSConverter, &odst))
2292 return NULL;
2293
2294 src = PyBytes_AsString(osrc);
2295 dst = PyBytes_AsString(odst);
2296
2297 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002298 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002299 Py_END_ALLOW_THREADS
2300
Stefan Krah30b341f2010-11-27 11:44:18 +00002301 Py_DECREF(osrc);
2302 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002303 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002304 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002305
2306 Py_RETURN_NONE;
2307}
2308#endif /* MS_WINDOWS */
2309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002311PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002312"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002313Return a list containing the names of the entries in the directory.\n\
2314\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002315 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002316\n\
2317The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002318entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002319
Barry Warsaw53699e91996-12-10 23:23:01 +00002320static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002321posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002322{
Victor Stinner8c62be82010-05-06 00:08:46 +00002323 /* XXX Should redo this putting the (now four) versions of opendir
2324 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002325#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002326
Victor Stinner8c62be82010-05-06 00:08:46 +00002327 PyObject *d, *v;
2328 HANDLE hFindFile;
2329 BOOL result;
2330 WIN32_FIND_DATA FileData;
2331 PyObject *opath;
2332 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2333 char *bufptr = namebuf;
2334 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002335
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002336 PyObject *po = NULL;
2337 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002338 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002339 Py_UNICODE *wnamebuf, *po_wchars;
2340
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002341 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002342 po_wchars = L".";
2343 len = 1;
2344 } else {
2345 po_wchars = PyUnicode_AS_UNICODE(po);
2346 len = PyUnicode_GET_SIZE(po);
2347 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002348 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002349 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2350 if (!wnamebuf) {
2351 PyErr_NoMemory();
2352 return NULL;
2353 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002354 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002355 if (len > 0) {
2356 Py_UNICODE wch = wnamebuf[len-1];
2357 if (wch != L'/' && wch != L'\\' && wch != L':')
2358 wnamebuf[len++] = L'\\';
2359 wcscpy(wnamebuf + len, L"*.*");
2360 }
2361 if ((d = PyList_New(0)) == NULL) {
2362 free(wnamebuf);
2363 return NULL;
2364 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002365 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002367 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 if (hFindFile == INVALID_HANDLE_VALUE) {
2369 int error = GetLastError();
2370 if (error == ERROR_FILE_NOT_FOUND) {
2371 free(wnamebuf);
2372 return d;
2373 }
2374 Py_DECREF(d);
2375 win32_error_unicode("FindFirstFileW", wnamebuf);
2376 free(wnamebuf);
2377 return NULL;
2378 }
2379 do {
2380 /* Skip over . and .. */
2381 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2382 wcscmp(wFileData.cFileName, L"..") != 0) {
2383 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2384 if (v == NULL) {
2385 Py_DECREF(d);
2386 d = NULL;
2387 break;
2388 }
2389 if (PyList_Append(d, v) != 0) {
2390 Py_DECREF(v);
2391 Py_DECREF(d);
2392 d = NULL;
2393 break;
2394 }
2395 Py_DECREF(v);
2396 }
2397 Py_BEGIN_ALLOW_THREADS
2398 result = FindNextFileW(hFindFile, &wFileData);
2399 Py_END_ALLOW_THREADS
2400 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2401 it got to the end of the directory. */
2402 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2403 Py_DECREF(d);
2404 win32_error_unicode("FindNextFileW", wnamebuf);
2405 FindClose(hFindFile);
2406 free(wnamebuf);
2407 return NULL;
2408 }
2409 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002410
Victor Stinner8c62be82010-05-06 00:08:46 +00002411 if (FindClose(hFindFile) == FALSE) {
2412 Py_DECREF(d);
2413 win32_error_unicode("FindClose", wnamebuf);
2414 free(wnamebuf);
2415 return NULL;
2416 }
2417 free(wnamebuf);
2418 return d;
2419 }
2420 /* Drop the argument parsing error as narrow strings
2421 are also valid. */
2422 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002423
Victor Stinner8c62be82010-05-06 00:08:46 +00002424 if (!PyArg_ParseTuple(args, "O&:listdir",
2425 PyUnicode_FSConverter, &opath))
2426 return NULL;
2427 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2428 PyErr_SetString(PyExc_ValueError, "path too long");
2429 Py_DECREF(opath);
2430 return NULL;
2431 }
2432 strcpy(namebuf, PyBytes_AsString(opath));
2433 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002434 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002435 if (len > 0) {
2436 char ch = namebuf[len-1];
2437 if (ch != SEP && ch != ALTSEP && ch != ':')
2438 namebuf[len++] = '/';
2439 strcpy(namebuf + len, "*.*");
2440 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002441
Victor Stinner8c62be82010-05-06 00:08:46 +00002442 if ((d = PyList_New(0)) == NULL)
2443 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002444
Antoine Pitroub73caab2010-08-09 23:39:31 +00002445 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002446 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002447 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002448 if (hFindFile == INVALID_HANDLE_VALUE) {
2449 int error = GetLastError();
2450 if (error == ERROR_FILE_NOT_FOUND)
2451 return d;
2452 Py_DECREF(d);
2453 return win32_error("FindFirstFile", namebuf);
2454 }
2455 do {
2456 /* Skip over . and .. */
2457 if (strcmp(FileData.cFileName, ".") != 0 &&
2458 strcmp(FileData.cFileName, "..") != 0) {
2459 v = PyBytes_FromString(FileData.cFileName);
2460 if (v == NULL) {
2461 Py_DECREF(d);
2462 d = NULL;
2463 break;
2464 }
2465 if (PyList_Append(d, v) != 0) {
2466 Py_DECREF(v);
2467 Py_DECREF(d);
2468 d = NULL;
2469 break;
2470 }
2471 Py_DECREF(v);
2472 }
2473 Py_BEGIN_ALLOW_THREADS
2474 result = FindNextFile(hFindFile, &FileData);
2475 Py_END_ALLOW_THREADS
2476 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2477 it got to the end of the directory. */
2478 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2479 Py_DECREF(d);
2480 win32_error("FindNextFile", namebuf);
2481 FindClose(hFindFile);
2482 return NULL;
2483 }
2484 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002485
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 if (FindClose(hFindFile) == FALSE) {
2487 Py_DECREF(d);
2488 return win32_error("FindClose", namebuf);
2489 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002490
Victor Stinner8c62be82010-05-06 00:08:46 +00002491 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002492
Tim Peters0bb44a42000-09-15 07:44:49 +00002493#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002494
2495#ifndef MAX_PATH
2496#define MAX_PATH CCHMAXPATH
2497#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002498 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002499 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002500 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002501 PyObject *d, *v;
2502 char namebuf[MAX_PATH+5];
2503 HDIR hdir = 1;
2504 ULONG srchcnt = 1;
2505 FILEFINDBUF3 ep;
2506 APIRET rc;
2507
Victor Stinner8c62be82010-05-06 00:08:46 +00002508 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002509 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002510 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002511 name = PyBytes_AsString(oname);
2512 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002513 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002514 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002515 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002516 return NULL;
2517 }
2518 strcpy(namebuf, name);
2519 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002520 if (*pt == ALTSEP)
2521 *pt = SEP;
2522 if (namebuf[len-1] != SEP)
2523 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002524 strcpy(namebuf + len, "*.*");
2525
Neal Norwitz6c913782007-10-14 03:23:09 +00002526 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002527 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002528 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002529 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002530
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002531 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2532 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002533 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002534 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2535 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2536 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002537
2538 if (rc != NO_ERROR) {
2539 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002540 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002541 }
2542
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002543 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002544 do {
2545 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002546 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002547 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002548
2549 strcpy(namebuf, ep.achName);
2550
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002551 /* Leave Case of Name Alone -- In Native Form */
2552 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002553
Christian Heimes72b710a2008-05-26 13:28:38 +00002554 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002555 if (v == NULL) {
2556 Py_DECREF(d);
2557 d = NULL;
2558 break;
2559 }
2560 if (PyList_Append(d, v) != 0) {
2561 Py_DECREF(v);
2562 Py_DECREF(d);
2563 d = NULL;
2564 break;
2565 }
2566 Py_DECREF(v);
2567 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2568 }
2569
Victor Stinnerdcb24032010-04-22 12:08:36 +00002570 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002571 return d;
2572#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002573 PyObject *oname;
2574 char *name;
2575 PyObject *d, *v;
2576 DIR *dirp;
2577 struct dirent *ep;
2578 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002579
Victor Stinner8c62be82010-05-06 00:08:46 +00002580 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002581 /* v is never read, so it does not need to be initialized yet. */
2582 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 arg_is_unicode = 0;
2584 PyErr_Clear();
2585 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002586 oname = NULL;
2587 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002588 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002589 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002590 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002591 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002592 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002593 Py_BEGIN_ALLOW_THREADS
2594 dirp = opendir(name);
2595 Py_END_ALLOW_THREADS
2596 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002597 return posix_error_with_allocated_filename(oname);
2598 }
2599 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002600 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002601 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002602 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002603 Py_DECREF(oname);
2604 return NULL;
2605 }
2606 for (;;) {
2607 errno = 0;
2608 Py_BEGIN_ALLOW_THREADS
2609 ep = readdir(dirp);
2610 Py_END_ALLOW_THREADS
2611 if (ep == NULL) {
2612 if (errno == 0) {
2613 break;
2614 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002615 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002616 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002617 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002618 Py_DECREF(d);
2619 return posix_error_with_allocated_filename(oname);
2620 }
2621 }
2622 if (ep->d_name[0] == '.' &&
2623 (NAMLEN(ep) == 1 ||
2624 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2625 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002626 if (arg_is_unicode)
2627 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2628 else
2629 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002630 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002631 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 break;
2633 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 if (PyList_Append(d, v) != 0) {
2635 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002636 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002637 break;
2638 }
2639 Py_DECREF(v);
2640 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002641 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002642 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002643 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002645
Victor Stinner8c62be82010-05-06 00:08:46 +00002646 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002647
Tim Peters0bb44a42000-09-15 07:44:49 +00002648#endif /* which OS */
2649} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002650
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002651#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002652/* A helper function for abspath on win32 */
2653static PyObject *
2654posix__getfullpathname(PyObject *self, PyObject *args)
2655{
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 PyObject *opath;
2657 char *path;
2658 char outbuf[MAX_PATH*2];
2659 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002660#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002661 PyUnicodeObject *po;
2662 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2663 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2664 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2665 Py_UNICODE *wtemp;
2666 DWORD result;
2667 PyObject *v;
2668 result = GetFullPathNameW(wpath,
2669 sizeof(woutbuf)/sizeof(woutbuf[0]),
2670 woutbuf, &wtemp);
2671 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2672 woutbufp = malloc(result * sizeof(Py_UNICODE));
2673 if (!woutbufp)
2674 return PyErr_NoMemory();
2675 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2676 }
2677 if (result)
2678 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2679 else
2680 v = win32_error_unicode("GetFullPathNameW", wpath);
2681 if (woutbufp != woutbuf)
2682 free(woutbufp);
2683 return v;
2684 }
2685 /* Drop the argument parsing error as narrow strings
2686 are also valid. */
2687 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002688
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002689#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002690 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2691 PyUnicode_FSConverter, &opath))
2692 return NULL;
2693 path = PyBytes_AsString(opath);
2694 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2695 outbuf, &temp)) {
2696 win32_error("GetFullPathName", path);
2697 Py_DECREF(opath);
2698 return NULL;
2699 }
2700 Py_DECREF(opath);
2701 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2702 return PyUnicode_Decode(outbuf, strlen(outbuf),
2703 Py_FileSystemDefaultEncoding, NULL);
2704 }
2705 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002706} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002707
Brian Curtinf5e76d02010-11-24 13:14:05 +00002708/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2709static int has_GetFinalPathNameByHandle = 0;
2710static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2711 DWORD);
2712static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2713 DWORD);
2714static int
2715check_GetFinalPathNameByHandle()
2716{
2717 HINSTANCE hKernel32;
2718 /* only recheck */
2719 if (!has_GetFinalPathNameByHandle)
2720 {
2721 hKernel32 = GetModuleHandle("KERNEL32");
2722 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2723 "GetFinalPathNameByHandleA");
2724 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2725 "GetFinalPathNameByHandleW");
2726 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2727 Py_GetFinalPathNameByHandleW;
2728 }
2729 return has_GetFinalPathNameByHandle;
2730}
2731
Brian Curtind40e6f72010-07-08 21:39:08 +00002732/* A helper function for samepath on windows */
2733static PyObject *
2734posix__getfinalpathname(PyObject *self, PyObject *args)
2735{
2736 HANDLE hFile;
2737 int buf_size;
2738 wchar_t *target_path;
2739 int result_length;
2740 PyObject *result;
2741 wchar_t *path;
2742
Brian Curtin94622b02010-09-24 00:03:39 +00002743 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002744 return NULL;
2745 }
2746
2747 if(!check_GetFinalPathNameByHandle()) {
2748 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2749 NotImplementedError. */
2750 return PyErr_Format(PyExc_NotImplementedError,
2751 "GetFinalPathNameByHandle not available on this platform");
2752 }
2753
2754 hFile = CreateFileW(
2755 path,
2756 0, /* desired access */
2757 0, /* share mode */
2758 NULL, /* security attributes */
2759 OPEN_EXISTING,
2760 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2761 FILE_FLAG_BACKUP_SEMANTICS,
2762 NULL);
2763
2764 if(hFile == INVALID_HANDLE_VALUE) {
2765 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002766 return PyErr_Format(PyExc_RuntimeError,
2767 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002768 }
2769
2770 /* We have a good handle to the target, use it to determine the
2771 target path name. */
2772 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2773
2774 if(!buf_size)
2775 return win32_error_unicode("GetFinalPathNameByHandle", path);
2776
2777 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2778 if(!target_path)
2779 return PyErr_NoMemory();
2780
2781 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2782 buf_size, VOLUME_NAME_DOS);
2783 if(!result_length)
2784 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2785
2786 if(!CloseHandle(hFile))
2787 return win32_error_unicode("GetFinalPathNameByHandle", path);
2788
2789 target_path[result_length] = 0;
2790 result = PyUnicode_FromUnicode(target_path, result_length);
2791 free(target_path);
2792 return result;
2793
2794} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002795
2796static PyObject *
2797posix__getfileinformation(PyObject *self, PyObject *args)
2798{
2799 HANDLE hFile;
2800 BY_HANDLE_FILE_INFORMATION info;
2801 int fd;
2802
2803 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2804 return NULL;
2805
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002806 if (!_PyVerify_fd(fd))
2807 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002808
2809 hFile = (HANDLE)_get_osfhandle(fd);
2810 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002811 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002812
2813 if (!GetFileInformationByHandle(hFile, &info))
2814 return win32_error("_getfileinformation", NULL);
2815
2816 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2817 info.nFileIndexHigh,
2818 info.nFileIndexLow);
2819}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002820#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002822PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002823"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002824Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002825
Barry Warsaw53699e91996-12-10 23:23:01 +00002826static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002827posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002828{
Victor Stinner8c62be82010-05-06 00:08:46 +00002829 int res;
2830 PyObject *opath;
2831 char *path;
2832 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002833
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002834#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002835 PyUnicodeObject *po;
2836 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2837 Py_BEGIN_ALLOW_THREADS
2838 /* PyUnicode_AS_UNICODE OK without thread lock as
2839 it is a simple dereference. */
2840 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2841 Py_END_ALLOW_THREADS
2842 if (!res)
2843 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2844 Py_INCREF(Py_None);
2845 return Py_None;
2846 }
2847 /* Drop the argument parsing error as narrow strings
2848 are also valid. */
2849 PyErr_Clear();
2850 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2851 PyUnicode_FSConverter, &opath, &mode))
2852 return NULL;
2853 path = PyBytes_AsString(opath);
2854 Py_BEGIN_ALLOW_THREADS
2855 /* PyUnicode_AS_UNICODE OK without thread lock as
2856 it is a simple dereference. */
2857 res = CreateDirectoryA(path, NULL);
2858 Py_END_ALLOW_THREADS
2859 if (!res) {
2860 win32_error("mkdir", path);
2861 Py_DECREF(opath);
2862 return NULL;
2863 }
2864 Py_DECREF(opath);
2865 Py_INCREF(Py_None);
2866 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002867#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002868
Victor Stinner8c62be82010-05-06 00:08:46 +00002869 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2870 PyUnicode_FSConverter, &opath, &mode))
2871 return NULL;
2872 path = PyBytes_AsString(opath);
2873 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002874#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002875 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002876#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002877 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002878#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 Py_END_ALLOW_THREADS
2880 if (res < 0)
2881 return posix_error_with_allocated_filename(opath);
2882 Py_DECREF(opath);
2883 Py_INCREF(Py_None);
2884 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002885#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002886}
2887
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002888
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002889/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2890#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002891#include <sys/resource.h>
2892#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002893
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002894
2895#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002896PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002897"nice(inc) -> new_priority\n\n\
2898Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002899
Barry Warsaw53699e91996-12-10 23:23:01 +00002900static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002901posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002902{
Victor Stinner8c62be82010-05-06 00:08:46 +00002903 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002904
Victor Stinner8c62be82010-05-06 00:08:46 +00002905 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2906 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002907
Victor Stinner8c62be82010-05-06 00:08:46 +00002908 /* There are two flavours of 'nice': one that returns the new
2909 priority (as required by almost all standards out there) and the
2910 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2911 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002912
Victor Stinner8c62be82010-05-06 00:08:46 +00002913 If we are of the nice family that returns the new priority, we
2914 need to clear errno before the call, and check if errno is filled
2915 before calling posix_error() on a returnvalue of -1, because the
2916 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002917
Victor Stinner8c62be82010-05-06 00:08:46 +00002918 errno = 0;
2919 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002920#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002921 if (value == 0)
2922 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002923#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002924 if (value == -1 && errno != 0)
2925 /* either nice() or getpriority() returned an error */
2926 return posix_error();
2927 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002928}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002929#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002930
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002931PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002932"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002933Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002934
Barry Warsaw53699e91996-12-10 23:23:01 +00002935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002936posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002937{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002938#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002939 PyObject *o1, *o2;
2940 char *p1, *p2;
2941 BOOL result;
2942 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2943 goto error;
2944 if (!convert_to_unicode(&o1))
2945 goto error;
2946 if (!convert_to_unicode(&o2)) {
2947 Py_DECREF(o1);
2948 goto error;
2949 }
2950 Py_BEGIN_ALLOW_THREADS
2951 result = MoveFileW(PyUnicode_AsUnicode(o1),
2952 PyUnicode_AsUnicode(o2));
2953 Py_END_ALLOW_THREADS
2954 Py_DECREF(o1);
2955 Py_DECREF(o2);
2956 if (!result)
2957 return win32_error("rename", NULL);
2958 Py_INCREF(Py_None);
2959 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002960error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002961 PyErr_Clear();
2962 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2963 return NULL;
2964 Py_BEGIN_ALLOW_THREADS
2965 result = MoveFileA(p1, p2);
2966 Py_END_ALLOW_THREADS
2967 if (!result)
2968 return win32_error("rename", NULL);
2969 Py_INCREF(Py_None);
2970 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002971#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002972 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002973#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002974}
2975
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002977PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002978"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002979Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002980
Barry Warsaw53699e91996-12-10 23:23:01 +00002981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002982posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002983{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002984#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002985 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002986#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002987 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002988#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002989}
2990
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002992PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002993"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002994Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002995
Barry Warsaw53699e91996-12-10 23:23:01 +00002996static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002997posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002998{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002999#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003000 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003001#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003002 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003003#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003004}
3005
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003006
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003007#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003008PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003009"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003011
Barry Warsaw53699e91996-12-10 23:23:01 +00003012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003013posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003014{
Victor Stinner8c62be82010-05-06 00:08:46 +00003015 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003016#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003017 wchar_t *command;
3018 if (!PyArg_ParseTuple(args, "u:system", &command))
3019 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003020
Victor Stinner8c62be82010-05-06 00:08:46 +00003021 Py_BEGIN_ALLOW_THREADS
3022 sts = _wsystem(command);
3023 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003024#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003025 PyObject *command_obj;
3026 char *command;
3027 if (!PyArg_ParseTuple(args, "O&:system",
3028 PyUnicode_FSConverter, &command_obj))
3029 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003030
Victor Stinner8c62be82010-05-06 00:08:46 +00003031 command = PyBytes_AsString(command_obj);
3032 Py_BEGIN_ALLOW_THREADS
3033 sts = system(command);
3034 Py_END_ALLOW_THREADS
3035 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003036#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003037 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003038}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003039#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003040
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003042PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003043"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003044Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003045
Barry Warsaw53699e91996-12-10 23:23:01 +00003046static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003047posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003048{
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 int i;
3050 if (!PyArg_ParseTuple(args, "i:umask", &i))
3051 return NULL;
3052 i = (int)umask(i);
3053 if (i < 0)
3054 return posix_error();
3055 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003056}
3057
Brian Curtind40e6f72010-07-08 21:39:08 +00003058#ifdef MS_WINDOWS
3059
3060/* override the default DeleteFileW behavior so that directory
3061symlinks can be removed with this function, the same as with
3062Unix symlinks */
3063BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3064{
3065 WIN32_FILE_ATTRIBUTE_DATA info;
3066 WIN32_FIND_DATAW find_data;
3067 HANDLE find_data_handle;
3068 int is_directory = 0;
3069 int is_link = 0;
3070
3071 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3072 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3073
3074 /* Get WIN32_FIND_DATA structure for the path to determine if
3075 it is a symlink */
3076 if(is_directory &&
3077 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3078 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3079
3080 if(find_data_handle != INVALID_HANDLE_VALUE) {
3081 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3082 FindClose(find_data_handle);
3083 }
3084 }
3085 }
3086
3087 if (is_directory && is_link)
3088 return RemoveDirectoryW(lpFileName);
3089
3090 return DeleteFileW(lpFileName);
3091}
3092#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003093
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003094PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003095"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003099"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003101
Barry Warsaw53699e91996-12-10 23:23:01 +00003102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003103posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003104{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003105#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003106 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3107 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003108#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003109 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003110#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003111}
3112
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003113
Guido van Rossumb6775db1994-08-01 11:34:53 +00003114#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003115PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003116"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003117Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003118
Barry Warsaw53699e91996-12-10 23:23:01 +00003119static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003120posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003121{
Victor Stinner8c62be82010-05-06 00:08:46 +00003122 struct utsname u;
3123 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003124
Victor Stinner8c62be82010-05-06 00:08:46 +00003125 Py_BEGIN_ALLOW_THREADS
3126 res = uname(&u);
3127 Py_END_ALLOW_THREADS
3128 if (res < 0)
3129 return posix_error();
3130 return Py_BuildValue("(sssss)",
3131 u.sysname,
3132 u.nodename,
3133 u.release,
3134 u.version,
3135 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003136}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003137#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003138
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003139static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003140extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003141{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003142 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 if (PyFloat_Check(t)) {
3144 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003145 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003146 if (!intobj)
3147 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003148#if SIZEOF_TIME_T > SIZEOF_LONG
3149 intval = PyLong_AsUnsignedLongLongMask(intobj);
3150#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003151 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003152#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 Py_DECREF(intobj);
3154 if (intval == -1 && PyErr_Occurred())
3155 return -1;
3156 *sec = intval;
3157 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3158 if (*usec < 0)
3159 /* If rounding gave us a negative number,
3160 truncate. */
3161 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003162 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003164#if SIZEOF_TIME_T > SIZEOF_LONG
3165 intval = PyLong_AsUnsignedLongLongMask(t);
3166#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003167 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003168#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003169 if (intval == -1 && PyErr_Occurred())
3170 return -1;
3171 *sec = intval;
3172 *usec = 0;
3173 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003174}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003176PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003177"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003178utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003179Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003180second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003181
Barry Warsaw53699e91996-12-10 23:23:01 +00003182static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003183posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003184{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003185#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003186 PyObject *arg;
3187 PyUnicodeObject *obwpath;
3188 wchar_t *wpath = NULL;
3189 PyObject *oapath;
3190 char *apath;
3191 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003192 time_t atimesec, mtimesec;
3193 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 FILETIME atime, mtime;
3195 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003196
Victor Stinner8c62be82010-05-06 00:08:46 +00003197 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3198 wpath = PyUnicode_AS_UNICODE(obwpath);
3199 Py_BEGIN_ALLOW_THREADS
3200 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3201 NULL, OPEN_EXISTING,
3202 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3203 Py_END_ALLOW_THREADS
3204 if (hFile == INVALID_HANDLE_VALUE)
3205 return win32_error_unicode("utime", wpath);
3206 } else
3207 /* Drop the argument parsing error as narrow strings
3208 are also valid. */
3209 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003210
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 if (!wpath) {
3212 if (!PyArg_ParseTuple(args, "O&O:utime",
3213 PyUnicode_FSConverter, &oapath, &arg))
3214 return NULL;
3215 apath = PyBytes_AsString(oapath);
3216 Py_BEGIN_ALLOW_THREADS
3217 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3218 NULL, OPEN_EXISTING,
3219 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3220 Py_END_ALLOW_THREADS
3221 if (hFile == INVALID_HANDLE_VALUE) {
3222 win32_error("utime", apath);
3223 Py_DECREF(oapath);
3224 return NULL;
3225 }
3226 Py_DECREF(oapath);
3227 }
3228
3229 if (arg == Py_None) {
3230 SYSTEMTIME now;
3231 GetSystemTime(&now);
3232 if (!SystemTimeToFileTime(&now, &mtime) ||
3233 !SystemTimeToFileTime(&now, &atime)) {
3234 win32_error("utime", NULL);
3235 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003236 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003237 }
3238 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3239 PyErr_SetString(PyExc_TypeError,
3240 "utime() arg 2 must be a tuple (atime, mtime)");
3241 goto done;
3242 }
3243 else {
3244 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3245 &atimesec, &ausec) == -1)
3246 goto done;
3247 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3248 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3249 &mtimesec, &musec) == -1)
3250 goto done;
3251 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3252 }
3253 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3254 /* Avoid putting the file name into the error here,
3255 as that may confuse the user into believing that
3256 something is wrong with the file, when it also
3257 could be the time stamp that gives a problem. */
3258 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003259 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003260 }
3261 Py_INCREF(Py_None);
3262 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003263done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003264 CloseHandle(hFile);
3265 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003266#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003267
Victor Stinner8c62be82010-05-06 00:08:46 +00003268 PyObject *opath;
3269 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003270 time_t atime, mtime;
3271 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003272 int res;
3273 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003274
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003275#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003276 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003277#define ATIME buf[0].tv_sec
3278#define MTIME buf[1].tv_sec
3279#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003280/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003281 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003282#define ATIME buf.actime
3283#define MTIME buf.modtime
3284#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003285#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003286 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003287#define ATIME buf[0]
3288#define MTIME buf[1]
3289#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003290#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003291
Mark Hammond817c9292003-12-03 01:22:38 +00003292
Victor Stinner8c62be82010-05-06 00:08:46 +00003293 if (!PyArg_ParseTuple(args, "O&O:utime",
3294 PyUnicode_FSConverter, &opath, &arg))
3295 return NULL;
3296 path = PyBytes_AsString(opath);
3297 if (arg == Py_None) {
3298 /* optional time values not given */
3299 Py_BEGIN_ALLOW_THREADS
3300 res = utime(path, NULL);
3301 Py_END_ALLOW_THREADS
3302 }
3303 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3304 PyErr_SetString(PyExc_TypeError,
3305 "utime() arg 2 must be a tuple (atime, mtime)");
3306 Py_DECREF(opath);
3307 return NULL;
3308 }
3309 else {
3310 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3311 &atime, &ausec) == -1) {
3312 Py_DECREF(opath);
3313 return NULL;
3314 }
3315 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3316 &mtime, &musec) == -1) {
3317 Py_DECREF(opath);
3318 return NULL;
3319 }
3320 ATIME = atime;
3321 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003322#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003323 buf[0].tv_usec = ausec;
3324 buf[1].tv_usec = musec;
3325 Py_BEGIN_ALLOW_THREADS
3326 res = utimes(path, buf);
3327 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003328#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003329 Py_BEGIN_ALLOW_THREADS
3330 res = utime(path, UTIME_ARG);
3331 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003332#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 }
3334 if (res < 0) {
3335 return posix_error_with_allocated_filename(opath);
3336 }
3337 Py_DECREF(opath);
3338 Py_INCREF(Py_None);
3339 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003340#undef UTIME_ARG
3341#undef ATIME
3342#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003343#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003344}
3345
Guido van Rossum85e3b011991-06-03 12:42:10 +00003346
Guido van Rossum3b066191991-06-04 19:40:25 +00003347/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003348
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003349PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003350"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003351Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003352
Barry Warsaw53699e91996-12-10 23:23:01 +00003353static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003354posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003355{
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 int sts;
3357 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3358 return NULL;
3359 _exit(sts);
3360 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003361}
3362
Martin v. Löwis114619e2002-10-07 06:44:21 +00003363#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3364static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003365free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003366{
Victor Stinner8c62be82010-05-06 00:08:46 +00003367 Py_ssize_t i;
3368 for (i = 0; i < count; i++)
3369 PyMem_Free(array[i]);
3370 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003371}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003372
Antoine Pitrou69f71142009-05-24 21:25:49 +00003373static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003374int fsconvert_strdup(PyObject *o, char**out)
3375{
Victor Stinner8c62be82010-05-06 00:08:46 +00003376 PyObject *bytes;
3377 Py_ssize_t size;
3378 if (!PyUnicode_FSConverter(o, &bytes))
3379 return 0;
3380 size = PyBytes_GET_SIZE(bytes);
3381 *out = PyMem_Malloc(size+1);
3382 if (!*out)
3383 return 0;
3384 memcpy(*out, PyBytes_AsString(bytes), size+1);
3385 Py_DECREF(bytes);
3386 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003387}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003388#endif
3389
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003390
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003391#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003392PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003393"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003394Execute an executable path with arguments, replacing current process.\n\
3395\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003396 path: path of executable file\n\
3397 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003398
Barry Warsaw53699e91996-12-10 23:23:01 +00003399static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003400posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003401{
Victor Stinner8c62be82010-05-06 00:08:46 +00003402 PyObject *opath;
3403 char *path;
3404 PyObject *argv;
3405 char **argvlist;
3406 Py_ssize_t i, argc;
3407 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003408
Victor Stinner8c62be82010-05-06 00:08:46 +00003409 /* execv has two arguments: (path, argv), where
3410 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003411
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 if (!PyArg_ParseTuple(args, "O&O:execv",
3413 PyUnicode_FSConverter,
3414 &opath, &argv))
3415 return NULL;
3416 path = PyBytes_AsString(opath);
3417 if (PyList_Check(argv)) {
3418 argc = PyList_Size(argv);
3419 getitem = PyList_GetItem;
3420 }
3421 else if (PyTuple_Check(argv)) {
3422 argc = PyTuple_Size(argv);
3423 getitem = PyTuple_GetItem;
3424 }
3425 else {
3426 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3427 Py_DECREF(opath);
3428 return NULL;
3429 }
3430 if (argc < 1) {
3431 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3432 Py_DECREF(opath);
3433 return NULL;
3434 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003435
Victor Stinner8c62be82010-05-06 00:08:46 +00003436 argvlist = PyMem_NEW(char *, argc+1);
3437 if (argvlist == NULL) {
3438 Py_DECREF(opath);
3439 return PyErr_NoMemory();
3440 }
3441 for (i = 0; i < argc; i++) {
3442 if (!fsconvert_strdup((*getitem)(argv, i),
3443 &argvlist[i])) {
3444 free_string_array(argvlist, i);
3445 PyErr_SetString(PyExc_TypeError,
3446 "execv() arg 2 must contain only strings");
3447 Py_DECREF(opath);
3448 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003449
Victor Stinner8c62be82010-05-06 00:08:46 +00003450 }
3451 }
3452 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003453
Victor Stinner8c62be82010-05-06 00:08:46 +00003454 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003455
Victor Stinner8c62be82010-05-06 00:08:46 +00003456 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003457
Victor Stinner8c62be82010-05-06 00:08:46 +00003458 free_string_array(argvlist, argc);
3459 Py_DECREF(opath);
3460 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003461}
3462
Victor Stinner13bb71c2010-04-23 21:41:56 +00003463static char**
3464parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3465{
Victor Stinner8c62be82010-05-06 00:08:46 +00003466 char **envlist;
3467 Py_ssize_t i, pos, envc;
3468 PyObject *keys=NULL, *vals=NULL;
3469 PyObject *key, *val, *key2, *val2;
3470 char *p, *k, *v;
3471 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003472
Victor Stinner8c62be82010-05-06 00:08:46 +00003473 i = PyMapping_Size(env);
3474 if (i < 0)
3475 return NULL;
3476 envlist = PyMem_NEW(char *, i + 1);
3477 if (envlist == NULL) {
3478 PyErr_NoMemory();
3479 return NULL;
3480 }
3481 envc = 0;
3482 keys = PyMapping_Keys(env);
3483 vals = PyMapping_Values(env);
3484 if (!keys || !vals)
3485 goto error;
3486 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3487 PyErr_Format(PyExc_TypeError,
3488 "env.keys() or env.values() is not a list");
3489 goto error;
3490 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003491
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 for (pos = 0; pos < i; pos++) {
3493 key = PyList_GetItem(keys, pos);
3494 val = PyList_GetItem(vals, pos);
3495 if (!key || !val)
3496 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003497
Victor Stinner8c62be82010-05-06 00:08:46 +00003498 if (PyUnicode_FSConverter(key, &key2) == 0)
3499 goto error;
3500 if (PyUnicode_FSConverter(val, &val2) == 0) {
3501 Py_DECREF(key2);
3502 goto error;
3503 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003504
3505#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003506 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3507 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003508#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003509 k = PyBytes_AsString(key2);
3510 v = PyBytes_AsString(val2);
3511 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003512
Victor Stinner8c62be82010-05-06 00:08:46 +00003513 p = PyMem_NEW(char, len);
3514 if (p == NULL) {
3515 PyErr_NoMemory();
3516 Py_DECREF(key2);
3517 Py_DECREF(val2);
3518 goto error;
3519 }
3520 PyOS_snprintf(p, len, "%s=%s", k, v);
3521 envlist[envc++] = p;
3522 Py_DECREF(key2);
3523 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003524#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003525 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003526#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003527 }
3528 Py_DECREF(vals);
3529 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003530
Victor Stinner8c62be82010-05-06 00:08:46 +00003531 envlist[envc] = 0;
3532 *envc_ptr = envc;
3533 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003534
3535error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 Py_XDECREF(keys);
3537 Py_XDECREF(vals);
3538 while (--envc >= 0)
3539 PyMem_DEL(envlist[envc]);
3540 PyMem_DEL(envlist);
3541 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003542}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003543
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003544PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003545"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003546Execute a path with arguments and environment, replacing current process.\n\
3547\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003548 path: path of executable file\n\
3549 args: tuple or list of arguments\n\
3550 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003551
Barry Warsaw53699e91996-12-10 23:23:01 +00003552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003553posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003554{
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 PyObject *opath;
3556 char *path;
3557 PyObject *argv, *env;
3558 char **argvlist;
3559 char **envlist;
3560 Py_ssize_t i, argc, envc;
3561 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3562 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003563
Victor Stinner8c62be82010-05-06 00:08:46 +00003564 /* execve has three arguments: (path, argv, env), where
3565 argv is a list or tuple of strings and env is a dictionary
3566 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003567
Victor Stinner8c62be82010-05-06 00:08:46 +00003568 if (!PyArg_ParseTuple(args, "O&OO:execve",
3569 PyUnicode_FSConverter,
3570 &opath, &argv, &env))
3571 return NULL;
3572 path = PyBytes_AsString(opath);
3573 if (PyList_Check(argv)) {
3574 argc = PyList_Size(argv);
3575 getitem = PyList_GetItem;
3576 }
3577 else if (PyTuple_Check(argv)) {
3578 argc = PyTuple_Size(argv);
3579 getitem = PyTuple_GetItem;
3580 }
3581 else {
3582 PyErr_SetString(PyExc_TypeError,
3583 "execve() arg 2 must be a tuple or list");
3584 goto fail_0;
3585 }
3586 if (!PyMapping_Check(env)) {
3587 PyErr_SetString(PyExc_TypeError,
3588 "execve() arg 3 must be a mapping object");
3589 goto fail_0;
3590 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003591
Victor Stinner8c62be82010-05-06 00:08:46 +00003592 argvlist = PyMem_NEW(char *, argc+1);
3593 if (argvlist == NULL) {
3594 PyErr_NoMemory();
3595 goto fail_0;
3596 }
3597 for (i = 0; i < argc; i++) {
3598 if (!fsconvert_strdup((*getitem)(argv, i),
3599 &argvlist[i]))
3600 {
3601 lastarg = i;
3602 goto fail_1;
3603 }
3604 }
3605 lastarg = argc;
3606 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003607
Victor Stinner8c62be82010-05-06 00:08:46 +00003608 envlist = parse_envlist(env, &envc);
3609 if (envlist == NULL)
3610 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003611
Victor Stinner8c62be82010-05-06 00:08:46 +00003612 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003613
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003615
Victor Stinner8c62be82010-05-06 00:08:46 +00003616 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003617
Victor Stinner8c62be82010-05-06 00:08:46 +00003618 while (--envc >= 0)
3619 PyMem_DEL(envlist[envc]);
3620 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003621 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003622 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003623 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003624 Py_DECREF(opath);
3625 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003626}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003627#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003628
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003629
Guido van Rossuma1065681999-01-25 23:20:23 +00003630#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003631PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003632"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003633Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003634\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003635 mode: mode of process creation\n\
3636 path: path of executable file\n\
3637 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003638
3639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003640posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003641{
Victor Stinner8c62be82010-05-06 00:08:46 +00003642 PyObject *opath;
3643 char *path;
3644 PyObject *argv;
3645 char **argvlist;
3646 int mode, i;
3647 Py_ssize_t argc;
3648 Py_intptr_t spawnval;
3649 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003650
Victor Stinner8c62be82010-05-06 00:08:46 +00003651 /* spawnv has three arguments: (mode, path, argv), where
3652 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003653
Victor Stinner8c62be82010-05-06 00:08:46 +00003654 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3655 PyUnicode_FSConverter,
3656 &opath, &argv))
3657 return NULL;
3658 path = PyBytes_AsString(opath);
3659 if (PyList_Check(argv)) {
3660 argc = PyList_Size(argv);
3661 getitem = PyList_GetItem;
3662 }
3663 else if (PyTuple_Check(argv)) {
3664 argc = PyTuple_Size(argv);
3665 getitem = PyTuple_GetItem;
3666 }
3667 else {
3668 PyErr_SetString(PyExc_TypeError,
3669 "spawnv() arg 2 must be a tuple or list");
3670 Py_DECREF(opath);
3671 return NULL;
3672 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003673
Victor Stinner8c62be82010-05-06 00:08:46 +00003674 argvlist = PyMem_NEW(char *, argc+1);
3675 if (argvlist == NULL) {
3676 Py_DECREF(opath);
3677 return PyErr_NoMemory();
3678 }
3679 for (i = 0; i < argc; i++) {
3680 if (!fsconvert_strdup((*getitem)(argv, i),
3681 &argvlist[i])) {
3682 free_string_array(argvlist, i);
3683 PyErr_SetString(
3684 PyExc_TypeError,
3685 "spawnv() arg 2 must contain only strings");
3686 Py_DECREF(opath);
3687 return NULL;
3688 }
3689 }
3690 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003691
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003692#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003693 Py_BEGIN_ALLOW_THREADS
3694 spawnval = spawnv(mode, path, argvlist);
3695 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003696#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003697 if (mode == _OLD_P_OVERLAY)
3698 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003699
Victor Stinner8c62be82010-05-06 00:08:46 +00003700 Py_BEGIN_ALLOW_THREADS
3701 spawnval = _spawnv(mode, path, argvlist);
3702 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003703#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003704
Victor Stinner8c62be82010-05-06 00:08:46 +00003705 free_string_array(argvlist, argc);
3706 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003707
Victor Stinner8c62be82010-05-06 00:08:46 +00003708 if (spawnval == -1)
3709 return posix_error();
3710 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003711#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003713#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003714 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003715#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003716}
3717
3718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003719PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003720"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003721Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003722\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003723 mode: mode of process creation\n\
3724 path: path of executable file\n\
3725 args: tuple or list of arguments\n\
3726 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003727
3728static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003729posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003730{
Victor Stinner8c62be82010-05-06 00:08:46 +00003731 PyObject *opath;
3732 char *path;
3733 PyObject *argv, *env;
3734 char **argvlist;
3735 char **envlist;
3736 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003737 int mode;
3738 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003739 Py_intptr_t spawnval;
3740 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3741 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003742
Victor Stinner8c62be82010-05-06 00:08:46 +00003743 /* spawnve has four arguments: (mode, path, argv, env), where
3744 argv is a list or tuple of strings and env is a dictionary
3745 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003746
Victor Stinner8c62be82010-05-06 00:08:46 +00003747 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3748 PyUnicode_FSConverter,
3749 &opath, &argv, &env))
3750 return NULL;
3751 path = PyBytes_AsString(opath);
3752 if (PyList_Check(argv)) {
3753 argc = PyList_Size(argv);
3754 getitem = PyList_GetItem;
3755 }
3756 else if (PyTuple_Check(argv)) {
3757 argc = PyTuple_Size(argv);
3758 getitem = PyTuple_GetItem;
3759 }
3760 else {
3761 PyErr_SetString(PyExc_TypeError,
3762 "spawnve() arg 2 must be a tuple or list");
3763 goto fail_0;
3764 }
3765 if (!PyMapping_Check(env)) {
3766 PyErr_SetString(PyExc_TypeError,
3767 "spawnve() arg 3 must be a mapping object");
3768 goto fail_0;
3769 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003770
Victor Stinner8c62be82010-05-06 00:08:46 +00003771 argvlist = PyMem_NEW(char *, argc+1);
3772 if (argvlist == NULL) {
3773 PyErr_NoMemory();
3774 goto fail_0;
3775 }
3776 for (i = 0; i < argc; i++) {
3777 if (!fsconvert_strdup((*getitem)(argv, i),
3778 &argvlist[i]))
3779 {
3780 lastarg = i;
3781 goto fail_1;
3782 }
3783 }
3784 lastarg = argc;
3785 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003786
Victor Stinner8c62be82010-05-06 00:08:46 +00003787 envlist = parse_envlist(env, &envc);
3788 if (envlist == NULL)
3789 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003790
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003791#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003792 Py_BEGIN_ALLOW_THREADS
3793 spawnval = spawnve(mode, path, argvlist, envlist);
3794 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003795#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003796 if (mode == _OLD_P_OVERLAY)
3797 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003798
Victor Stinner8c62be82010-05-06 00:08:46 +00003799 Py_BEGIN_ALLOW_THREADS
3800 spawnval = _spawnve(mode, path, argvlist, envlist);
3801 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003802#endif
Tim Peters25059d32001-12-07 20:35:43 +00003803
Victor Stinner8c62be82010-05-06 00:08:46 +00003804 if (spawnval == -1)
3805 (void) posix_error();
3806 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003807#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003808 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003809#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003810 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003811#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003812
Victor Stinner8c62be82010-05-06 00:08:46 +00003813 while (--envc >= 0)
3814 PyMem_DEL(envlist[envc]);
3815 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003816 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003817 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003818 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 Py_DECREF(opath);
3820 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003821}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003822
3823/* OS/2 supports spawnvp & spawnvpe natively */
3824#if defined(PYOS_OS2)
3825PyDoc_STRVAR(posix_spawnvp__doc__,
3826"spawnvp(mode, file, args)\n\n\
3827Execute the program 'file' in a new process, using the environment\n\
3828search path to find the file.\n\
3829\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003830 mode: mode of process creation\n\
3831 file: executable file name\n\
3832 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003833
3834static PyObject *
3835posix_spawnvp(PyObject *self, PyObject *args)
3836{
Victor Stinner8c62be82010-05-06 00:08:46 +00003837 PyObject *opath;
3838 char *path;
3839 PyObject *argv;
3840 char **argvlist;
3841 int mode, i, argc;
3842 Py_intptr_t spawnval;
3843 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003844
Victor Stinner8c62be82010-05-06 00:08:46 +00003845 /* spawnvp has three arguments: (mode, path, argv), where
3846 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003847
Victor Stinner8c62be82010-05-06 00:08:46 +00003848 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3849 PyUnicode_FSConverter,
3850 &opath, &argv))
3851 return NULL;
3852 path = PyBytes_AsString(opath);
3853 if (PyList_Check(argv)) {
3854 argc = PyList_Size(argv);
3855 getitem = PyList_GetItem;
3856 }
3857 else if (PyTuple_Check(argv)) {
3858 argc = PyTuple_Size(argv);
3859 getitem = PyTuple_GetItem;
3860 }
3861 else {
3862 PyErr_SetString(PyExc_TypeError,
3863 "spawnvp() arg 2 must be a tuple or list");
3864 Py_DECREF(opath);
3865 return NULL;
3866 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003867
Victor Stinner8c62be82010-05-06 00:08:46 +00003868 argvlist = PyMem_NEW(char *, argc+1);
3869 if (argvlist == NULL) {
3870 Py_DECREF(opath);
3871 return PyErr_NoMemory();
3872 }
3873 for (i = 0; i < argc; i++) {
3874 if (!fsconvert_strdup((*getitem)(argv, i),
3875 &argvlist[i])) {
3876 free_string_array(argvlist, i);
3877 PyErr_SetString(
3878 PyExc_TypeError,
3879 "spawnvp() arg 2 must contain only strings");
3880 Py_DECREF(opath);
3881 return NULL;
3882 }
3883 }
3884 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003885
Victor Stinner8c62be82010-05-06 00:08:46 +00003886 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003887#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003888 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003889#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003890 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003891#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003892 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003893
Victor Stinner8c62be82010-05-06 00:08:46 +00003894 free_string_array(argvlist, argc);
3895 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003896
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 if (spawnval == -1)
3898 return posix_error();
3899 else
3900 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003901}
3902
3903
3904PyDoc_STRVAR(posix_spawnvpe__doc__,
3905"spawnvpe(mode, file, args, env)\n\n\
3906Execute the program 'file' in a new process, using the environment\n\
3907search path to find the file.\n\
3908\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003909 mode: mode of process creation\n\
3910 file: executable file name\n\
3911 args: tuple or list of arguments\n\
3912 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003913
3914static PyObject *
3915posix_spawnvpe(PyObject *self, PyObject *args)
3916{
Victor Stinner8c62be82010-05-06 00:08:46 +00003917 PyObject *opath
3918 char *path;
3919 PyObject *argv, *env;
3920 char **argvlist;
3921 char **envlist;
3922 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003923 int mode;
3924 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003925 Py_intptr_t spawnval;
3926 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3927 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003928
Victor Stinner8c62be82010-05-06 00:08:46 +00003929 /* spawnvpe has four arguments: (mode, path, argv, env), where
3930 argv is a list or tuple of strings and env is a dictionary
3931 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003932
Victor Stinner8c62be82010-05-06 00:08:46 +00003933 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3934 PyUnicode_FSConverter,
3935 &opath, &argv, &env))
3936 return NULL;
3937 path = PyBytes_AsString(opath);
3938 if (PyList_Check(argv)) {
3939 argc = PyList_Size(argv);
3940 getitem = PyList_GetItem;
3941 }
3942 else if (PyTuple_Check(argv)) {
3943 argc = PyTuple_Size(argv);
3944 getitem = PyTuple_GetItem;
3945 }
3946 else {
3947 PyErr_SetString(PyExc_TypeError,
3948 "spawnvpe() arg 2 must be a tuple or list");
3949 goto fail_0;
3950 }
3951 if (!PyMapping_Check(env)) {
3952 PyErr_SetString(PyExc_TypeError,
3953 "spawnvpe() arg 3 must be a mapping object");
3954 goto fail_0;
3955 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003956
Victor Stinner8c62be82010-05-06 00:08:46 +00003957 argvlist = PyMem_NEW(char *, argc+1);
3958 if (argvlist == NULL) {
3959 PyErr_NoMemory();
3960 goto fail_0;
3961 }
3962 for (i = 0; i < argc; i++) {
3963 if (!fsconvert_strdup((*getitem)(argv, i),
3964 &argvlist[i]))
3965 {
3966 lastarg = i;
3967 goto fail_1;
3968 }
3969 }
3970 lastarg = argc;
3971 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003972
Victor Stinner8c62be82010-05-06 00:08:46 +00003973 envlist = parse_envlist(env, &envc);
3974 if (envlist == NULL)
3975 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003976
Victor Stinner8c62be82010-05-06 00:08:46 +00003977 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003978#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003979 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003980#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003981 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003982#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003983 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003984
Victor Stinner8c62be82010-05-06 00:08:46 +00003985 if (spawnval == -1)
3986 (void) posix_error();
3987 else
3988 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003989
Victor Stinner8c62be82010-05-06 00:08:46 +00003990 while (--envc >= 0)
3991 PyMem_DEL(envlist[envc]);
3992 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003993 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003994 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003995 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003996 Py_DECREF(opath);
3997 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003998}
3999#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004000#endif /* HAVE_SPAWNV */
4001
4002
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004003#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004004PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004005"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004006Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4007\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004008Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004009
4010static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004011posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004012{
Victor Stinner8c62be82010-05-06 00:08:46 +00004013 pid_t pid;
4014 int result = 0;
4015 _PyImport_AcquireLock();
4016 pid = fork1();
4017 if (pid == 0) {
4018 /* child: this clobbers and resets the import lock. */
4019 PyOS_AfterFork();
4020 } else {
4021 /* parent: release the import lock. */
4022 result = _PyImport_ReleaseLock();
4023 }
4024 if (pid == -1)
4025 return posix_error();
4026 if (result < 0) {
4027 /* Don't clobber the OSError if the fork failed. */
4028 PyErr_SetString(PyExc_RuntimeError,
4029 "not holding the import lock");
4030 return NULL;
4031 }
4032 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004033}
4034#endif
4035
4036
Guido van Rossumad0ee831995-03-01 10:34:45 +00004037#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004038PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004039"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004040Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004041Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004042
Barry Warsaw53699e91996-12-10 23:23:01 +00004043static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004044posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004045{
Victor Stinner8c62be82010-05-06 00:08:46 +00004046 pid_t pid;
4047 int result = 0;
4048 _PyImport_AcquireLock();
4049 pid = fork();
4050 if (pid == 0) {
4051 /* child: this clobbers and resets the import lock. */
4052 PyOS_AfterFork();
4053 } else {
4054 /* parent: release the import lock. */
4055 result = _PyImport_ReleaseLock();
4056 }
4057 if (pid == -1)
4058 return posix_error();
4059 if (result < 0) {
4060 /* Don't clobber the OSError if the fork failed. */
4061 PyErr_SetString(PyExc_RuntimeError,
4062 "not holding the import lock");
4063 return NULL;
4064 }
4065 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004066}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004067#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004068
Neal Norwitzb59798b2003-03-21 01:43:31 +00004069/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004070/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4071#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004072#define DEV_PTY_FILE "/dev/ptc"
4073#define HAVE_DEV_PTMX
4074#else
4075#define DEV_PTY_FILE "/dev/ptmx"
4076#endif
4077
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004078#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004079#ifdef HAVE_PTY_H
4080#include <pty.h>
4081#else
4082#ifdef HAVE_LIBUTIL_H
4083#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004084#else
4085#ifdef HAVE_UTIL_H
4086#include <util.h>
4087#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004088#endif /* HAVE_LIBUTIL_H */
4089#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004090#ifdef HAVE_STROPTS_H
4091#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004092#endif
4093#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004094
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004095#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004096PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004097"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004098Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004099
4100static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004101posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004102{
Victor Stinner8c62be82010-05-06 00:08:46 +00004103 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004104#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004105 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004106#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004107#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004108 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004109#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004110 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004111#endif
4112#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004113
Thomas Wouters70c21a12000-07-14 14:28:33 +00004114#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004115 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4116 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004117#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004118 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4119 if (slave_name == NULL)
4120 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004121
Victor Stinner8c62be82010-05-06 00:08:46 +00004122 slave_fd = open(slave_name, O_RDWR);
4123 if (slave_fd < 0)
4124 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004125#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004126 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4127 if (master_fd < 0)
4128 return posix_error();
4129 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4130 /* change permission of slave */
4131 if (grantpt(master_fd) < 0) {
4132 PyOS_setsig(SIGCHLD, sig_saved);
4133 return posix_error();
4134 }
4135 /* unlock slave */
4136 if (unlockpt(master_fd) < 0) {
4137 PyOS_setsig(SIGCHLD, sig_saved);
4138 return posix_error();
4139 }
4140 PyOS_setsig(SIGCHLD, sig_saved);
4141 slave_name = ptsname(master_fd); /* get name of slave */
4142 if (slave_name == NULL)
4143 return posix_error();
4144 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4145 if (slave_fd < 0)
4146 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004147#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004148 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4149 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004150#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004151 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004152#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004153#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004154#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004155
Victor Stinner8c62be82010-05-06 00:08:46 +00004156 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004157
Fred Drake8cef4cf2000-06-28 16:40:38 +00004158}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004159#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004160
4161#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004162PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004163"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004164Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4165Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004166To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004167
4168static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004169posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004170{
Victor Stinner8c62be82010-05-06 00:08:46 +00004171 int master_fd = -1, result = 0;
4172 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004173
Victor Stinner8c62be82010-05-06 00:08:46 +00004174 _PyImport_AcquireLock();
4175 pid = forkpty(&master_fd, NULL, NULL, NULL);
4176 if (pid == 0) {
4177 /* child: this clobbers and resets the import lock. */
4178 PyOS_AfterFork();
4179 } else {
4180 /* parent: release the import lock. */
4181 result = _PyImport_ReleaseLock();
4182 }
4183 if (pid == -1)
4184 return posix_error();
4185 if (result < 0) {
4186 /* Don't clobber the OSError if the fork failed. */
4187 PyErr_SetString(PyExc_RuntimeError,
4188 "not holding the import lock");
4189 return NULL;
4190 }
4191 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004192}
4193#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004194
Guido van Rossumad0ee831995-03-01 10:34:45 +00004195#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004196PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004197"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004198Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004199
Barry Warsaw53699e91996-12-10 23:23:01 +00004200static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004201posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004202{
Victor Stinner8c62be82010-05-06 00:08:46 +00004203 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004204}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004205#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004206
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004207
Guido van Rossumad0ee831995-03-01 10:34:45 +00004208#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004209PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004210"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004211Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004212
Barry Warsaw53699e91996-12-10 23:23:01 +00004213static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004214posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004215{
Victor Stinner8c62be82010-05-06 00:08:46 +00004216 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004217}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004218#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004219
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004220
Guido van Rossumad0ee831995-03-01 10:34:45 +00004221#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004222PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004223"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004224Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004225
Barry Warsaw53699e91996-12-10 23:23:01 +00004226static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004227posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004228{
Victor Stinner8c62be82010-05-06 00:08:46 +00004229 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004230}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004231#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004232
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004234PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004235"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004236Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004237
Barry Warsaw53699e91996-12-10 23:23:01 +00004238static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004239posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004240{
Victor Stinner8c62be82010-05-06 00:08:46 +00004241 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004242}
4243
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004244
Fred Drakec9680921999-12-13 16:37:25 +00004245#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004246PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004247"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004248Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004249
4250static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004251posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004252{
4253 PyObject *result = NULL;
4254
Fred Drakec9680921999-12-13 16:37:25 +00004255#ifdef NGROUPS_MAX
4256#define MAX_GROUPS NGROUPS_MAX
4257#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004258 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004259#define MAX_GROUPS 64
4260#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004261 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004262
4263 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4264 * This is a helper variable to store the intermediate result when
4265 * that happens.
4266 *
4267 * To keep the code readable the OSX behaviour is unconditional,
4268 * according to the POSIX spec this should be safe on all unix-y
4269 * systems.
4270 */
4271 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004272 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004273
Victor Stinner8c62be82010-05-06 00:08:46 +00004274 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004275 if (n < 0) {
4276 if (errno == EINVAL) {
4277 n = getgroups(0, NULL);
4278 if (n == -1) {
4279 return posix_error();
4280 }
4281 if (n == 0) {
4282 /* Avoid malloc(0) */
4283 alt_grouplist = grouplist;
4284 } else {
4285 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4286 if (alt_grouplist == NULL) {
4287 errno = EINVAL;
4288 return posix_error();
4289 }
4290 n = getgroups(n, alt_grouplist);
4291 if (n == -1) {
4292 PyMem_Free(alt_grouplist);
4293 return posix_error();
4294 }
4295 }
4296 } else {
4297 return posix_error();
4298 }
4299 }
4300 result = PyList_New(n);
4301 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 int i;
4303 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004304 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004305 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004306 Py_DECREF(result);
4307 result = NULL;
4308 break;
Fred Drakec9680921999-12-13 16:37:25 +00004309 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004310 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004311 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004312 }
4313
4314 if (alt_grouplist != grouplist) {
4315 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004316 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004317
Fred Drakec9680921999-12-13 16:37:25 +00004318 return result;
4319}
4320#endif
4321
Antoine Pitroub7572f02009-12-02 20:46:48 +00004322#ifdef HAVE_INITGROUPS
4323PyDoc_STRVAR(posix_initgroups__doc__,
4324"initgroups(username, gid) -> None\n\n\
4325Call the system initgroups() to initialize the group access list with all of\n\
4326the groups of which the specified username is a member, plus the specified\n\
4327group id.");
4328
4329static PyObject *
4330posix_initgroups(PyObject *self, PyObject *args)
4331{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004332 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004333 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004334 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004335 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004336
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004337 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4338 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004339 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004340 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004341
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004342 res = initgroups(username, (gid_t) gid);
4343 Py_DECREF(oname);
4344 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004345 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004346
Victor Stinner8c62be82010-05-06 00:08:46 +00004347 Py_INCREF(Py_None);
4348 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004349}
4350#endif
4351
Martin v. Löwis606edc12002-06-13 21:09:11 +00004352#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004353PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004354"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004355Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004356
4357static PyObject *
4358posix_getpgid(PyObject *self, PyObject *args)
4359{
Victor Stinner8c62be82010-05-06 00:08:46 +00004360 pid_t pid, pgid;
4361 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4362 return NULL;
4363 pgid = getpgid(pid);
4364 if (pgid < 0)
4365 return posix_error();
4366 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004367}
4368#endif /* HAVE_GETPGID */
4369
4370
Guido van Rossumb6775db1994-08-01 11:34:53 +00004371#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004372PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004373"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004374Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004375
Barry Warsaw53699e91996-12-10 23:23:01 +00004376static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004377posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004378{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004379#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004381#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004382 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004383#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004384}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004385#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004386
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004387
Guido van Rossumb6775db1994-08-01 11:34:53 +00004388#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004389PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004390"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004391Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004392
Barry Warsaw53699e91996-12-10 23:23:01 +00004393static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004394posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004395{
Guido van Rossum64933891994-10-20 21:56:42 +00004396#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004397 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004398#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004399 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004400#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004401 return posix_error();
4402 Py_INCREF(Py_None);
4403 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004404}
4405
Guido van Rossumb6775db1994-08-01 11:34:53 +00004406#endif /* HAVE_SETPGRP */
4407
Guido van Rossumad0ee831995-03-01 10:34:45 +00004408#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004409
4410#ifdef MS_WINDOWS
4411#include <tlhelp32.h>
4412
4413static PyObject*
4414win32_getppid()
4415{
4416 HANDLE snapshot;
4417 pid_t mypid;
4418 PyObject* result = NULL;
4419 BOOL have_record;
4420 PROCESSENTRY32 pe;
4421
4422 mypid = getpid(); /* This function never fails */
4423
4424 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4425 if (snapshot == INVALID_HANDLE_VALUE)
4426 return PyErr_SetFromWindowsErr(GetLastError());
4427
4428 pe.dwSize = sizeof(pe);
4429 have_record = Process32First(snapshot, &pe);
4430 while (have_record) {
4431 if (mypid == (pid_t)pe.th32ProcessID) {
4432 /* We could cache the ulong value in a static variable. */
4433 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4434 break;
4435 }
4436
4437 have_record = Process32Next(snapshot, &pe);
4438 }
4439
4440 /* If our loop exits and our pid was not found (result will be NULL)
4441 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4442 * error anyway, so let's raise it. */
4443 if (!result)
4444 result = PyErr_SetFromWindowsErr(GetLastError());
4445
4446 CloseHandle(snapshot);
4447
4448 return result;
4449}
4450#endif /*MS_WINDOWS*/
4451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004452PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004453"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004454Return the parent's process id. If the parent process has already exited,\n\
4455Windows machines will still return its id; others systems will return the id\n\
4456of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004457
Barry Warsaw53699e91996-12-10 23:23:01 +00004458static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004459posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004460{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004461#ifdef MS_WINDOWS
4462 return win32_getppid();
4463#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004465#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004466}
4467#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004468
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004469
Fred Drake12c6e2d1999-12-14 21:25:03 +00004470#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004471PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004472"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004473Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004474
4475static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004476posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004477{
Victor Stinner8c62be82010-05-06 00:08:46 +00004478 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004479#ifdef MS_WINDOWS
4480 wchar_t user_name[UNLEN + 1];
4481 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4482
4483 if (GetUserNameW(user_name, &num_chars)) {
4484 /* num_chars is the number of unicode chars plus null terminator */
4485 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4486 }
4487 else
4488 result = PyErr_SetFromWindowsErr(GetLastError());
4489#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004490 char *name;
4491 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004492
Victor Stinner8c62be82010-05-06 00:08:46 +00004493 errno = 0;
4494 name = getlogin();
4495 if (name == NULL) {
4496 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004497 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004498 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004499 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004500 }
4501 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004502 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004503 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004504#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004505 return result;
4506}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004507#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004508
Guido van Rossumad0ee831995-03-01 10:34:45 +00004509#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004510PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004511"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004512Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004513
Barry Warsaw53699e91996-12-10 23:23:01 +00004514static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004515posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004516{
Victor Stinner8c62be82010-05-06 00:08:46 +00004517 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004518}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004519#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004520
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004521
Guido van Rossumad0ee831995-03-01 10:34:45 +00004522#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004523PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004524"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004525Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004526
Barry Warsaw53699e91996-12-10 23:23:01 +00004527static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004528posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004529{
Victor Stinner8c62be82010-05-06 00:08:46 +00004530 pid_t pid;
4531 int sig;
4532 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4533 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004534#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004535 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4536 APIRET rc;
4537 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004538 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004539
4540 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4541 APIRET rc;
4542 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004543 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004544
4545 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004546 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004547#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004548 if (kill(pid, sig) == -1)
4549 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004550#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004551 Py_INCREF(Py_None);
4552 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004553}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004554#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004555
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004556#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004557PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004558"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004559Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004560
4561static PyObject *
4562posix_killpg(PyObject *self, PyObject *args)
4563{
Victor Stinner8c62be82010-05-06 00:08:46 +00004564 int sig;
4565 pid_t pgid;
4566 /* XXX some man pages make the `pgid` parameter an int, others
4567 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4568 take the same type. Moreover, pid_t is always at least as wide as
4569 int (else compilation of this module fails), which is safe. */
4570 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4571 return NULL;
4572 if (killpg(pgid, sig) == -1)
4573 return posix_error();
4574 Py_INCREF(Py_None);
4575 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004576}
4577#endif
4578
Brian Curtineb24d742010-04-12 17:16:38 +00004579#ifdef MS_WINDOWS
4580PyDoc_STRVAR(win32_kill__doc__,
4581"kill(pid, sig)\n\n\
4582Kill a process with a signal.");
4583
4584static PyObject *
4585win32_kill(PyObject *self, PyObject *args)
4586{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004587 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004588 DWORD pid, sig, err;
4589 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004590
Victor Stinner8c62be82010-05-06 00:08:46 +00004591 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4592 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004593
Victor Stinner8c62be82010-05-06 00:08:46 +00004594 /* Console processes which share a common console can be sent CTRL+C or
4595 CTRL+BREAK events, provided they handle said events. */
4596 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4597 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4598 err = GetLastError();
4599 PyErr_SetFromWindowsErr(err);
4600 }
4601 else
4602 Py_RETURN_NONE;
4603 }
Brian Curtineb24d742010-04-12 17:16:38 +00004604
Victor Stinner8c62be82010-05-06 00:08:46 +00004605 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4606 attempt to open and terminate the process. */
4607 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4608 if (handle == NULL) {
4609 err = GetLastError();
4610 return PyErr_SetFromWindowsErr(err);
4611 }
Brian Curtineb24d742010-04-12 17:16:38 +00004612
Victor Stinner8c62be82010-05-06 00:08:46 +00004613 if (TerminateProcess(handle, sig) == 0) {
4614 err = GetLastError();
4615 result = PyErr_SetFromWindowsErr(err);
4616 } else {
4617 Py_INCREF(Py_None);
4618 result = Py_None;
4619 }
Brian Curtineb24d742010-04-12 17:16:38 +00004620
Victor Stinner8c62be82010-05-06 00:08:46 +00004621 CloseHandle(handle);
4622 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004623}
4624#endif /* MS_WINDOWS */
4625
Guido van Rossumc0125471996-06-28 18:55:32 +00004626#ifdef HAVE_PLOCK
4627
4628#ifdef HAVE_SYS_LOCK_H
4629#include <sys/lock.h>
4630#endif
4631
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004632PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004633"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004634Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004635
Barry Warsaw53699e91996-12-10 23:23:01 +00004636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004637posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004638{
Victor Stinner8c62be82010-05-06 00:08:46 +00004639 int op;
4640 if (!PyArg_ParseTuple(args, "i:plock", &op))
4641 return NULL;
4642 if (plock(op) == -1)
4643 return posix_error();
4644 Py_INCREF(Py_None);
4645 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004646}
4647#endif
4648
Guido van Rossumb6775db1994-08-01 11:34:53 +00004649#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004650PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004651"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004652Set the current process's user id.");
4653
Barry Warsaw53699e91996-12-10 23:23:01 +00004654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004655posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004656{
Victor Stinner8c62be82010-05-06 00:08:46 +00004657 long uid_arg;
4658 uid_t uid;
4659 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4660 return NULL;
4661 uid = uid_arg;
4662 if (uid != uid_arg) {
4663 PyErr_SetString(PyExc_OverflowError, "user id too big");
4664 return NULL;
4665 }
4666 if (setuid(uid) < 0)
4667 return posix_error();
4668 Py_INCREF(Py_None);
4669 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004670}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004671#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004672
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004673
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004674#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004675PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004676"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004677Set the current process's effective user id.");
4678
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004679static PyObject *
4680posix_seteuid (PyObject *self, PyObject *args)
4681{
Victor Stinner8c62be82010-05-06 00:08:46 +00004682 long euid_arg;
4683 uid_t euid;
4684 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4685 return NULL;
4686 euid = euid_arg;
4687 if (euid != euid_arg) {
4688 PyErr_SetString(PyExc_OverflowError, "user id too big");
4689 return NULL;
4690 }
4691 if (seteuid(euid) < 0) {
4692 return posix_error();
4693 } else {
4694 Py_INCREF(Py_None);
4695 return Py_None;
4696 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004697}
4698#endif /* HAVE_SETEUID */
4699
4700#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004701PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004702"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004703Set the current process's effective group id.");
4704
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004705static PyObject *
4706posix_setegid (PyObject *self, PyObject *args)
4707{
Victor Stinner8c62be82010-05-06 00:08:46 +00004708 long egid_arg;
4709 gid_t egid;
4710 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4711 return NULL;
4712 egid = egid_arg;
4713 if (egid != egid_arg) {
4714 PyErr_SetString(PyExc_OverflowError, "group id too big");
4715 return NULL;
4716 }
4717 if (setegid(egid) < 0) {
4718 return posix_error();
4719 } else {
4720 Py_INCREF(Py_None);
4721 return Py_None;
4722 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004723}
4724#endif /* HAVE_SETEGID */
4725
4726#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004727PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004728"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004729Set the current process's real and effective user ids.");
4730
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004731static PyObject *
4732posix_setreuid (PyObject *self, PyObject *args)
4733{
Victor Stinner8c62be82010-05-06 00:08:46 +00004734 long ruid_arg, euid_arg;
4735 uid_t ruid, euid;
4736 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4737 return NULL;
4738 if (ruid_arg == -1)
4739 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4740 else
4741 ruid = ruid_arg; /* otherwise, assign from our long */
4742 if (euid_arg == -1)
4743 euid = (uid_t)-1;
4744 else
4745 euid = euid_arg;
4746 if ((euid_arg != -1 && euid != euid_arg) ||
4747 (ruid_arg != -1 && ruid != ruid_arg)) {
4748 PyErr_SetString(PyExc_OverflowError, "user id too big");
4749 return NULL;
4750 }
4751 if (setreuid(ruid, euid) < 0) {
4752 return posix_error();
4753 } else {
4754 Py_INCREF(Py_None);
4755 return Py_None;
4756 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004757}
4758#endif /* HAVE_SETREUID */
4759
4760#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004761PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004762"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004763Set the current process's real and effective group ids.");
4764
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004765static PyObject *
4766posix_setregid (PyObject *self, PyObject *args)
4767{
Victor Stinner8c62be82010-05-06 00:08:46 +00004768 long rgid_arg, egid_arg;
4769 gid_t rgid, egid;
4770 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4771 return NULL;
4772 if (rgid_arg == -1)
4773 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4774 else
4775 rgid = rgid_arg; /* otherwise, assign from our long */
4776 if (egid_arg == -1)
4777 egid = (gid_t)-1;
4778 else
4779 egid = egid_arg;
4780 if ((egid_arg != -1 && egid != egid_arg) ||
4781 (rgid_arg != -1 && rgid != rgid_arg)) {
4782 PyErr_SetString(PyExc_OverflowError, "group id too big");
4783 return NULL;
4784 }
4785 if (setregid(rgid, egid) < 0) {
4786 return posix_error();
4787 } else {
4788 Py_INCREF(Py_None);
4789 return Py_None;
4790 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004791}
4792#endif /* HAVE_SETREGID */
4793
Guido van Rossumb6775db1994-08-01 11:34:53 +00004794#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004795PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004796"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004797Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004798
Barry Warsaw53699e91996-12-10 23:23:01 +00004799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004800posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004801{
Victor Stinner8c62be82010-05-06 00:08:46 +00004802 long gid_arg;
4803 gid_t gid;
4804 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4805 return NULL;
4806 gid = gid_arg;
4807 if (gid != gid_arg) {
4808 PyErr_SetString(PyExc_OverflowError, "group id too big");
4809 return NULL;
4810 }
4811 if (setgid(gid) < 0)
4812 return posix_error();
4813 Py_INCREF(Py_None);
4814 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004815}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004816#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004817
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004818#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004819PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004820"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004821Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004822
4823static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004824posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004825{
Victor Stinner8c62be82010-05-06 00:08:46 +00004826 int i, len;
4827 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004828
Victor Stinner8c62be82010-05-06 00:08:46 +00004829 if (!PySequence_Check(groups)) {
4830 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4831 return NULL;
4832 }
4833 len = PySequence_Size(groups);
4834 if (len > MAX_GROUPS) {
4835 PyErr_SetString(PyExc_ValueError, "too many groups");
4836 return NULL;
4837 }
4838 for(i = 0; i < len; i++) {
4839 PyObject *elem;
4840 elem = PySequence_GetItem(groups, i);
4841 if (!elem)
4842 return NULL;
4843 if (!PyLong_Check(elem)) {
4844 PyErr_SetString(PyExc_TypeError,
4845 "groups must be integers");
4846 Py_DECREF(elem);
4847 return NULL;
4848 } else {
4849 unsigned long x = PyLong_AsUnsignedLong(elem);
4850 if (PyErr_Occurred()) {
4851 PyErr_SetString(PyExc_TypeError,
4852 "group id too big");
4853 Py_DECREF(elem);
4854 return NULL;
4855 }
4856 grouplist[i] = x;
4857 /* read back the value to see if it fitted in gid_t */
4858 if (grouplist[i] != x) {
4859 PyErr_SetString(PyExc_TypeError,
4860 "group id too big");
4861 Py_DECREF(elem);
4862 return NULL;
4863 }
4864 }
4865 Py_DECREF(elem);
4866 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004867
Victor Stinner8c62be82010-05-06 00:08:46 +00004868 if (setgroups(len, grouplist) < 0)
4869 return posix_error();
4870 Py_INCREF(Py_None);
4871 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004872}
4873#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004874
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004875#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4876static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004877wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004878{
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 PyObject *result;
4880 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004881
Victor Stinner8c62be82010-05-06 00:08:46 +00004882 if (pid == -1)
4883 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004884
Victor Stinner8c62be82010-05-06 00:08:46 +00004885 if (struct_rusage == NULL) {
4886 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4887 if (m == NULL)
4888 return NULL;
4889 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4890 Py_DECREF(m);
4891 if (struct_rusage == NULL)
4892 return NULL;
4893 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004894
Victor Stinner8c62be82010-05-06 00:08:46 +00004895 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4896 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4897 if (!result)
4898 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004899
4900#ifndef doubletime
4901#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4902#endif
4903
Victor Stinner8c62be82010-05-06 00:08:46 +00004904 PyStructSequence_SET_ITEM(result, 0,
4905 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4906 PyStructSequence_SET_ITEM(result, 1,
4907 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004908#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004909 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4910 SET_INT(result, 2, ru->ru_maxrss);
4911 SET_INT(result, 3, ru->ru_ixrss);
4912 SET_INT(result, 4, ru->ru_idrss);
4913 SET_INT(result, 5, ru->ru_isrss);
4914 SET_INT(result, 6, ru->ru_minflt);
4915 SET_INT(result, 7, ru->ru_majflt);
4916 SET_INT(result, 8, ru->ru_nswap);
4917 SET_INT(result, 9, ru->ru_inblock);
4918 SET_INT(result, 10, ru->ru_oublock);
4919 SET_INT(result, 11, ru->ru_msgsnd);
4920 SET_INT(result, 12, ru->ru_msgrcv);
4921 SET_INT(result, 13, ru->ru_nsignals);
4922 SET_INT(result, 14, ru->ru_nvcsw);
4923 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004924#undef SET_INT
4925
Victor Stinner8c62be82010-05-06 00:08:46 +00004926 if (PyErr_Occurred()) {
4927 Py_DECREF(result);
4928 return NULL;
4929 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004930
Victor Stinner8c62be82010-05-06 00:08:46 +00004931 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004932}
4933#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4934
4935#ifdef HAVE_WAIT3
4936PyDoc_STRVAR(posix_wait3__doc__,
4937"wait3(options) -> (pid, status, rusage)\n\n\
4938Wait for completion of a child process.");
4939
4940static PyObject *
4941posix_wait3(PyObject *self, PyObject *args)
4942{
Victor Stinner8c62be82010-05-06 00:08:46 +00004943 pid_t pid;
4944 int options;
4945 struct rusage ru;
4946 WAIT_TYPE status;
4947 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004948
Victor Stinner8c62be82010-05-06 00:08:46 +00004949 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4950 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004951
Victor Stinner8c62be82010-05-06 00:08:46 +00004952 Py_BEGIN_ALLOW_THREADS
4953 pid = wait3(&status, options, &ru);
4954 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004955
Victor Stinner8c62be82010-05-06 00:08:46 +00004956 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004957}
4958#endif /* HAVE_WAIT3 */
4959
4960#ifdef HAVE_WAIT4
4961PyDoc_STRVAR(posix_wait4__doc__,
4962"wait4(pid, options) -> (pid, status, rusage)\n\n\
4963Wait for completion of a given child process.");
4964
4965static PyObject *
4966posix_wait4(PyObject *self, PyObject *args)
4967{
Victor Stinner8c62be82010-05-06 00:08:46 +00004968 pid_t pid;
4969 int options;
4970 struct rusage ru;
4971 WAIT_TYPE status;
4972 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004973
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4975 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004976
Victor Stinner8c62be82010-05-06 00:08:46 +00004977 Py_BEGIN_ALLOW_THREADS
4978 pid = wait4(pid, &status, options, &ru);
4979 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004980
Victor Stinner8c62be82010-05-06 00:08:46 +00004981 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004982}
4983#endif /* HAVE_WAIT4 */
4984
Guido van Rossumb6775db1994-08-01 11:34:53 +00004985#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004986PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004987"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004988Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004989
Barry Warsaw53699e91996-12-10 23:23:01 +00004990static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004991posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004992{
Victor Stinner8c62be82010-05-06 00:08:46 +00004993 pid_t pid;
4994 int options;
4995 WAIT_TYPE status;
4996 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004997
Victor Stinner8c62be82010-05-06 00:08:46 +00004998 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4999 return NULL;
5000 Py_BEGIN_ALLOW_THREADS
5001 pid = waitpid(pid, &status, options);
5002 Py_END_ALLOW_THREADS
5003 if (pid == -1)
5004 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005005
Victor Stinner8c62be82010-05-06 00:08:46 +00005006 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005007}
5008
Tim Petersab034fa2002-02-01 11:27:43 +00005009#elif defined(HAVE_CWAIT)
5010
5011/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005012PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005013"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005014"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005015
5016static PyObject *
5017posix_waitpid(PyObject *self, PyObject *args)
5018{
Victor Stinner8c62be82010-05-06 00:08:46 +00005019 Py_intptr_t pid;
5020 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005021
Victor Stinner8c62be82010-05-06 00:08:46 +00005022 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5023 return NULL;
5024 Py_BEGIN_ALLOW_THREADS
5025 pid = _cwait(&status, pid, options);
5026 Py_END_ALLOW_THREADS
5027 if (pid == -1)
5028 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005029
Victor Stinner8c62be82010-05-06 00:08:46 +00005030 /* shift the status left a byte so this is more like the POSIX waitpid */
5031 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005032}
5033#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005034
Guido van Rossumad0ee831995-03-01 10:34:45 +00005035#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005036PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005037"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005038Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005039
Barry Warsaw53699e91996-12-10 23:23:01 +00005040static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005041posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005042{
Victor Stinner8c62be82010-05-06 00:08:46 +00005043 pid_t pid;
5044 WAIT_TYPE status;
5045 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005046
Victor Stinner8c62be82010-05-06 00:08:46 +00005047 Py_BEGIN_ALLOW_THREADS
5048 pid = wait(&status);
5049 Py_END_ALLOW_THREADS
5050 if (pid == -1)
5051 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005052
Victor Stinner8c62be82010-05-06 00:08:46 +00005053 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005054}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005055#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005056
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005058PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005059"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005060Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005061
Barry Warsaw53699e91996-12-10 23:23:01 +00005062static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005063posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005064{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005065#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005066 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005067#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005068#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005069 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5070 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005071#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005072 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005073#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005074#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005075}
5076
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005077
Guido van Rossumb6775db1994-08-01 11:34:53 +00005078#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005079PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005080"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005081Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005082
Barry Warsaw53699e91996-12-10 23:23:01 +00005083static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005084posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005085{
Victor Stinner8c62be82010-05-06 00:08:46 +00005086 PyObject* v;
5087 char buf[MAXPATHLEN];
5088 PyObject *opath;
5089 char *path;
5090 int n;
5091 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005092
Victor Stinner8c62be82010-05-06 00:08:46 +00005093 if (!PyArg_ParseTuple(args, "O&:readlink",
5094 PyUnicode_FSConverter, &opath))
5095 return NULL;
5096 path = PyBytes_AsString(opath);
5097 v = PySequence_GetItem(args, 0);
5098 if (v == NULL) {
5099 Py_DECREF(opath);
5100 return NULL;
5101 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005102
Victor Stinner8c62be82010-05-06 00:08:46 +00005103 if (PyUnicode_Check(v)) {
5104 arg_is_unicode = 1;
5105 }
5106 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005107
Victor Stinner8c62be82010-05-06 00:08:46 +00005108 Py_BEGIN_ALLOW_THREADS
5109 n = readlink(path, buf, (int) sizeof buf);
5110 Py_END_ALLOW_THREADS
5111 if (n < 0)
5112 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005113
Victor Stinner8c62be82010-05-06 00:08:46 +00005114 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005115 if (arg_is_unicode)
5116 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5117 else
5118 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005119}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005120#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005121
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005122
Brian Curtin52173d42010-12-02 18:29:18 +00005123#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005124PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005125"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005126Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005127
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005128static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005129posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005130{
Victor Stinner8c62be82010-05-06 00:08:46 +00005131 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005132}
5133#endif /* HAVE_SYMLINK */
5134
Brian Curtind40e6f72010-07-08 21:39:08 +00005135#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5136
5137PyDoc_STRVAR(win_readlink__doc__,
5138"readlink(path) -> path\n\n\
5139Return a string representing the path to which the symbolic link points.");
5140
Brian Curtind40e6f72010-07-08 21:39:08 +00005141/* Windows readlink implementation */
5142static PyObject *
5143win_readlink(PyObject *self, PyObject *args)
5144{
5145 wchar_t *path;
5146 DWORD n_bytes_returned;
5147 DWORD io_result;
5148 PyObject *result;
5149 HANDLE reparse_point_handle;
5150
5151 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5152 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5153 wchar_t *print_name;
5154
5155 if (!PyArg_ParseTuple(args,
5156 "u:readlink",
5157 &path))
5158 return NULL;
5159
5160 /* First get a handle to the reparse point */
5161 Py_BEGIN_ALLOW_THREADS
5162 reparse_point_handle = CreateFileW(
5163 path,
5164 0,
5165 0,
5166 0,
5167 OPEN_EXISTING,
5168 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5169 0);
5170 Py_END_ALLOW_THREADS
5171
5172 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5173 {
5174 return win32_error_unicode("readlink", path);
5175 }
5176
5177 Py_BEGIN_ALLOW_THREADS
5178 /* New call DeviceIoControl to read the reparse point */
5179 io_result = DeviceIoControl(
5180 reparse_point_handle,
5181 FSCTL_GET_REPARSE_POINT,
5182 0, 0, /* in buffer */
5183 target_buffer, sizeof(target_buffer),
5184 &n_bytes_returned,
5185 0 /* we're not using OVERLAPPED_IO */
5186 );
5187 CloseHandle(reparse_point_handle);
5188 Py_END_ALLOW_THREADS
5189
5190 if (io_result==0)
5191 {
5192 return win32_error_unicode("readlink", path);
5193 }
5194
5195 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5196 {
5197 PyErr_SetString(PyExc_ValueError,
5198 "not a symbolic link");
5199 return NULL;
5200 }
Brian Curtin74e45612010-07-09 15:58:59 +00005201 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5202 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5203
5204 result = PyUnicode_FromWideChar(print_name,
5205 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005206 return result;
5207}
5208
5209#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5210
Brian Curtin52173d42010-12-02 18:29:18 +00005211#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005212
5213/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5214static int has_CreateSymbolicLinkW = 0;
5215static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5216static int
5217check_CreateSymbolicLinkW()
5218{
5219 HINSTANCE hKernel32;
5220 /* only recheck */
5221 if (has_CreateSymbolicLinkW)
5222 return has_CreateSymbolicLinkW;
5223 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005224 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5225 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005226 if (Py_CreateSymbolicLinkW)
5227 has_CreateSymbolicLinkW = 1;
5228 return has_CreateSymbolicLinkW;
5229}
5230
5231PyDoc_STRVAR(win_symlink__doc__,
5232"symlink(src, dst, target_is_directory=False)\n\n\
5233Create a symbolic link pointing to src named dst.\n\
5234target_is_directory is required if the target is to be interpreted as\n\
5235a directory.\n\
5236This function requires Windows 6.0 or greater, and raises a\n\
5237NotImplementedError otherwise.");
5238
5239static PyObject *
5240win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5241{
5242 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5243 PyObject *src, *dest;
5244 int target_is_directory = 0;
5245 DWORD res;
5246 WIN32_FILE_ATTRIBUTE_DATA src_info;
5247
5248 if (!check_CreateSymbolicLinkW())
5249 {
5250 /* raise NotImplementedError */
5251 return PyErr_Format(PyExc_NotImplementedError,
5252 "CreateSymbolicLinkW not found");
5253 }
5254 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5255 kwlist, &src, &dest, &target_is_directory))
5256 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005257
5258 if (win32_can_symlink == 0)
5259 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5260
Brian Curtind40e6f72010-07-08 21:39:08 +00005261 if (!convert_to_unicode(&src)) { return NULL; }
5262 if (!convert_to_unicode(&dest)) {
5263 Py_DECREF(src);
5264 return NULL;
5265 }
5266
5267 /* if src is a directory, ensure target_is_directory==1 */
5268 if(
5269 GetFileAttributesExW(
5270 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5271 ))
5272 {
5273 target_is_directory = target_is_directory ||
5274 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5275 }
5276
5277 Py_BEGIN_ALLOW_THREADS
5278 res = Py_CreateSymbolicLinkW(
5279 PyUnicode_AsUnicode(dest),
5280 PyUnicode_AsUnicode(src),
5281 target_is_directory);
5282 Py_END_ALLOW_THREADS
5283 Py_DECREF(src);
5284 Py_DECREF(dest);
5285 if (!res)
5286 {
5287 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5288 }
5289
5290 Py_INCREF(Py_None);
5291 return Py_None;
5292}
Brian Curtin52173d42010-12-02 18:29:18 +00005293#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005294
5295#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005296#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5297static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005298system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005299{
5300 ULONG value = 0;
5301
5302 Py_BEGIN_ALLOW_THREADS
5303 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5304 Py_END_ALLOW_THREADS
5305
5306 return value;
5307}
5308
5309static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005310posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005311{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005312 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005313 return Py_BuildValue("ddddd",
5314 (double)0 /* t.tms_utime / HZ */,
5315 (double)0 /* t.tms_stime / HZ */,
5316 (double)0 /* t.tms_cutime / HZ */,
5317 (double)0 /* t.tms_cstime / HZ */,
5318 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005319}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005320#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005321#define NEED_TICKS_PER_SECOND
5322static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005323static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005324posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005325{
Victor Stinner8c62be82010-05-06 00:08:46 +00005326 struct tms t;
5327 clock_t c;
5328 errno = 0;
5329 c = times(&t);
5330 if (c == (clock_t) -1)
5331 return posix_error();
5332 return Py_BuildValue("ddddd",
5333 (double)t.tms_utime / ticks_per_second,
5334 (double)t.tms_stime / ticks_per_second,
5335 (double)t.tms_cutime / ticks_per_second,
5336 (double)t.tms_cstime / ticks_per_second,
5337 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005338}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005339#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005340#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005341
5342
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005343#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005344#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005345static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005346posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005347{
Victor Stinner8c62be82010-05-06 00:08:46 +00005348 FILETIME create, exit, kernel, user;
5349 HANDLE hProc;
5350 hProc = GetCurrentProcess();
5351 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5352 /* The fields of a FILETIME structure are the hi and lo part
5353 of a 64-bit value expressed in 100 nanosecond units.
5354 1e7 is one second in such units; 1e-7 the inverse.
5355 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5356 */
5357 return Py_BuildValue(
5358 "ddddd",
5359 (double)(user.dwHighDateTime*429.4967296 +
5360 user.dwLowDateTime*1e-7),
5361 (double)(kernel.dwHighDateTime*429.4967296 +
5362 kernel.dwLowDateTime*1e-7),
5363 (double)0,
5364 (double)0,
5365 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005366}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005367#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005368
5369#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005370PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005371"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005372Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005373#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005374
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005375
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005376#ifdef HAVE_GETSID
5377PyDoc_STRVAR(posix_getsid__doc__,
5378"getsid(pid) -> sid\n\n\
5379Call the system call getsid().");
5380
5381static PyObject *
5382posix_getsid(PyObject *self, PyObject *args)
5383{
Victor Stinner8c62be82010-05-06 00:08:46 +00005384 pid_t pid;
5385 int sid;
5386 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5387 return NULL;
5388 sid = getsid(pid);
5389 if (sid < 0)
5390 return posix_error();
5391 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005392}
5393#endif /* HAVE_GETSID */
5394
5395
Guido van Rossumb6775db1994-08-01 11:34:53 +00005396#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005397PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005398"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005399Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005400
Barry Warsaw53699e91996-12-10 23:23:01 +00005401static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005402posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005403{
Victor Stinner8c62be82010-05-06 00:08:46 +00005404 if (setsid() < 0)
5405 return posix_error();
5406 Py_INCREF(Py_None);
5407 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005408}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005409#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005410
Guido van Rossumb6775db1994-08-01 11:34:53 +00005411#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005412PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005413"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005414Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005415
Barry Warsaw53699e91996-12-10 23:23:01 +00005416static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005417posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005418{
Victor Stinner8c62be82010-05-06 00:08:46 +00005419 pid_t pid;
5420 int pgrp;
5421 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5422 return NULL;
5423 if (setpgid(pid, pgrp) < 0)
5424 return posix_error();
5425 Py_INCREF(Py_None);
5426 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005427}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005428#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005429
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005430
Guido van Rossumb6775db1994-08-01 11:34:53 +00005431#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005432PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005433"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005434Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005435
Barry Warsaw53699e91996-12-10 23:23:01 +00005436static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005437posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005438{
Victor Stinner8c62be82010-05-06 00:08:46 +00005439 int fd;
5440 pid_t pgid;
5441 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5442 return NULL;
5443 pgid = tcgetpgrp(fd);
5444 if (pgid < 0)
5445 return posix_error();
5446 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005447}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005448#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005449
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005450
Guido van Rossumb6775db1994-08-01 11:34:53 +00005451#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005452PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005453"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005454Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005455
Barry Warsaw53699e91996-12-10 23:23:01 +00005456static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005457posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005458{
Victor Stinner8c62be82010-05-06 00:08:46 +00005459 int fd;
5460 pid_t pgid;
5461 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5462 return NULL;
5463 if (tcsetpgrp(fd, pgid) < 0)
5464 return posix_error();
5465 Py_INCREF(Py_None);
5466 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005467}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005468#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005469
Guido van Rossum687dd131993-05-17 08:34:16 +00005470/* Functions acting on file descriptors */
5471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005472PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005473"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005474Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005475
Barry Warsaw53699e91996-12-10 23:23:01 +00005476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005477posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005478{
Victor Stinner8c62be82010-05-06 00:08:46 +00005479 PyObject *ofile;
5480 char *file;
5481 int flag;
5482 int mode = 0777;
5483 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005484
5485#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005486 PyUnicodeObject *po;
5487 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5488 Py_BEGIN_ALLOW_THREADS
5489 /* PyUnicode_AS_UNICODE OK without thread
5490 lock as it is a simple dereference. */
5491 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5492 Py_END_ALLOW_THREADS
5493 if (fd < 0)
5494 return posix_error();
5495 return PyLong_FromLong((long)fd);
5496 }
5497 /* Drop the argument parsing error as narrow strings
5498 are also valid. */
5499 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005500#endif
5501
Victor Stinner8c62be82010-05-06 00:08:46 +00005502 if (!PyArg_ParseTuple(args, "O&i|i",
5503 PyUnicode_FSConverter, &ofile,
5504 &flag, &mode))
5505 return NULL;
5506 file = PyBytes_AsString(ofile);
5507 Py_BEGIN_ALLOW_THREADS
5508 fd = open(file, flag, mode);
5509 Py_END_ALLOW_THREADS
5510 if (fd < 0)
5511 return posix_error_with_allocated_filename(ofile);
5512 Py_DECREF(ofile);
5513 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005514}
5515
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005517PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005518"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005519Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005520
Barry Warsaw53699e91996-12-10 23:23:01 +00005521static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005522posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005523{
Victor Stinner8c62be82010-05-06 00:08:46 +00005524 int fd, res;
5525 if (!PyArg_ParseTuple(args, "i:close", &fd))
5526 return NULL;
5527 if (!_PyVerify_fd(fd))
5528 return posix_error();
5529 Py_BEGIN_ALLOW_THREADS
5530 res = close(fd);
5531 Py_END_ALLOW_THREADS
5532 if (res < 0)
5533 return posix_error();
5534 Py_INCREF(Py_None);
5535 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005536}
5537
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005538
Victor Stinner8c62be82010-05-06 00:08:46 +00005539PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005540"closerange(fd_low, fd_high)\n\n\
5541Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5542
5543static PyObject *
5544posix_closerange(PyObject *self, PyObject *args)
5545{
Victor Stinner8c62be82010-05-06 00:08:46 +00005546 int fd_from, fd_to, i;
5547 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5548 return NULL;
5549 Py_BEGIN_ALLOW_THREADS
5550 for (i = fd_from; i < fd_to; i++)
5551 if (_PyVerify_fd(i))
5552 close(i);
5553 Py_END_ALLOW_THREADS
5554 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005555}
5556
5557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005559"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560Return a duplicate of 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_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005564{
Victor Stinner8c62be82010-05-06 00:08:46 +00005565 int fd;
5566 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5567 return NULL;
5568 if (!_PyVerify_fd(fd))
5569 return posix_error();
5570 Py_BEGIN_ALLOW_THREADS
5571 fd = dup(fd);
5572 Py_END_ALLOW_THREADS
5573 if (fd < 0)
5574 return posix_error();
5575 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005576}
5577
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005578
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005579PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005580"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005581Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005582
Barry Warsaw53699e91996-12-10 23:23:01 +00005583static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005584posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005585{
Victor Stinner8c62be82010-05-06 00:08:46 +00005586 int fd, fd2, res;
5587 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5588 return NULL;
5589 if (!_PyVerify_fd_dup2(fd, fd2))
5590 return posix_error();
5591 Py_BEGIN_ALLOW_THREADS
5592 res = dup2(fd, fd2);
5593 Py_END_ALLOW_THREADS
5594 if (res < 0)
5595 return posix_error();
5596 Py_INCREF(Py_None);
5597 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005598}
5599
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005601PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005602"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005603Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005604
Barry Warsaw53699e91996-12-10 23:23:01 +00005605static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005606posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005607{
Victor Stinner8c62be82010-05-06 00:08:46 +00005608 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005609#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005610 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005611#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005612 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005613#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 PyObject *posobj;
5615 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5616 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005617#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005618 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5619 switch (how) {
5620 case 0: how = SEEK_SET; break;
5621 case 1: how = SEEK_CUR; break;
5622 case 2: how = SEEK_END; break;
5623 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005624#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005625
5626#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005627 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005628#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005629 pos = PyLong_Check(posobj) ?
5630 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005631#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 if (PyErr_Occurred())
5633 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005634
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 if (!_PyVerify_fd(fd))
5636 return posix_error();
5637 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005638#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005639 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005640#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005641 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005642#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005643 Py_END_ALLOW_THREADS
5644 if (res < 0)
5645 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005646
5647#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005648 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005649#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005650 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005651#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005652}
5653
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005655PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005656"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005657Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005658
Barry Warsaw53699e91996-12-10 23:23:01 +00005659static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005660posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005661{
Victor Stinner8c62be82010-05-06 00:08:46 +00005662 int fd, size;
5663 Py_ssize_t n;
5664 PyObject *buffer;
5665 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5666 return NULL;
5667 if (size < 0) {
5668 errno = EINVAL;
5669 return posix_error();
5670 }
5671 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5672 if (buffer == NULL)
5673 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005674 if (!_PyVerify_fd(fd)) {
5675 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005676 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005677 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 Py_BEGIN_ALLOW_THREADS
5679 n = read(fd, PyBytes_AS_STRING(buffer), size);
5680 Py_END_ALLOW_THREADS
5681 if (n < 0) {
5682 Py_DECREF(buffer);
5683 return posix_error();
5684 }
5685 if (n != size)
5686 _PyBytes_Resize(&buffer, n);
5687 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005688}
5689
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005690
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005691PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005692"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005693Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005694
Barry Warsaw53699e91996-12-10 23:23:01 +00005695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005696posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005697{
Victor Stinner8c62be82010-05-06 00:08:46 +00005698 Py_buffer pbuf;
5699 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005700 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005701
Victor Stinner8c62be82010-05-06 00:08:46 +00005702 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5703 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005704 if (!_PyVerify_fd(fd)) {
5705 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005706 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005707 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005708 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005709 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005710#if defined(MS_WIN64) || defined(MS_WINDOWS)
5711 if (len > INT_MAX)
5712 len = INT_MAX;
5713 size = write(fd, pbuf.buf, (int)len);
5714#else
Victor Stinner72344792011-01-11 00:04:12 +00005715 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005716#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005717 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005718 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 if (size < 0)
5720 return posix_error();
5721 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005722}
5723
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005725PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005726"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005727Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005728
Barry Warsaw53699e91996-12-10 23:23:01 +00005729static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005730posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005731{
Victor Stinner8c62be82010-05-06 00:08:46 +00005732 int fd;
5733 STRUCT_STAT st;
5734 int res;
5735 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5736 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005737#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005738 /* on OpenVMS we must ensure that all bytes are written to the file */
5739 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005740#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005741 if (!_PyVerify_fd(fd))
5742 return posix_error();
5743 Py_BEGIN_ALLOW_THREADS
5744 res = FSTAT(fd, &st);
5745 Py_END_ALLOW_THREADS
5746 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005747#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005748 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005749#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005750 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005751#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005752 }
Tim Peters5aa91602002-01-30 05:46:57 +00005753
Victor Stinner8c62be82010-05-06 00:08:46 +00005754 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005755}
5756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005757PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005758"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005759Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005760connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005761
5762static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005763posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005764{
Victor Stinner8c62be82010-05-06 00:08:46 +00005765 int fd;
5766 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5767 return NULL;
5768 if (!_PyVerify_fd(fd))
5769 return PyBool_FromLong(0);
5770 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005771}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005772
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005773#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005774PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005775"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005776Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005777
Barry Warsaw53699e91996-12-10 23:23:01 +00005778static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005779posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005780{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005781#if defined(PYOS_OS2)
5782 HFILE read, write;
5783 APIRET rc;
5784
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005786 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005788 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005789 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005790
5791 return Py_BuildValue("(ii)", read, write);
5792#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005793#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005794 int fds[2];
5795 int res;
5796 Py_BEGIN_ALLOW_THREADS
5797 res = pipe(fds);
5798 Py_END_ALLOW_THREADS
5799 if (res != 0)
5800 return posix_error();
5801 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005802#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005803 HANDLE read, write;
5804 int read_fd, write_fd;
5805 BOOL ok;
5806 Py_BEGIN_ALLOW_THREADS
5807 ok = CreatePipe(&read, &write, NULL, 0);
5808 Py_END_ALLOW_THREADS
5809 if (!ok)
5810 return win32_error("CreatePipe", NULL);
5811 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5812 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5813 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005814#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005815#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005816}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005817#endif /* HAVE_PIPE */
5818
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005819
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005820#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005821PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005822"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005823Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005824
Barry Warsaw53699e91996-12-10 23:23:01 +00005825static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005826posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005827{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005828 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005829 char *filename;
5830 int mode = 0666;
5831 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005832 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5833 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005834 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005835 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005836 Py_BEGIN_ALLOW_THREADS
5837 res = mkfifo(filename, mode);
5838 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005839 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005840 if (res < 0)
5841 return posix_error();
5842 Py_INCREF(Py_None);
5843 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005844}
5845#endif
5846
5847
Neal Norwitz11690112002-07-30 01:08:28 +00005848#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005849PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005850"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005851Create a filesystem node (file, device special file or named pipe)\n\
5852named filename. mode specifies both the permissions to use and the\n\
5853type of node to be created, being combined (bitwise OR) with one of\n\
5854S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005855device defines the newly created device special file (probably using\n\
5856os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005857
5858
5859static PyObject *
5860posix_mknod(PyObject *self, PyObject *args)
5861{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005862 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005863 char *filename;
5864 int mode = 0600;
5865 int device = 0;
5866 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005867 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5868 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005869 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005870 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005871 Py_BEGIN_ALLOW_THREADS
5872 res = mknod(filename, mode, device);
5873 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005874 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005875 if (res < 0)
5876 return posix_error();
5877 Py_INCREF(Py_None);
5878 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005879}
5880#endif
5881
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005882#ifdef HAVE_DEVICE_MACROS
5883PyDoc_STRVAR(posix_major__doc__,
5884"major(device) -> major number\n\
5885Extracts a device major number from a raw device number.");
5886
5887static PyObject *
5888posix_major(PyObject *self, PyObject *args)
5889{
Victor Stinner8c62be82010-05-06 00:08:46 +00005890 int device;
5891 if (!PyArg_ParseTuple(args, "i:major", &device))
5892 return NULL;
5893 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005894}
5895
5896PyDoc_STRVAR(posix_minor__doc__,
5897"minor(device) -> minor number\n\
5898Extracts a device minor number from a raw device number.");
5899
5900static PyObject *
5901posix_minor(PyObject *self, PyObject *args)
5902{
Victor Stinner8c62be82010-05-06 00:08:46 +00005903 int device;
5904 if (!PyArg_ParseTuple(args, "i:minor", &device))
5905 return NULL;
5906 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005907}
5908
5909PyDoc_STRVAR(posix_makedev__doc__,
5910"makedev(major, minor) -> device number\n\
5911Composes a raw device number from the major and minor device numbers.");
5912
5913static PyObject *
5914posix_makedev(PyObject *self, PyObject *args)
5915{
Victor Stinner8c62be82010-05-06 00:08:46 +00005916 int major, minor;
5917 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5918 return NULL;
5919 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005920}
5921#endif /* device macros */
5922
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005923
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005924#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005925PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005926"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005928
Barry Warsaw53699e91996-12-10 23:23:01 +00005929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005930posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005931{
Victor Stinner8c62be82010-05-06 00:08:46 +00005932 int fd;
5933 off_t length;
5934 int res;
5935 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005936
Victor Stinner8c62be82010-05-06 00:08:46 +00005937 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5938 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005939
5940#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005941 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005942#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 length = PyLong_Check(lenobj) ?
5944 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005945#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 if (PyErr_Occurred())
5947 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005948
Victor Stinner8c62be82010-05-06 00:08:46 +00005949 Py_BEGIN_ALLOW_THREADS
5950 res = ftruncate(fd, length);
5951 Py_END_ALLOW_THREADS
5952 if (res < 0)
5953 return posix_error();
5954 Py_INCREF(Py_None);
5955 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005956}
5957#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005958
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005959#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005960PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005961"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005962Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005963
Fred Drake762e2061999-08-26 17:23:54 +00005964/* Save putenv() parameters as values here, so we can collect them when they
5965 * get re-set with another call for the same key. */
5966static PyObject *posix_putenv_garbage;
5967
Tim Peters5aa91602002-01-30 05:46:57 +00005968static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005969posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005970{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005971#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005972 wchar_t *s1, *s2;
5973 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005974#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005975 PyObject *os1, *os2;
5976 char *s1, *s2;
5977 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005978#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005979 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005980 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005981
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005982#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005983 if (!PyArg_ParseTuple(args,
5984 "uu:putenv",
5985 &s1, &s2))
5986 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005987#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 if (!PyArg_ParseTuple(args,
5989 "O&O&:putenv",
5990 PyUnicode_FSConverter, &os1,
5991 PyUnicode_FSConverter, &os2))
5992 return NULL;
5993 s1 = PyBytes_AsString(os1);
5994 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005995#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005996
5997#if defined(PYOS_OS2)
5998 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5999 APIRET rc;
6000
Guido van Rossumd48f2521997-12-05 22:19:34 +00006001 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006002 if (rc != NO_ERROR) {
6003 os2_error(rc);
6004 goto error;
6005 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006006
6007 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6008 APIRET rc;
6009
Guido van Rossumd48f2521997-12-05 22:19:34 +00006010 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006011 if (rc != NO_ERROR) {
6012 os2_error(rc);
6013 goto error;
6014 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006015 } else {
6016#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006017 /* XXX This can leak memory -- not easy to fix :-( */
6018 /* len includes space for a trailing \0; the size arg to
6019 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006020#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 len = wcslen(s1) + wcslen(s2) + 2;
6022 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006023#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006024 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006025 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006026#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006027 if (newstr == NULL) {
6028 PyErr_NoMemory();
6029 goto error;
6030 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006031#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006032 newenv = PyUnicode_AsUnicode(newstr);
6033 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6034 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006035 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006036 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006037 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006038#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006039 newenv = PyBytes_AS_STRING(newstr);
6040 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6041 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006042 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006043 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006044 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006045#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006046
Victor Stinner8c62be82010-05-06 00:08:46 +00006047 /* Install the first arg and newstr in posix_putenv_garbage;
6048 * this will cause previous value to be collected. This has to
6049 * happen after the real putenv() call because the old value
6050 * was still accessible until then. */
6051 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006052#ifdef MS_WINDOWS
6053 PyTuple_GET_ITEM(args, 0),
6054#else
6055 os1,
6056#endif
6057 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 /* really not much we can do; just leak */
6059 PyErr_Clear();
6060 }
6061 else {
6062 Py_DECREF(newstr);
6063 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006064
6065#if defined(PYOS_OS2)
6066 }
6067#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006068
Martin v. Löwis011e8422009-05-05 04:43:17 +00006069#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006070 Py_DECREF(os1);
6071 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006072#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006073 Py_RETURN_NONE;
6074
6075error:
6076#ifndef MS_WINDOWS
6077 Py_DECREF(os1);
6078 Py_DECREF(os2);
6079#endif
6080 Py_XDECREF(newstr);
6081 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006082}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006083#endif /* putenv */
6084
Guido van Rossumc524d952001-10-19 01:31:59 +00006085#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006086PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006087"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006088Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006089
6090static PyObject *
6091posix_unsetenv(PyObject *self, PyObject *args)
6092{
Victor Stinner84ae1182010-05-06 22:05:07 +00006093#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006094 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006095
Victor Stinner8c62be82010-05-06 00:08:46 +00006096 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6097 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006098#else
6099 PyObject *os1;
6100 char *s1;
6101
6102 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6103 PyUnicode_FSConverter, &os1))
6104 return NULL;
6105 s1 = PyBytes_AsString(os1);
6106#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006107
Victor Stinner8c62be82010-05-06 00:08:46 +00006108 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006109
Victor Stinner8c62be82010-05-06 00:08:46 +00006110 /* Remove the key from posix_putenv_garbage;
6111 * this will cause it to be collected. This has to
6112 * happen after the real unsetenv() call because the
6113 * old value was still accessible until then.
6114 */
6115 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006116#ifdef MS_WINDOWS
6117 PyTuple_GET_ITEM(args, 0)
6118#else
6119 os1
6120#endif
6121 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006122 /* really not much we can do; just leak */
6123 PyErr_Clear();
6124 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006125
Victor Stinner84ae1182010-05-06 22:05:07 +00006126#ifndef MS_WINDOWS
6127 Py_DECREF(os1);
6128#endif
6129 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006130}
6131#endif /* unsetenv */
6132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006133PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006134"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006136
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006138posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006139{
Victor Stinner8c62be82010-05-06 00:08:46 +00006140 int code;
6141 char *message;
6142 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6143 return NULL;
6144 message = strerror(code);
6145 if (message == NULL) {
6146 PyErr_SetString(PyExc_ValueError,
6147 "strerror() argument out of range");
6148 return NULL;
6149 }
6150 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006151}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006152
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006153
Guido van Rossumc9641791998-08-04 15:26:23 +00006154#ifdef HAVE_SYS_WAIT_H
6155
Fred Drake106c1a02002-04-23 15:58:02 +00006156#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006157PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006158"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006159Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006160
6161static PyObject *
6162posix_WCOREDUMP(PyObject *self, PyObject *args)
6163{
Victor Stinner8c62be82010-05-06 00:08:46 +00006164 WAIT_TYPE status;
6165 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006166
Victor Stinner8c62be82010-05-06 00:08:46 +00006167 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6168 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006169
Victor Stinner8c62be82010-05-06 00:08:46 +00006170 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006171}
6172#endif /* WCOREDUMP */
6173
6174#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006175PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006176"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006177Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006178job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006179
6180static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006181posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006182{
Victor Stinner8c62be82010-05-06 00:08:46 +00006183 WAIT_TYPE status;
6184 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006185
Victor Stinner8c62be82010-05-06 00:08:46 +00006186 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6187 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006188
Victor Stinner8c62be82010-05-06 00:08:46 +00006189 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006190}
6191#endif /* WIFCONTINUED */
6192
Guido van Rossumc9641791998-08-04 15:26:23 +00006193#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006194PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006195"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006196Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006197
6198static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006199posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006200{
Victor Stinner8c62be82010-05-06 00:08:46 +00006201 WAIT_TYPE status;
6202 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006203
Victor Stinner8c62be82010-05-06 00:08:46 +00006204 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6205 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006206
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006208}
6209#endif /* WIFSTOPPED */
6210
6211#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006212PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006213"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006214Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006215
6216static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006217posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006218{
Victor Stinner8c62be82010-05-06 00:08:46 +00006219 WAIT_TYPE status;
6220 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006221
Victor Stinner8c62be82010-05-06 00:08:46 +00006222 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6223 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006224
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006226}
6227#endif /* WIFSIGNALED */
6228
6229#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006230PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006231"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006232Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006233system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006234
6235static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006236posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006237{
Victor Stinner8c62be82010-05-06 00:08:46 +00006238 WAIT_TYPE status;
6239 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006240
Victor Stinner8c62be82010-05-06 00:08:46 +00006241 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6242 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006243
Victor Stinner8c62be82010-05-06 00:08:46 +00006244 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006245}
6246#endif /* WIFEXITED */
6247
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006248#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006249PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006250"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006251Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006252
6253static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006254posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006255{
Victor Stinner8c62be82010-05-06 00:08:46 +00006256 WAIT_TYPE status;
6257 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006258
Victor Stinner8c62be82010-05-06 00:08:46 +00006259 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6260 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006261
Victor Stinner8c62be82010-05-06 00:08:46 +00006262 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006263}
6264#endif /* WEXITSTATUS */
6265
6266#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006267PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006268"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006269Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006270value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006271
6272static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006273posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006274{
Victor Stinner8c62be82010-05-06 00:08:46 +00006275 WAIT_TYPE status;
6276 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006277
Victor Stinner8c62be82010-05-06 00:08:46 +00006278 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6279 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006280
Victor Stinner8c62be82010-05-06 00:08:46 +00006281 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006282}
6283#endif /* WTERMSIG */
6284
6285#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006286PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006287"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006288Return the signal that stopped the process that provided\n\
6289the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006290
6291static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006292posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006293{
Victor Stinner8c62be82010-05-06 00:08:46 +00006294 WAIT_TYPE status;
6295 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006296
Victor Stinner8c62be82010-05-06 00:08:46 +00006297 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6298 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006299
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006301}
6302#endif /* WSTOPSIG */
6303
6304#endif /* HAVE_SYS_WAIT_H */
6305
6306
Thomas Wouters477c8d52006-05-27 19:21:47 +00006307#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006308#ifdef _SCO_DS
6309/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6310 needed definitions in sys/statvfs.h */
6311#define _SVID3
6312#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006313#include <sys/statvfs.h>
6314
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006315static PyObject*
6316_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006317 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6318 if (v == NULL)
6319 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006320
6321#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006322 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6323 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6324 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6325 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6326 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6327 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6328 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6329 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6330 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6331 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006332#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006333 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6334 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6335 PyStructSequence_SET_ITEM(v, 2,
6336 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6337 PyStructSequence_SET_ITEM(v, 3,
6338 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6339 PyStructSequence_SET_ITEM(v, 4,
6340 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6341 PyStructSequence_SET_ITEM(v, 5,
6342 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6343 PyStructSequence_SET_ITEM(v, 6,
6344 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6345 PyStructSequence_SET_ITEM(v, 7,
6346 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6347 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6348 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006349#endif
6350
Victor Stinner8c62be82010-05-06 00:08:46 +00006351 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006352}
6353
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006354PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006355"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006356Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006357
6358static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006359posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006360{
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 int fd, res;
6362 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006363
Victor Stinner8c62be82010-05-06 00:08:46 +00006364 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6365 return NULL;
6366 Py_BEGIN_ALLOW_THREADS
6367 res = fstatvfs(fd, &st);
6368 Py_END_ALLOW_THREADS
6369 if (res != 0)
6370 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006371
Victor Stinner8c62be82010-05-06 00:08:46 +00006372 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006373}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006374#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006375
6376
Thomas Wouters477c8d52006-05-27 19:21:47 +00006377#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006378#include <sys/statvfs.h>
6379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006380PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006381"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006382Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006383
6384static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006385posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006386{
Victor Stinner8c62be82010-05-06 00:08:46 +00006387 char *path;
6388 int res;
6389 struct statvfs st;
6390 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6391 return NULL;
6392 Py_BEGIN_ALLOW_THREADS
6393 res = statvfs(path, &st);
6394 Py_END_ALLOW_THREADS
6395 if (res != 0)
6396 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006397
Victor Stinner8c62be82010-05-06 00:08:46 +00006398 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006399}
6400#endif /* HAVE_STATVFS */
6401
Fred Drakec9680921999-12-13 16:37:25 +00006402/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6403 * It maps strings representing configuration variable names to
6404 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006405 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006406 * rarely-used constants. There are three separate tables that use
6407 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006408 *
6409 * This code is always included, even if none of the interfaces that
6410 * need it are included. The #if hackery needed to avoid it would be
6411 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006412 */
6413struct constdef {
6414 char *name;
6415 long value;
6416};
6417
Fred Drake12c6e2d1999-12-14 21:25:03 +00006418static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006419conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006420 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006421{
Christian Heimes217cfd12007-12-02 14:31:20 +00006422 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006423 *valuep = PyLong_AS_LONG(arg);
6424 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006425 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006426 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006427 /* look up the value in the table using a binary search */
6428 size_t lo = 0;
6429 size_t mid;
6430 size_t hi = tablesize;
6431 int cmp;
6432 const char *confname;
6433 if (!PyUnicode_Check(arg)) {
6434 PyErr_SetString(PyExc_TypeError,
6435 "configuration names must be strings or integers");
6436 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006437 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006438 confname = _PyUnicode_AsString(arg);
6439 if (confname == NULL)
6440 return 0;
6441 while (lo < hi) {
6442 mid = (lo + hi) / 2;
6443 cmp = strcmp(confname, table[mid].name);
6444 if (cmp < 0)
6445 hi = mid;
6446 else if (cmp > 0)
6447 lo = mid + 1;
6448 else {
6449 *valuep = table[mid].value;
6450 return 1;
6451 }
6452 }
6453 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6454 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006455 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006456}
6457
6458
6459#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6460static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006461#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006462 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006463#endif
6464#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006465 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006466#endif
Fred Drakec9680921999-12-13 16:37:25 +00006467#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006468 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006469#endif
6470#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006471 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006472#endif
6473#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006474 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006475#endif
6476#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006477 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006478#endif
6479#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006480 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006481#endif
6482#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006483 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006484#endif
6485#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006486 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006487#endif
6488#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006490#endif
6491#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006492 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006493#endif
6494#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006495 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006496#endif
6497#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006498 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006499#endif
6500#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006501 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006502#endif
6503#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006504 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006505#endif
6506#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006507 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006508#endif
6509#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006510 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006511#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006512#ifdef _PC_ACL_ENABLED
6513 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6514#endif
6515#ifdef _PC_MIN_HOLE_SIZE
6516 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6517#endif
6518#ifdef _PC_ALLOC_SIZE_MIN
6519 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6520#endif
6521#ifdef _PC_REC_INCR_XFER_SIZE
6522 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6523#endif
6524#ifdef _PC_REC_MAX_XFER_SIZE
6525 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6526#endif
6527#ifdef _PC_REC_MIN_XFER_SIZE
6528 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6529#endif
6530#ifdef _PC_REC_XFER_ALIGN
6531 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6532#endif
6533#ifdef _PC_SYMLINK_MAX
6534 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6535#endif
6536#ifdef _PC_XATTR_ENABLED
6537 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6538#endif
6539#ifdef _PC_XATTR_EXISTS
6540 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6541#endif
6542#ifdef _PC_TIMESTAMP_RESOLUTION
6543 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6544#endif
Fred Drakec9680921999-12-13 16:37:25 +00006545};
6546
Fred Drakec9680921999-12-13 16:37:25 +00006547static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006548conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006549{
6550 return conv_confname(arg, valuep, posix_constants_pathconf,
6551 sizeof(posix_constants_pathconf)
6552 / sizeof(struct constdef));
6553}
6554#endif
6555
6556#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006557PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006558"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006559Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006560If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006561
6562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006563posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006564{
6565 PyObject *result = NULL;
6566 int name, fd;
6567
Fred Drake12c6e2d1999-12-14 21:25:03 +00006568 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6569 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006570 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006571
Stefan Krah0e803b32010-11-26 16:16:47 +00006572 errno = 0;
6573 limit = fpathconf(fd, name);
6574 if (limit == -1 && errno != 0)
6575 posix_error();
6576 else
6577 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006578 }
6579 return result;
6580}
6581#endif
6582
6583
6584#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006585PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006586"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006587Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006588If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006589
6590static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006591posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006592{
6593 PyObject *result = NULL;
6594 int name;
6595 char *path;
6596
6597 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6598 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006599 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006600
Victor Stinner8c62be82010-05-06 00:08:46 +00006601 errno = 0;
6602 limit = pathconf(path, name);
6603 if (limit == -1 && errno != 0) {
6604 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006605 /* could be a path or name problem */
6606 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006607 else
Stefan Krah99439262010-11-26 12:58:05 +00006608 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006609 }
6610 else
6611 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006612 }
6613 return result;
6614}
6615#endif
6616
6617#ifdef HAVE_CONFSTR
6618static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006619#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006620 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006621#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006622#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006623 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006624#endif
6625#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006626 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006627#endif
Fred Draked86ed291999-12-15 15:34:33 +00006628#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006629 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006630#endif
6631#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006632 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006633#endif
6634#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006635 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006636#endif
6637#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006638 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006639#endif
Fred Drakec9680921999-12-13 16:37:25 +00006640#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006641 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006642#endif
6643#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006644 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006645#endif
6646#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006647 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006648#endif
6649#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006650 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006651#endif
6652#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006653 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006654#endif
6655#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006656 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006657#endif
6658#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006659 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006660#endif
6661#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006662 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006663#endif
Fred Draked86ed291999-12-15 15:34:33 +00006664#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006665 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006666#endif
Fred Drakec9680921999-12-13 16:37:25 +00006667#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006668 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006669#endif
Fred Draked86ed291999-12-15 15:34:33 +00006670#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006671 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006672#endif
6673#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006675#endif
6676#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006677 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006678#endif
6679#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006680 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006681#endif
Fred Drakec9680921999-12-13 16:37:25 +00006682#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006684#endif
6685#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006686 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006687#endif
6688#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006689 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006690#endif
6691#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006692 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006693#endif
6694#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006695 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006696#endif
6697#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006698 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006699#endif
6700#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006701 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006702#endif
6703#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006704 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006705#endif
6706#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006707 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006708#endif
6709#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006710 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006711#endif
6712#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006713 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006714#endif
6715#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006716 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006717#endif
6718#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006719 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006720#endif
6721#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006722 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006723#endif
6724#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006725 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006726#endif
6727#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006728 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006729#endif
Fred Draked86ed291999-12-15 15:34:33 +00006730#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006731 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006732#endif
6733#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006734 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006735#endif
6736#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006737 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006738#endif
6739#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006740 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006741#endif
6742#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006743 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006744#endif
6745#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006746 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006747#endif
6748#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006749 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006750#endif
6751#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006752 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006753#endif
6754#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006755 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006756#endif
6757#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006758 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006759#endif
6760#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006761 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006762#endif
6763#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006764 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006765#endif
6766#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006767 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006768#endif
Fred Drakec9680921999-12-13 16:37:25 +00006769};
6770
6771static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006772conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006773{
6774 return conv_confname(arg, valuep, posix_constants_confstr,
6775 sizeof(posix_constants_confstr)
6776 / sizeof(struct constdef));
6777}
6778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006779PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006780"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006781Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006782
6783static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006784posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006785{
6786 PyObject *result = NULL;
6787 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006788 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006789 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006790
Victor Stinnercb043522010-09-10 23:49:04 +00006791 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6792 return NULL;
6793
6794 errno = 0;
6795 len = confstr(name, buffer, sizeof(buffer));
6796 if (len == 0) {
6797 if (errno) {
6798 posix_error();
6799 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006800 }
6801 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006802 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006803 }
6804 }
Victor Stinnercb043522010-09-10 23:49:04 +00006805
6806 if ((unsigned int)len >= sizeof(buffer)) {
6807 char *buf = PyMem_Malloc(len);
6808 if (buf == NULL)
6809 return PyErr_NoMemory();
6810 confstr(name, buf, len);
6811 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6812 PyMem_Free(buf);
6813 }
6814 else
6815 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006816 return result;
6817}
6818#endif
6819
6820
6821#ifdef HAVE_SYSCONF
6822static struct constdef posix_constants_sysconf[] = {
6823#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006824 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006825#endif
6826#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006827 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006828#endif
6829#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006830 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006831#endif
6832#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006833 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006834#endif
6835#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006836 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006837#endif
6838#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006839 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006840#endif
6841#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006842 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006843#endif
6844#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006845 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006846#endif
6847#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006848 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006849#endif
6850#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006852#endif
Fred Draked86ed291999-12-15 15:34:33 +00006853#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006854 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006855#endif
6856#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006857 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006858#endif
Fred Drakec9680921999-12-13 16:37:25 +00006859#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006861#endif
Fred Drakec9680921999-12-13 16:37:25 +00006862#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006863 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006864#endif
6865#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006866 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006867#endif
6868#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006869 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006870#endif
6871#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006872 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006873#endif
6874#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006875 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006876#endif
Fred Draked86ed291999-12-15 15:34:33 +00006877#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006878 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006879#endif
Fred Drakec9680921999-12-13 16:37:25 +00006880#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006881 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006882#endif
6883#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006884 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006885#endif
6886#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006887 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006888#endif
6889#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006890 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006891#endif
6892#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006893 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006894#endif
Fred Draked86ed291999-12-15 15:34:33 +00006895#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006896 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006897#endif
Fred Drakec9680921999-12-13 16:37:25 +00006898#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006899 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006900#endif
6901#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006902 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006903#endif
6904#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006905 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006906#endif
6907#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006908 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006909#endif
6910#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006912#endif
6913#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006914 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006915#endif
6916#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006917 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006918#endif
6919#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006921#endif
6922#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006923 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006924#endif
6925#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006926 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006927#endif
6928#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006929 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006930#endif
6931#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006932 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006933#endif
6934#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006935 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006936#endif
6937#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006938 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006939#endif
6940#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006941 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006942#endif
6943#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006944 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006945#endif
6946#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006947 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006948#endif
6949#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006950 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006951#endif
6952#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006953 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006954#endif
6955#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006956 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006957#endif
6958#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006959 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006960#endif
6961#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006962 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006963#endif
6964#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006965 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006966#endif
Fred Draked86ed291999-12-15 15:34:33 +00006967#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006968 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006969#endif
Fred Drakec9680921999-12-13 16:37:25 +00006970#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006971 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006972#endif
6973#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006974 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006975#endif
6976#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006977 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006978#endif
Fred Draked86ed291999-12-15 15:34:33 +00006979#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006980 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006981#endif
Fred Drakec9680921999-12-13 16:37:25 +00006982#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006983 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006984#endif
Fred Draked86ed291999-12-15 15:34:33 +00006985#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006986 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006987#endif
6988#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006989 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006990#endif
Fred Drakec9680921999-12-13 16:37:25 +00006991#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006992 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006993#endif
6994#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006995 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006996#endif
6997#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006998 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006999#endif
7000#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007001 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007002#endif
Fred Draked86ed291999-12-15 15:34:33 +00007003#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007004 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007005#endif
Fred Drakec9680921999-12-13 16:37:25 +00007006#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007007 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007008#endif
7009#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007010 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007011#endif
7012#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007013 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007014#endif
7015#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007016 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007017#endif
7018#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007019 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007020#endif
7021#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007022 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007023#endif
7024#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007025 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007026#endif
Fred Draked86ed291999-12-15 15:34:33 +00007027#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007028 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007029#endif
Fred Drakec9680921999-12-13 16:37:25 +00007030#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007031 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007032#endif
7033#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007034 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007035#endif
Fred Draked86ed291999-12-15 15:34:33 +00007036#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007037 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007038#endif
Fred Drakec9680921999-12-13 16:37:25 +00007039#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007040 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007041#endif
7042#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007043 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007044#endif
7045#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007046 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007047#endif
7048#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007049 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007050#endif
7051#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007052 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007053#endif
7054#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007055 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007056#endif
7057#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007059#endif
7060#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007061 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007062#endif
7063#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007064 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007065#endif
Fred Draked86ed291999-12-15 15:34:33 +00007066#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007067 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007068#endif
7069#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007070 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007071#endif
Fred Drakec9680921999-12-13 16:37:25 +00007072#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007073 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007074#endif
7075#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007076 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007077#endif
7078#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007079 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007080#endif
7081#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007082 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007083#endif
7084#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007085 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007086#endif
7087#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007088 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007089#endif
7090#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007091 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007092#endif
7093#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007094 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007095#endif
7096#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007097 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007098#endif
7099#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007100 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007101#endif
7102#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007103 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007104#endif
7105#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007106 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007107#endif
7108#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007109 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007110#endif
7111#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007112 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007113#endif
7114#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007116#endif
7117#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007118 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007119#endif
7120#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007121 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007122#endif
7123#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007124 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007125#endif
7126#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007127 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007128#endif
7129#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007130 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007131#endif
7132#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007133 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007134#endif
7135#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007136 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007137#endif
7138#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007139 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007140#endif
7141#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007142 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007143#endif
7144#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007145 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007146#endif
7147#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007148 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007149#endif
7150#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007151 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007152#endif
7153#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007154 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007155#endif
7156#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007157 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007158#endif
7159#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007160 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007161#endif
7162#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007163 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007164#endif
7165#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007166 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007167#endif
7168#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007169 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007170#endif
7171#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007172 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007173#endif
7174#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007175 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007176#endif
Fred Draked86ed291999-12-15 15:34:33 +00007177#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007178 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007179#endif
Fred Drakec9680921999-12-13 16:37:25 +00007180#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007181 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007182#endif
7183#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007184 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007185#endif
7186#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007188#endif
7189#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007191#endif
7192#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007193 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007194#endif
7195#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007196 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007197#endif
7198#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007199 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007200#endif
7201#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007202 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007203#endif
7204#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007206#endif
7207#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007208 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007209#endif
7210#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007211 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007212#endif
7213#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007214 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007215#endif
7216#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007217 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007218#endif
7219#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007221#endif
7222#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007223 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007224#endif
7225#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007226 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007227#endif
7228#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007229 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007230#endif
7231#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007232 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007233#endif
7234#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007235 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007236#endif
7237#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007239#endif
7240#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007242#endif
7243#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007244 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007245#endif
7246#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007247 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007248#endif
7249#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007250 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007251#endif
7252#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007253 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007254#endif
7255#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007256 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007257#endif
7258#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007259 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007260#endif
7261#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007262 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007263#endif
7264#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007265 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007266#endif
7267#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007268 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007269#endif
7270#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007272#endif
7273#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007274 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007275#endif
7276#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007277 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007278#endif
7279#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007280 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007281#endif
7282#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007283 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007284#endif
7285#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007286 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007287#endif
7288#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007289 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007290#endif
7291#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007292 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007293#endif
7294#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007295 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007296#endif
7297#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007298 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007299#endif
7300#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007301 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007302#endif
7303#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007304 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007305#endif
7306#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007307 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007308#endif
7309#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007311#endif
7312#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007313 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007314#endif
7315};
7316
7317static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007318conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007319{
7320 return conv_confname(arg, valuep, posix_constants_sysconf,
7321 sizeof(posix_constants_sysconf)
7322 / sizeof(struct constdef));
7323}
7324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007325PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007326"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007327Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007328
7329static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007330posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007331{
7332 PyObject *result = NULL;
7333 int name;
7334
7335 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7336 int value;
7337
7338 errno = 0;
7339 value = sysconf(name);
7340 if (value == -1 && errno != 0)
7341 posix_error();
7342 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007343 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007344 }
7345 return result;
7346}
7347#endif
7348
7349
Fred Drakebec628d1999-12-15 18:31:10 +00007350/* This code is used to ensure that the tables of configuration value names
7351 * are in sorted order as required by conv_confname(), and also to build the
7352 * the exported dictionaries that are used to publish information about the
7353 * names available on the host platform.
7354 *
7355 * Sorting the table at runtime ensures that the table is properly ordered
7356 * when used, even for platforms we're not able to test on. It also makes
7357 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007358 */
Fred Drakebec628d1999-12-15 18:31:10 +00007359
7360static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007361cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007362{
7363 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007364 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007365 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007366 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007367
7368 return strcmp(c1->name, c2->name);
7369}
7370
7371static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007372setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007373 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007374{
Fred Drakebec628d1999-12-15 18:31:10 +00007375 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007376 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007377
7378 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7379 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007380 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007382
Barry Warsaw3155db32000-04-13 15:20:40 +00007383 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007384 PyObject *o = PyLong_FromLong(table[i].value);
7385 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7386 Py_XDECREF(o);
7387 Py_DECREF(d);
7388 return -1;
7389 }
7390 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007391 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007392 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007393}
7394
Fred Drakebec628d1999-12-15 18:31:10 +00007395/* Return -1 on failure, 0 on success. */
7396static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007397setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007398{
7399#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007400 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007401 sizeof(posix_constants_pathconf)
7402 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007403 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007404 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007405#endif
7406#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007407 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007408 sizeof(posix_constants_confstr)
7409 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007410 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007411 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007412#endif
7413#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007414 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007415 sizeof(posix_constants_sysconf)
7416 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007417 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007418 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007419#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007420 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007421}
Fred Draked86ed291999-12-15 15:34:33 +00007422
7423
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007424PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007425"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007426Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007427in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007428
7429static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007430posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007431{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007432 abort();
7433 /*NOTREACHED*/
7434 Py_FatalError("abort() called from Python code didn't abort!");
7435 return NULL;
7436}
Fred Drakebec628d1999-12-15 18:31:10 +00007437
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007438#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007439PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007440"startfile(filepath [, operation]) - Start a file with its associated\n\
7441application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007442\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007443When \"operation\" is not specified or \"open\", this acts like\n\
7444double-clicking the file in Explorer, or giving the file name as an\n\
7445argument to the DOS \"start\" command: the file is opened with whatever\n\
7446application (if any) its extension is associated.\n\
7447When another \"operation\" is given, it specifies what should be done with\n\
7448the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007449\n\
7450startfile returns as soon as the associated application is launched.\n\
7451There is no option to wait for the application to close, and no way\n\
7452to retrieve the application's exit status.\n\
7453\n\
7454The filepath is relative to the current directory. If you want to use\n\
7455an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007456the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007457
7458static PyObject *
7459win32_startfile(PyObject *self, PyObject *args)
7460{
Victor Stinner8c62be82010-05-06 00:08:46 +00007461 PyObject *ofilepath;
7462 char *filepath;
7463 char *operation = NULL;
7464 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007465
Victor Stinner8c62be82010-05-06 00:08:46 +00007466 PyObject *unipath, *woperation = NULL;
7467 if (!PyArg_ParseTuple(args, "U|s:startfile",
7468 &unipath, &operation)) {
7469 PyErr_Clear();
7470 goto normal;
7471 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007472
Victor Stinner8c62be82010-05-06 00:08:46 +00007473 if (operation) {
7474 woperation = PyUnicode_DecodeASCII(operation,
7475 strlen(operation), NULL);
7476 if (!woperation) {
7477 PyErr_Clear();
7478 operation = NULL;
7479 goto normal;
7480 }
7481 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007482
Victor Stinner8c62be82010-05-06 00:08:46 +00007483 Py_BEGIN_ALLOW_THREADS
7484 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7485 PyUnicode_AS_UNICODE(unipath),
7486 NULL, NULL, SW_SHOWNORMAL);
7487 Py_END_ALLOW_THREADS
7488
7489 Py_XDECREF(woperation);
7490 if (rc <= (HINSTANCE)32) {
7491 PyObject *errval = win32_error_unicode("startfile",
7492 PyUnicode_AS_UNICODE(unipath));
7493 return errval;
7494 }
7495 Py_INCREF(Py_None);
7496 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007497
7498normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007499 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7500 PyUnicode_FSConverter, &ofilepath,
7501 &operation))
7502 return NULL;
7503 filepath = PyBytes_AsString(ofilepath);
7504 Py_BEGIN_ALLOW_THREADS
7505 rc = ShellExecute((HWND)0, operation, filepath,
7506 NULL, NULL, SW_SHOWNORMAL);
7507 Py_END_ALLOW_THREADS
7508 if (rc <= (HINSTANCE)32) {
7509 PyObject *errval = win32_error("startfile", filepath);
7510 Py_DECREF(ofilepath);
7511 return errval;
7512 }
7513 Py_DECREF(ofilepath);
7514 Py_INCREF(Py_None);
7515 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007516}
7517#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007518
Martin v. Löwis438b5342002-12-27 10:16:42 +00007519#ifdef HAVE_GETLOADAVG
7520PyDoc_STRVAR(posix_getloadavg__doc__,
7521"getloadavg() -> (float, float, float)\n\n\
7522Return the number of processes in the system run queue averaged over\n\
7523the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7524was unobtainable");
7525
7526static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007527posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007528{
7529 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007530 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007531 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7532 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007533 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007534 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007535}
7536#endif
7537
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007538#ifdef MS_WINDOWS
7539
7540PyDoc_STRVAR(win32_urandom__doc__,
7541"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007542Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007543
7544typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7545 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7546 DWORD dwFlags );
7547typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7548 BYTE *pbBuffer );
7549
7550static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007551/* This handle is never explicitly released. Instead, the operating
7552 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007553static HCRYPTPROV hCryptProv = 0;
7554
Tim Peters4ad82172004-08-30 17:02:04 +00007555static PyObject*
7556win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007557{
Victor Stinner8c62be82010-05-06 00:08:46 +00007558 int howMany;
7559 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007560
Victor Stinner8c62be82010-05-06 00:08:46 +00007561 /* Read arguments */
7562 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7563 return NULL;
7564 if (howMany < 0)
7565 return PyErr_Format(PyExc_ValueError,
7566 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007567
Victor Stinner8c62be82010-05-06 00:08:46 +00007568 if (hCryptProv == 0) {
7569 HINSTANCE hAdvAPI32 = NULL;
7570 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007571
Victor Stinner8c62be82010-05-06 00:08:46 +00007572 /* Obtain handle to the DLL containing CryptoAPI
7573 This should not fail */
7574 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7575 if(hAdvAPI32 == NULL)
7576 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007577
Victor Stinner8c62be82010-05-06 00:08:46 +00007578 /* Obtain pointers to the CryptoAPI functions
7579 This will fail on some early versions of Win95 */
7580 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7581 hAdvAPI32,
7582 "CryptAcquireContextA");
7583 if (pCryptAcquireContext == NULL)
7584 return PyErr_Format(PyExc_NotImplementedError,
7585 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007586
Victor Stinner8c62be82010-05-06 00:08:46 +00007587 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7588 hAdvAPI32, "CryptGenRandom");
7589 if (pCryptGenRandom == NULL)
7590 return PyErr_Format(PyExc_NotImplementedError,
7591 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007592
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 /* Acquire context */
7594 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7595 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7596 return win32_error("CryptAcquireContext", NULL);
7597 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007598
Victor Stinner8c62be82010-05-06 00:08:46 +00007599 /* Allocate bytes */
7600 result = PyBytes_FromStringAndSize(NULL, howMany);
7601 if (result != NULL) {
7602 /* Get random data */
7603 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7604 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7605 PyBytes_AS_STRING(result))) {
7606 Py_DECREF(result);
7607 return win32_error("CryptGenRandom", NULL);
7608 }
7609 }
7610 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007611}
7612#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007613
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007614PyDoc_STRVAR(device_encoding__doc__,
7615"device_encoding(fd) -> str\n\n\
7616Return a string describing the encoding of the device\n\
7617if the output is a terminal; else return None.");
7618
7619static PyObject *
7620device_encoding(PyObject *self, PyObject *args)
7621{
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 int fd;
7623 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7624 return NULL;
7625 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7626 Py_INCREF(Py_None);
7627 return Py_None;
7628 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007629#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 if (fd == 0) {
7631 char buf[100];
7632 sprintf(buf, "cp%d", GetConsoleCP());
7633 return PyUnicode_FromString(buf);
7634 }
7635 if (fd == 1 || fd == 2) {
7636 char buf[100];
7637 sprintf(buf, "cp%d", GetConsoleOutputCP());
7638 return PyUnicode_FromString(buf);
7639 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007640#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007641 {
7642 char *codeset = nl_langinfo(CODESET);
7643 if (codeset != NULL && codeset[0] != 0)
7644 return PyUnicode_FromString(codeset);
7645 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007646#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007647 Py_INCREF(Py_None);
7648 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007649}
7650
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007651#ifdef __VMS
7652/* Use openssl random routine */
7653#include <openssl/rand.h>
7654PyDoc_STRVAR(vms_urandom__doc__,
7655"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007656Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007657
7658static PyObject*
7659vms_urandom(PyObject *self, PyObject *args)
7660{
Victor Stinner8c62be82010-05-06 00:08:46 +00007661 int howMany;
7662 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007663
Victor Stinner8c62be82010-05-06 00:08:46 +00007664 /* Read arguments */
7665 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7666 return NULL;
7667 if (howMany < 0)
7668 return PyErr_Format(PyExc_ValueError,
7669 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007670
Victor Stinner8c62be82010-05-06 00:08:46 +00007671 /* Allocate bytes */
7672 result = PyBytes_FromStringAndSize(NULL, howMany);
7673 if (result != NULL) {
7674 /* Get random data */
7675 if (RAND_pseudo_bytes((unsigned char*)
7676 PyBytes_AS_STRING(result),
7677 howMany) < 0) {
7678 Py_DECREF(result);
7679 return PyErr_Format(PyExc_ValueError,
7680 "RAND_pseudo_bytes");
7681 }
7682 }
7683 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007684}
7685#endif
7686
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007687#ifdef HAVE_SETRESUID
7688PyDoc_STRVAR(posix_setresuid__doc__,
7689"setresuid(ruid, euid, suid)\n\n\
7690Set the current process's real, effective, and saved user ids.");
7691
7692static PyObject*
7693posix_setresuid (PyObject *self, PyObject *args)
7694{
Victor Stinner8c62be82010-05-06 00:08:46 +00007695 /* We assume uid_t is no larger than a long. */
7696 long ruid, euid, suid;
7697 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7698 return NULL;
7699 if (setresuid(ruid, euid, suid) < 0)
7700 return posix_error();
7701 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007702}
7703#endif
7704
7705#ifdef HAVE_SETRESGID
7706PyDoc_STRVAR(posix_setresgid__doc__,
7707"setresgid(rgid, egid, sgid)\n\n\
7708Set the current process's real, effective, and saved group ids.");
7709
7710static PyObject*
7711posix_setresgid (PyObject *self, PyObject *args)
7712{
Victor Stinner8c62be82010-05-06 00:08:46 +00007713 /* We assume uid_t is no larger than a long. */
7714 long rgid, egid, sgid;
7715 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7716 return NULL;
7717 if (setresgid(rgid, egid, sgid) < 0)
7718 return posix_error();
7719 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007720}
7721#endif
7722
7723#ifdef HAVE_GETRESUID
7724PyDoc_STRVAR(posix_getresuid__doc__,
7725"getresuid() -> (ruid, euid, suid)\n\n\
7726Get tuple of the current process's real, effective, and saved user ids.");
7727
7728static PyObject*
7729posix_getresuid (PyObject *self, PyObject *noargs)
7730{
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 uid_t ruid, euid, suid;
7732 long l_ruid, l_euid, l_suid;
7733 if (getresuid(&ruid, &euid, &suid) < 0)
7734 return posix_error();
7735 /* Force the values into long's as we don't know the size of uid_t. */
7736 l_ruid = ruid;
7737 l_euid = euid;
7738 l_suid = suid;
7739 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007740}
7741#endif
7742
7743#ifdef HAVE_GETRESGID
7744PyDoc_STRVAR(posix_getresgid__doc__,
7745"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007746Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007747
7748static PyObject*
7749posix_getresgid (PyObject *self, PyObject *noargs)
7750{
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 uid_t rgid, egid, sgid;
7752 long l_rgid, l_egid, l_sgid;
7753 if (getresgid(&rgid, &egid, &sgid) < 0)
7754 return posix_error();
7755 /* Force the values into long's as we don't know the size of uid_t. */
7756 l_rgid = rgid;
7757 l_egid = egid;
7758 l_sgid = sgid;
7759 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007760}
7761#endif
7762
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007763static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007764 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007765#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007767#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007768 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007769#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007770 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007771#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007773#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007775#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007776#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007777 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007778#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007779#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007781#endif /* HAVE_LCHMOD */
7782#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007784#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007785#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007786 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007787#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007788#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007789 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007790#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007791#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007792 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007793#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007794#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007795 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007796#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007797#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007798 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7799 METH_NOARGS, posix_getcwd__doc__},
7800 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7801 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007802#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007803#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007805#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7807 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7808 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007809#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007810 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007811#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007812#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007813 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007814#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007815#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007816 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007817#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007818 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7819 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7820 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007821 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007822#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007824#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007825#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007826 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007827 win_symlink__doc__},
7828#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007829#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007830 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007831#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007833#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007835#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7837 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7838 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007839#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007840 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007841#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007843#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7845 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007846#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007847#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7849 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007850#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7852 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007853#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007854#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007855#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007857#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007858#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007860#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007861#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007863#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007864#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007866#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007867#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007869#endif /* HAVE_GETEGID */
7870#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007872#endif /* HAVE_GETEUID */
7873#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007875#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007876#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007878#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007880#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007882#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007883#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007884 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007885#endif /* HAVE_GETPPID */
7886#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007887 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007888#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007889#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007890 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007891#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007892#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007893 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007894#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007895#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007896 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007897#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007898#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007900#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007901#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7903 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007904 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007905#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007906#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007907 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007908#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007909#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007910 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007911#endif /* HAVE_SETEUID */
7912#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007913 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007914#endif /* HAVE_SETEGID */
7915#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007917#endif /* HAVE_SETREUID */
7918#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007919 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007920#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007921#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007923#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007924#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007926#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007927#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007929#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007930#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007932#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007933#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007935#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007936#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007938#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007939#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007940 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007941#endif /* HAVE_WAIT3 */
7942#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007944#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007945#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007946 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007947#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007948#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007949 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007950#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007951#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007952 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007953#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007954#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007955 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007956#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007957#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007959#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007960#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007962#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7964 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7965 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7966 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7967 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7968 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7969 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7970 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7971 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7972 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7973 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007974#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007976#endif
7977#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007979#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007980#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007982#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007983#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7985 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7986 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007987#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007988#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007989 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007990#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007991#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007992 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007993#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007994#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007996#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007998#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008000#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008001#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008003#endif
8004#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008005 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008006#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008007#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008008#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008009 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008010#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008011#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008012 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008013#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008014#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008016#endif /* WIFSTOPPED */
8017#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008018 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008019#endif /* WIFSIGNALED */
8020#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008022#endif /* WIFEXITED */
8023#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008025#endif /* WEXITSTATUS */
8026#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008028#endif /* WTERMSIG */
8029#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008031#endif /* WSTOPSIG */
8032#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008033#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008034 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008035#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008036#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008037 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008038#endif
Fred Drakec9680921999-12-13 16:37:25 +00008039#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008041#endif
8042#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008044#endif
8045#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008046 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008047#endif
8048#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008049 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008050#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008052#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008054 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008055 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008056#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008057#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008058 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008059#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008060 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008061 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008062 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008063 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008064 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008065 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008066#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008067 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008068#endif
8069#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008070 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008071#endif
8072#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008073 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008074#endif
8075#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008076 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008077#endif
8078
Victor Stinner8c62be82010-05-06 00:08:46 +00008079 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008080};
8081
8082
Barry Warsaw4a342091996-12-19 23:50:02 +00008083static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008084ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008085{
Victor Stinner8c62be82010-05-06 00:08:46 +00008086 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008087}
8088
Guido van Rossumd48f2521997-12-05 22:19:34 +00008089#if defined(PYOS_OS2)
8090/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008091static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008092{
8093 APIRET rc;
8094 ULONG values[QSV_MAX+1];
8095 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008096 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008097
8098 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008099 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008100 Py_END_ALLOW_THREADS
8101
8102 if (rc != NO_ERROR) {
8103 os2_error(rc);
8104 return -1;
8105 }
8106
Fred Drake4d1e64b2002-04-15 19:40:07 +00008107 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8108 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8109 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8110 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8111 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8112 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8113 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008114
8115 switch (values[QSV_VERSION_MINOR]) {
8116 case 0: ver = "2.00"; break;
8117 case 10: ver = "2.10"; break;
8118 case 11: ver = "2.11"; break;
8119 case 30: ver = "3.00"; break;
8120 case 40: ver = "4.00"; break;
8121 case 50: ver = "5.00"; break;
8122 default:
Tim Peters885d4572001-11-28 20:27:42 +00008123 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008124 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008125 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008126 ver = &tmp[0];
8127 }
8128
8129 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008130 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008131 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008132
8133 /* Add Indicator of Which Drive was Used to Boot the System */
8134 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8135 tmp[1] = ':';
8136 tmp[2] = '\0';
8137
Fred Drake4d1e64b2002-04-15 19:40:07 +00008138 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008139}
8140#endif
8141
Brian Curtin52173d42010-12-02 18:29:18 +00008142#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008143static int
Brian Curtin52173d42010-12-02 18:29:18 +00008144enable_symlink()
8145{
8146 HANDLE tok;
8147 TOKEN_PRIVILEGES tok_priv;
8148 LUID luid;
8149 int meth_idx = 0;
8150
8151 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008152 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008153
8154 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008155 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008156
8157 tok_priv.PrivilegeCount = 1;
8158 tok_priv.Privileges[0].Luid = luid;
8159 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8160
8161 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8162 sizeof(TOKEN_PRIVILEGES),
8163 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008164 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008165
Brian Curtin3b4499c2010-12-28 14:31:47 +00008166 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8167 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008168}
8169#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8170
Barry Warsaw4a342091996-12-19 23:50:02 +00008171static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008172all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008173{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008174#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008175 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008176#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008177#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008178 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008179#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008180#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008181 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008182#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008183#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008184 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008185#endif
Fred Drakec9680921999-12-13 16:37:25 +00008186#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008187 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008188#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008189#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008190 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008191#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008192#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008193 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008194#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008195#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008196 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008197#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008198#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008199 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008200#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008201#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008202 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008203#endif
8204#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008205 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008206#endif
8207#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008208 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008209#endif
8210#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008211 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008212#endif
8213#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008215#endif
8216#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008218#endif
8219#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008221#endif
8222#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008224#endif
8225#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008226 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008227#endif
8228#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008230#endif
8231#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008233#endif
8234#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008236#endif
8237#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008239#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008240#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008242#endif
8243#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008245#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008246#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008248#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008249#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008251#endif
8252#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008253 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008254#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008255
Tim Peters5aa91602002-01-30 05:46:57 +00008256/* MS Windows */
8257#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008258 /* Don't inherit in child processes. */
8259 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008260#endif
8261#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008262 /* Optimize for short life (keep in memory). */
8263 /* MS forgot to define this one with a non-underscore form too. */
8264 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008265#endif
8266#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008267 /* Automatically delete when last handle is closed. */
8268 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008269#endif
8270#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008271 /* Optimize for random access. */
8272 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008273#endif
8274#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 /* Optimize for sequential access. */
8276 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008277#endif
8278
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008279/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008280#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008281 /* Send a SIGIO signal whenever input or output
8282 becomes available on file descriptor */
8283 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008284#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008285#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008286 /* Direct disk access. */
8287 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008288#endif
8289#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 /* Must be a directory. */
8291 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008292#endif
8293#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008294 /* Do not follow links. */
8295 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008296#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008297#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008298 /* Do not update the access time. */
8299 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008300#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008301
Victor Stinner8c62be82010-05-06 00:08:46 +00008302 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008303#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008305#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008306#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008308#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008309#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008311#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008312#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008314#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008315#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008316 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008317#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008318#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008319 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008320#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008321#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008322 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008323#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008324#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008325 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008326#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008327#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008329#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008330#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008332#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008333#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008335#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008336#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008338#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008339#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008341#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008342#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008343 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008344#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008345#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008347#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008348#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008349 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008350#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008351#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008352 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008353#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008354
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008355 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008356#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008357 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008358#endif /* ST_RDONLY */
8359#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008360 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008361#endif /* ST_NOSUID */
8362
Guido van Rossum246bc171999-02-01 23:54:31 +00008363#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008364#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008365 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8366 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8367 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8368 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8369 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8370 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8371 if (ins(d, "P_PM", (long)P_PM)) return -1;
8372 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8373 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8374 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8375 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8376 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8377 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8378 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8379 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8380 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8381 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8382 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8383 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8384 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008385#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008386 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8387 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8388 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8389 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8390 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008391#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008392#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008393
Guido van Rossumd48f2521997-12-05 22:19:34 +00008394#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008395 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008396#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008397 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008398}
8399
8400
Tim Peters5aa91602002-01-30 05:46:57 +00008401#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008402#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008403#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008404
8405#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008406#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008407#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008408
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008409#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008410#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008411#define MODNAME "posix"
8412#endif
8413
Martin v. Löwis1a214512008-06-11 05:26:20 +00008414static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008415 PyModuleDef_HEAD_INIT,
8416 MODNAME,
8417 posix__doc__,
8418 -1,
8419 posix_methods,
8420 NULL,
8421 NULL,
8422 NULL,
8423 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008424};
8425
8426
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008427PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008428INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008429{
Victor Stinner8c62be82010-05-06 00:08:46 +00008430 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008431
Brian Curtin52173d42010-12-02 18:29:18 +00008432#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008433 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008434#endif
8435
Victor Stinner8c62be82010-05-06 00:08:46 +00008436 m = PyModule_Create(&posixmodule);
8437 if (m == NULL)
8438 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008439
Victor Stinner8c62be82010-05-06 00:08:46 +00008440 /* Initialize environ dictionary */
8441 v = convertenviron();
8442 Py_XINCREF(v);
8443 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8444 return NULL;
8445 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008446
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 if (all_ins(m))
8448 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008449
Victor Stinner8c62be82010-05-06 00:08:46 +00008450 if (setup_confname_tables(m))
8451 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008452
Victor Stinner8c62be82010-05-06 00:08:46 +00008453 Py_INCREF(PyExc_OSError);
8454 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008455
Guido van Rossumb3d39562000-01-31 18:41:26 +00008456#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008457 if (posix_putenv_garbage == NULL)
8458 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008459#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008460
Victor Stinner8c62be82010-05-06 00:08:46 +00008461 if (!initialized) {
8462 stat_result_desc.name = MODNAME ".stat_result";
8463 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8464 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8465 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8466 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8467 structseq_new = StatResultType.tp_new;
8468 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008469
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 statvfs_result_desc.name = MODNAME ".statvfs_result";
8471 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008472#ifdef NEED_TICKS_PER_SECOND
8473# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008474 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008475# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008476 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008477# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008479# endif
8480#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 }
8482 Py_INCREF((PyObject*) &StatResultType);
8483 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8484 Py_INCREF((PyObject*) &StatVFSResultType);
8485 PyModule_AddObject(m, "statvfs_result",
8486 (PyObject*) &StatVFSResultType);
8487 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008488
8489#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008490 /*
8491 * Step 2 of weak-linking support on Mac OS X.
8492 *
8493 * The code below removes functions that are not available on the
8494 * currently active platform.
8495 *
8496 * This block allow one to use a python binary that was build on
8497 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8498 * OSX 10.4.
8499 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008500#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008501 if (fstatvfs == NULL) {
8502 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8503 return NULL;
8504 }
8505 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008506#endif /* HAVE_FSTATVFS */
8507
8508#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 if (statvfs == NULL) {
8510 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8511 return NULL;
8512 }
8513 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008514#endif /* HAVE_STATVFS */
8515
8516# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008517 if (lchown == NULL) {
8518 if (PyObject_DelAttrString(m, "lchown") == -1) {
8519 return NULL;
8520 }
8521 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008522#endif /* HAVE_LCHOWN */
8523
8524
8525#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008526 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008527
Guido van Rossumb6775db1994-08-01 11:34:53 +00008528}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008529
8530#ifdef __cplusplus
8531}
8532#endif