blob: 6f13776cce0588600c6ced3f2ef7e1a25964dd94 [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
282#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000283#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284
Guido van Rossumd48f2521997-12-05 22:19:34 +0000285#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000287#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288
Tim Petersbc2e10e2002-03-03 23:17:02 +0000289#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000290#if defined(PATH_MAX) && PATH_MAX > 1024
291#define MAXPATHLEN PATH_MAX
292#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000293#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000294#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000295#endif /* MAXPATHLEN */
296
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000297#ifdef UNION_WAIT
298/* Emulate some macros on systems that have a union instead of macros */
299
300#ifndef WIFEXITED
301#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
302#endif
303
304#ifndef WEXITSTATUS
305#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
306#endif
307
308#ifndef WTERMSIG
309#define WTERMSIG(u_wait) ((u_wait).w_termsig)
310#endif
311
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312#define WAIT_TYPE union wait
313#define WAIT_STATUS_INT(s) (s.w_status)
314
315#else /* !UNION_WAIT */
316#define WAIT_TYPE int
317#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000318#endif /* UNION_WAIT */
319
Greg Wardb48bc172000-03-01 21:51:56 +0000320/* Don't use the "_r" form if we don't need it (also, won't have a
321 prototype for it, at least on Solaris -- maybe others as well?). */
322#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
323#define USE_CTERMID_R
324#endif
325
Fred Drake699f3522000-06-29 21:12:41 +0000326/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000327#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000328#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000329# define STAT win32_stat
330# define FSTAT win32_fstat
331# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000332#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000333# define STAT stat
334# define FSTAT fstat
335# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000336#endif
337
Tim Peters11b23062003-04-23 02:39:17 +0000338#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000339#include <sys/mkdev.h>
340#else
341#if defined(MAJOR_IN_SYSMACROS)
342#include <sys/sysmacros.h>
343#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000344#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
345#include <sys/mkdev.h>
346#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000347#endif
Fred Drake699f3522000-06-29 21:12:41 +0000348
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000349#if defined _MSC_VER && _MSC_VER >= 1400
350/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
351 * valid and throw an assertion if it isn't.
352 * Normally, an invalid fd is likely to be a C program error and therefore
353 * an assertion can be useful, but it does contradict the POSIX standard
354 * which for write(2) states:
355 * "Otherwise, -1 shall be returned and errno set to indicate the error."
356 * "[EBADF] The fildes argument is not a valid file descriptor open for
357 * writing."
358 * Furthermore, python allows the user to enter any old integer
359 * as a fd and should merely raise a python exception on error.
360 * The Microsoft CRT doesn't provide an official way to check for the
361 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000362 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000363 * internal structures involved.
364 * The structures below must be updated for each version of visual studio
365 * according to the file internal.h in the CRT source, until MS comes
366 * up with a less hacky way to do this.
367 * (all of this is to avoid globally modifying the CRT behaviour using
368 * _set_invalid_parameter_handler() and _CrtSetReportMode())
369 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000370/* The actual size of the structure is determined at runtime.
371 * Only the first items must be present.
372 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000373typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000374 intptr_t osfhnd;
375 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000376} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000377
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000378extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000379#define IOINFO_L2E 5
380#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
381#define IOINFO_ARRAYS 64
382#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
383#define FOPEN 0x01
384#define _NO_CONSOLE_FILENO (intptr_t)-2
385
386/* This function emulates what the windows CRT does to validate file handles */
387int
388_PyVerify_fd(int fd)
389{
Victor Stinner8c62be82010-05-06 00:08:46 +0000390 const int i1 = fd >> IOINFO_L2E;
391 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000392
Antoine Pitrou22e41552010-08-15 18:07:50 +0000393 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000394
Victor Stinner8c62be82010-05-06 00:08:46 +0000395 /* Determine the actual size of the ioinfo structure,
396 * as used by the CRT loaded in memory
397 */
398 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
399 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
400 }
401 if (sizeof_ioinfo == 0) {
402 /* This should not happen... */
403 goto fail;
404 }
405
406 /* See that it isn't a special CLEAR fileno */
407 if (fd != _NO_CONSOLE_FILENO) {
408 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
409 * we check pointer validity and other info
410 */
411 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
412 /* finally, check that the file is open */
413 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
414 if (info->osfile & FOPEN) {
415 return 1;
416 }
417 }
418 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000419 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000420 errno = EBADF;
421 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422}
423
424/* the special case of checking dup2. The target fd must be in a sensible range */
425static int
426_PyVerify_fd_dup2(int fd1, int fd2)
427{
Victor Stinner8c62be82010-05-06 00:08:46 +0000428 if (!_PyVerify_fd(fd1))
429 return 0;
430 if (fd2 == _NO_CONSOLE_FILENO)
431 return 0;
432 if ((unsigned)fd2 < _NHANDLE_)
433 return 1;
434 else
435 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000436}
437#else
438/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
439#define _PyVerify_fd_dup2(A, B) (1)
440#endif
441
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000442#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000443/* The following structure was copied from
444 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
445 include doesn't seem to be present in the Windows SDK (at least as included
446 with Visual Studio Express). */
447typedef struct _REPARSE_DATA_BUFFER {
448 ULONG ReparseTag;
449 USHORT ReparseDataLength;
450 USHORT Reserved;
451 union {
452 struct {
453 USHORT SubstituteNameOffset;
454 USHORT SubstituteNameLength;
455 USHORT PrintNameOffset;
456 USHORT PrintNameLength;
457 ULONG Flags;
458 WCHAR PathBuffer[1];
459 } SymbolicLinkReparseBuffer;
460
461 struct {
462 USHORT SubstituteNameOffset;
463 USHORT SubstituteNameLength;
464 USHORT PrintNameOffset;
465 USHORT PrintNameLength;
466 WCHAR PathBuffer[1];
467 } MountPointReparseBuffer;
468
469 struct {
470 UCHAR DataBuffer[1];
471 } GenericReparseBuffer;
472 };
473} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
474
475#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
476 GenericReparseBuffer)
477#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
478
479static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000480win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000481{
482 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
483 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
484 DWORD n_bytes_returned;
485 const wchar_t *ptr;
486 wchar_t *buf;
487 size_t len;
488
489 if (0 == DeviceIoControl(
490 reparse_point_handle,
491 FSCTL_GET_REPARSE_POINT,
492 NULL, 0, /* in buffer */
493 target_buffer, sizeof(target_buffer),
494 &n_bytes_returned,
495 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000496 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000497
498 if (reparse_tag)
499 *reparse_tag = rdb->ReparseTag;
500
501 if (target_path) {
502 switch (rdb->ReparseTag) {
503 case IO_REPARSE_TAG_SYMLINK:
504 /* XXX: Maybe should use SubstituteName? */
505 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
506 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
507 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
508 break;
509 case IO_REPARSE_TAG_MOUNT_POINT:
510 ptr = rdb->MountPointReparseBuffer.PathBuffer +
511 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
512 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
513 break;
514 default:
515 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000516 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000517 }
518 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
519 if (!buf) {
520 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000521 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000522 }
523 wcsncpy(buf, ptr, len);
524 buf[len] = L'\0';
525 if (wcsncmp(buf, L"\\??\\", 4) == 0)
526 buf[1] = L'\\';
527 *target_path = buf;
528 }
529
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000530 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000531}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000532#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000533
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000535#ifdef WITH_NEXT_FRAMEWORK
536/* On Darwin/MacOSX a shared library or framework has no access to
537** environ directly, we must obtain it with _NSGetEnviron().
538*/
539#include <crt_externs.h>
540static char **environ;
541#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000543#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544
Barry Warsaw53699e91996-12-10 23:23:01 +0000545static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000546convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547{
Victor Stinner8c62be82010-05-06 00:08:46 +0000548 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000549#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000550 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000551#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000552 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000553#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000554#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000555 APIRET rc;
556 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
557#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000558
Victor Stinner8c62be82010-05-06 00:08:46 +0000559 d = PyDict_New();
560 if (d == NULL)
561 return NULL;
562#ifdef WITH_NEXT_FRAMEWORK
563 if (environ == NULL)
564 environ = *_NSGetEnviron();
565#endif
566#ifdef MS_WINDOWS
567 /* _wenviron must be initialized in this way if the program is started
568 through main() instead of wmain(). */
569 _wgetenv(L"");
570 if (_wenviron == NULL)
571 return d;
572 /* This part ignores errors */
573 for (e = _wenviron; *e != NULL; e++) {
574 PyObject *k;
575 PyObject *v;
576 wchar_t *p = wcschr(*e, L'=');
577 if (p == NULL)
578 continue;
579 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
580 if (k == NULL) {
581 PyErr_Clear();
582 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000583 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000584 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
585 if (v == NULL) {
586 PyErr_Clear();
587 Py_DECREF(k);
588 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000589 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000590 if (PyDict_GetItem(d, k) == NULL) {
591 if (PyDict_SetItem(d, k, v) != 0)
592 PyErr_Clear();
593 }
594 Py_DECREF(k);
595 Py_DECREF(v);
596 }
597#else
598 if (environ == NULL)
599 return d;
600 /* This part ignores errors */
601 for (e = environ; *e != NULL; e++) {
602 PyObject *k;
603 PyObject *v;
604 char *p = strchr(*e, '=');
605 if (p == NULL)
606 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000607 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000608 if (k == NULL) {
609 PyErr_Clear();
610 continue;
611 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000612 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000613 if (v == NULL) {
614 PyErr_Clear();
615 Py_DECREF(k);
616 continue;
617 }
618 if (PyDict_GetItem(d, k) == NULL) {
619 if (PyDict_SetItem(d, k, v) != 0)
620 PyErr_Clear();
621 }
622 Py_DECREF(k);
623 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000624 }
625#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000626#if defined(PYOS_OS2)
627 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
628 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
629 PyObject *v = PyBytes_FromString(buffer);
630 PyDict_SetItemString(d, "BEGINLIBPATH", v);
631 Py_DECREF(v);
632 }
633 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
634 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
635 PyObject *v = PyBytes_FromString(buffer);
636 PyDict_SetItemString(d, "ENDLIBPATH", v);
637 Py_DECREF(v);
638 }
639#endif
640 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641}
642
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643/* Set a POSIX-specific error from errno, and return NULL */
644
Barry Warsawd58d7641998-07-23 16:14:40 +0000645static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000646posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000647{
Victor Stinner8c62be82010-05-06 00:08:46 +0000648 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649}
Barry Warsawd58d7641998-07-23 16:14:40 +0000650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000651posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000652{
Victor Stinner8c62be82010-05-06 00:08:46 +0000653 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000654}
655
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000656
Mark Hammondef8b6542001-05-13 08:04:26 +0000657static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000658posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000659{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000660 PyObject *name_str, *rc;
661 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
662 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000663 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000664 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
665 name_str);
666 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000667 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000668}
669
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000670#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000671static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000672win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000673{
Victor Stinner8c62be82010-05-06 00:08:46 +0000674 /* XXX We should pass the function name along in the future.
675 (winreg.c also wants to pass the function name.)
676 This would however require an additional param to the
677 Windows error object, which is non-trivial.
678 */
679 errno = GetLastError();
680 if (filename)
681 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
682 else
683 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000684}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000685
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686static PyObject *
687win32_error_unicode(char* function, Py_UNICODE* filename)
688{
Victor Stinner8c62be82010-05-06 00:08:46 +0000689 /* XXX - see win32_error for comments on 'function' */
690 errno = GetLastError();
691 if (filename)
692 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
693 else
694 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000695}
696
Thomas Wouters477c8d52006-05-27 19:21:47 +0000697static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000698convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699{
Victor Stinner8c62be82010-05-06 00:08:46 +0000700 if (PyUnicode_CheckExact(*param))
701 Py_INCREF(*param);
702 else if (PyUnicode_Check(*param))
703 /* For a Unicode subtype that's not a Unicode object,
704 return a true Unicode object with the same data. */
705 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
706 PyUnicode_GET_SIZE(*param));
707 else
708 *param = PyUnicode_FromEncodedObject(*param,
709 Py_FileSystemDefaultEncoding,
710 "strict");
711 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000712}
713
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000714#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715
Guido van Rossumd48f2521997-12-05 22:19:34 +0000716#if defined(PYOS_OS2)
717/**********************************************************************
718 * Helper Function to Trim and Format OS/2 Messages
719 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000720static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000721os2_formatmsg(char *msgbuf, int msglen, char *reason)
722{
723 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
724
725 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
726 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
727
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000728 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000729 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
730 }
731
732 /* Add Optional Reason Text */
733 if (reason) {
734 strcat(msgbuf, " : ");
735 strcat(msgbuf, reason);
736 }
737}
738
739/**********************************************************************
740 * Decode an OS/2 Operating System Error Code
741 *
742 * A convenience function to lookup an OS/2 error code and return a
743 * text message we can use to raise a Python exception.
744 *
745 * Notes:
746 * The messages for errors returned from the OS/2 kernel reside in
747 * the file OSO001.MSG in the \OS2 directory hierarchy.
748 *
749 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000750static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000751os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
752{
753 APIRET rc;
754 ULONG msglen;
755
756 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
757 Py_BEGIN_ALLOW_THREADS
758 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
759 errorcode, "oso001.msg", &msglen);
760 Py_END_ALLOW_THREADS
761
762 if (rc == NO_ERROR)
763 os2_formatmsg(msgbuf, msglen, reason);
764 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000765 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000766 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000767
768 return msgbuf;
769}
770
771/* Set an OS/2-specific error and return NULL. OS/2 kernel
772 errors are not in a global variable e.g. 'errno' nor are
773 they congruent with posix error numbers. */
774
Victor Stinner8c62be82010-05-06 00:08:46 +0000775static PyObject *
776os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000777{
778 char text[1024];
779 PyObject *v;
780
781 os2_strerror(text, sizeof(text), code, "");
782
783 v = Py_BuildValue("(is)", code, text);
784 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000785 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000786 Py_DECREF(v);
787 }
788 return NULL; /* Signal to Python that an Exception is Pending */
789}
790
791#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792
793/* POSIX generic methods */
794
Barry Warsaw53699e91996-12-10 23:23:01 +0000795static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000796posix_fildes(PyObject *fdobj, int (*func)(int))
797{
Victor Stinner8c62be82010-05-06 00:08:46 +0000798 int fd;
799 int res;
800 fd = PyObject_AsFileDescriptor(fdobj);
801 if (fd < 0)
802 return NULL;
803 if (!_PyVerify_fd(fd))
804 return posix_error();
805 Py_BEGIN_ALLOW_THREADS
806 res = (*func)(fd);
807 Py_END_ALLOW_THREADS
808 if (res < 0)
809 return posix_error();
810 Py_INCREF(Py_None);
811 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000812}
Guido van Rossum21142a01999-01-08 21:05:37 +0000813
814static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816{
Victor Stinner8c62be82010-05-06 00:08:46 +0000817 PyObject *opath1 = NULL;
818 char *path1;
819 int res;
820 if (!PyArg_ParseTuple(args, format,
821 PyUnicode_FSConverter, &opath1))
822 return NULL;
823 path1 = PyBytes_AsString(opath1);
824 Py_BEGIN_ALLOW_THREADS
825 res = (*func)(path1);
826 Py_END_ALLOW_THREADS
827 if (res < 0)
828 return posix_error_with_allocated_filename(opath1);
829 Py_DECREF(opath1);
830 Py_INCREF(Py_None);
831 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000832}
833
Barry Warsaw53699e91996-12-10 23:23:01 +0000834static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000835posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000836 char *format,
837 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838{
Victor Stinner8c62be82010-05-06 00:08:46 +0000839 PyObject *opath1 = NULL, *opath2 = NULL;
840 char *path1, *path2;
841 int res;
842 if (!PyArg_ParseTuple(args, format,
843 PyUnicode_FSConverter, &opath1,
844 PyUnicode_FSConverter, &opath2)) {
845 return NULL;
846 }
847 path1 = PyBytes_AsString(opath1);
848 path2 = PyBytes_AsString(opath2);
849 Py_BEGIN_ALLOW_THREADS
850 res = (*func)(path1, path2);
851 Py_END_ALLOW_THREADS
852 Py_DECREF(opath1);
853 Py_DECREF(opath2);
854 if (res != 0)
855 /* XXX how to report both path1 and path2??? */
856 return posix_error();
857 Py_INCREF(Py_None);
858 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859}
860
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000861#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000863win32_1str(PyObject* args, char* func,
864 char* format, BOOL (__stdcall *funcA)(LPCSTR),
865 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000866{
Victor Stinner8c62be82010-05-06 00:08:46 +0000867 PyObject *uni;
868 char *ansi;
869 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000870
Victor Stinner8c62be82010-05-06 00:08:46 +0000871 if (!PyArg_ParseTuple(args, wformat, &uni))
872 PyErr_Clear();
873 else {
874 Py_BEGIN_ALLOW_THREADS
875 result = funcW(PyUnicode_AsUnicode(uni));
876 Py_END_ALLOW_THREADS
877 if (!result)
878 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
879 Py_INCREF(Py_None);
880 return Py_None;
881 }
882 if (!PyArg_ParseTuple(args, format, &ansi))
883 return NULL;
884 Py_BEGIN_ALLOW_THREADS
885 result = funcA(ansi);
886 Py_END_ALLOW_THREADS
887 if (!result)
888 return win32_error(func, ansi);
889 Py_INCREF(Py_None);
890 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891
892}
893
894/* This is a reimplementation of the C library's chdir function,
895 but one that produces Win32 errors instead of DOS error codes.
896 chdir is essentially a wrapper around SetCurrentDirectory; however,
897 it also needs to set "magic" environment variables indicating
898 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000899static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900win32_chdir(LPCSTR path)
901{
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 char new_path[MAX_PATH+1];
903 int result;
904 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 if(!SetCurrentDirectoryA(path))
907 return FALSE;
908 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
909 if (!result)
910 return FALSE;
911 /* In the ANSI API, there should not be any paths longer
912 than MAX_PATH. */
913 assert(result <= MAX_PATH+1);
914 if (strncmp(new_path, "\\\\", 2) == 0 ||
915 strncmp(new_path, "//", 2) == 0)
916 /* UNC path, nothing to do. */
917 return TRUE;
918 env[1] = new_path[0];
919 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920}
921
922/* The Unicode version differs from the ANSI version
923 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000924static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000925win32_wchdir(LPCWSTR path)
926{
Victor Stinner8c62be82010-05-06 00:08:46 +0000927 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
928 int result;
929 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930
Victor Stinner8c62be82010-05-06 00:08:46 +0000931 if(!SetCurrentDirectoryW(path))
932 return FALSE;
933 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
934 if (!result)
935 return FALSE;
936 if (result > MAX_PATH+1) {
937 new_path = malloc(result * sizeof(wchar_t));
938 if (!new_path) {
939 SetLastError(ERROR_OUTOFMEMORY);
940 return FALSE;
941 }
942 result = GetCurrentDirectoryW(result, new_path);
943 if (!result) {
944 free(new_path);
945 return FALSE;
946 }
947 }
948 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
949 wcsncmp(new_path, L"//", 2) == 0)
950 /* UNC path, nothing to do. */
951 return TRUE;
952 env[1] = new_path[0];
953 result = SetEnvironmentVariableW(env, new_path);
954 if (new_path != _new_path)
955 free(new_path);
956 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957}
958#endif
959
Martin v. Löwis14694662006-02-03 12:54:16 +0000960#ifdef MS_WINDOWS
961/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
962 - time stamps are restricted to second resolution
963 - file modification times suffer from forth-and-back conversions between
964 UTC and local time
965 Therefore, we implement our own stat, based on the Win32 API directly.
966*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000967#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000968
969struct win32_stat{
970 int st_dev;
971 __int64 st_ino;
972 unsigned short st_mode;
973 int st_nlink;
974 int st_uid;
975 int st_gid;
976 int st_rdev;
977 __int64 st_size;
978 int st_atime;
979 int st_atime_nsec;
980 int st_mtime;
981 int st_mtime_nsec;
982 int st_ctime;
983 int st_ctime_nsec;
984};
985
986static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
987
988static void
989FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
990{
Victor Stinner8c62be82010-05-06 00:08:46 +0000991 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
992 /* Cannot simply cast and dereference in_ptr,
993 since it might not be aligned properly */
994 __int64 in;
995 memcpy(&in, in_ptr, sizeof(in));
996 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
997 /* XXX Win32 supports time stamps past 2038; we currently don't */
998 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000999}
1000
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001static void
1002time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
1003{
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
3140extract_time(PyObject *t, long* sec, long* usec)
3141{
Victor Stinner8c62be82010-05-06 00:08:46 +00003142 long intval;
3143 if (PyFloat_Check(t)) {
3144 double tval = PyFloat_AsDouble(t);
3145 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
3146 if (!intobj)
3147 return -1;
3148 intval = PyLong_AsLong(intobj);
3149 Py_DECREF(intobj);
3150 if (intval == -1 && PyErr_Occurred())
3151 return -1;
3152 *sec = intval;
3153 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3154 if (*usec < 0)
3155 /* If rounding gave us a negative number,
3156 truncate. */
3157 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003158 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 }
3160 intval = PyLong_AsLong(t);
3161 if (intval == -1 && PyErr_Occurred())
3162 return -1;
3163 *sec = intval;
3164 *usec = 0;
3165 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003166}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003168PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003169"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003170utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003171Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003172second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003173
Barry Warsaw53699e91996-12-10 23:23:01 +00003174static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003175posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003176{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003177#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003178 PyObject *arg;
3179 PyUnicodeObject *obwpath;
3180 wchar_t *wpath = NULL;
3181 PyObject *oapath;
3182 char *apath;
3183 HANDLE hFile;
3184 long atimesec, mtimesec, ausec, musec;
3185 FILETIME atime, mtime;
3186 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003187
Victor Stinner8c62be82010-05-06 00:08:46 +00003188 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3189 wpath = PyUnicode_AS_UNICODE(obwpath);
3190 Py_BEGIN_ALLOW_THREADS
3191 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3192 NULL, OPEN_EXISTING,
3193 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3194 Py_END_ALLOW_THREADS
3195 if (hFile == INVALID_HANDLE_VALUE)
3196 return win32_error_unicode("utime", wpath);
3197 } else
3198 /* Drop the argument parsing error as narrow strings
3199 are also valid. */
3200 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003201
Victor Stinner8c62be82010-05-06 00:08:46 +00003202 if (!wpath) {
3203 if (!PyArg_ParseTuple(args, "O&O:utime",
3204 PyUnicode_FSConverter, &oapath, &arg))
3205 return NULL;
3206 apath = PyBytes_AsString(oapath);
3207 Py_BEGIN_ALLOW_THREADS
3208 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3209 NULL, OPEN_EXISTING,
3210 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3211 Py_END_ALLOW_THREADS
3212 if (hFile == INVALID_HANDLE_VALUE) {
3213 win32_error("utime", apath);
3214 Py_DECREF(oapath);
3215 return NULL;
3216 }
3217 Py_DECREF(oapath);
3218 }
3219
3220 if (arg == Py_None) {
3221 SYSTEMTIME now;
3222 GetSystemTime(&now);
3223 if (!SystemTimeToFileTime(&now, &mtime) ||
3224 !SystemTimeToFileTime(&now, &atime)) {
3225 win32_error("utime", NULL);
3226 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003227 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003228 }
3229 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3230 PyErr_SetString(PyExc_TypeError,
3231 "utime() arg 2 must be a tuple (atime, mtime)");
3232 goto done;
3233 }
3234 else {
3235 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3236 &atimesec, &ausec) == -1)
3237 goto done;
3238 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3239 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3240 &mtimesec, &musec) == -1)
3241 goto done;
3242 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3243 }
3244 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3245 /* Avoid putting the file name into the error here,
3246 as that may confuse the user into believing that
3247 something is wrong with the file, when it also
3248 could be the time stamp that gives a problem. */
3249 win32_error("utime", NULL);
3250 }
3251 Py_INCREF(Py_None);
3252 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003253done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003254 CloseHandle(hFile);
3255 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003256#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003257
Victor Stinner8c62be82010-05-06 00:08:46 +00003258 PyObject *opath;
3259 char *path;
3260 long atime, mtime, ausec, musec;
3261 int res;
3262 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003263
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003264#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003265 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003266#define ATIME buf[0].tv_sec
3267#define MTIME buf[1].tv_sec
3268#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003269/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003270 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003271#define ATIME buf.actime
3272#define MTIME buf.modtime
3273#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003274#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003275 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003276#define ATIME buf[0]
3277#define MTIME buf[1]
3278#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003279#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003280
Mark Hammond817c9292003-12-03 01:22:38 +00003281
Victor Stinner8c62be82010-05-06 00:08:46 +00003282 if (!PyArg_ParseTuple(args, "O&O:utime",
3283 PyUnicode_FSConverter, &opath, &arg))
3284 return NULL;
3285 path = PyBytes_AsString(opath);
3286 if (arg == Py_None) {
3287 /* optional time values not given */
3288 Py_BEGIN_ALLOW_THREADS
3289 res = utime(path, NULL);
3290 Py_END_ALLOW_THREADS
3291 }
3292 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3293 PyErr_SetString(PyExc_TypeError,
3294 "utime() arg 2 must be a tuple (atime, mtime)");
3295 Py_DECREF(opath);
3296 return NULL;
3297 }
3298 else {
3299 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3300 &atime, &ausec) == -1) {
3301 Py_DECREF(opath);
3302 return NULL;
3303 }
3304 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3305 &mtime, &musec) == -1) {
3306 Py_DECREF(opath);
3307 return NULL;
3308 }
3309 ATIME = atime;
3310 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003311#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003312 buf[0].tv_usec = ausec;
3313 buf[1].tv_usec = musec;
3314 Py_BEGIN_ALLOW_THREADS
3315 res = utimes(path, buf);
3316 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003317#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003318 Py_BEGIN_ALLOW_THREADS
3319 res = utime(path, UTIME_ARG);
3320 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003321#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003322 }
3323 if (res < 0) {
3324 return posix_error_with_allocated_filename(opath);
3325 }
3326 Py_DECREF(opath);
3327 Py_INCREF(Py_None);
3328 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003329#undef UTIME_ARG
3330#undef ATIME
3331#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003332#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003333}
3334
Guido van Rossum85e3b011991-06-03 12:42:10 +00003335
Guido van Rossum3b066191991-06-04 19:40:25 +00003336/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003338PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003339"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003340Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003341
Barry Warsaw53699e91996-12-10 23:23:01 +00003342static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003343posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003344{
Victor Stinner8c62be82010-05-06 00:08:46 +00003345 int sts;
3346 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3347 return NULL;
3348 _exit(sts);
3349 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003350}
3351
Martin v. Löwis114619e2002-10-07 06:44:21 +00003352#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3353static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003354free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003355{
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 Py_ssize_t i;
3357 for (i = 0; i < count; i++)
3358 PyMem_Free(array[i]);
3359 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003360}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003361
Antoine Pitrou69f71142009-05-24 21:25:49 +00003362static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003363int fsconvert_strdup(PyObject *o, char**out)
3364{
Victor Stinner8c62be82010-05-06 00:08:46 +00003365 PyObject *bytes;
3366 Py_ssize_t size;
3367 if (!PyUnicode_FSConverter(o, &bytes))
3368 return 0;
3369 size = PyBytes_GET_SIZE(bytes);
3370 *out = PyMem_Malloc(size+1);
3371 if (!*out)
3372 return 0;
3373 memcpy(*out, PyBytes_AsString(bytes), size+1);
3374 Py_DECREF(bytes);
3375 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003376}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003377#endif
3378
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003379
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003380#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003381PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003382"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003383Execute an executable path with arguments, replacing current process.\n\
3384\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003385 path: path of executable file\n\
3386 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003387
Barry Warsaw53699e91996-12-10 23:23:01 +00003388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003389posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003390{
Victor Stinner8c62be82010-05-06 00:08:46 +00003391 PyObject *opath;
3392 char *path;
3393 PyObject *argv;
3394 char **argvlist;
3395 Py_ssize_t i, argc;
3396 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003397
Victor Stinner8c62be82010-05-06 00:08:46 +00003398 /* execv has two arguments: (path, argv), where
3399 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003400
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 if (!PyArg_ParseTuple(args, "O&O:execv",
3402 PyUnicode_FSConverter,
3403 &opath, &argv))
3404 return NULL;
3405 path = PyBytes_AsString(opath);
3406 if (PyList_Check(argv)) {
3407 argc = PyList_Size(argv);
3408 getitem = PyList_GetItem;
3409 }
3410 else if (PyTuple_Check(argv)) {
3411 argc = PyTuple_Size(argv);
3412 getitem = PyTuple_GetItem;
3413 }
3414 else {
3415 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3416 Py_DECREF(opath);
3417 return NULL;
3418 }
3419 if (argc < 1) {
3420 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3421 Py_DECREF(opath);
3422 return NULL;
3423 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003424
Victor Stinner8c62be82010-05-06 00:08:46 +00003425 argvlist = PyMem_NEW(char *, argc+1);
3426 if (argvlist == NULL) {
3427 Py_DECREF(opath);
3428 return PyErr_NoMemory();
3429 }
3430 for (i = 0; i < argc; i++) {
3431 if (!fsconvert_strdup((*getitem)(argv, i),
3432 &argvlist[i])) {
3433 free_string_array(argvlist, i);
3434 PyErr_SetString(PyExc_TypeError,
3435 "execv() arg 2 must contain only strings");
3436 Py_DECREF(opath);
3437 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003438
Victor Stinner8c62be82010-05-06 00:08:46 +00003439 }
3440 }
3441 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003442
Victor Stinner8c62be82010-05-06 00:08:46 +00003443 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003444
Victor Stinner8c62be82010-05-06 00:08:46 +00003445 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003446
Victor Stinner8c62be82010-05-06 00:08:46 +00003447 free_string_array(argvlist, argc);
3448 Py_DECREF(opath);
3449 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003450}
3451
Victor Stinner13bb71c2010-04-23 21:41:56 +00003452static char**
3453parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3454{
Victor Stinner8c62be82010-05-06 00:08:46 +00003455 char **envlist;
3456 Py_ssize_t i, pos, envc;
3457 PyObject *keys=NULL, *vals=NULL;
3458 PyObject *key, *val, *key2, *val2;
3459 char *p, *k, *v;
3460 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003461
Victor Stinner8c62be82010-05-06 00:08:46 +00003462 i = PyMapping_Size(env);
3463 if (i < 0)
3464 return NULL;
3465 envlist = PyMem_NEW(char *, i + 1);
3466 if (envlist == NULL) {
3467 PyErr_NoMemory();
3468 return NULL;
3469 }
3470 envc = 0;
3471 keys = PyMapping_Keys(env);
3472 vals = PyMapping_Values(env);
3473 if (!keys || !vals)
3474 goto error;
3475 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3476 PyErr_Format(PyExc_TypeError,
3477 "env.keys() or env.values() is not a list");
3478 goto error;
3479 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003480
Victor Stinner8c62be82010-05-06 00:08:46 +00003481 for (pos = 0; pos < i; pos++) {
3482 key = PyList_GetItem(keys, pos);
3483 val = PyList_GetItem(vals, pos);
3484 if (!key || !val)
3485 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003486
Victor Stinner8c62be82010-05-06 00:08:46 +00003487 if (PyUnicode_FSConverter(key, &key2) == 0)
3488 goto error;
3489 if (PyUnicode_FSConverter(val, &val2) == 0) {
3490 Py_DECREF(key2);
3491 goto error;
3492 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003493
3494#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003495 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3496 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003497#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003498 k = PyBytes_AsString(key2);
3499 v = PyBytes_AsString(val2);
3500 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003501
Victor Stinner8c62be82010-05-06 00:08:46 +00003502 p = PyMem_NEW(char, len);
3503 if (p == NULL) {
3504 PyErr_NoMemory();
3505 Py_DECREF(key2);
3506 Py_DECREF(val2);
3507 goto error;
3508 }
3509 PyOS_snprintf(p, len, "%s=%s", k, v);
3510 envlist[envc++] = p;
3511 Py_DECREF(key2);
3512 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003513#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003515#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003516 }
3517 Py_DECREF(vals);
3518 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003519
Victor Stinner8c62be82010-05-06 00:08:46 +00003520 envlist[envc] = 0;
3521 *envc_ptr = envc;
3522 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003523
3524error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003525 Py_XDECREF(keys);
3526 Py_XDECREF(vals);
3527 while (--envc >= 0)
3528 PyMem_DEL(envlist[envc]);
3529 PyMem_DEL(envlist);
3530 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003531}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003533PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003534"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003535Execute a path with arguments and environment, replacing current process.\n\
3536\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003537 path: path of executable file\n\
3538 args: tuple or list of arguments\n\
3539 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003540
Barry Warsaw53699e91996-12-10 23:23:01 +00003541static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003542posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003543{
Victor Stinner8c62be82010-05-06 00:08:46 +00003544 PyObject *opath;
3545 char *path;
3546 PyObject *argv, *env;
3547 char **argvlist;
3548 char **envlist;
3549 Py_ssize_t i, argc, envc;
3550 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3551 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003552
Victor Stinner8c62be82010-05-06 00:08:46 +00003553 /* execve has three arguments: (path, argv, env), where
3554 argv is a list or tuple of strings and env is a dictionary
3555 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003556
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 if (!PyArg_ParseTuple(args, "O&OO:execve",
3558 PyUnicode_FSConverter,
3559 &opath, &argv, &env))
3560 return NULL;
3561 path = PyBytes_AsString(opath);
3562 if (PyList_Check(argv)) {
3563 argc = PyList_Size(argv);
3564 getitem = PyList_GetItem;
3565 }
3566 else if (PyTuple_Check(argv)) {
3567 argc = PyTuple_Size(argv);
3568 getitem = PyTuple_GetItem;
3569 }
3570 else {
3571 PyErr_SetString(PyExc_TypeError,
3572 "execve() arg 2 must be a tuple or list");
3573 goto fail_0;
3574 }
3575 if (!PyMapping_Check(env)) {
3576 PyErr_SetString(PyExc_TypeError,
3577 "execve() arg 3 must be a mapping object");
3578 goto fail_0;
3579 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003580
Victor Stinner8c62be82010-05-06 00:08:46 +00003581 argvlist = PyMem_NEW(char *, argc+1);
3582 if (argvlist == NULL) {
3583 PyErr_NoMemory();
3584 goto fail_0;
3585 }
3586 for (i = 0; i < argc; i++) {
3587 if (!fsconvert_strdup((*getitem)(argv, i),
3588 &argvlist[i]))
3589 {
3590 lastarg = i;
3591 goto fail_1;
3592 }
3593 }
3594 lastarg = argc;
3595 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003596
Victor Stinner8c62be82010-05-06 00:08:46 +00003597 envlist = parse_envlist(env, &envc);
3598 if (envlist == NULL)
3599 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003600
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003602
Victor Stinner8c62be82010-05-06 00:08:46 +00003603 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003604
Victor Stinner8c62be82010-05-06 00:08:46 +00003605 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003606
Victor Stinner8c62be82010-05-06 00:08:46 +00003607 while (--envc >= 0)
3608 PyMem_DEL(envlist[envc]);
3609 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003610 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003611 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003612 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003613 Py_DECREF(opath);
3614 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003615}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003616#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003618
Guido van Rossuma1065681999-01-25 23:20:23 +00003619#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003620PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003621"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003622Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003623\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003624 mode: mode of process creation\n\
3625 path: path of executable file\n\
3626 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003627
3628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003629posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003630{
Victor Stinner8c62be82010-05-06 00:08:46 +00003631 PyObject *opath;
3632 char *path;
3633 PyObject *argv;
3634 char **argvlist;
3635 int mode, i;
3636 Py_ssize_t argc;
3637 Py_intptr_t spawnval;
3638 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003639
Victor Stinner8c62be82010-05-06 00:08:46 +00003640 /* spawnv has three arguments: (mode, path, argv), where
3641 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003642
Victor Stinner8c62be82010-05-06 00:08:46 +00003643 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3644 PyUnicode_FSConverter,
3645 &opath, &argv))
3646 return NULL;
3647 path = PyBytes_AsString(opath);
3648 if (PyList_Check(argv)) {
3649 argc = PyList_Size(argv);
3650 getitem = PyList_GetItem;
3651 }
3652 else if (PyTuple_Check(argv)) {
3653 argc = PyTuple_Size(argv);
3654 getitem = PyTuple_GetItem;
3655 }
3656 else {
3657 PyErr_SetString(PyExc_TypeError,
3658 "spawnv() arg 2 must be a tuple or list");
3659 Py_DECREF(opath);
3660 return NULL;
3661 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003662
Victor Stinner8c62be82010-05-06 00:08:46 +00003663 argvlist = PyMem_NEW(char *, argc+1);
3664 if (argvlist == NULL) {
3665 Py_DECREF(opath);
3666 return PyErr_NoMemory();
3667 }
3668 for (i = 0; i < argc; i++) {
3669 if (!fsconvert_strdup((*getitem)(argv, i),
3670 &argvlist[i])) {
3671 free_string_array(argvlist, i);
3672 PyErr_SetString(
3673 PyExc_TypeError,
3674 "spawnv() arg 2 must contain only strings");
3675 Py_DECREF(opath);
3676 return NULL;
3677 }
3678 }
3679 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003680
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003681#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003682 Py_BEGIN_ALLOW_THREADS
3683 spawnval = spawnv(mode, path, argvlist);
3684 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003685#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003686 if (mode == _OLD_P_OVERLAY)
3687 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003688
Victor Stinner8c62be82010-05-06 00:08:46 +00003689 Py_BEGIN_ALLOW_THREADS
3690 spawnval = _spawnv(mode, path, argvlist);
3691 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003692#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003693
Victor Stinner8c62be82010-05-06 00:08:46 +00003694 free_string_array(argvlist, argc);
3695 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003696
Victor Stinner8c62be82010-05-06 00:08:46 +00003697 if (spawnval == -1)
3698 return posix_error();
3699 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003700#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003701 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003702#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003703 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003704#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003705}
3706
3707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003708PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003709"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003710Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003711\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 mode: mode of process creation\n\
3713 path: path of executable file\n\
3714 args: tuple or list of arguments\n\
3715 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003716
3717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003718posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003719{
Victor Stinner8c62be82010-05-06 00:08:46 +00003720 PyObject *opath;
3721 char *path;
3722 PyObject *argv, *env;
3723 char **argvlist;
3724 char **envlist;
3725 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003726 int mode;
3727 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003728 Py_intptr_t spawnval;
3729 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3730 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003731
Victor Stinner8c62be82010-05-06 00:08:46 +00003732 /* spawnve has four arguments: (mode, path, argv, env), where
3733 argv is a list or tuple of strings and env is a dictionary
3734 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003735
Victor Stinner8c62be82010-05-06 00:08:46 +00003736 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3737 PyUnicode_FSConverter,
3738 &opath, &argv, &env))
3739 return NULL;
3740 path = PyBytes_AsString(opath);
3741 if (PyList_Check(argv)) {
3742 argc = PyList_Size(argv);
3743 getitem = PyList_GetItem;
3744 }
3745 else if (PyTuple_Check(argv)) {
3746 argc = PyTuple_Size(argv);
3747 getitem = PyTuple_GetItem;
3748 }
3749 else {
3750 PyErr_SetString(PyExc_TypeError,
3751 "spawnve() arg 2 must be a tuple or list");
3752 goto fail_0;
3753 }
3754 if (!PyMapping_Check(env)) {
3755 PyErr_SetString(PyExc_TypeError,
3756 "spawnve() arg 3 must be a mapping object");
3757 goto fail_0;
3758 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003759
Victor Stinner8c62be82010-05-06 00:08:46 +00003760 argvlist = PyMem_NEW(char *, argc+1);
3761 if (argvlist == NULL) {
3762 PyErr_NoMemory();
3763 goto fail_0;
3764 }
3765 for (i = 0; i < argc; i++) {
3766 if (!fsconvert_strdup((*getitem)(argv, i),
3767 &argvlist[i]))
3768 {
3769 lastarg = i;
3770 goto fail_1;
3771 }
3772 }
3773 lastarg = argc;
3774 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003775
Victor Stinner8c62be82010-05-06 00:08:46 +00003776 envlist = parse_envlist(env, &envc);
3777 if (envlist == NULL)
3778 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003779
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003780#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003781 Py_BEGIN_ALLOW_THREADS
3782 spawnval = spawnve(mode, path, argvlist, envlist);
3783 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003784#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003785 if (mode == _OLD_P_OVERLAY)
3786 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003787
Victor Stinner8c62be82010-05-06 00:08:46 +00003788 Py_BEGIN_ALLOW_THREADS
3789 spawnval = _spawnve(mode, path, argvlist, envlist);
3790 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003791#endif
Tim Peters25059d32001-12-07 20:35:43 +00003792
Victor Stinner8c62be82010-05-06 00:08:46 +00003793 if (spawnval == -1)
3794 (void) posix_error();
3795 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003796#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003797 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003798#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003799 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003800#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003801
Victor Stinner8c62be82010-05-06 00:08:46 +00003802 while (--envc >= 0)
3803 PyMem_DEL(envlist[envc]);
3804 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003805 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003806 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003807 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003808 Py_DECREF(opath);
3809 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003810}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003811
3812/* OS/2 supports spawnvp & spawnvpe natively */
3813#if defined(PYOS_OS2)
3814PyDoc_STRVAR(posix_spawnvp__doc__,
3815"spawnvp(mode, file, args)\n\n\
3816Execute the program 'file' in a new process, using the environment\n\
3817search path to find the file.\n\
3818\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 mode: mode of process creation\n\
3820 file: executable file name\n\
3821 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003822
3823static PyObject *
3824posix_spawnvp(PyObject *self, PyObject *args)
3825{
Victor Stinner8c62be82010-05-06 00:08:46 +00003826 PyObject *opath;
3827 char *path;
3828 PyObject *argv;
3829 char **argvlist;
3830 int mode, i, argc;
3831 Py_intptr_t spawnval;
3832 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003833
Victor Stinner8c62be82010-05-06 00:08:46 +00003834 /* spawnvp has three arguments: (mode, path, argv), where
3835 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003836
Victor Stinner8c62be82010-05-06 00:08:46 +00003837 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3838 PyUnicode_FSConverter,
3839 &opath, &argv))
3840 return NULL;
3841 path = PyBytes_AsString(opath);
3842 if (PyList_Check(argv)) {
3843 argc = PyList_Size(argv);
3844 getitem = PyList_GetItem;
3845 }
3846 else if (PyTuple_Check(argv)) {
3847 argc = PyTuple_Size(argv);
3848 getitem = PyTuple_GetItem;
3849 }
3850 else {
3851 PyErr_SetString(PyExc_TypeError,
3852 "spawnvp() arg 2 must be a tuple or list");
3853 Py_DECREF(opath);
3854 return NULL;
3855 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003856
Victor Stinner8c62be82010-05-06 00:08:46 +00003857 argvlist = PyMem_NEW(char *, argc+1);
3858 if (argvlist == NULL) {
3859 Py_DECREF(opath);
3860 return PyErr_NoMemory();
3861 }
3862 for (i = 0; i < argc; i++) {
3863 if (!fsconvert_strdup((*getitem)(argv, i),
3864 &argvlist[i])) {
3865 free_string_array(argvlist, i);
3866 PyErr_SetString(
3867 PyExc_TypeError,
3868 "spawnvp() arg 2 must contain only strings");
3869 Py_DECREF(opath);
3870 return NULL;
3871 }
3872 }
3873 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003874
Victor Stinner8c62be82010-05-06 00:08:46 +00003875 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003876#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003877 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003878#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003879 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003880#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003881 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003882
Victor Stinner8c62be82010-05-06 00:08:46 +00003883 free_string_array(argvlist, argc);
3884 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003885
Victor Stinner8c62be82010-05-06 00:08:46 +00003886 if (spawnval == -1)
3887 return posix_error();
3888 else
3889 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003890}
3891
3892
3893PyDoc_STRVAR(posix_spawnvpe__doc__,
3894"spawnvpe(mode, file, args, env)\n\n\
3895Execute the program 'file' in a new process, using the environment\n\
3896search path to find the file.\n\
3897\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003898 mode: mode of process creation\n\
3899 file: executable file name\n\
3900 args: tuple or list of arguments\n\
3901 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003902
3903static PyObject *
3904posix_spawnvpe(PyObject *self, PyObject *args)
3905{
Victor Stinner8c62be82010-05-06 00:08:46 +00003906 PyObject *opath
3907 char *path;
3908 PyObject *argv, *env;
3909 char **argvlist;
3910 char **envlist;
3911 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003912 int mode;
3913 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003914 Py_intptr_t spawnval;
3915 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3916 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003917
Victor Stinner8c62be82010-05-06 00:08:46 +00003918 /* spawnvpe has four arguments: (mode, path, argv, env), where
3919 argv is a list or tuple of strings and env is a dictionary
3920 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003921
Victor Stinner8c62be82010-05-06 00:08:46 +00003922 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3923 PyUnicode_FSConverter,
3924 &opath, &argv, &env))
3925 return NULL;
3926 path = PyBytes_AsString(opath);
3927 if (PyList_Check(argv)) {
3928 argc = PyList_Size(argv);
3929 getitem = PyList_GetItem;
3930 }
3931 else if (PyTuple_Check(argv)) {
3932 argc = PyTuple_Size(argv);
3933 getitem = PyTuple_GetItem;
3934 }
3935 else {
3936 PyErr_SetString(PyExc_TypeError,
3937 "spawnvpe() arg 2 must be a tuple or list");
3938 goto fail_0;
3939 }
3940 if (!PyMapping_Check(env)) {
3941 PyErr_SetString(PyExc_TypeError,
3942 "spawnvpe() arg 3 must be a mapping object");
3943 goto fail_0;
3944 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003945
Victor Stinner8c62be82010-05-06 00:08:46 +00003946 argvlist = PyMem_NEW(char *, argc+1);
3947 if (argvlist == NULL) {
3948 PyErr_NoMemory();
3949 goto fail_0;
3950 }
3951 for (i = 0; i < argc; i++) {
3952 if (!fsconvert_strdup((*getitem)(argv, i),
3953 &argvlist[i]))
3954 {
3955 lastarg = i;
3956 goto fail_1;
3957 }
3958 }
3959 lastarg = argc;
3960 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003961
Victor Stinner8c62be82010-05-06 00:08:46 +00003962 envlist = parse_envlist(env, &envc);
3963 if (envlist == NULL)
3964 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003965
Victor Stinner8c62be82010-05-06 00:08:46 +00003966 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003967#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003968 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003969#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003970 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003971#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003972 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003973
Victor Stinner8c62be82010-05-06 00:08:46 +00003974 if (spawnval == -1)
3975 (void) posix_error();
3976 else
3977 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003978
Victor Stinner8c62be82010-05-06 00:08:46 +00003979 while (--envc >= 0)
3980 PyMem_DEL(envlist[envc]);
3981 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003982 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003983 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003984 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003985 Py_DECREF(opath);
3986 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003987}
3988#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003989#endif /* HAVE_SPAWNV */
3990
3991
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003992#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003993PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003994"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003995Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3996\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003997Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003998
3999static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004000posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004001{
Victor Stinner8c62be82010-05-06 00:08:46 +00004002 pid_t pid;
4003 int result = 0;
4004 _PyImport_AcquireLock();
4005 pid = fork1();
4006 if (pid == 0) {
4007 /* child: this clobbers and resets the import lock. */
4008 PyOS_AfterFork();
4009 } else {
4010 /* parent: release the import lock. */
4011 result = _PyImport_ReleaseLock();
4012 }
4013 if (pid == -1)
4014 return posix_error();
4015 if (result < 0) {
4016 /* Don't clobber the OSError if the fork failed. */
4017 PyErr_SetString(PyExc_RuntimeError,
4018 "not holding the import lock");
4019 return NULL;
4020 }
4021 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004022}
4023#endif
4024
4025
Guido van Rossumad0ee831995-03-01 10:34:45 +00004026#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004027PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004028"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004029Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004030Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004031
Barry Warsaw53699e91996-12-10 23:23:01 +00004032static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004033posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004034{
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 pid_t pid;
4036 int result = 0;
4037 _PyImport_AcquireLock();
4038 pid = fork();
4039 if (pid == 0) {
4040 /* child: this clobbers and resets the import lock. */
4041 PyOS_AfterFork();
4042 } else {
4043 /* parent: release the import lock. */
4044 result = _PyImport_ReleaseLock();
4045 }
4046 if (pid == -1)
4047 return posix_error();
4048 if (result < 0) {
4049 /* Don't clobber the OSError if the fork failed. */
4050 PyErr_SetString(PyExc_RuntimeError,
4051 "not holding the import lock");
4052 return NULL;
4053 }
4054 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004055}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004056#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004057
Neal Norwitzb59798b2003-03-21 01:43:31 +00004058/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004059/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4060#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004061#define DEV_PTY_FILE "/dev/ptc"
4062#define HAVE_DEV_PTMX
4063#else
4064#define DEV_PTY_FILE "/dev/ptmx"
4065#endif
4066
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004067#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004068#ifdef HAVE_PTY_H
4069#include <pty.h>
4070#else
4071#ifdef HAVE_LIBUTIL_H
4072#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004073#else
4074#ifdef HAVE_UTIL_H
4075#include <util.h>
4076#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004077#endif /* HAVE_LIBUTIL_H */
4078#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004079#ifdef HAVE_STROPTS_H
4080#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004081#endif
4082#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004083
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004084#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004085PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004086"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004087Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004088
4089static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004090posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004091{
Victor Stinner8c62be82010-05-06 00:08:46 +00004092 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004093#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004094 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004095#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004096#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004097 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004098#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004099 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004100#endif
4101#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004102
Thomas Wouters70c21a12000-07-14 14:28:33 +00004103#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004104 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4105 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004106#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004107 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4108 if (slave_name == NULL)
4109 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004110
Victor Stinner8c62be82010-05-06 00:08:46 +00004111 slave_fd = open(slave_name, O_RDWR);
4112 if (slave_fd < 0)
4113 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004114#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004115 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4116 if (master_fd < 0)
4117 return posix_error();
4118 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4119 /* change permission of slave */
4120 if (grantpt(master_fd) < 0) {
4121 PyOS_setsig(SIGCHLD, sig_saved);
4122 return posix_error();
4123 }
4124 /* unlock slave */
4125 if (unlockpt(master_fd) < 0) {
4126 PyOS_setsig(SIGCHLD, sig_saved);
4127 return posix_error();
4128 }
4129 PyOS_setsig(SIGCHLD, sig_saved);
4130 slave_name = ptsname(master_fd); /* get name of slave */
4131 if (slave_name == NULL)
4132 return posix_error();
4133 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4134 if (slave_fd < 0)
4135 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004136#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004137 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4138 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004139#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004140 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004141#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004142#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004143#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004144
Victor Stinner8c62be82010-05-06 00:08:46 +00004145 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004146
Fred Drake8cef4cf2000-06-28 16:40:38 +00004147}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004148#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004149
4150#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004151PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004152"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004153Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4154Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004155To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004156
4157static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004158posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004159{
Victor Stinner8c62be82010-05-06 00:08:46 +00004160 int master_fd = -1, result = 0;
4161 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004162
Victor Stinner8c62be82010-05-06 00:08:46 +00004163 _PyImport_AcquireLock();
4164 pid = forkpty(&master_fd, NULL, NULL, NULL);
4165 if (pid == 0) {
4166 /* child: this clobbers and resets the import lock. */
4167 PyOS_AfterFork();
4168 } else {
4169 /* parent: release the import lock. */
4170 result = _PyImport_ReleaseLock();
4171 }
4172 if (pid == -1)
4173 return posix_error();
4174 if (result < 0) {
4175 /* Don't clobber the OSError if the fork failed. */
4176 PyErr_SetString(PyExc_RuntimeError,
4177 "not holding the import lock");
4178 return NULL;
4179 }
4180 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004181}
4182#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004183
Guido van Rossumad0ee831995-03-01 10:34:45 +00004184#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004185PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004186"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004187Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004188
Barry Warsaw53699e91996-12-10 23:23:01 +00004189static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004190posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004191{
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004193}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004194#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004195
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004196
Guido van Rossumad0ee831995-03-01 10:34:45 +00004197#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004198PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004199"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004200Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004201
Barry Warsaw53699e91996-12-10 23:23:01 +00004202static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004203posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004204{
Victor Stinner8c62be82010-05-06 00:08:46 +00004205 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004206}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004207#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004209
Guido van Rossumad0ee831995-03-01 10:34:45 +00004210#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004211PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004212"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004213Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004214
Barry Warsaw53699e91996-12-10 23:23:01 +00004215static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004216posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004217{
Victor Stinner8c62be82010-05-06 00:08:46 +00004218 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004219}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004220#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004223PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004224"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004225Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004226
Barry Warsaw53699e91996-12-10 23:23:01 +00004227static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004228posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004229{
Victor Stinner8c62be82010-05-06 00:08:46 +00004230 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004231}
4232
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004233
Fred Drakec9680921999-12-13 16:37:25 +00004234#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004235PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004236"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004237Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004238
4239static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004240posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004241{
4242 PyObject *result = NULL;
4243
Fred Drakec9680921999-12-13 16:37:25 +00004244#ifdef NGROUPS_MAX
4245#define MAX_GROUPS NGROUPS_MAX
4246#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004247 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004248#define MAX_GROUPS 64
4249#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004250 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004251
4252 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4253 * This is a helper variable to store the intermediate result when
4254 * that happens.
4255 *
4256 * To keep the code readable the OSX behaviour is unconditional,
4257 * according to the POSIX spec this should be safe on all unix-y
4258 * systems.
4259 */
4260 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004261 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004262
Victor Stinner8c62be82010-05-06 00:08:46 +00004263 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004264 if (n < 0) {
4265 if (errno == EINVAL) {
4266 n = getgroups(0, NULL);
4267 if (n == -1) {
4268 return posix_error();
4269 }
4270 if (n == 0) {
4271 /* Avoid malloc(0) */
4272 alt_grouplist = grouplist;
4273 } else {
4274 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4275 if (alt_grouplist == NULL) {
4276 errno = EINVAL;
4277 return posix_error();
4278 }
4279 n = getgroups(n, alt_grouplist);
4280 if (n == -1) {
4281 PyMem_Free(alt_grouplist);
4282 return posix_error();
4283 }
4284 }
4285 } else {
4286 return posix_error();
4287 }
4288 }
4289 result = PyList_New(n);
4290 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004291 int i;
4292 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004293 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004294 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004295 Py_DECREF(result);
4296 result = NULL;
4297 break;
Fred Drakec9680921999-12-13 16:37:25 +00004298 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004299 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004300 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004301 }
4302
4303 if (alt_grouplist != grouplist) {
4304 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004305 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004306
Fred Drakec9680921999-12-13 16:37:25 +00004307 return result;
4308}
4309#endif
4310
Antoine Pitroub7572f02009-12-02 20:46:48 +00004311#ifdef HAVE_INITGROUPS
4312PyDoc_STRVAR(posix_initgroups__doc__,
4313"initgroups(username, gid) -> None\n\n\
4314Call the system initgroups() to initialize the group access list with all of\n\
4315the groups of which the specified username is a member, plus the specified\n\
4316group id.");
4317
4318static PyObject *
4319posix_initgroups(PyObject *self, PyObject *args)
4320{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004321 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004322 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004323 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004324 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004325
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004326 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4327 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004328 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004329 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004330
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004331 res = initgroups(username, (gid_t) gid);
4332 Py_DECREF(oname);
4333 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004334 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004335
Victor Stinner8c62be82010-05-06 00:08:46 +00004336 Py_INCREF(Py_None);
4337 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004338}
4339#endif
4340
Martin v. Löwis606edc12002-06-13 21:09:11 +00004341#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004342PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004343"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004344Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004345
4346static PyObject *
4347posix_getpgid(PyObject *self, PyObject *args)
4348{
Victor Stinner8c62be82010-05-06 00:08:46 +00004349 pid_t pid, pgid;
4350 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4351 return NULL;
4352 pgid = getpgid(pid);
4353 if (pgid < 0)
4354 return posix_error();
4355 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004356}
4357#endif /* HAVE_GETPGID */
4358
4359
Guido van Rossumb6775db1994-08-01 11:34:53 +00004360#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004361PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004362"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004363Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004364
Barry Warsaw53699e91996-12-10 23:23:01 +00004365static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004366posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004367{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004368#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004369 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004370#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004372#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004373}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004374#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004375
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004376
Guido van Rossumb6775db1994-08-01 11:34:53 +00004377#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004378PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004379"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004380Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004381
Barry Warsaw53699e91996-12-10 23:23:01 +00004382static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004383posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004384{
Guido van Rossum64933891994-10-20 21:56:42 +00004385#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004386 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004387#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004388 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004389#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 return posix_error();
4391 Py_INCREF(Py_None);
4392 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004393}
4394
Guido van Rossumb6775db1994-08-01 11:34:53 +00004395#endif /* HAVE_SETPGRP */
4396
Guido van Rossumad0ee831995-03-01 10:34:45 +00004397#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004398
4399#ifdef MS_WINDOWS
4400#include <tlhelp32.h>
4401
4402static PyObject*
4403win32_getppid()
4404{
4405 HANDLE snapshot;
4406 pid_t mypid;
4407 PyObject* result = NULL;
4408 BOOL have_record;
4409 PROCESSENTRY32 pe;
4410
4411 mypid = getpid(); /* This function never fails */
4412
4413 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4414 if (snapshot == INVALID_HANDLE_VALUE)
4415 return PyErr_SetFromWindowsErr(GetLastError());
4416
4417 pe.dwSize = sizeof(pe);
4418 have_record = Process32First(snapshot, &pe);
4419 while (have_record) {
4420 if (mypid == (pid_t)pe.th32ProcessID) {
4421 /* We could cache the ulong value in a static variable. */
4422 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4423 break;
4424 }
4425
4426 have_record = Process32Next(snapshot, &pe);
4427 }
4428
4429 /* If our loop exits and our pid was not found (result will be NULL)
4430 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4431 * error anyway, so let's raise it. */
4432 if (!result)
4433 result = PyErr_SetFromWindowsErr(GetLastError());
4434
4435 CloseHandle(snapshot);
4436
4437 return result;
4438}
4439#endif /*MS_WINDOWS*/
4440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004441PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004442"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004443Return the parent's process id. If the parent process has already exited,\n\
4444Windows machines will still return its id; others systems will return the id\n\
4445of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004446
Barry Warsaw53699e91996-12-10 23:23:01 +00004447static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004448posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004449{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004450#ifdef MS_WINDOWS
4451 return win32_getppid();
4452#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004453 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004454#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004455}
4456#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004457
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004458
Fred Drake12c6e2d1999-12-14 21:25:03 +00004459#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004460PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004461"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004462Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004463
4464static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004465posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004466{
Victor Stinner8c62be82010-05-06 00:08:46 +00004467 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004468#ifdef MS_WINDOWS
4469 wchar_t user_name[UNLEN + 1];
4470 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4471
4472 if (GetUserNameW(user_name, &num_chars)) {
4473 /* num_chars is the number of unicode chars plus null terminator */
4474 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4475 }
4476 else
4477 result = PyErr_SetFromWindowsErr(GetLastError());
4478#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004479 char *name;
4480 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004481
Victor Stinner8c62be82010-05-06 00:08:46 +00004482 errno = 0;
4483 name = getlogin();
4484 if (name == NULL) {
4485 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004486 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004487 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004488 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004489 }
4490 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004491 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004492 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004493#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004494 return result;
4495}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004496#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004497
Guido van Rossumad0ee831995-03-01 10:34:45 +00004498#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004499PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004500"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004501Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004502
Barry Warsaw53699e91996-12-10 23:23:01 +00004503static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004504posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004505{
Victor Stinner8c62be82010-05-06 00:08:46 +00004506 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004507}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004508#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004509
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004510
Guido van Rossumad0ee831995-03-01 10:34:45 +00004511#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004512PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004513"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004514Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004515
Barry Warsaw53699e91996-12-10 23:23:01 +00004516static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004517posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004518{
Victor Stinner8c62be82010-05-06 00:08:46 +00004519 pid_t pid;
4520 int sig;
4521 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4522 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004523#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004524 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4525 APIRET rc;
4526 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004527 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004528
4529 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4530 APIRET rc;
4531 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004532 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004533
4534 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004535 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004536#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004537 if (kill(pid, sig) == -1)
4538 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004539#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004540 Py_INCREF(Py_None);
4541 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004542}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004543#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004544
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004545#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004546PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004547"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004548Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004549
4550static PyObject *
4551posix_killpg(PyObject *self, PyObject *args)
4552{
Victor Stinner8c62be82010-05-06 00:08:46 +00004553 int sig;
4554 pid_t pgid;
4555 /* XXX some man pages make the `pgid` parameter an int, others
4556 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4557 take the same type. Moreover, pid_t is always at least as wide as
4558 int (else compilation of this module fails), which is safe. */
4559 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4560 return NULL;
4561 if (killpg(pgid, sig) == -1)
4562 return posix_error();
4563 Py_INCREF(Py_None);
4564 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004565}
4566#endif
4567
Brian Curtineb24d742010-04-12 17:16:38 +00004568#ifdef MS_WINDOWS
4569PyDoc_STRVAR(win32_kill__doc__,
4570"kill(pid, sig)\n\n\
4571Kill a process with a signal.");
4572
4573static PyObject *
4574win32_kill(PyObject *self, PyObject *args)
4575{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004576 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004577 DWORD pid, sig, err;
4578 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004579
Victor Stinner8c62be82010-05-06 00:08:46 +00004580 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4581 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004582
Victor Stinner8c62be82010-05-06 00:08:46 +00004583 /* Console processes which share a common console can be sent CTRL+C or
4584 CTRL+BREAK events, provided they handle said events. */
4585 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4586 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4587 err = GetLastError();
4588 PyErr_SetFromWindowsErr(err);
4589 }
4590 else
4591 Py_RETURN_NONE;
4592 }
Brian Curtineb24d742010-04-12 17:16:38 +00004593
Victor Stinner8c62be82010-05-06 00:08:46 +00004594 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4595 attempt to open and terminate the process. */
4596 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4597 if (handle == NULL) {
4598 err = GetLastError();
4599 return PyErr_SetFromWindowsErr(err);
4600 }
Brian Curtineb24d742010-04-12 17:16:38 +00004601
Victor Stinner8c62be82010-05-06 00:08:46 +00004602 if (TerminateProcess(handle, sig) == 0) {
4603 err = GetLastError();
4604 result = PyErr_SetFromWindowsErr(err);
4605 } else {
4606 Py_INCREF(Py_None);
4607 result = Py_None;
4608 }
Brian Curtineb24d742010-04-12 17:16:38 +00004609
Victor Stinner8c62be82010-05-06 00:08:46 +00004610 CloseHandle(handle);
4611 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004612}
4613#endif /* MS_WINDOWS */
4614
Guido van Rossumc0125471996-06-28 18:55:32 +00004615#ifdef HAVE_PLOCK
4616
4617#ifdef HAVE_SYS_LOCK_H
4618#include <sys/lock.h>
4619#endif
4620
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004621PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004622"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004623Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Barry Warsaw53699e91996-12-10 23:23:01 +00004625static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004626posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004627{
Victor Stinner8c62be82010-05-06 00:08:46 +00004628 int op;
4629 if (!PyArg_ParseTuple(args, "i:plock", &op))
4630 return NULL;
4631 if (plock(op) == -1)
4632 return posix_error();
4633 Py_INCREF(Py_None);
4634 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004635}
4636#endif
4637
Guido van Rossumb6775db1994-08-01 11:34:53 +00004638#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004639PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004640"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004641Set the current process's user id.");
4642
Barry Warsaw53699e91996-12-10 23:23:01 +00004643static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004644posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004645{
Victor Stinner8c62be82010-05-06 00:08:46 +00004646 long uid_arg;
4647 uid_t uid;
4648 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4649 return NULL;
4650 uid = uid_arg;
4651 if (uid != uid_arg) {
4652 PyErr_SetString(PyExc_OverflowError, "user id too big");
4653 return NULL;
4654 }
4655 if (setuid(uid) < 0)
4656 return posix_error();
4657 Py_INCREF(Py_None);
4658 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004659}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004660#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004661
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004662
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004663#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004664PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004665"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004666Set the current process's effective user id.");
4667
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004668static PyObject *
4669posix_seteuid (PyObject *self, PyObject *args)
4670{
Victor Stinner8c62be82010-05-06 00:08:46 +00004671 long euid_arg;
4672 uid_t euid;
4673 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4674 return NULL;
4675 euid = euid_arg;
4676 if (euid != euid_arg) {
4677 PyErr_SetString(PyExc_OverflowError, "user id too big");
4678 return NULL;
4679 }
4680 if (seteuid(euid) < 0) {
4681 return posix_error();
4682 } else {
4683 Py_INCREF(Py_None);
4684 return Py_None;
4685 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004686}
4687#endif /* HAVE_SETEUID */
4688
4689#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004690PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004691"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004692Set the current process's effective group id.");
4693
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004694static PyObject *
4695posix_setegid (PyObject *self, PyObject *args)
4696{
Victor Stinner8c62be82010-05-06 00:08:46 +00004697 long egid_arg;
4698 gid_t egid;
4699 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4700 return NULL;
4701 egid = egid_arg;
4702 if (egid != egid_arg) {
4703 PyErr_SetString(PyExc_OverflowError, "group id too big");
4704 return NULL;
4705 }
4706 if (setegid(egid) < 0) {
4707 return posix_error();
4708 } else {
4709 Py_INCREF(Py_None);
4710 return Py_None;
4711 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004712}
4713#endif /* HAVE_SETEGID */
4714
4715#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004716PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004717"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004718Set the current process's real and effective user ids.");
4719
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004720static PyObject *
4721posix_setreuid (PyObject *self, PyObject *args)
4722{
Victor Stinner8c62be82010-05-06 00:08:46 +00004723 long ruid_arg, euid_arg;
4724 uid_t ruid, euid;
4725 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4726 return NULL;
4727 if (ruid_arg == -1)
4728 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4729 else
4730 ruid = ruid_arg; /* otherwise, assign from our long */
4731 if (euid_arg == -1)
4732 euid = (uid_t)-1;
4733 else
4734 euid = euid_arg;
4735 if ((euid_arg != -1 && euid != euid_arg) ||
4736 (ruid_arg != -1 && ruid != ruid_arg)) {
4737 PyErr_SetString(PyExc_OverflowError, "user id too big");
4738 return NULL;
4739 }
4740 if (setreuid(ruid, euid) < 0) {
4741 return posix_error();
4742 } else {
4743 Py_INCREF(Py_None);
4744 return Py_None;
4745 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004746}
4747#endif /* HAVE_SETREUID */
4748
4749#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004750PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004751"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004752Set the current process's real and effective group ids.");
4753
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004754static PyObject *
4755posix_setregid (PyObject *self, PyObject *args)
4756{
Victor Stinner8c62be82010-05-06 00:08:46 +00004757 long rgid_arg, egid_arg;
4758 gid_t rgid, egid;
4759 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4760 return NULL;
4761 if (rgid_arg == -1)
4762 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4763 else
4764 rgid = rgid_arg; /* otherwise, assign from our long */
4765 if (egid_arg == -1)
4766 egid = (gid_t)-1;
4767 else
4768 egid = egid_arg;
4769 if ((egid_arg != -1 && egid != egid_arg) ||
4770 (rgid_arg != -1 && rgid != rgid_arg)) {
4771 PyErr_SetString(PyExc_OverflowError, "group id too big");
4772 return NULL;
4773 }
4774 if (setregid(rgid, egid) < 0) {
4775 return posix_error();
4776 } else {
4777 Py_INCREF(Py_None);
4778 return Py_None;
4779 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004780}
4781#endif /* HAVE_SETREGID */
4782
Guido van Rossumb6775db1994-08-01 11:34:53 +00004783#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004784PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004785"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004786Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004787
Barry Warsaw53699e91996-12-10 23:23:01 +00004788static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004789posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004790{
Victor Stinner8c62be82010-05-06 00:08:46 +00004791 long gid_arg;
4792 gid_t gid;
4793 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4794 return NULL;
4795 gid = gid_arg;
4796 if (gid != gid_arg) {
4797 PyErr_SetString(PyExc_OverflowError, "group id too big");
4798 return NULL;
4799 }
4800 if (setgid(gid) < 0)
4801 return posix_error();
4802 Py_INCREF(Py_None);
4803 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004804}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004805#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004806
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004807#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004808PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004809"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004810Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004811
4812static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004813posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004814{
Victor Stinner8c62be82010-05-06 00:08:46 +00004815 int i, len;
4816 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004817
Victor Stinner8c62be82010-05-06 00:08:46 +00004818 if (!PySequence_Check(groups)) {
4819 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4820 return NULL;
4821 }
4822 len = PySequence_Size(groups);
4823 if (len > MAX_GROUPS) {
4824 PyErr_SetString(PyExc_ValueError, "too many groups");
4825 return NULL;
4826 }
4827 for(i = 0; i < len; i++) {
4828 PyObject *elem;
4829 elem = PySequence_GetItem(groups, i);
4830 if (!elem)
4831 return NULL;
4832 if (!PyLong_Check(elem)) {
4833 PyErr_SetString(PyExc_TypeError,
4834 "groups must be integers");
4835 Py_DECREF(elem);
4836 return NULL;
4837 } else {
4838 unsigned long x = PyLong_AsUnsignedLong(elem);
4839 if (PyErr_Occurred()) {
4840 PyErr_SetString(PyExc_TypeError,
4841 "group id too big");
4842 Py_DECREF(elem);
4843 return NULL;
4844 }
4845 grouplist[i] = x;
4846 /* read back the value to see if it fitted in gid_t */
4847 if (grouplist[i] != x) {
4848 PyErr_SetString(PyExc_TypeError,
4849 "group id too big");
4850 Py_DECREF(elem);
4851 return NULL;
4852 }
4853 }
4854 Py_DECREF(elem);
4855 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004856
Victor Stinner8c62be82010-05-06 00:08:46 +00004857 if (setgroups(len, grouplist) < 0)
4858 return posix_error();
4859 Py_INCREF(Py_None);
4860 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004861}
4862#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004863
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004864#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4865static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004866wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004867{
Victor Stinner8c62be82010-05-06 00:08:46 +00004868 PyObject *result;
4869 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004870
Victor Stinner8c62be82010-05-06 00:08:46 +00004871 if (pid == -1)
4872 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004873
Victor Stinner8c62be82010-05-06 00:08:46 +00004874 if (struct_rusage == NULL) {
4875 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4876 if (m == NULL)
4877 return NULL;
4878 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4879 Py_DECREF(m);
4880 if (struct_rusage == NULL)
4881 return NULL;
4882 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004883
Victor Stinner8c62be82010-05-06 00:08:46 +00004884 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4885 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4886 if (!result)
4887 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004888
4889#ifndef doubletime
4890#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4891#endif
4892
Victor Stinner8c62be82010-05-06 00:08:46 +00004893 PyStructSequence_SET_ITEM(result, 0,
4894 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4895 PyStructSequence_SET_ITEM(result, 1,
4896 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004897#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004898 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4899 SET_INT(result, 2, ru->ru_maxrss);
4900 SET_INT(result, 3, ru->ru_ixrss);
4901 SET_INT(result, 4, ru->ru_idrss);
4902 SET_INT(result, 5, ru->ru_isrss);
4903 SET_INT(result, 6, ru->ru_minflt);
4904 SET_INT(result, 7, ru->ru_majflt);
4905 SET_INT(result, 8, ru->ru_nswap);
4906 SET_INT(result, 9, ru->ru_inblock);
4907 SET_INT(result, 10, ru->ru_oublock);
4908 SET_INT(result, 11, ru->ru_msgsnd);
4909 SET_INT(result, 12, ru->ru_msgrcv);
4910 SET_INT(result, 13, ru->ru_nsignals);
4911 SET_INT(result, 14, ru->ru_nvcsw);
4912 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004913#undef SET_INT
4914
Victor Stinner8c62be82010-05-06 00:08:46 +00004915 if (PyErr_Occurred()) {
4916 Py_DECREF(result);
4917 return NULL;
4918 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004919
Victor Stinner8c62be82010-05-06 00:08:46 +00004920 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004921}
4922#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4923
4924#ifdef HAVE_WAIT3
4925PyDoc_STRVAR(posix_wait3__doc__,
4926"wait3(options) -> (pid, status, rusage)\n\n\
4927Wait for completion of a child process.");
4928
4929static PyObject *
4930posix_wait3(PyObject *self, PyObject *args)
4931{
Victor Stinner8c62be82010-05-06 00:08:46 +00004932 pid_t pid;
4933 int options;
4934 struct rusage ru;
4935 WAIT_TYPE status;
4936 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004937
Victor Stinner8c62be82010-05-06 00:08:46 +00004938 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4939 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004940
Victor Stinner8c62be82010-05-06 00:08:46 +00004941 Py_BEGIN_ALLOW_THREADS
4942 pid = wait3(&status, options, &ru);
4943 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004944
Victor Stinner8c62be82010-05-06 00:08:46 +00004945 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004946}
4947#endif /* HAVE_WAIT3 */
4948
4949#ifdef HAVE_WAIT4
4950PyDoc_STRVAR(posix_wait4__doc__,
4951"wait4(pid, options) -> (pid, status, rusage)\n\n\
4952Wait for completion of a given child process.");
4953
4954static PyObject *
4955posix_wait4(PyObject *self, PyObject *args)
4956{
Victor Stinner8c62be82010-05-06 00:08:46 +00004957 pid_t pid;
4958 int options;
4959 struct rusage ru;
4960 WAIT_TYPE status;
4961 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004962
Victor Stinner8c62be82010-05-06 00:08:46 +00004963 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4964 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004965
Victor Stinner8c62be82010-05-06 00:08:46 +00004966 Py_BEGIN_ALLOW_THREADS
4967 pid = wait4(pid, &status, options, &ru);
4968 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004969
Victor Stinner8c62be82010-05-06 00:08:46 +00004970 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004971}
4972#endif /* HAVE_WAIT4 */
4973
Guido van Rossumb6775db1994-08-01 11:34:53 +00004974#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004975PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004976"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004977Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004978
Barry Warsaw53699e91996-12-10 23:23:01 +00004979static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004980posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004981{
Victor Stinner8c62be82010-05-06 00:08:46 +00004982 pid_t pid;
4983 int options;
4984 WAIT_TYPE status;
4985 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004986
Victor Stinner8c62be82010-05-06 00:08:46 +00004987 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4988 return NULL;
4989 Py_BEGIN_ALLOW_THREADS
4990 pid = waitpid(pid, &status, options);
4991 Py_END_ALLOW_THREADS
4992 if (pid == -1)
4993 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004994
Victor Stinner8c62be82010-05-06 00:08:46 +00004995 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004996}
4997
Tim Petersab034fa2002-02-01 11:27:43 +00004998#elif defined(HAVE_CWAIT)
4999
5000/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005001PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005002"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005003"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005004
5005static PyObject *
5006posix_waitpid(PyObject *self, PyObject *args)
5007{
Victor Stinner8c62be82010-05-06 00:08:46 +00005008 Py_intptr_t pid;
5009 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005010
Victor Stinner8c62be82010-05-06 00:08:46 +00005011 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5012 return NULL;
5013 Py_BEGIN_ALLOW_THREADS
5014 pid = _cwait(&status, pid, options);
5015 Py_END_ALLOW_THREADS
5016 if (pid == -1)
5017 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005018
Victor Stinner8c62be82010-05-06 00:08:46 +00005019 /* shift the status left a byte so this is more like the POSIX waitpid */
5020 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005021}
5022#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005023
Guido van Rossumad0ee831995-03-01 10:34:45 +00005024#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005026"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005027Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005028
Barry Warsaw53699e91996-12-10 23:23:01 +00005029static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005030posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005031{
Victor Stinner8c62be82010-05-06 00:08:46 +00005032 pid_t pid;
5033 WAIT_TYPE status;
5034 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005035
Victor Stinner8c62be82010-05-06 00:08:46 +00005036 Py_BEGIN_ALLOW_THREADS
5037 pid = wait(&status);
5038 Py_END_ALLOW_THREADS
5039 if (pid == -1)
5040 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005041
Victor Stinner8c62be82010-05-06 00:08:46 +00005042 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005043}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005044#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005045
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005047PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005048"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005049Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005050
Barry Warsaw53699e91996-12-10 23:23:01 +00005051static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005052posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005053{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005054#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005055 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005056#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005057#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005058 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5059 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005060#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005061 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005062#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005063#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005064}
5065
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005066
Guido van Rossumb6775db1994-08-01 11:34:53 +00005067#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005068PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005069"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005070Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005071
Barry Warsaw53699e91996-12-10 23:23:01 +00005072static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005073posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005074{
Victor Stinner8c62be82010-05-06 00:08:46 +00005075 PyObject* v;
5076 char buf[MAXPATHLEN];
5077 PyObject *opath;
5078 char *path;
5079 int n;
5080 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005081
Victor Stinner8c62be82010-05-06 00:08:46 +00005082 if (!PyArg_ParseTuple(args, "O&:readlink",
5083 PyUnicode_FSConverter, &opath))
5084 return NULL;
5085 path = PyBytes_AsString(opath);
5086 v = PySequence_GetItem(args, 0);
5087 if (v == NULL) {
5088 Py_DECREF(opath);
5089 return NULL;
5090 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005091
Victor Stinner8c62be82010-05-06 00:08:46 +00005092 if (PyUnicode_Check(v)) {
5093 arg_is_unicode = 1;
5094 }
5095 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005096
Victor Stinner8c62be82010-05-06 00:08:46 +00005097 Py_BEGIN_ALLOW_THREADS
5098 n = readlink(path, buf, (int) sizeof buf);
5099 Py_END_ALLOW_THREADS
5100 if (n < 0)
5101 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005102
Victor Stinner8c62be82010-05-06 00:08:46 +00005103 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005104 if (arg_is_unicode)
5105 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5106 else
5107 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005108}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005109#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005110
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005111
Brian Curtin52173d42010-12-02 18:29:18 +00005112#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005113PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005114"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005115Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005116
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005118posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005119{
Victor Stinner8c62be82010-05-06 00:08:46 +00005120 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005121}
5122#endif /* HAVE_SYMLINK */
5123
Brian Curtind40e6f72010-07-08 21:39:08 +00005124#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5125
5126PyDoc_STRVAR(win_readlink__doc__,
5127"readlink(path) -> path\n\n\
5128Return a string representing the path to which the symbolic link points.");
5129
Brian Curtind40e6f72010-07-08 21:39:08 +00005130/* Windows readlink implementation */
5131static PyObject *
5132win_readlink(PyObject *self, PyObject *args)
5133{
5134 wchar_t *path;
5135 DWORD n_bytes_returned;
5136 DWORD io_result;
5137 PyObject *result;
5138 HANDLE reparse_point_handle;
5139
5140 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5141 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5142 wchar_t *print_name;
5143
5144 if (!PyArg_ParseTuple(args,
5145 "u:readlink",
5146 &path))
5147 return NULL;
5148
5149 /* First get a handle to the reparse point */
5150 Py_BEGIN_ALLOW_THREADS
5151 reparse_point_handle = CreateFileW(
5152 path,
5153 0,
5154 0,
5155 0,
5156 OPEN_EXISTING,
5157 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5158 0);
5159 Py_END_ALLOW_THREADS
5160
5161 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5162 {
5163 return win32_error_unicode("readlink", path);
5164 }
5165
5166 Py_BEGIN_ALLOW_THREADS
5167 /* New call DeviceIoControl to read the reparse point */
5168 io_result = DeviceIoControl(
5169 reparse_point_handle,
5170 FSCTL_GET_REPARSE_POINT,
5171 0, 0, /* in buffer */
5172 target_buffer, sizeof(target_buffer),
5173 &n_bytes_returned,
5174 0 /* we're not using OVERLAPPED_IO */
5175 );
5176 CloseHandle(reparse_point_handle);
5177 Py_END_ALLOW_THREADS
5178
5179 if (io_result==0)
5180 {
5181 return win32_error_unicode("readlink", path);
5182 }
5183
5184 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5185 {
5186 PyErr_SetString(PyExc_ValueError,
5187 "not a symbolic link");
5188 return NULL;
5189 }
Brian Curtin74e45612010-07-09 15:58:59 +00005190 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5191 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5192
5193 result = PyUnicode_FromWideChar(print_name,
5194 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005195 return result;
5196}
5197
5198#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5199
Brian Curtin52173d42010-12-02 18:29:18 +00005200#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005201
5202/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5203static int has_CreateSymbolicLinkW = 0;
5204static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5205static int
5206check_CreateSymbolicLinkW()
5207{
5208 HINSTANCE hKernel32;
5209 /* only recheck */
5210 if (has_CreateSymbolicLinkW)
5211 return has_CreateSymbolicLinkW;
5212 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005213 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5214 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005215 if (Py_CreateSymbolicLinkW)
5216 has_CreateSymbolicLinkW = 1;
5217 return has_CreateSymbolicLinkW;
5218}
5219
5220PyDoc_STRVAR(win_symlink__doc__,
5221"symlink(src, dst, target_is_directory=False)\n\n\
5222Create a symbolic link pointing to src named dst.\n\
5223target_is_directory is required if the target is to be interpreted as\n\
5224a directory.\n\
5225This function requires Windows 6.0 or greater, and raises a\n\
5226NotImplementedError otherwise.");
5227
5228static PyObject *
5229win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5230{
5231 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5232 PyObject *src, *dest;
5233 int target_is_directory = 0;
5234 DWORD res;
5235 WIN32_FILE_ATTRIBUTE_DATA src_info;
5236
5237 if (!check_CreateSymbolicLinkW())
5238 {
5239 /* raise NotImplementedError */
5240 return PyErr_Format(PyExc_NotImplementedError,
5241 "CreateSymbolicLinkW not found");
5242 }
5243 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5244 kwlist, &src, &dest, &target_is_directory))
5245 return NULL;
5246 if (!convert_to_unicode(&src)) { return NULL; }
5247 if (!convert_to_unicode(&dest)) {
5248 Py_DECREF(src);
5249 return NULL;
5250 }
5251
5252 /* if src is a directory, ensure target_is_directory==1 */
5253 if(
5254 GetFileAttributesExW(
5255 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5256 ))
5257 {
5258 target_is_directory = target_is_directory ||
5259 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5260 }
5261
5262 Py_BEGIN_ALLOW_THREADS
5263 res = Py_CreateSymbolicLinkW(
5264 PyUnicode_AsUnicode(dest),
5265 PyUnicode_AsUnicode(src),
5266 target_is_directory);
5267 Py_END_ALLOW_THREADS
5268 Py_DECREF(src);
5269 Py_DECREF(dest);
5270 if (!res)
5271 {
5272 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5273 }
5274
5275 Py_INCREF(Py_None);
5276 return Py_None;
5277}
Brian Curtin52173d42010-12-02 18:29:18 +00005278#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005279
5280#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005281#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5282static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005283system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005284{
5285 ULONG value = 0;
5286
5287 Py_BEGIN_ALLOW_THREADS
5288 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5289 Py_END_ALLOW_THREADS
5290
5291 return value;
5292}
5293
5294static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005295posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005296{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005297 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005298 return Py_BuildValue("ddddd",
5299 (double)0 /* t.tms_utime / HZ */,
5300 (double)0 /* t.tms_stime / HZ */,
5301 (double)0 /* t.tms_cutime / HZ */,
5302 (double)0 /* t.tms_cstime / HZ */,
5303 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005304}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005305#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005306#define NEED_TICKS_PER_SECOND
5307static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005308static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005309posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005310{
Victor Stinner8c62be82010-05-06 00:08:46 +00005311 struct tms t;
5312 clock_t c;
5313 errno = 0;
5314 c = times(&t);
5315 if (c == (clock_t) -1)
5316 return posix_error();
5317 return Py_BuildValue("ddddd",
5318 (double)t.tms_utime / ticks_per_second,
5319 (double)t.tms_stime / ticks_per_second,
5320 (double)t.tms_cutime / ticks_per_second,
5321 (double)t.tms_cstime / ticks_per_second,
5322 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005323}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005324#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005325#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005326
5327
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005328#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005329#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005330static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005331posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005332{
Victor Stinner8c62be82010-05-06 00:08:46 +00005333 FILETIME create, exit, kernel, user;
5334 HANDLE hProc;
5335 hProc = GetCurrentProcess();
5336 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5337 /* The fields of a FILETIME structure are the hi and lo part
5338 of a 64-bit value expressed in 100 nanosecond units.
5339 1e7 is one second in such units; 1e-7 the inverse.
5340 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5341 */
5342 return Py_BuildValue(
5343 "ddddd",
5344 (double)(user.dwHighDateTime*429.4967296 +
5345 user.dwLowDateTime*1e-7),
5346 (double)(kernel.dwHighDateTime*429.4967296 +
5347 kernel.dwLowDateTime*1e-7),
5348 (double)0,
5349 (double)0,
5350 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005351}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005352#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005353
5354#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005355PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005356"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005357Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005358#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005359
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005360
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005361#ifdef HAVE_GETSID
5362PyDoc_STRVAR(posix_getsid__doc__,
5363"getsid(pid) -> sid\n\n\
5364Call the system call getsid().");
5365
5366static PyObject *
5367posix_getsid(PyObject *self, PyObject *args)
5368{
Victor Stinner8c62be82010-05-06 00:08:46 +00005369 pid_t pid;
5370 int sid;
5371 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5372 return NULL;
5373 sid = getsid(pid);
5374 if (sid < 0)
5375 return posix_error();
5376 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005377}
5378#endif /* HAVE_GETSID */
5379
5380
Guido van Rossumb6775db1994-08-01 11:34:53 +00005381#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005382PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005383"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005384Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005385
Barry Warsaw53699e91996-12-10 23:23:01 +00005386static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005387posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005388{
Victor Stinner8c62be82010-05-06 00:08:46 +00005389 if (setsid() < 0)
5390 return posix_error();
5391 Py_INCREF(Py_None);
5392 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005393}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005394#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005395
Guido van Rossumb6775db1994-08-01 11:34:53 +00005396#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005397PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005398"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005399Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005400
Barry Warsaw53699e91996-12-10 23:23:01 +00005401static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005402posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005403{
Victor Stinner8c62be82010-05-06 00:08:46 +00005404 pid_t pid;
5405 int pgrp;
5406 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5407 return NULL;
5408 if (setpgid(pid, pgrp) < 0)
5409 return posix_error();
5410 Py_INCREF(Py_None);
5411 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005412}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005413#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005414
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005415
Guido van Rossumb6775db1994-08-01 11:34:53 +00005416#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005417PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005418"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005419Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005420
Barry Warsaw53699e91996-12-10 23:23:01 +00005421static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005422posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005423{
Victor Stinner8c62be82010-05-06 00:08:46 +00005424 int fd;
5425 pid_t pgid;
5426 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5427 return NULL;
5428 pgid = tcgetpgrp(fd);
5429 if (pgid < 0)
5430 return posix_error();
5431 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005432}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005433#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005434
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005435
Guido van Rossumb6775db1994-08-01 11:34:53 +00005436#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005437PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005438"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005439Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005440
Barry Warsaw53699e91996-12-10 23:23:01 +00005441static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005442posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005443{
Victor Stinner8c62be82010-05-06 00:08:46 +00005444 int fd;
5445 pid_t pgid;
5446 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5447 return NULL;
5448 if (tcsetpgrp(fd, pgid) < 0)
5449 return posix_error();
5450 Py_INCREF(Py_None);
5451 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005452}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005453#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005454
Guido van Rossum687dd131993-05-17 08:34:16 +00005455/* Functions acting on file descriptors */
5456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005457PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005458"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005459Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005460
Barry Warsaw53699e91996-12-10 23:23:01 +00005461static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005462posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005463{
Victor Stinner8c62be82010-05-06 00:08:46 +00005464 PyObject *ofile;
5465 char *file;
5466 int flag;
5467 int mode = 0777;
5468 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005469
5470#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005471 PyUnicodeObject *po;
5472 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5473 Py_BEGIN_ALLOW_THREADS
5474 /* PyUnicode_AS_UNICODE OK without thread
5475 lock as it is a simple dereference. */
5476 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5477 Py_END_ALLOW_THREADS
5478 if (fd < 0)
5479 return posix_error();
5480 return PyLong_FromLong((long)fd);
5481 }
5482 /* Drop the argument parsing error as narrow strings
5483 are also valid. */
5484 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005485#endif
5486
Victor Stinner8c62be82010-05-06 00:08:46 +00005487 if (!PyArg_ParseTuple(args, "O&i|i",
5488 PyUnicode_FSConverter, &ofile,
5489 &flag, &mode))
5490 return NULL;
5491 file = PyBytes_AsString(ofile);
5492 Py_BEGIN_ALLOW_THREADS
5493 fd = open(file, flag, mode);
5494 Py_END_ALLOW_THREADS
5495 if (fd < 0)
5496 return posix_error_with_allocated_filename(ofile);
5497 Py_DECREF(ofile);
5498 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005499}
5500
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005502PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005503"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005505
Barry Warsaw53699e91996-12-10 23:23:01 +00005506static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005507posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005508{
Victor Stinner8c62be82010-05-06 00:08:46 +00005509 int fd, res;
5510 if (!PyArg_ParseTuple(args, "i:close", &fd))
5511 return NULL;
5512 if (!_PyVerify_fd(fd))
5513 return posix_error();
5514 Py_BEGIN_ALLOW_THREADS
5515 res = close(fd);
5516 Py_END_ALLOW_THREADS
5517 if (res < 0)
5518 return posix_error();
5519 Py_INCREF(Py_None);
5520 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005521}
5522
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005523
Victor Stinner8c62be82010-05-06 00:08:46 +00005524PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005525"closerange(fd_low, fd_high)\n\n\
5526Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5527
5528static PyObject *
5529posix_closerange(PyObject *self, PyObject *args)
5530{
Victor Stinner8c62be82010-05-06 00:08:46 +00005531 int fd_from, fd_to, i;
5532 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5533 return NULL;
5534 Py_BEGIN_ALLOW_THREADS
5535 for (i = fd_from; i < fd_to; i++)
5536 if (_PyVerify_fd(i))
5537 close(i);
5538 Py_END_ALLOW_THREADS
5539 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005540}
5541
5542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005543PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005544"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005545Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005546
Barry Warsaw53699e91996-12-10 23:23:01 +00005547static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005548posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005549{
Victor Stinner8c62be82010-05-06 00:08:46 +00005550 int fd;
5551 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5552 return NULL;
5553 if (!_PyVerify_fd(fd))
5554 return posix_error();
5555 Py_BEGIN_ALLOW_THREADS
5556 fd = dup(fd);
5557 Py_END_ALLOW_THREADS
5558 if (fd < 0)
5559 return posix_error();
5560 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005561}
5562
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005564PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005565"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005567
Barry Warsaw53699e91996-12-10 23:23:01 +00005568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005569posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005570{
Victor Stinner8c62be82010-05-06 00:08:46 +00005571 int fd, fd2, res;
5572 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5573 return NULL;
5574 if (!_PyVerify_fd_dup2(fd, fd2))
5575 return posix_error();
5576 Py_BEGIN_ALLOW_THREADS
5577 res = dup2(fd, fd2);
5578 Py_END_ALLOW_THREADS
5579 if (res < 0)
5580 return posix_error();
5581 Py_INCREF(Py_None);
5582 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005583}
5584
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005586PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005587"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005588Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005589
Barry Warsaw53699e91996-12-10 23:23:01 +00005590static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005591posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005592{
Victor Stinner8c62be82010-05-06 00:08:46 +00005593 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005594#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005595 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005596#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005597 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005598#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005599 PyObject *posobj;
5600 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5601 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005602#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005603 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5604 switch (how) {
5605 case 0: how = SEEK_SET; break;
5606 case 1: how = SEEK_CUR; break;
5607 case 2: how = SEEK_END; break;
5608 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005609#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005610
5611#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005612 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005613#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 pos = PyLong_Check(posobj) ?
5615 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005616#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005617 if (PyErr_Occurred())
5618 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005619
Victor Stinner8c62be82010-05-06 00:08:46 +00005620 if (!_PyVerify_fd(fd))
5621 return posix_error();
5622 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005623#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005624 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005625#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005626 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005627#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005628 Py_END_ALLOW_THREADS
5629 if (res < 0)
5630 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005631
5632#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005633 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005634#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005636#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005637}
5638
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005640PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005641"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005642Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005643
Barry Warsaw53699e91996-12-10 23:23:01 +00005644static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005645posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005646{
Victor Stinner8c62be82010-05-06 00:08:46 +00005647 int fd, size;
5648 Py_ssize_t n;
5649 PyObject *buffer;
5650 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5651 return NULL;
5652 if (size < 0) {
5653 errno = EINVAL;
5654 return posix_error();
5655 }
5656 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5657 if (buffer == NULL)
5658 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005659 if (!_PyVerify_fd(fd)) {
5660 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005661 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005662 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005663 Py_BEGIN_ALLOW_THREADS
5664 n = read(fd, PyBytes_AS_STRING(buffer), size);
5665 Py_END_ALLOW_THREADS
5666 if (n < 0) {
5667 Py_DECREF(buffer);
5668 return posix_error();
5669 }
5670 if (n != size)
5671 _PyBytes_Resize(&buffer, n);
5672 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005673}
5674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005676PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005677"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005678Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005679
Barry Warsaw53699e91996-12-10 23:23:01 +00005680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005681posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005682{
Victor Stinner8c62be82010-05-06 00:08:46 +00005683 Py_buffer pbuf;
5684 int fd;
5685 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005686
Victor Stinner8c62be82010-05-06 00:08:46 +00005687 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5688 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005689 if (!_PyVerify_fd(fd)) {
5690 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005691 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005692 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005693 Py_BEGIN_ALLOW_THREADS
5694 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5695 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005696 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005697 if (size < 0)
5698 return posix_error();
5699 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005700}
5701
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005703PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005704"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005705Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005706
Barry Warsaw53699e91996-12-10 23:23:01 +00005707static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005708posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005709{
Victor Stinner8c62be82010-05-06 00:08:46 +00005710 int fd;
5711 STRUCT_STAT st;
5712 int res;
5713 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5714 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005715#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005716 /* on OpenVMS we must ensure that all bytes are written to the file */
5717 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005718#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 if (!_PyVerify_fd(fd))
5720 return posix_error();
5721 Py_BEGIN_ALLOW_THREADS
5722 res = FSTAT(fd, &st);
5723 Py_END_ALLOW_THREADS
5724 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005725#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005726 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005727#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005728 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005729#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005730 }
Tim Peters5aa91602002-01-30 05:46:57 +00005731
Victor Stinner8c62be82010-05-06 00:08:46 +00005732 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005733}
5734
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005735PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005736"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005737Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005738connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005739
5740static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005741posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005742{
Victor Stinner8c62be82010-05-06 00:08:46 +00005743 int fd;
5744 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5745 return NULL;
5746 if (!_PyVerify_fd(fd))
5747 return PyBool_FromLong(0);
5748 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005749}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005750
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005751#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005752PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005753"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005754Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005755
Barry Warsaw53699e91996-12-10 23:23:01 +00005756static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005757posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005758{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005759#if defined(PYOS_OS2)
5760 HFILE read, write;
5761 APIRET rc;
5762
Victor Stinner8c62be82010-05-06 00:08:46 +00005763 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005764 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005765 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005766 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005767 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005768
5769 return Py_BuildValue("(ii)", read, write);
5770#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005771#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005772 int fds[2];
5773 int res;
5774 Py_BEGIN_ALLOW_THREADS
5775 res = pipe(fds);
5776 Py_END_ALLOW_THREADS
5777 if (res != 0)
5778 return posix_error();
5779 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005780#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005781 HANDLE read, write;
5782 int read_fd, write_fd;
5783 BOOL ok;
5784 Py_BEGIN_ALLOW_THREADS
5785 ok = CreatePipe(&read, &write, NULL, 0);
5786 Py_END_ALLOW_THREADS
5787 if (!ok)
5788 return win32_error("CreatePipe", NULL);
5789 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5790 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5791 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005792#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005793#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005794}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005795#endif /* HAVE_PIPE */
5796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005797
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005798#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005799PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005800"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005802
Barry Warsaw53699e91996-12-10 23:23:01 +00005803static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005804posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005805{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005806 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005807 char *filename;
5808 int mode = 0666;
5809 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005810 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5811 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005812 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005813 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005814 Py_BEGIN_ALLOW_THREADS
5815 res = mkfifo(filename, mode);
5816 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005817 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005818 if (res < 0)
5819 return posix_error();
5820 Py_INCREF(Py_None);
5821 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005822}
5823#endif
5824
5825
Neal Norwitz11690112002-07-30 01:08:28 +00005826#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005827PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005828"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005829Create a filesystem node (file, device special file or named pipe)\n\
5830named filename. mode specifies both the permissions to use and the\n\
5831type of node to be created, being combined (bitwise OR) with one of\n\
5832S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005833device defines the newly created device special file (probably using\n\
5834os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005835
5836
5837static PyObject *
5838posix_mknod(PyObject *self, PyObject *args)
5839{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005840 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005841 char *filename;
5842 int mode = 0600;
5843 int device = 0;
5844 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005845 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5846 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005847 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005848 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005849 Py_BEGIN_ALLOW_THREADS
5850 res = mknod(filename, mode, device);
5851 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005852 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005853 if (res < 0)
5854 return posix_error();
5855 Py_INCREF(Py_None);
5856 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005857}
5858#endif
5859
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005860#ifdef HAVE_DEVICE_MACROS
5861PyDoc_STRVAR(posix_major__doc__,
5862"major(device) -> major number\n\
5863Extracts a device major number from a raw device number.");
5864
5865static PyObject *
5866posix_major(PyObject *self, PyObject *args)
5867{
Victor Stinner8c62be82010-05-06 00:08:46 +00005868 int device;
5869 if (!PyArg_ParseTuple(args, "i:major", &device))
5870 return NULL;
5871 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005872}
5873
5874PyDoc_STRVAR(posix_minor__doc__,
5875"minor(device) -> minor number\n\
5876Extracts a device minor number from a raw device number.");
5877
5878static PyObject *
5879posix_minor(PyObject *self, PyObject *args)
5880{
Victor Stinner8c62be82010-05-06 00:08:46 +00005881 int device;
5882 if (!PyArg_ParseTuple(args, "i:minor", &device))
5883 return NULL;
5884 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005885}
5886
5887PyDoc_STRVAR(posix_makedev__doc__,
5888"makedev(major, minor) -> device number\n\
5889Composes a raw device number from the major and minor device numbers.");
5890
5891static PyObject *
5892posix_makedev(PyObject *self, PyObject *args)
5893{
Victor Stinner8c62be82010-05-06 00:08:46 +00005894 int major, minor;
5895 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5896 return NULL;
5897 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005898}
5899#endif /* device macros */
5900
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005901
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005902#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005903PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005904"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005905Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005906
Barry Warsaw53699e91996-12-10 23:23:01 +00005907static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005908posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005909{
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 int fd;
5911 off_t length;
5912 int res;
5913 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005914
Victor Stinner8c62be82010-05-06 00:08:46 +00005915 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5916 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005917
5918#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005919 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005920#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 length = PyLong_Check(lenobj) ?
5922 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005923#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005924 if (PyErr_Occurred())
5925 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005926
Victor Stinner8c62be82010-05-06 00:08:46 +00005927 Py_BEGIN_ALLOW_THREADS
5928 res = ftruncate(fd, length);
5929 Py_END_ALLOW_THREADS
5930 if (res < 0)
5931 return posix_error();
5932 Py_INCREF(Py_None);
5933 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005934}
5935#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005936
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005937#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005938PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005939"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005940Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005941
Fred Drake762e2061999-08-26 17:23:54 +00005942/* Save putenv() parameters as values here, so we can collect them when they
5943 * get re-set with another call for the same key. */
5944static PyObject *posix_putenv_garbage;
5945
Tim Peters5aa91602002-01-30 05:46:57 +00005946static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005947posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005948{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005949#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005950 wchar_t *s1, *s2;
5951 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005952#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005953 PyObject *os1, *os2;
5954 char *s1, *s2;
5955 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005956#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005957 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005958 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005959
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005960#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005961 if (!PyArg_ParseTuple(args,
5962 "uu:putenv",
5963 &s1, &s2))
5964 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005965#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005966 if (!PyArg_ParseTuple(args,
5967 "O&O&:putenv",
5968 PyUnicode_FSConverter, &os1,
5969 PyUnicode_FSConverter, &os2))
5970 return NULL;
5971 s1 = PyBytes_AsString(os1);
5972 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005973#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005974
5975#if defined(PYOS_OS2)
5976 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5977 APIRET rc;
5978
Guido van Rossumd48f2521997-12-05 22:19:34 +00005979 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005980 if (rc != NO_ERROR) {
5981 os2_error(rc);
5982 goto error;
5983 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005984
5985 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5986 APIRET rc;
5987
Guido van Rossumd48f2521997-12-05 22:19:34 +00005988 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005989 if (rc != NO_ERROR) {
5990 os2_error(rc);
5991 goto error;
5992 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005993 } else {
5994#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005995 /* XXX This can leak memory -- not easy to fix :-( */
5996 /* len includes space for a trailing \0; the size arg to
5997 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005998#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005999 len = wcslen(s1) + wcslen(s2) + 2;
6000 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006001#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006002 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006003 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006004#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006005 if (newstr == NULL) {
6006 PyErr_NoMemory();
6007 goto error;
6008 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006009#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 newenv = PyUnicode_AsUnicode(newstr);
6011 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6012 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006013 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006014 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006016#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006017 newenv = PyBytes_AS_STRING(newstr);
6018 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6019 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006020 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006021 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006022 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006023#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006024
Victor Stinner8c62be82010-05-06 00:08:46 +00006025 /* Install the first arg and newstr in posix_putenv_garbage;
6026 * this will cause previous value to be collected. This has to
6027 * happen after the real putenv() call because the old value
6028 * was still accessible until then. */
6029 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006030#ifdef MS_WINDOWS
6031 PyTuple_GET_ITEM(args, 0),
6032#else
6033 os1,
6034#endif
6035 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006036 /* really not much we can do; just leak */
6037 PyErr_Clear();
6038 }
6039 else {
6040 Py_DECREF(newstr);
6041 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006042
6043#if defined(PYOS_OS2)
6044 }
6045#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006046
Martin v. Löwis011e8422009-05-05 04:43:17 +00006047#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006048 Py_DECREF(os1);
6049 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006050#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006051 Py_RETURN_NONE;
6052
6053error:
6054#ifndef MS_WINDOWS
6055 Py_DECREF(os1);
6056 Py_DECREF(os2);
6057#endif
6058 Py_XDECREF(newstr);
6059 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006060}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006061#endif /* putenv */
6062
Guido van Rossumc524d952001-10-19 01:31:59 +00006063#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006064PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006065"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006066Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006067
6068static PyObject *
6069posix_unsetenv(PyObject *self, PyObject *args)
6070{
Victor Stinner84ae1182010-05-06 22:05:07 +00006071#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006072 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006073
Victor Stinner8c62be82010-05-06 00:08:46 +00006074 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6075 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006076#else
6077 PyObject *os1;
6078 char *s1;
6079
6080 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6081 PyUnicode_FSConverter, &os1))
6082 return NULL;
6083 s1 = PyBytes_AsString(os1);
6084#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006085
Victor Stinner8c62be82010-05-06 00:08:46 +00006086 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006087
Victor Stinner8c62be82010-05-06 00:08:46 +00006088 /* Remove the key from posix_putenv_garbage;
6089 * this will cause it to be collected. This has to
6090 * happen after the real unsetenv() call because the
6091 * old value was still accessible until then.
6092 */
6093 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006094#ifdef MS_WINDOWS
6095 PyTuple_GET_ITEM(args, 0)
6096#else
6097 os1
6098#endif
6099 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006100 /* really not much we can do; just leak */
6101 PyErr_Clear();
6102 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006103
Victor Stinner84ae1182010-05-06 22:05:07 +00006104#ifndef MS_WINDOWS
6105 Py_DECREF(os1);
6106#endif
6107 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006108}
6109#endif /* unsetenv */
6110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006111PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006112"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006113Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006114
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006115static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006116posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006117{
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 int code;
6119 char *message;
6120 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6121 return NULL;
6122 message = strerror(code);
6123 if (message == NULL) {
6124 PyErr_SetString(PyExc_ValueError,
6125 "strerror() argument out of range");
6126 return NULL;
6127 }
6128 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006129}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006130
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006131
Guido van Rossumc9641791998-08-04 15:26:23 +00006132#ifdef HAVE_SYS_WAIT_H
6133
Fred Drake106c1a02002-04-23 15:58:02 +00006134#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006136"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006137Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006138
6139static PyObject *
6140posix_WCOREDUMP(PyObject *self, PyObject *args)
6141{
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 WAIT_TYPE status;
6143 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006144
Victor Stinner8c62be82010-05-06 00:08:46 +00006145 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6146 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006147
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006149}
6150#endif /* WCOREDUMP */
6151
6152#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006153PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006154"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006155Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006156job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006157
6158static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006159posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006160{
Victor Stinner8c62be82010-05-06 00:08:46 +00006161 WAIT_TYPE status;
6162 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006163
Victor Stinner8c62be82010-05-06 00:08:46 +00006164 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6165 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006166
Victor Stinner8c62be82010-05-06 00:08:46 +00006167 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006168}
6169#endif /* WIFCONTINUED */
6170
Guido van Rossumc9641791998-08-04 15:26:23 +00006171#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006172PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006173"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006174Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006175
6176static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006177posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006178{
Victor Stinner8c62be82010-05-06 00:08:46 +00006179 WAIT_TYPE status;
6180 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006181
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6183 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006184
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006186}
6187#endif /* WIFSTOPPED */
6188
6189#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006190PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006191"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006192Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006193
6194static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006195posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006196{
Victor Stinner8c62be82010-05-06 00:08:46 +00006197 WAIT_TYPE status;
6198 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006199
Victor Stinner8c62be82010-05-06 00:08:46 +00006200 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6201 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006202
Victor Stinner8c62be82010-05-06 00:08:46 +00006203 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006204}
6205#endif /* WIFSIGNALED */
6206
6207#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006208PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006209"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006210Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006211system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006212
6213static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006214posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006215{
Victor Stinner8c62be82010-05-06 00:08:46 +00006216 WAIT_TYPE status;
6217 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006218
Victor Stinner8c62be82010-05-06 00:08:46 +00006219 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6220 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006221
Victor Stinner8c62be82010-05-06 00:08:46 +00006222 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006223}
6224#endif /* WIFEXITED */
6225
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006226#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006227PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006228"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006229Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006230
6231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006232posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006233{
Victor Stinner8c62be82010-05-06 00:08:46 +00006234 WAIT_TYPE status;
6235 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006236
Victor Stinner8c62be82010-05-06 00:08:46 +00006237 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6238 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006239
Victor Stinner8c62be82010-05-06 00:08:46 +00006240 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006241}
6242#endif /* WEXITSTATUS */
6243
6244#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006245PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006246"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006247Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006248value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006249
6250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006251posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006252{
Victor Stinner8c62be82010-05-06 00:08:46 +00006253 WAIT_TYPE status;
6254 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006255
Victor Stinner8c62be82010-05-06 00:08:46 +00006256 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6257 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006258
Victor Stinner8c62be82010-05-06 00:08:46 +00006259 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006260}
6261#endif /* WTERMSIG */
6262
6263#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006264PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006265"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006266Return the signal that stopped the process that provided\n\
6267the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006268
6269static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006270posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006271{
Victor Stinner8c62be82010-05-06 00:08:46 +00006272 WAIT_TYPE status;
6273 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006274
Victor Stinner8c62be82010-05-06 00:08:46 +00006275 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6276 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006277
Victor Stinner8c62be82010-05-06 00:08:46 +00006278 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006279}
6280#endif /* WSTOPSIG */
6281
6282#endif /* HAVE_SYS_WAIT_H */
6283
6284
Thomas Wouters477c8d52006-05-27 19:21:47 +00006285#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006286#ifdef _SCO_DS
6287/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6288 needed definitions in sys/statvfs.h */
6289#define _SVID3
6290#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006291#include <sys/statvfs.h>
6292
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006293static PyObject*
6294_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006295 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6296 if (v == NULL)
6297 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006298
6299#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6301 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6302 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6303 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6304 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6305 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6306 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6307 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6308 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6309 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006310#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006311 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6312 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6313 PyStructSequence_SET_ITEM(v, 2,
6314 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6315 PyStructSequence_SET_ITEM(v, 3,
6316 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6317 PyStructSequence_SET_ITEM(v, 4,
6318 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6319 PyStructSequence_SET_ITEM(v, 5,
6320 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6321 PyStructSequence_SET_ITEM(v, 6,
6322 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6323 PyStructSequence_SET_ITEM(v, 7,
6324 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6325 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6326 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006327#endif
6328
Victor Stinner8c62be82010-05-06 00:08:46 +00006329 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006330}
6331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006332PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006333"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006334Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006335
6336static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006337posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006338{
Victor Stinner8c62be82010-05-06 00:08:46 +00006339 int fd, res;
6340 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006341
Victor Stinner8c62be82010-05-06 00:08:46 +00006342 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6343 return NULL;
6344 Py_BEGIN_ALLOW_THREADS
6345 res = fstatvfs(fd, &st);
6346 Py_END_ALLOW_THREADS
6347 if (res != 0)
6348 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006349
Victor Stinner8c62be82010-05-06 00:08:46 +00006350 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006351}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006352#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006353
6354
Thomas Wouters477c8d52006-05-27 19:21:47 +00006355#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006356#include <sys/statvfs.h>
6357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006358PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006359"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006360Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006361
6362static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006363posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006364{
Victor Stinner8c62be82010-05-06 00:08:46 +00006365 char *path;
6366 int res;
6367 struct statvfs st;
6368 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6369 return NULL;
6370 Py_BEGIN_ALLOW_THREADS
6371 res = statvfs(path, &st);
6372 Py_END_ALLOW_THREADS
6373 if (res != 0)
6374 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006375
Victor Stinner8c62be82010-05-06 00:08:46 +00006376 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006377}
6378#endif /* HAVE_STATVFS */
6379
Fred Drakec9680921999-12-13 16:37:25 +00006380/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6381 * It maps strings representing configuration variable names to
6382 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006383 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006384 * rarely-used constants. There are three separate tables that use
6385 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006386 *
6387 * This code is always included, even if none of the interfaces that
6388 * need it are included. The #if hackery needed to avoid it would be
6389 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006390 */
6391struct constdef {
6392 char *name;
6393 long value;
6394};
6395
Fred Drake12c6e2d1999-12-14 21:25:03 +00006396static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006397conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006398 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006399{
Christian Heimes217cfd12007-12-02 14:31:20 +00006400 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006401 *valuep = PyLong_AS_LONG(arg);
6402 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006403 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006404 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006405 /* look up the value in the table using a binary search */
6406 size_t lo = 0;
6407 size_t mid;
6408 size_t hi = tablesize;
6409 int cmp;
6410 const char *confname;
6411 if (!PyUnicode_Check(arg)) {
6412 PyErr_SetString(PyExc_TypeError,
6413 "configuration names must be strings or integers");
6414 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006415 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006416 confname = _PyUnicode_AsString(arg);
6417 if (confname == NULL)
6418 return 0;
6419 while (lo < hi) {
6420 mid = (lo + hi) / 2;
6421 cmp = strcmp(confname, table[mid].name);
6422 if (cmp < 0)
6423 hi = mid;
6424 else if (cmp > 0)
6425 lo = mid + 1;
6426 else {
6427 *valuep = table[mid].value;
6428 return 1;
6429 }
6430 }
6431 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6432 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006433 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006434}
6435
6436
6437#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6438static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006439#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006440 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006441#endif
6442#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006443 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006444#endif
Fred Drakec9680921999-12-13 16:37:25 +00006445#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006446 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006447#endif
6448#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006449 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006450#endif
6451#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006452 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006453#endif
6454#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006455 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006456#endif
6457#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006458 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006459#endif
6460#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006461 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006462#endif
6463#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006464 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006465#endif
6466#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006468#endif
6469#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006471#endif
6472#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006473 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006474#endif
6475#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006477#endif
6478#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006479 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006480#endif
6481#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006482 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006483#endif
6484#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006486#endif
6487#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006488 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006489#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006490#ifdef _PC_ACL_ENABLED
6491 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6492#endif
6493#ifdef _PC_MIN_HOLE_SIZE
6494 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6495#endif
6496#ifdef _PC_ALLOC_SIZE_MIN
6497 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6498#endif
6499#ifdef _PC_REC_INCR_XFER_SIZE
6500 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6501#endif
6502#ifdef _PC_REC_MAX_XFER_SIZE
6503 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6504#endif
6505#ifdef _PC_REC_MIN_XFER_SIZE
6506 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6507#endif
6508#ifdef _PC_REC_XFER_ALIGN
6509 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6510#endif
6511#ifdef _PC_SYMLINK_MAX
6512 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6513#endif
6514#ifdef _PC_XATTR_ENABLED
6515 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6516#endif
6517#ifdef _PC_XATTR_EXISTS
6518 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6519#endif
6520#ifdef _PC_TIMESTAMP_RESOLUTION
6521 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6522#endif
Fred Drakec9680921999-12-13 16:37:25 +00006523};
6524
Fred Drakec9680921999-12-13 16:37:25 +00006525static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006526conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006527{
6528 return conv_confname(arg, valuep, posix_constants_pathconf,
6529 sizeof(posix_constants_pathconf)
6530 / sizeof(struct constdef));
6531}
6532#endif
6533
6534#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006535PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006536"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006537Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006538If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006539
6540static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006541posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006542{
6543 PyObject *result = NULL;
6544 int name, fd;
6545
Fred Drake12c6e2d1999-12-14 21:25:03 +00006546 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6547 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006548 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006549
Stefan Krah0e803b32010-11-26 16:16:47 +00006550 errno = 0;
6551 limit = fpathconf(fd, name);
6552 if (limit == -1 && errno != 0)
6553 posix_error();
6554 else
6555 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006556 }
6557 return result;
6558}
6559#endif
6560
6561
6562#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006563PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006564"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006565Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006566If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006567
6568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006569posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006570{
6571 PyObject *result = NULL;
6572 int name;
6573 char *path;
6574
6575 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6576 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006577 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006578
Victor Stinner8c62be82010-05-06 00:08:46 +00006579 errno = 0;
6580 limit = pathconf(path, name);
6581 if (limit == -1 && errno != 0) {
6582 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006583 /* could be a path or name problem */
6584 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006585 else
Stefan Krah99439262010-11-26 12:58:05 +00006586 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006587 }
6588 else
6589 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006590 }
6591 return result;
6592}
6593#endif
6594
6595#ifdef HAVE_CONFSTR
6596static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006597#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006599#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006600#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006601 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006602#endif
6603#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006604 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006605#endif
Fred Draked86ed291999-12-15 15:34:33 +00006606#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006607 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006608#endif
6609#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006611#endif
6612#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006614#endif
6615#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006617#endif
Fred Drakec9680921999-12-13 16:37:25 +00006618#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006620#endif
6621#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006622 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006623#endif
6624#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006625 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006626#endif
6627#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006628 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006629#endif
6630#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006632#endif
6633#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006635#endif
6636#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006637 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006638#endif
6639#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006641#endif
Fred Draked86ed291999-12-15 15:34:33 +00006642#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006644#endif
Fred Drakec9680921999-12-13 16:37:25 +00006645#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006647#endif
Fred Draked86ed291999-12-15 15:34:33 +00006648#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006650#endif
6651#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006653#endif
6654#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006656#endif
6657#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006659#endif
Fred Drakec9680921999-12-13 16:37:25 +00006660#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006662#endif
6663#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006664 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006665#endif
6666#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006668#endif
6669#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006671#endif
6672#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006674#endif
6675#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006676 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006677#endif
6678#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006680#endif
6681#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006683#endif
6684#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006686#endif
6687#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006689#endif
6690#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006692#endif
6693#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006695#endif
6696#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006698#endif
6699#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006701#endif
6702#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006704#endif
6705#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006707#endif
Fred Draked86ed291999-12-15 15:34:33 +00006708#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006710#endif
6711#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006713#endif
6714#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006716#endif
6717#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006719#endif
6720#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006722#endif
6723#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006724 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006725#endif
6726#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006728#endif
6729#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006731#endif
6732#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006734#endif
6735#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006736 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006737#endif
6738#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006739 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006740#endif
6741#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006742 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006743#endif
6744#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006745 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006746#endif
Fred Drakec9680921999-12-13 16:37:25 +00006747};
6748
6749static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006750conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006751{
6752 return conv_confname(arg, valuep, posix_constants_confstr,
6753 sizeof(posix_constants_confstr)
6754 / sizeof(struct constdef));
6755}
6756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006757PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006758"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006759Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006760
6761static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006762posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006763{
6764 PyObject *result = NULL;
6765 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006766 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006767 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006768
Victor Stinnercb043522010-09-10 23:49:04 +00006769 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6770 return NULL;
6771
6772 errno = 0;
6773 len = confstr(name, buffer, sizeof(buffer));
6774 if (len == 0) {
6775 if (errno) {
6776 posix_error();
6777 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006778 }
6779 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006780 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006781 }
6782 }
Victor Stinnercb043522010-09-10 23:49:04 +00006783
6784 if ((unsigned int)len >= sizeof(buffer)) {
6785 char *buf = PyMem_Malloc(len);
6786 if (buf == NULL)
6787 return PyErr_NoMemory();
6788 confstr(name, buf, len);
6789 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6790 PyMem_Free(buf);
6791 }
6792 else
6793 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006794 return result;
6795}
6796#endif
6797
6798
6799#ifdef HAVE_SYSCONF
6800static struct constdef posix_constants_sysconf[] = {
6801#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006803#endif
6804#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006806#endif
6807#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006808 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006809#endif
6810#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006812#endif
6813#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006815#endif
6816#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006817 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006818#endif
6819#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006821#endif
6822#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006824#endif
6825#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006827#endif
6828#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006830#endif
Fred Draked86ed291999-12-15 15:34:33 +00006831#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006832 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006833#endif
6834#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006836#endif
Fred Drakec9680921999-12-13 16:37:25 +00006837#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006838 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006839#endif
Fred Drakec9680921999-12-13 16:37:25 +00006840#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006841 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006842#endif
6843#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006845#endif
6846#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006848#endif
6849#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006851#endif
6852#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006854#endif
Fred Draked86ed291999-12-15 15:34:33 +00006855#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006857#endif
Fred Drakec9680921999-12-13 16:37:25 +00006858#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006860#endif
6861#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006863#endif
6864#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006866#endif
6867#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006869#endif
6870#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006872#endif
Fred Draked86ed291999-12-15 15:34:33 +00006873#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006875#endif
Fred Drakec9680921999-12-13 16:37:25 +00006876#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006878#endif
6879#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006881#endif
6882#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006884#endif
6885#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006887#endif
6888#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006889 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006890#endif
6891#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006893#endif
6894#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006896#endif
6897#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006899#endif
6900#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006902#endif
6903#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006905#endif
6906#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006908#endif
6909#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006911#endif
6912#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006914#endif
6915#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006917#endif
6918#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006920#endif
6921#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006923#endif
6924#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006926#endif
6927#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006929#endif
6930#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006932#endif
6933#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006934 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006935#endif
6936#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006938#endif
6939#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006941#endif
6942#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006944#endif
Fred Draked86ed291999-12-15 15:34:33 +00006945#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006947#endif
Fred Drakec9680921999-12-13 16:37:25 +00006948#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006950#endif
6951#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006953#endif
6954#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006956#endif
Fred Draked86ed291999-12-15 15:34:33 +00006957#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006959#endif
Fred Drakec9680921999-12-13 16:37:25 +00006960#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006961 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006962#endif
Fred Draked86ed291999-12-15 15:34:33 +00006963#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006965#endif
6966#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006968#endif
Fred Drakec9680921999-12-13 16:37:25 +00006969#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006971#endif
6972#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006974#endif
6975#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006977#endif
6978#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006980#endif
Fred Draked86ed291999-12-15 15:34:33 +00006981#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006983#endif
Fred Drakec9680921999-12-13 16:37:25 +00006984#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006986#endif
6987#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006989#endif
6990#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006991 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006992#endif
6993#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006994 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006995#endif
6996#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006998#endif
6999#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007001#endif
7002#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007004#endif
Fred Draked86ed291999-12-15 15:34:33 +00007005#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007007#endif
Fred Drakec9680921999-12-13 16:37:25 +00007008#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007010#endif
7011#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
Fred Draked86ed291999-12-15 15:34:33 +00007014#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007016#endif
Fred Drakec9680921999-12-13 16:37:25 +00007017#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
7020#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007022#endif
7023#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007025#endif
7026#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007028#endif
7029#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007031#endif
7032#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007034#endif
7035#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007037#endif
7038#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007040#endif
7041#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007043#endif
Fred Draked86ed291999-12-15 15:34:33 +00007044#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007046#endif
7047#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007049#endif
Fred Drakec9680921999-12-13 16:37:25 +00007050#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007052#endif
7053#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007055#endif
7056#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007058#endif
7059#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007061#endif
7062#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
7065#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007067#endif
7068#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007070#endif
7071#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007073#endif
7074#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007076#endif
7077#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007079#endif
7080#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007082#endif
7083#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007085#endif
7086#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007088#endif
7089#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007091#endif
7092#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007094#endif
7095#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007097#endif
7098#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007100#endif
7101#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007103#endif
7104#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007106#endif
7107#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007109#endif
7110#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007112#endif
7113#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007115#endif
7116#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007118#endif
7119#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007120 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007121#endif
7122#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007124#endif
7125#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007127#endif
7128#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007130#endif
7131#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007133#endif
7134#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007136#endif
7137#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007139#endif
7140#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007142#endif
7143#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007145#endif
7146#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007148#endif
7149#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007151#endif
7152#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
Fred Draked86ed291999-12-15 15:34:33 +00007155#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007157#endif
Fred Drakec9680921999-12-13 16:37:25 +00007158#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007160#endif
7161#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
7164#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007166#endif
7167#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007169#endif
7170#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007172#endif
7173#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007175#endif
7176#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007178#endif
7179#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007181#endif
7182#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007184#endif
7185#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007187#endif
7188#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007190#endif
7191#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007193#endif
7194#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007196#endif
7197#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007199#endif
7200#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007202#endif
7203#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007205#endif
7206#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007208#endif
7209#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007211#endif
7212#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007214#endif
7215#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007217#endif
7218#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007220#endif
7221#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007223#endif
7224#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007226#endif
7227#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007229#endif
7230#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
7233#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007235#endif
7236#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007238#endif
7239#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007241#endif
7242#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007244#endif
7245#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007247#endif
7248#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007250#endif
7251#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007253#endif
7254#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007256#endif
7257#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007259#endif
7260#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007262#endif
7263#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007265#endif
7266#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007268#endif
7269#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007271#endif
7272#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007274#endif
7275#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007277#endif
7278#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007280#endif
7281#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007283#endif
7284#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007286#endif
7287#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007288 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007289#endif
7290#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007291 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007292#endif
7293};
7294
7295static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007296conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007297{
7298 return conv_confname(arg, valuep, posix_constants_sysconf,
7299 sizeof(posix_constants_sysconf)
7300 / sizeof(struct constdef));
7301}
7302
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007303PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007304"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007305Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007306
7307static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007308posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007309{
7310 PyObject *result = NULL;
7311 int name;
7312
7313 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7314 int value;
7315
7316 errno = 0;
7317 value = sysconf(name);
7318 if (value == -1 && errno != 0)
7319 posix_error();
7320 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007321 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007322 }
7323 return result;
7324}
7325#endif
7326
7327
Fred Drakebec628d1999-12-15 18:31:10 +00007328/* This code is used to ensure that the tables of configuration value names
7329 * are in sorted order as required by conv_confname(), and also to build the
7330 * the exported dictionaries that are used to publish information about the
7331 * names available on the host platform.
7332 *
7333 * Sorting the table at runtime ensures that the table is properly ordered
7334 * when used, even for platforms we're not able to test on. It also makes
7335 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007336 */
Fred Drakebec628d1999-12-15 18:31:10 +00007337
7338static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007339cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007340{
7341 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007342 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007343 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007345
7346 return strcmp(c1->name, c2->name);
7347}
7348
7349static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007350setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007352{
Fred Drakebec628d1999-12-15 18:31:10 +00007353 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007354 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007355
7356 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7357 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007358 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007359 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007360
Barry Warsaw3155db32000-04-13 15:20:40 +00007361 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007362 PyObject *o = PyLong_FromLong(table[i].value);
7363 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7364 Py_XDECREF(o);
7365 Py_DECREF(d);
7366 return -1;
7367 }
7368 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007369 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007370 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007371}
7372
Fred Drakebec628d1999-12-15 18:31:10 +00007373/* Return -1 on failure, 0 on success. */
7374static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007375setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007376{
7377#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007378 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007379 sizeof(posix_constants_pathconf)
7380 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007381 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007382 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007383#endif
7384#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007385 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007386 sizeof(posix_constants_confstr)
7387 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007388 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007389 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007390#endif
7391#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007392 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007393 sizeof(posix_constants_sysconf)
7394 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007395 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007396 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007397#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007398 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007399}
Fred Draked86ed291999-12-15 15:34:33 +00007400
7401
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007402PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007403"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007404Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007405in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007406
7407static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007408posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007409{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007410 abort();
7411 /*NOTREACHED*/
7412 Py_FatalError("abort() called from Python code didn't abort!");
7413 return NULL;
7414}
Fred Drakebec628d1999-12-15 18:31:10 +00007415
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007416#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007417PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007418"startfile(filepath [, operation]) - Start a file with its associated\n\
7419application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007420\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007421When \"operation\" is not specified or \"open\", this acts like\n\
7422double-clicking the file in Explorer, or giving the file name as an\n\
7423argument to the DOS \"start\" command: the file is opened with whatever\n\
7424application (if any) its extension is associated.\n\
7425When another \"operation\" is given, it specifies what should be done with\n\
7426the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007427\n\
7428startfile returns as soon as the associated application is launched.\n\
7429There is no option to wait for the application to close, and no way\n\
7430to retrieve the application's exit status.\n\
7431\n\
7432The filepath is relative to the current directory. If you want to use\n\
7433an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007434the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007435
7436static PyObject *
7437win32_startfile(PyObject *self, PyObject *args)
7438{
Victor Stinner8c62be82010-05-06 00:08:46 +00007439 PyObject *ofilepath;
7440 char *filepath;
7441 char *operation = NULL;
7442 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007443
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 PyObject *unipath, *woperation = NULL;
7445 if (!PyArg_ParseTuple(args, "U|s:startfile",
7446 &unipath, &operation)) {
7447 PyErr_Clear();
7448 goto normal;
7449 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007450
Victor Stinner8c62be82010-05-06 00:08:46 +00007451 if (operation) {
7452 woperation = PyUnicode_DecodeASCII(operation,
7453 strlen(operation), NULL);
7454 if (!woperation) {
7455 PyErr_Clear();
7456 operation = NULL;
7457 goto normal;
7458 }
7459 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007460
Victor Stinner8c62be82010-05-06 00:08:46 +00007461 Py_BEGIN_ALLOW_THREADS
7462 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7463 PyUnicode_AS_UNICODE(unipath),
7464 NULL, NULL, SW_SHOWNORMAL);
7465 Py_END_ALLOW_THREADS
7466
7467 Py_XDECREF(woperation);
7468 if (rc <= (HINSTANCE)32) {
7469 PyObject *errval = win32_error_unicode("startfile",
7470 PyUnicode_AS_UNICODE(unipath));
7471 return errval;
7472 }
7473 Py_INCREF(Py_None);
7474 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007475
7476normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007477 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7478 PyUnicode_FSConverter, &ofilepath,
7479 &operation))
7480 return NULL;
7481 filepath = PyBytes_AsString(ofilepath);
7482 Py_BEGIN_ALLOW_THREADS
7483 rc = ShellExecute((HWND)0, operation, filepath,
7484 NULL, NULL, SW_SHOWNORMAL);
7485 Py_END_ALLOW_THREADS
7486 if (rc <= (HINSTANCE)32) {
7487 PyObject *errval = win32_error("startfile", filepath);
7488 Py_DECREF(ofilepath);
7489 return errval;
7490 }
7491 Py_DECREF(ofilepath);
7492 Py_INCREF(Py_None);
7493 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007494}
7495#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007496
Martin v. Löwis438b5342002-12-27 10:16:42 +00007497#ifdef HAVE_GETLOADAVG
7498PyDoc_STRVAR(posix_getloadavg__doc__,
7499"getloadavg() -> (float, float, float)\n\n\
7500Return the number of processes in the system run queue averaged over\n\
7501the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7502was unobtainable");
7503
7504static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007505posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007506{
7507 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007508 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007509 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7510 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007511 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007512 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007513}
7514#endif
7515
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007516#ifdef MS_WINDOWS
7517
7518PyDoc_STRVAR(win32_urandom__doc__,
7519"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007520Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007521
7522typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7523 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7524 DWORD dwFlags );
7525typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7526 BYTE *pbBuffer );
7527
7528static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007529/* This handle is never explicitly released. Instead, the operating
7530 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007531static HCRYPTPROV hCryptProv = 0;
7532
Tim Peters4ad82172004-08-30 17:02:04 +00007533static PyObject*
7534win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007535{
Victor Stinner8c62be82010-05-06 00:08:46 +00007536 int howMany;
7537 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007538
Victor Stinner8c62be82010-05-06 00:08:46 +00007539 /* Read arguments */
7540 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7541 return NULL;
7542 if (howMany < 0)
7543 return PyErr_Format(PyExc_ValueError,
7544 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007545
Victor Stinner8c62be82010-05-06 00:08:46 +00007546 if (hCryptProv == 0) {
7547 HINSTANCE hAdvAPI32 = NULL;
7548 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007549
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 /* Obtain handle to the DLL containing CryptoAPI
7551 This should not fail */
7552 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7553 if(hAdvAPI32 == NULL)
7554 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007555
Victor Stinner8c62be82010-05-06 00:08:46 +00007556 /* Obtain pointers to the CryptoAPI functions
7557 This will fail on some early versions of Win95 */
7558 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7559 hAdvAPI32,
7560 "CryptAcquireContextA");
7561 if (pCryptAcquireContext == NULL)
7562 return PyErr_Format(PyExc_NotImplementedError,
7563 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007564
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7566 hAdvAPI32, "CryptGenRandom");
7567 if (pCryptGenRandom == NULL)
7568 return PyErr_Format(PyExc_NotImplementedError,
7569 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007570
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 /* Acquire context */
7572 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7573 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7574 return win32_error("CryptAcquireContext", NULL);
7575 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007576
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 /* Allocate bytes */
7578 result = PyBytes_FromStringAndSize(NULL, howMany);
7579 if (result != NULL) {
7580 /* Get random data */
7581 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7582 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7583 PyBytes_AS_STRING(result))) {
7584 Py_DECREF(result);
7585 return win32_error("CryptGenRandom", NULL);
7586 }
7587 }
7588 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007589}
7590#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007591
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007592PyDoc_STRVAR(device_encoding__doc__,
7593"device_encoding(fd) -> str\n\n\
7594Return a string describing the encoding of the device\n\
7595if the output is a terminal; else return None.");
7596
7597static PyObject *
7598device_encoding(PyObject *self, PyObject *args)
7599{
Victor Stinner8c62be82010-05-06 00:08:46 +00007600 int fd;
7601 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7602 return NULL;
7603 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7604 Py_INCREF(Py_None);
7605 return Py_None;
7606 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007607#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007608 if (fd == 0) {
7609 char buf[100];
7610 sprintf(buf, "cp%d", GetConsoleCP());
7611 return PyUnicode_FromString(buf);
7612 }
7613 if (fd == 1 || fd == 2) {
7614 char buf[100];
7615 sprintf(buf, "cp%d", GetConsoleOutputCP());
7616 return PyUnicode_FromString(buf);
7617 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007618#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007619 {
7620 char *codeset = nl_langinfo(CODESET);
7621 if (codeset != NULL && codeset[0] != 0)
7622 return PyUnicode_FromString(codeset);
7623 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007624#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 Py_INCREF(Py_None);
7626 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007627}
7628
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007629#ifdef __VMS
7630/* Use openssl random routine */
7631#include <openssl/rand.h>
7632PyDoc_STRVAR(vms_urandom__doc__,
7633"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007634Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007635
7636static PyObject*
7637vms_urandom(PyObject *self, PyObject *args)
7638{
Victor Stinner8c62be82010-05-06 00:08:46 +00007639 int howMany;
7640 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007641
Victor Stinner8c62be82010-05-06 00:08:46 +00007642 /* Read arguments */
7643 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7644 return NULL;
7645 if (howMany < 0)
7646 return PyErr_Format(PyExc_ValueError,
7647 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007648
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 /* Allocate bytes */
7650 result = PyBytes_FromStringAndSize(NULL, howMany);
7651 if (result != NULL) {
7652 /* Get random data */
7653 if (RAND_pseudo_bytes((unsigned char*)
7654 PyBytes_AS_STRING(result),
7655 howMany) < 0) {
7656 Py_DECREF(result);
7657 return PyErr_Format(PyExc_ValueError,
7658 "RAND_pseudo_bytes");
7659 }
7660 }
7661 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007662}
7663#endif
7664
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007665#ifdef HAVE_SETRESUID
7666PyDoc_STRVAR(posix_setresuid__doc__,
7667"setresuid(ruid, euid, suid)\n\n\
7668Set the current process's real, effective, and saved user ids.");
7669
7670static PyObject*
7671posix_setresuid (PyObject *self, PyObject *args)
7672{
Victor Stinner8c62be82010-05-06 00:08:46 +00007673 /* We assume uid_t is no larger than a long. */
7674 long ruid, euid, suid;
7675 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7676 return NULL;
7677 if (setresuid(ruid, euid, suid) < 0)
7678 return posix_error();
7679 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007680}
7681#endif
7682
7683#ifdef HAVE_SETRESGID
7684PyDoc_STRVAR(posix_setresgid__doc__,
7685"setresgid(rgid, egid, sgid)\n\n\
7686Set the current process's real, effective, and saved group ids.");
7687
7688static PyObject*
7689posix_setresgid (PyObject *self, PyObject *args)
7690{
Victor Stinner8c62be82010-05-06 00:08:46 +00007691 /* We assume uid_t is no larger than a long. */
7692 long rgid, egid, sgid;
7693 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7694 return NULL;
7695 if (setresgid(rgid, egid, sgid) < 0)
7696 return posix_error();
7697 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007698}
7699#endif
7700
7701#ifdef HAVE_GETRESUID
7702PyDoc_STRVAR(posix_getresuid__doc__,
7703"getresuid() -> (ruid, euid, suid)\n\n\
7704Get tuple of the current process's real, effective, and saved user ids.");
7705
7706static PyObject*
7707posix_getresuid (PyObject *self, PyObject *noargs)
7708{
Victor Stinner8c62be82010-05-06 00:08:46 +00007709 uid_t ruid, euid, suid;
7710 long l_ruid, l_euid, l_suid;
7711 if (getresuid(&ruid, &euid, &suid) < 0)
7712 return posix_error();
7713 /* Force the values into long's as we don't know the size of uid_t. */
7714 l_ruid = ruid;
7715 l_euid = euid;
7716 l_suid = suid;
7717 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007718}
7719#endif
7720
7721#ifdef HAVE_GETRESGID
7722PyDoc_STRVAR(posix_getresgid__doc__,
7723"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007724Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007725
7726static PyObject*
7727posix_getresgid (PyObject *self, PyObject *noargs)
7728{
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 uid_t rgid, egid, sgid;
7730 long l_rgid, l_egid, l_sgid;
7731 if (getresgid(&rgid, &egid, &sgid) < 0)
7732 return posix_error();
7733 /* Force the values into long's as we don't know the size of uid_t. */
7734 l_rgid = rgid;
7735 l_egid = egid;
7736 l_sgid = sgid;
7737 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007738}
7739#endif
7740
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007741static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007742 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007743#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007744 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007745#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007747#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007748 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007749#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007750 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007751#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007752 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007753#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007754#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007755 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007756#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007757#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007758 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007759#endif /* HAVE_LCHMOD */
7760#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007761 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007762#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007763#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007764 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007765#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007766#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007768#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007769#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007770 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007771#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007772#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007774#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007775#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7777 METH_NOARGS, posix_getcwd__doc__},
7778 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7779 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007780#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007781#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007783#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7785 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7786 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007787#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007788 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007789#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007790#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007791 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007792#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007793#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007794 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007795#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007796 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7797 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7798 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007799 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007800#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007802#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007803#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
7804 {"_symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
7805 win_symlink__doc__},
7806#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007807#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007809#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007810 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007811#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007813#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7815 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7816 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007817#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007818 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007819#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007821#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7823 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007824#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007825#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007826 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7827 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007828#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7830 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007831#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007832#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007833#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007835#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007836#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007837 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007838#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007839#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007840 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007841#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007842#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007843 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007844#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007845#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007846 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007847#endif /* HAVE_GETEGID */
7848#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007849 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007850#endif /* HAVE_GETEUID */
7851#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007852 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007853#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007854#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007856#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007858#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007860#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007861#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007863#endif /* HAVE_GETPPID */
7864#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007866#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007867#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007869#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007870#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007872#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007873#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007875#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007876#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007878#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007879#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7881 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007882 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007883#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007884#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007885 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007886#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007887#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007888 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007889#endif /* HAVE_SETEUID */
7890#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007891 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007892#endif /* HAVE_SETEGID */
7893#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007894 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007895#endif /* HAVE_SETREUID */
7896#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007898#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007899#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007900 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007901#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007902#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007903 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007904#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007905#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007907#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007908#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007910#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007911#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007913#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007914#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007916#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007917#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007919#endif /* HAVE_WAIT3 */
7920#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007922#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007923#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007925#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007926#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007927 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007928#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007929#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007930 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007931#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007932#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007933 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007934#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007935#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007937#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007938#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007940#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7942 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7943 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7944 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7945 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7946 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7947 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7948 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7949 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7950 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7951 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007952#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007953 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007954#endif
7955#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007956 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007957#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007958#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007959 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007960#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007961#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007962 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7963 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7964 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007965#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007966#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007967 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007968#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007969#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007970 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007971#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007972#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007973 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007974#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007976#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007978#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007979#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007981#endif
7982#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007984#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007985#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007986#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007988#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007989#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007991#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007992#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007993 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007994#endif /* WIFSTOPPED */
7995#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007996 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007997#endif /* WIFSIGNALED */
7998#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008000#endif /* WIFEXITED */
8001#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008003#endif /* WEXITSTATUS */
8004#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008005 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008006#endif /* WTERMSIG */
8007#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008008 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008009#endif /* WSTOPSIG */
8010#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008011#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008012 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008013#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008014#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008016#endif
Fred Drakec9680921999-12-13 16:37:25 +00008017#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008018 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008019#endif
8020#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008022#endif
8023#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008025#endif
8026#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008028#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008030#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008031 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008032 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008033 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008034#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008035#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008037#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008038 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008040 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008041 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008043 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008044#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008046#endif
8047#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008049#endif
8050#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008052#endif
8053#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008054 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008055#endif
8056
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008058};
8059
8060
Barry Warsaw4a342091996-12-19 23:50:02 +00008061static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008062ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008063{
Victor Stinner8c62be82010-05-06 00:08:46 +00008064 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008065}
8066
Guido van Rossumd48f2521997-12-05 22:19:34 +00008067#if defined(PYOS_OS2)
8068/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008069static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008070{
8071 APIRET rc;
8072 ULONG values[QSV_MAX+1];
8073 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008074 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008075
8076 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008077 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008078 Py_END_ALLOW_THREADS
8079
8080 if (rc != NO_ERROR) {
8081 os2_error(rc);
8082 return -1;
8083 }
8084
Fred Drake4d1e64b2002-04-15 19:40:07 +00008085 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8086 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8087 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8088 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8089 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8090 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8091 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008092
8093 switch (values[QSV_VERSION_MINOR]) {
8094 case 0: ver = "2.00"; break;
8095 case 10: ver = "2.10"; break;
8096 case 11: ver = "2.11"; break;
8097 case 30: ver = "3.00"; break;
8098 case 40: ver = "4.00"; break;
8099 case 50: ver = "5.00"; break;
8100 default:
Tim Peters885d4572001-11-28 20:27:42 +00008101 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008103 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008104 ver = &tmp[0];
8105 }
8106
8107 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008108 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008109 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008110
8111 /* Add Indicator of Which Drive was Used to Boot the System */
8112 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8113 tmp[1] = ':';
8114 tmp[2] = '\0';
8115
Fred Drake4d1e64b2002-04-15 19:40:07 +00008116 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008117}
8118#endif
8119
Brian Curtin52173d42010-12-02 18:29:18 +00008120#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
8121void
8122enable_symlink()
8123{
8124 HANDLE tok;
8125 TOKEN_PRIVILEGES tok_priv;
8126 LUID luid;
8127 int meth_idx = 0;
8128
8129 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
8130 return;
8131
8132 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
8133 return;
8134
8135 tok_priv.PrivilegeCount = 1;
8136 tok_priv.Privileges[0].Luid = luid;
8137 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8138
8139 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8140 sizeof(TOKEN_PRIVILEGES),
8141 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
8142 return;
8143
8144 if(GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
8145 /* We couldn't acquire the necessary privilege, so leave the
8146 method hidden for this user. */
8147 return;
8148 } else {
8149 /* We've successfully acquired the symlink privilege so rename
8150 the method to it's proper "os.symlink" name. */
8151 while(posix_methods[meth_idx].ml_meth != (PyCFunction)win_symlink)
8152 meth_idx++;
8153 posix_methods[meth_idx].ml_name = "symlink";
8154
8155 return;
8156 }
8157}
8158#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8159
Barry Warsaw4a342091996-12-19 23:50:02 +00008160static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008161all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008162{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008163#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008164 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008165#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008166#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008167 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008168#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008169#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008170 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008171#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008172#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008173 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008174#endif
Fred Drakec9680921999-12-13 16:37:25 +00008175#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008176 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008177#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008178#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008179 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008180#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008181#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008182 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008183#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008184#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008185 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008186#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008187#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008188 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008189#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008190#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008191 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008192#endif
8193#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008194 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008195#endif
8196#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008197 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008198#endif
8199#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008200 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008201#endif
8202#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008203 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008204#endif
8205#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008206 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008207#endif
8208#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008209 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008210#endif
8211#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008212 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008213#endif
8214#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008215 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008216#endif
8217#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008218 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008219#endif
8220#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008221 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008222#endif
8223#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008224 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008225#endif
8226#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008227 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008228#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008229#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008230 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008231#endif
8232#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008233 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008234#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008235#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008236 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008237#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008238#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008239 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008240#endif
8241#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008242 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008243#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008244
Tim Peters5aa91602002-01-30 05:46:57 +00008245/* MS Windows */
8246#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 /* Don't inherit in child processes. */
8248 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008249#endif
8250#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008251 /* Optimize for short life (keep in memory). */
8252 /* MS forgot to define this one with a non-underscore form too. */
8253 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008254#endif
8255#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008256 /* Automatically delete when last handle is closed. */
8257 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008258#endif
8259#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008260 /* Optimize for random access. */
8261 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008262#endif
8263#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 /* Optimize for sequential access. */
8265 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008266#endif
8267
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008268/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008269#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 /* Send a SIGIO signal whenever input or output
8271 becomes available on file descriptor */
8272 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008273#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008274#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 /* Direct disk access. */
8276 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008277#endif
8278#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008279 /* Must be a directory. */
8280 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008281#endif
8282#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 /* Do not follow links. */
8284 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008285#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008286#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 /* Do not update the access time. */
8288 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008289#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008290
Victor Stinner8c62be82010-05-06 00:08:46 +00008291 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008292#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008294#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008295#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008297#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008298#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008299 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008300#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008301#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008302 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008303#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008304#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008306#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008307#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008308 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008309#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008310#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008312#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008313#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008315#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008316#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008317 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008318#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008319#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008320 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008321#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008322#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008323 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008324#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008325#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008327#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008328#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008329 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008330#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008331#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008332 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008333#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008334#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008336#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008337#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008338 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008339#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008340#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008341 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008342#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008343
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008344 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008345#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008346 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008347#endif /* ST_RDONLY */
8348#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008349 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008350#endif /* ST_NOSUID */
8351
Guido van Rossum246bc171999-02-01 23:54:31 +00008352#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008353#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008354 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8355 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8356 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8357 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8358 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8359 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8360 if (ins(d, "P_PM", (long)P_PM)) return -1;
8361 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8362 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8363 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8364 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8365 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8366 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8367 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8368 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8369 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8370 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8371 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8372 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8373 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008374#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8376 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8377 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8378 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8379 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008380#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008381#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008382
Guido van Rossumd48f2521997-12-05 22:19:34 +00008383#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008384 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008385#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008386 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008387}
8388
8389
Tim Peters5aa91602002-01-30 05:46:57 +00008390#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008391#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008392#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008393
8394#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008395#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008396#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008397
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008398#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008399#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008400#define MODNAME "posix"
8401#endif
8402
Martin v. Löwis1a214512008-06-11 05:26:20 +00008403static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 PyModuleDef_HEAD_INIT,
8405 MODNAME,
8406 posix__doc__,
8407 -1,
8408 posix_methods,
8409 NULL,
8410 NULL,
8411 NULL,
8412 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008413};
8414
8415
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008416PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008417INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008418{
Victor Stinner8c62be82010-05-06 00:08:46 +00008419 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008420
Brian Curtin52173d42010-12-02 18:29:18 +00008421#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
8422 enable_symlink();
8423#endif
8424
Victor Stinner8c62be82010-05-06 00:08:46 +00008425 m = PyModule_Create(&posixmodule);
8426 if (m == NULL)
8427 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008428
Victor Stinner8c62be82010-05-06 00:08:46 +00008429 /* Initialize environ dictionary */
8430 v = convertenviron();
8431 Py_XINCREF(v);
8432 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8433 return NULL;
8434 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008435
Victor Stinner8c62be82010-05-06 00:08:46 +00008436 if (all_ins(m))
8437 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008438
Victor Stinner8c62be82010-05-06 00:08:46 +00008439 if (setup_confname_tables(m))
8440 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008441
Victor Stinner8c62be82010-05-06 00:08:46 +00008442 Py_INCREF(PyExc_OSError);
8443 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008444
Guido van Rossumb3d39562000-01-31 18:41:26 +00008445#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008446 if (posix_putenv_garbage == NULL)
8447 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008448#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008449
Victor Stinner8c62be82010-05-06 00:08:46 +00008450 if (!initialized) {
8451 stat_result_desc.name = MODNAME ".stat_result";
8452 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8453 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8454 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8455 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8456 structseq_new = StatResultType.tp_new;
8457 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008458
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 statvfs_result_desc.name = MODNAME ".statvfs_result";
8460 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008461#ifdef NEED_TICKS_PER_SECOND
8462# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008463 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008464# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008466# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008468# endif
8469#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 }
8471 Py_INCREF((PyObject*) &StatResultType);
8472 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8473 Py_INCREF((PyObject*) &StatVFSResultType);
8474 PyModule_AddObject(m, "statvfs_result",
8475 (PyObject*) &StatVFSResultType);
8476 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008477
8478#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008479 /*
8480 * Step 2 of weak-linking support on Mac OS X.
8481 *
8482 * The code below removes functions that are not available on the
8483 * currently active platform.
8484 *
8485 * This block allow one to use a python binary that was build on
8486 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8487 * OSX 10.4.
8488 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008489#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008490 if (fstatvfs == NULL) {
8491 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8492 return NULL;
8493 }
8494 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008495#endif /* HAVE_FSTATVFS */
8496
8497#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008498 if (statvfs == NULL) {
8499 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8500 return NULL;
8501 }
8502 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008503#endif /* HAVE_STATVFS */
8504
8505# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008506 if (lchown == NULL) {
8507 if (PyObject_DelAttrString(m, "lchown") == -1) {
8508 return NULL;
8509 }
8510 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008511#endif /* HAVE_LCHOWN */
8512
8513
8514#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008516
Guido van Rossumb6775db1994-08-01 11:34:53 +00008517}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008518
8519#ifdef __cplusplus
8520}
8521#endif