blob: 0b7f3f09bc714bf0e0be2f1ded5dee075f68105c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#endif /* defined(__VMS) */
35
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#ifdef __cplusplus
37extern "C" {
38#endif
39
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000041"This module provides access to operating system functionality that is\n\
42standardized by the C Standard and the POSIX standard (a thinly\n\
43disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000046
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#if defined(PYOS_OS2)
48#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_TYPES_H */
65
66#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000069
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000071#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000072#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000073
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000075#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000077
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#ifdef HAVE_FCNTL_H
79#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000080#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000081
Guido van Rossuma6535fd2001-10-18 19:44:10 +000082#ifdef HAVE_GRP_H
83#include <grp.h>
84#endif
85
Barry Warsaw5676bd12003-01-07 20:57:09 +000086#ifdef HAVE_SYSEXITS_H
87#include <sysexits.h>
88#endif /* HAVE_SYSEXITS_H */
89
Anthony Baxter8a560de2004-10-13 15:30:56 +000090#ifdef HAVE_SYS_LOADAVG_H
91#include <sys/loadavg.h>
92#endif
93
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000094#ifdef HAVE_LANGINFO_H
95#include <langinfo.h>
96#endif
97
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000099/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000100#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#include <process.h>
102#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000103#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000104#define HAVE_GETCWD 1
105#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000106#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#if defined(__OS2__)
108#define HAVE_EXECV 1
109#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000110#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000111#include <process.h>
112#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000113#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_EXECV 1
115#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_OPENDIR 1
117#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000118#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_WAIT 1
120#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000121#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000122#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000123#define HAVE_GETPPID 1
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000124#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000128#define HAVE_SYSTEM 1
129#define HAVE_CWAIT 1
130#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000132#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000133#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
134/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136/* Unix functions that the configure script doesn't check for */
137#define HAVE_EXECV 1
138#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#define HAVE_FORK1 1
141#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_GETCWD 1
143#define HAVE_GETEGID 1
144#define HAVE_GETEUID 1
145#define HAVE_GETGID 1
146#define HAVE_GETPPID 1
147#define HAVE_GETUID 1
148#define HAVE_KILL 1
149#define HAVE_OPENDIR 1
150#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000154#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#endif /* _MSC_VER */
156#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000157#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000159
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000161
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000162#if defined(__sgi)&&_COMPILER_VERSION>=700
163/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 (default) */
165extern char *ctermid_r(char *);
166#endif
167
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000168#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000170extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000172#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000177#endif
178#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int chdir(char *);
180extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000182extern int chdir(const char *);
183extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000185#ifdef __BORLANDC__
186extern int chmod(const char *, int);
187#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000188extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000190/*#ifdef HAVE_FCHMOD
191extern int fchmod(int, mode_t);
192#endif*/
193/*#ifdef HAVE_LCHMOD
194extern int lchmod(const char *, mode_t);
195#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int chown(const char *, uid_t, gid_t);
197extern char *getcwd(char *, int);
198extern char *strerror(int);
199extern int link(const char *, const char *);
200extern int rename(const char *, const char *);
201extern int stat(const char *, struct stat *);
202extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000205#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000207extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000211#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#ifdef HAVE_UTIME_H
214#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000215#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000217#ifdef HAVE_SYS_UTIME_H
218#include <sys/utime.h>
219#define HAVE_UTIME_H /* pretend we do for the rest of this file */
220#endif /* HAVE_SYS_UTIME_H */
221
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#ifdef HAVE_SYS_TIMES_H
223#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000224#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
226#ifdef HAVE_SYS_PARAM_H
227#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000228#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229
230#ifdef HAVE_SYS_UTSNAME_H
231#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000232#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000234#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) strlen((dirent)->d_name)
237#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000238#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#include <direct.h>
240#define NAMLEN(dirent) strlen((dirent)->d_name)
241#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000247#endif
248#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#endif
251#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#endif
254#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259#endif
260#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262#endif
263#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000266#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000267#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000268#endif
269#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000270#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000271#endif
272#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000273#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000274#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000276#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000278#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000279#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000280#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
281#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000282static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000283#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285
Guido van Rossumd48f2521997-12-05 22:19:34 +0000286#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000288#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289
Tim Petersbc2e10e2002-03-03 23:17:02 +0000290#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000291#if defined(PATH_MAX) && PATH_MAX > 1024
292#define MAXPATHLEN PATH_MAX
293#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000294#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000295#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000296#endif /* MAXPATHLEN */
297
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000298#ifdef UNION_WAIT
299/* Emulate some macros on systems that have a union instead of macros */
300
301#ifndef WIFEXITED
302#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
303#endif
304
305#ifndef WEXITSTATUS
306#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
307#endif
308
309#ifndef WTERMSIG
310#define WTERMSIG(u_wait) ((u_wait).w_termsig)
311#endif
312
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313#define WAIT_TYPE union wait
314#define WAIT_STATUS_INT(s) (s.w_status)
315
316#else /* !UNION_WAIT */
317#define WAIT_TYPE int
318#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000319#endif /* UNION_WAIT */
320
Greg Wardb48bc172000-03-01 21:51:56 +0000321/* Don't use the "_r" form if we don't need it (also, won't have a
322 prototype for it, at least on Solaris -- maybe others as well?). */
323#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
324#define USE_CTERMID_R
325#endif
326
Fred Drake699f3522000-06-29 21:12:41 +0000327/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000328#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000329#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000330# define STAT win32_stat
331# define FSTAT win32_fstat
332# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000333#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000334# define STAT stat
335# define FSTAT fstat
336# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000337#endif
338
Tim Peters11b23062003-04-23 02:39:17 +0000339#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000340#include <sys/mkdev.h>
341#else
342#if defined(MAJOR_IN_SYSMACROS)
343#include <sys/sysmacros.h>
344#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000345#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
346#include <sys/mkdev.h>
347#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000348#endif
Fred Drake699f3522000-06-29 21:12:41 +0000349
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000350#if defined _MSC_VER && _MSC_VER >= 1400
351/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
352 * valid and throw an assertion if it isn't.
353 * Normally, an invalid fd is likely to be a C program error and therefore
354 * an assertion can be useful, but it does contradict the POSIX standard
355 * which for write(2) states:
356 * "Otherwise, -1 shall be returned and errno set to indicate the error."
357 * "[EBADF] The fildes argument is not a valid file descriptor open for
358 * writing."
359 * Furthermore, python allows the user to enter any old integer
360 * as a fd and should merely raise a python exception on error.
361 * The Microsoft CRT doesn't provide an official way to check for the
362 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000363 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000364 * internal structures involved.
365 * The structures below must be updated for each version of visual studio
366 * according to the file internal.h in the CRT source, until MS comes
367 * up with a less hacky way to do this.
368 * (all of this is to avoid globally modifying the CRT behaviour using
369 * _set_invalid_parameter_handler() and _CrtSetReportMode())
370 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000371/* The actual size of the structure is determined at runtime.
372 * Only the first items must be present.
373 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000374typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000375 intptr_t osfhnd;
376 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000377} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000378
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380#define IOINFO_L2E 5
381#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
382#define IOINFO_ARRAYS 64
383#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
384#define FOPEN 0x01
385#define _NO_CONSOLE_FILENO (intptr_t)-2
386
387/* This function emulates what the windows CRT does to validate file handles */
388int
389_PyVerify_fd(int fd)
390{
Victor Stinner8c62be82010-05-06 00:08:46 +0000391 const int i1 = fd >> IOINFO_L2E;
392 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000393
Antoine Pitrou22e41552010-08-15 18:07:50 +0000394 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000395
Victor Stinner8c62be82010-05-06 00:08:46 +0000396 /* Determine the actual size of the ioinfo structure,
397 * as used by the CRT loaded in memory
398 */
399 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
400 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
401 }
402 if (sizeof_ioinfo == 0) {
403 /* This should not happen... */
404 goto fail;
405 }
406
407 /* See that it isn't a special CLEAR fileno */
408 if (fd != _NO_CONSOLE_FILENO) {
409 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
410 * we check pointer validity and other info
411 */
412 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
413 /* finally, check that the file is open */
414 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
415 if (info->osfile & FOPEN) {
416 return 1;
417 }
418 }
419 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000420 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000421 errno = EBADF;
422 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000423}
424
425/* the special case of checking dup2. The target fd must be in a sensible range */
426static int
427_PyVerify_fd_dup2(int fd1, int fd2)
428{
Victor Stinner8c62be82010-05-06 00:08:46 +0000429 if (!_PyVerify_fd(fd1))
430 return 0;
431 if (fd2 == _NO_CONSOLE_FILENO)
432 return 0;
433 if ((unsigned)fd2 < _NHANDLE_)
434 return 1;
435 else
436 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000437}
438#else
439/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
440#define _PyVerify_fd_dup2(A, B) (1)
441#endif
442
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000443#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000444/* The following structure was copied from
445 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
446 include doesn't seem to be present in the Windows SDK (at least as included
447 with Visual Studio Express). */
448typedef struct _REPARSE_DATA_BUFFER {
449 ULONG ReparseTag;
450 USHORT ReparseDataLength;
451 USHORT Reserved;
452 union {
453 struct {
454 USHORT SubstituteNameOffset;
455 USHORT SubstituteNameLength;
456 USHORT PrintNameOffset;
457 USHORT PrintNameLength;
458 ULONG Flags;
459 WCHAR PathBuffer[1];
460 } SymbolicLinkReparseBuffer;
461
462 struct {
463 USHORT SubstituteNameOffset;
464 USHORT SubstituteNameLength;
465 USHORT PrintNameOffset;
466 USHORT PrintNameLength;
467 WCHAR PathBuffer[1];
468 } MountPointReparseBuffer;
469
470 struct {
471 UCHAR DataBuffer[1];
472 } GenericReparseBuffer;
473 };
474} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
475
476#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
477 GenericReparseBuffer)
478#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
479
480static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000481win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000482{
483 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
484 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
485 DWORD n_bytes_returned;
486 const wchar_t *ptr;
487 wchar_t *buf;
488 size_t len;
489
490 if (0 == DeviceIoControl(
491 reparse_point_handle,
492 FSCTL_GET_REPARSE_POINT,
493 NULL, 0, /* in buffer */
494 target_buffer, sizeof(target_buffer),
495 &n_bytes_returned,
496 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000497 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000498
499 if (reparse_tag)
500 *reparse_tag = rdb->ReparseTag;
501
502 if (target_path) {
503 switch (rdb->ReparseTag) {
504 case IO_REPARSE_TAG_SYMLINK:
505 /* XXX: Maybe should use SubstituteName? */
506 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
507 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
508 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
509 break;
510 case IO_REPARSE_TAG_MOUNT_POINT:
511 ptr = rdb->MountPointReparseBuffer.PathBuffer +
512 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
513 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
514 break;
515 default:
516 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000517 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000518 }
519 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
520 if (!buf) {
521 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000522 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000523 }
524 wcsncpy(buf, ptr, len);
525 buf[len] = L'\0';
526 if (wcsncmp(buf, L"\\??\\", 4) == 0)
527 buf[1] = L'\\';
528 *target_path = buf;
529 }
530
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000531 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000532}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000533#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000534
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000536#ifdef WITH_NEXT_FRAMEWORK
537/* On Darwin/MacOSX a shared library or framework has no access to
538** environ directly, we must obtain it with _NSGetEnviron().
539*/
540#include <crt_externs.h>
541static char **environ;
542#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000544#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545
Barry Warsaw53699e91996-12-10 23:23:01 +0000546static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000547convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548{
Victor Stinner8c62be82010-05-06 00:08:46 +0000549 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000550#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000551 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000552#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000553 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000554#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000555#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 APIRET rc;
557 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
558#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000559
Victor Stinner8c62be82010-05-06 00:08:46 +0000560 d = PyDict_New();
561 if (d == NULL)
562 return NULL;
563#ifdef WITH_NEXT_FRAMEWORK
564 if (environ == NULL)
565 environ = *_NSGetEnviron();
566#endif
567#ifdef MS_WINDOWS
568 /* _wenviron must be initialized in this way if the program is started
569 through main() instead of wmain(). */
570 _wgetenv(L"");
571 if (_wenviron == NULL)
572 return d;
573 /* This part ignores errors */
574 for (e = _wenviron; *e != NULL; e++) {
575 PyObject *k;
576 PyObject *v;
577 wchar_t *p = wcschr(*e, L'=');
578 if (p == NULL)
579 continue;
580 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
581 if (k == NULL) {
582 PyErr_Clear();
583 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000584 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000585 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
586 if (v == NULL) {
587 PyErr_Clear();
588 Py_DECREF(k);
589 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000590 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000591 if (PyDict_GetItem(d, k) == NULL) {
592 if (PyDict_SetItem(d, k, v) != 0)
593 PyErr_Clear();
594 }
595 Py_DECREF(k);
596 Py_DECREF(v);
597 }
598#else
599 if (environ == NULL)
600 return d;
601 /* This part ignores errors */
602 for (e = environ; *e != NULL; e++) {
603 PyObject *k;
604 PyObject *v;
605 char *p = strchr(*e, '=');
606 if (p == NULL)
607 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000608 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000609 if (k == NULL) {
610 PyErr_Clear();
611 continue;
612 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000613 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000614 if (v == NULL) {
615 PyErr_Clear();
616 Py_DECREF(k);
617 continue;
618 }
619 if (PyDict_GetItem(d, k) == NULL) {
620 if (PyDict_SetItem(d, k, v) != 0)
621 PyErr_Clear();
622 }
623 Py_DECREF(k);
624 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000625 }
626#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000627#if defined(PYOS_OS2)
628 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
629 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
630 PyObject *v = PyBytes_FromString(buffer);
631 PyDict_SetItemString(d, "BEGINLIBPATH", v);
632 Py_DECREF(v);
633 }
634 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
635 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
636 PyObject *v = PyBytes_FromString(buffer);
637 PyDict_SetItemString(d, "ENDLIBPATH", v);
638 Py_DECREF(v);
639 }
640#endif
641 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644/* Set a POSIX-specific error from errno, and return NULL */
645
Barry Warsawd58d7641998-07-23 16:14:40 +0000646static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000647posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000648{
Victor Stinner8c62be82010-05-06 00:08:46 +0000649 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650}
Barry Warsawd58d7641998-07-23 16:14:40 +0000651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000652posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000653{
Victor Stinner8c62be82010-05-06 00:08:46 +0000654 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000655}
656
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000657
Mark Hammondef8b6542001-05-13 08:04:26 +0000658static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000659posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000660{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000661 PyObject *name_str, *rc;
662 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
663 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000664 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000665 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
666 name_str);
667 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000668 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000669}
670
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000671#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000672static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000673win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000674{
Victor Stinner8c62be82010-05-06 00:08:46 +0000675 /* XXX We should pass the function name along in the future.
676 (winreg.c also wants to pass the function name.)
677 This would however require an additional param to the
678 Windows error object, which is non-trivial.
679 */
680 errno = GetLastError();
681 if (filename)
682 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
683 else
684 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000685}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000687static PyObject *
688win32_error_unicode(char* function, Py_UNICODE* filename)
689{
Victor Stinner8c62be82010-05-06 00:08:46 +0000690 /* XXX - see win32_error for comments on 'function' */
691 errno = GetLastError();
692 if (filename)
693 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
694 else
695 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000696}
697
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000699convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700{
Victor Stinner8c62be82010-05-06 00:08:46 +0000701 if (PyUnicode_CheckExact(*param))
702 Py_INCREF(*param);
703 else if (PyUnicode_Check(*param))
704 /* For a Unicode subtype that's not a Unicode object,
705 return a true Unicode object with the same data. */
706 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
707 PyUnicode_GET_SIZE(*param));
708 else
709 *param = PyUnicode_FromEncodedObject(*param,
710 Py_FileSystemDefaultEncoding,
711 "strict");
712 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000713}
714
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000715#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716
Guido van Rossumd48f2521997-12-05 22:19:34 +0000717#if defined(PYOS_OS2)
718/**********************************************************************
719 * Helper Function to Trim and Format OS/2 Messages
720 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000721static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000722os2_formatmsg(char *msgbuf, int msglen, char *reason)
723{
724 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
725
726 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
727 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
728
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000729 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000730 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
731 }
732
733 /* Add Optional Reason Text */
734 if (reason) {
735 strcat(msgbuf, " : ");
736 strcat(msgbuf, reason);
737 }
738}
739
740/**********************************************************************
741 * Decode an OS/2 Operating System Error Code
742 *
743 * A convenience function to lookup an OS/2 error code and return a
744 * text message we can use to raise a Python exception.
745 *
746 * Notes:
747 * The messages for errors returned from the OS/2 kernel reside in
748 * the file OSO001.MSG in the \OS2 directory hierarchy.
749 *
750 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000751static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000752os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
753{
754 APIRET rc;
755 ULONG msglen;
756
757 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
758 Py_BEGIN_ALLOW_THREADS
759 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
760 errorcode, "oso001.msg", &msglen);
761 Py_END_ALLOW_THREADS
762
763 if (rc == NO_ERROR)
764 os2_formatmsg(msgbuf, msglen, reason);
765 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000766 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000767 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000768
769 return msgbuf;
770}
771
772/* Set an OS/2-specific error and return NULL. OS/2 kernel
773 errors are not in a global variable e.g. 'errno' nor are
774 they congruent with posix error numbers. */
775
Victor Stinner8c62be82010-05-06 00:08:46 +0000776static PyObject *
777os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000778{
779 char text[1024];
780 PyObject *v;
781
782 os2_strerror(text, sizeof(text), code, "");
783
784 v = Py_BuildValue("(is)", code, text);
785 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000786 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000787 Py_DECREF(v);
788 }
789 return NULL; /* Signal to Python that an Exception is Pending */
790}
791
792#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000793
794/* POSIX generic methods */
795
Barry Warsaw53699e91996-12-10 23:23:01 +0000796static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000797posix_fildes(PyObject *fdobj, int (*func)(int))
798{
Victor Stinner8c62be82010-05-06 00:08:46 +0000799 int fd;
800 int res;
801 fd = PyObject_AsFileDescriptor(fdobj);
802 if (fd < 0)
803 return NULL;
804 if (!_PyVerify_fd(fd))
805 return posix_error();
806 Py_BEGIN_ALLOW_THREADS
807 res = (*func)(fd);
808 Py_END_ALLOW_THREADS
809 if (res < 0)
810 return posix_error();
811 Py_INCREF(Py_None);
812 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000813}
Guido van Rossum21142a01999-01-08 21:05:37 +0000814
815static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000816posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817{
Victor Stinner8c62be82010-05-06 00:08:46 +0000818 PyObject *opath1 = NULL;
819 char *path1;
820 int res;
821 if (!PyArg_ParseTuple(args, format,
822 PyUnicode_FSConverter, &opath1))
823 return NULL;
824 path1 = PyBytes_AsString(opath1);
825 Py_BEGIN_ALLOW_THREADS
826 res = (*func)(path1);
827 Py_END_ALLOW_THREADS
828 if (res < 0)
829 return posix_error_with_allocated_filename(opath1);
830 Py_DECREF(opath1);
831 Py_INCREF(Py_None);
832 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833}
834
Barry Warsaw53699e91996-12-10 23:23:01 +0000835static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000836posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000837 char *format,
838 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839{
Victor Stinner8c62be82010-05-06 00:08:46 +0000840 PyObject *opath1 = NULL, *opath2 = NULL;
841 char *path1, *path2;
842 int res;
843 if (!PyArg_ParseTuple(args, format,
844 PyUnicode_FSConverter, &opath1,
845 PyUnicode_FSConverter, &opath2)) {
846 return NULL;
847 }
848 path1 = PyBytes_AsString(opath1);
849 path2 = PyBytes_AsString(opath2);
850 Py_BEGIN_ALLOW_THREADS
851 res = (*func)(path1, path2);
852 Py_END_ALLOW_THREADS
853 Py_DECREF(opath1);
854 Py_DECREF(opath2);
855 if (res != 0)
856 /* XXX how to report both path1 and path2??? */
857 return posix_error();
858 Py_INCREF(Py_None);
859 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860}
861
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000862#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000864win32_1str(PyObject* args, char* func,
865 char* format, BOOL (__stdcall *funcA)(LPCSTR),
866 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867{
Victor Stinner8c62be82010-05-06 00:08:46 +0000868 PyObject *uni;
869 char *ansi;
870 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000871
Victor Stinner8c62be82010-05-06 00:08:46 +0000872 if (!PyArg_ParseTuple(args, wformat, &uni))
873 PyErr_Clear();
874 else {
875 Py_BEGIN_ALLOW_THREADS
876 result = funcW(PyUnicode_AsUnicode(uni));
877 Py_END_ALLOW_THREADS
878 if (!result)
879 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
880 Py_INCREF(Py_None);
881 return Py_None;
882 }
883 if (!PyArg_ParseTuple(args, format, &ansi))
884 return NULL;
885 Py_BEGIN_ALLOW_THREADS
886 result = funcA(ansi);
887 Py_END_ALLOW_THREADS
888 if (!result)
889 return win32_error(func, ansi);
890 Py_INCREF(Py_None);
891 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892
893}
894
895/* This is a reimplementation of the C library's chdir function,
896 but one that produces Win32 errors instead of DOS error codes.
897 chdir is essentially a wrapper around SetCurrentDirectory; however,
898 it also needs to set "magic" environment variables indicating
899 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000900static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000901win32_chdir(LPCSTR path)
902{
Victor Stinner8c62be82010-05-06 00:08:46 +0000903 char new_path[MAX_PATH+1];
904 int result;
905 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906
Victor Stinner8c62be82010-05-06 00:08:46 +0000907 if(!SetCurrentDirectoryA(path))
908 return FALSE;
909 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
910 if (!result)
911 return FALSE;
912 /* In the ANSI API, there should not be any paths longer
913 than MAX_PATH. */
914 assert(result <= MAX_PATH+1);
915 if (strncmp(new_path, "\\\\", 2) == 0 ||
916 strncmp(new_path, "//", 2) == 0)
917 /* UNC path, nothing to do. */
918 return TRUE;
919 env[1] = new_path[0];
920 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921}
922
923/* The Unicode version differs from the ANSI version
924 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000925static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926win32_wchdir(LPCWSTR path)
927{
Victor Stinner8c62be82010-05-06 00:08:46 +0000928 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
929 int result;
930 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000931
Victor Stinner8c62be82010-05-06 00:08:46 +0000932 if(!SetCurrentDirectoryW(path))
933 return FALSE;
934 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
935 if (!result)
936 return FALSE;
937 if (result > MAX_PATH+1) {
938 new_path = malloc(result * sizeof(wchar_t));
939 if (!new_path) {
940 SetLastError(ERROR_OUTOFMEMORY);
941 return FALSE;
942 }
943 result = GetCurrentDirectoryW(result, new_path);
944 if (!result) {
945 free(new_path);
946 return FALSE;
947 }
948 }
949 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
950 wcsncmp(new_path, L"//", 2) == 0)
951 /* UNC path, nothing to do. */
952 return TRUE;
953 env[1] = new_path[0];
954 result = SetEnvironmentVariableW(env, new_path);
955 if (new_path != _new_path)
956 free(new_path);
957 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958}
959#endif
960
Martin v. Löwis14694662006-02-03 12:54:16 +0000961#ifdef MS_WINDOWS
962/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
963 - time stamps are restricted to second resolution
964 - file modification times suffer from forth-and-back conversions between
965 UTC and local time
966 Therefore, we implement our own stat, based on the Win32 API directly.
967*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000968#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000969
970struct win32_stat{
971 int st_dev;
972 __int64 st_ino;
973 unsigned short st_mode;
974 int st_nlink;
975 int st_uid;
976 int st_gid;
977 int st_rdev;
978 __int64 st_size;
979 int st_atime;
980 int st_atime_nsec;
981 int st_mtime;
982 int st_mtime_nsec;
983 int st_ctime;
984 int st_ctime_nsec;
985};
986
987static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
988
989static void
990FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
991{
Victor Stinner8c62be82010-05-06 00:08:46 +0000992 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
993 /* Cannot simply cast and dereference in_ptr,
994 since it might not be aligned properly */
995 __int64 in;
996 memcpy(&in, in_ptr, sizeof(in));
997 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
998 /* XXX Win32 supports time stamps past 2038; we currently don't */
999 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +00001000}
1001
Thomas Wouters477c8d52006-05-27 19:21:47 +00001002static void
1003time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
1004{
Victor Stinner8c62be82010-05-06 00:08:46 +00001005 /* XXX endianness */
1006 __int64 out;
1007 out = time_in + secs_between_epochs;
1008 out = out * 10000000 + nsec_in / 100;
1009 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010}
1011
Martin v. Löwis14694662006-02-03 12:54:16 +00001012/* Below, we *know* that ugo+r is 0444 */
1013#if _S_IREAD != 0400
1014#error Unsupported C library
1015#endif
1016static int
1017attributes_to_mode(DWORD attr)
1018{
Victor Stinner8c62be82010-05-06 00:08:46 +00001019 int m = 0;
1020 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1021 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1022 else
1023 m |= _S_IFREG;
1024 if (attr & FILE_ATTRIBUTE_READONLY)
1025 m |= 0444;
1026 else
1027 m |= 0666;
1028 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001029}
1030
1031static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001032attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001033{
Victor Stinner8c62be82010-05-06 00:08:46 +00001034 memset(result, 0, sizeof(*result));
1035 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1036 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1037 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1038 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1039 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001040 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001041 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001042 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1043 /* first clear the S_IFMT bits */
1044 result->st_mode ^= (result->st_mode & 0170000);
1045 /* now set the bits that make this a symlink */
1046 result->st_mode |= 0120000;
1047 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001048
Victor Stinner8c62be82010-05-06 00:08:46 +00001049 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001050}
1051
Guido van Rossumd8faa362007-04-27 19:54:29 +00001052static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001053attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001054{
Victor Stinner8c62be82010-05-06 00:08:46 +00001055 HANDLE hFindFile;
1056 WIN32_FIND_DATAA FileData;
1057 hFindFile = FindFirstFileA(pszFile, &FileData);
1058 if (hFindFile == INVALID_HANDLE_VALUE)
1059 return FALSE;
1060 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001061 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001062 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001063 info->dwFileAttributes = FileData.dwFileAttributes;
1064 info->ftCreationTime = FileData.ftCreationTime;
1065 info->ftLastAccessTime = FileData.ftLastAccessTime;
1066 info->ftLastWriteTime = FileData.ftLastWriteTime;
1067 info->nFileSizeHigh = FileData.nFileSizeHigh;
1068 info->nFileSizeLow = FileData.nFileSizeLow;
1069/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001070 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1071 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001072 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001073}
1074
1075static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001076attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001077{
Victor Stinner8c62be82010-05-06 00:08:46 +00001078 HANDLE hFindFile;
1079 WIN32_FIND_DATAW FileData;
1080 hFindFile = FindFirstFileW(pszFile, &FileData);
1081 if (hFindFile == INVALID_HANDLE_VALUE)
1082 return FALSE;
1083 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001084 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001085 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001086 info->dwFileAttributes = FileData.dwFileAttributes;
1087 info->ftCreationTime = FileData.ftCreationTime;
1088 info->ftLastAccessTime = FileData.ftLastAccessTime;
1089 info->ftLastWriteTime = FileData.ftLastWriteTime;
1090 info->nFileSizeHigh = FileData.nFileSizeHigh;
1091 info->nFileSizeLow = FileData.nFileSizeLow;
1092/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001093 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1094 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001095 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001096}
1097
Brian Curtinf5e76d02010-11-24 13:14:05 +00001098#ifndef SYMLOOP_MAX
1099#define SYMLOOP_MAX ( 88 )
1100#endif
1101
1102static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001103win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001104
1105static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001106win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001107{
1108 int code;
1109 HANDLE hFile;
1110 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001111 ULONG reparse_tag = 0;
Hirokazu Yamamoto74673512010-12-05 02:48:08 +00001112 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001113 const char *dot;
1114
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001115 if (depth > SYMLOOP_MAX) {
1116 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1117 return -1;
1118 }
1119
Brian Curtinf5e76d02010-11-24 13:14:05 +00001120 hFile = CreateFileA(
1121 path,
1122 0, /* desired access */
1123 0, /* share mode */
1124 NULL, /* security attributes */
1125 OPEN_EXISTING,
1126 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1127 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1128 NULL);
1129
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001130 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001131 /* Either the target doesn't exist, or we don't have access to
1132 get a handle to it. If the former, we need to return an error.
1133 If the latter, we can use attributes_from_dir. */
1134 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001135 return -1;
1136 /* Could not get attributes on open file. Fall back to
1137 reading the directory. */
1138 if (!attributes_from_dir(path, &info, &reparse_tag))
1139 /* Very strange. This should not fail now */
1140 return -1;
1141 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1142 if (traverse) {
1143 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001144 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001145 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001146 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001147 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001148 } else {
1149 if (!GetFileInformationByHandle(hFile, &info)) {
1150 CloseHandle(hFile);
1151 return -1;;
1152 }
1153 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1154 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1155 CloseHandle(hFile);
1156 if (code < 0)
1157 return code;
1158 if (traverse) {
1159 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1160 free(target_path);
1161 return code;
1162 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001163 } else
1164 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001165 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001166 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001167
1168 /* Set S_IEXEC if it is an .exe, .bat, ... */
1169 dot = strrchr(path, '.');
1170 if (dot) {
1171 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1172 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1173 result->st_mode |= 0111;
1174 }
1175 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001176}
1177
1178static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001179win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001180{
1181 int code;
1182 HANDLE hFile;
1183 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001184 ULONG reparse_tag = 0;
1185 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001186 const wchar_t *dot;
1187
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001188 if (depth > SYMLOOP_MAX) {
1189 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1190 return -1;
1191 }
1192
Brian Curtinf5e76d02010-11-24 13:14:05 +00001193 hFile = CreateFileW(
1194 path,
1195 0, /* desired access */
1196 0, /* share mode */
1197 NULL, /* security attributes */
1198 OPEN_EXISTING,
1199 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1200 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1201 NULL);
1202
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001203 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001204 /* Either the target doesn't exist, or we don't have access to
1205 get a handle to it. If the former, we need to return an error.
1206 If the latter, we can use attributes_from_dir. */
1207 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001208 return -1;
1209 /* Could not get attributes on open file. Fall back to
1210 reading the directory. */
1211 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1212 /* Very strange. This should not fail now */
1213 return -1;
1214 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1215 if (traverse) {
1216 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001217 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001218 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001219 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001220 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001221 } else {
1222 if (!GetFileInformationByHandle(hFile, &info)) {
1223 CloseHandle(hFile);
1224 return -1;;
1225 }
1226 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1227 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1228 CloseHandle(hFile);
1229 if (code < 0)
1230 return code;
1231 if (traverse) {
1232 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1233 free(target_path);
1234 return code;
1235 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001236 } else
1237 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001238 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001239 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001240
1241 /* Set S_IEXEC if it is an .exe, .bat, ... */
1242 dot = wcsrchr(path, '.');
1243 if (dot) {
1244 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1245 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1246 result->st_mode |= 0111;
1247 }
1248 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001249}
1250
1251static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001252win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001253{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001254 /* Protocol violation: we explicitly clear errno, instead of
1255 setting it to a POSIX error. Callers should use GetLastError. */
1256 int code = win32_xstat_impl(path, result, traverse, 0);
1257 errno = 0;
1258 return code;
1259}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001260
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001261static int
1262win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1263{
1264 /* Protocol violation: we explicitly clear errno, instead of
1265 setting it to a POSIX error. Callers should use GetLastError. */
1266 int code = win32_xstat_impl_w(path, result, traverse, 0);
1267 errno = 0;
1268 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001269}
1270
Brian Curtind40e6f72010-07-08 21:39:08 +00001271/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1272 win32_stat_w
1273
1274 In Posix, stat automatically traverses symlinks and returns the stat
1275 structure for the target. In Windows, the equivalent GetFileAttributes by
1276 default does not traverse symlinks and instead returns attributes for
1277 the symlink.
1278
1279 Therefore, win32_lstat will get the attributes traditionally, and
1280 win32_stat will first explicitly resolve the symlink target and then will
1281 call win32_lstat on that result.
1282
1283 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1284
1285static int
1286win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001287{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001288 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001289}
1290
Victor Stinner8c62be82010-05-06 00:08:46 +00001291static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001292win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001293{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001294 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001295}
1296
1297static int
1298win32_stat(const char* path, struct win32_stat *result)
1299{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001300 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001301}
1302
1303static int
1304win32_stat_w(const wchar_t* path, struct win32_stat *result)
1305{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001306 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001307}
1308
1309static int
1310win32_fstat(int file_number, struct win32_stat *result)
1311{
Victor Stinner8c62be82010-05-06 00:08:46 +00001312 BY_HANDLE_FILE_INFORMATION info;
1313 HANDLE h;
1314 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001315
Victor Stinner8c62be82010-05-06 00:08:46 +00001316 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001317
Victor Stinner8c62be82010-05-06 00:08:46 +00001318 /* Protocol violation: we explicitly clear errno, instead of
1319 setting it to a POSIX error. Callers should use GetLastError. */
1320 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001321
Victor Stinner8c62be82010-05-06 00:08:46 +00001322 if (h == INVALID_HANDLE_VALUE) {
1323 /* This is really a C library error (invalid file handle).
1324 We set the Win32 error to the closes one matching. */
1325 SetLastError(ERROR_INVALID_HANDLE);
1326 return -1;
1327 }
1328 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001329
Victor Stinner8c62be82010-05-06 00:08:46 +00001330 type = GetFileType(h);
1331 if (type == FILE_TYPE_UNKNOWN) {
1332 DWORD error = GetLastError();
1333 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001334 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001335 }
1336 /* else: valid but unknown file */
1337 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001338
Victor Stinner8c62be82010-05-06 00:08:46 +00001339 if (type != FILE_TYPE_DISK) {
1340 if (type == FILE_TYPE_CHAR)
1341 result->st_mode = _S_IFCHR;
1342 else if (type == FILE_TYPE_PIPE)
1343 result->st_mode = _S_IFIFO;
1344 return 0;
1345 }
1346
1347 if (!GetFileInformationByHandle(h, &info)) {
1348 return -1;
1349 }
1350
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001351 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001352 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001353 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1354 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001355}
1356
1357#endif /* MS_WINDOWS */
1358
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001360"stat_result: Result from stat or lstat.\n\n\
1361This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001362 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001363or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1364\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001365Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1366or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001367\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001368See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001369
1370static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001371 {"st_mode", "protection bits"},
1372 {"st_ino", "inode"},
1373 {"st_dev", "device"},
1374 {"st_nlink", "number of hard links"},
1375 {"st_uid", "user ID of owner"},
1376 {"st_gid", "group ID of owner"},
1377 {"st_size", "total size, in bytes"},
1378 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1379 {NULL, "integer time of last access"},
1380 {NULL, "integer time of last modification"},
1381 {NULL, "integer time of last change"},
1382 {"st_atime", "time of last access"},
1383 {"st_mtime", "time of last modification"},
1384 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001385#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001386 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001387#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001388#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001389 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001390#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001391#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001392 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001393#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001394#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001395 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001396#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001397#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001398 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001399#endif
1400#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001401 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001402#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001403 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001404};
1405
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001406#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001407#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001408#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001409#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001410#endif
1411
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001412#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001413#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1414#else
1415#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1416#endif
1417
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001418#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001419#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1420#else
1421#define ST_RDEV_IDX ST_BLOCKS_IDX
1422#endif
1423
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001424#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1425#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1426#else
1427#define ST_FLAGS_IDX ST_RDEV_IDX
1428#endif
1429
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001430#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001431#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001432#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001433#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001434#endif
1435
1436#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1437#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1438#else
1439#define ST_BIRTHTIME_IDX ST_GEN_IDX
1440#endif
1441
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001442static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 "stat_result", /* name */
1444 stat_result__doc__, /* doc */
1445 stat_result_fields,
1446 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001447};
1448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001449PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001450"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1451This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001452 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001453or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001454\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001455See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001456
1457static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001458 {"f_bsize", },
1459 {"f_frsize", },
1460 {"f_blocks", },
1461 {"f_bfree", },
1462 {"f_bavail", },
1463 {"f_files", },
1464 {"f_ffree", },
1465 {"f_favail", },
1466 {"f_flag", },
1467 {"f_namemax",},
1468 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001469};
1470
1471static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001472 "statvfs_result", /* name */
1473 statvfs_result__doc__, /* doc */
1474 statvfs_result_fields,
1475 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001476};
1477
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001478static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001479static PyTypeObject StatResultType;
1480static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001481static newfunc structseq_new;
1482
1483static PyObject *
1484statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1485{
Victor Stinner8c62be82010-05-06 00:08:46 +00001486 PyStructSequence *result;
1487 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001488
Victor Stinner8c62be82010-05-06 00:08:46 +00001489 result = (PyStructSequence*)structseq_new(type, args, kwds);
1490 if (!result)
1491 return NULL;
1492 /* If we have been initialized from a tuple,
1493 st_?time might be set to None. Initialize it
1494 from the int slots. */
1495 for (i = 7; i <= 9; i++) {
1496 if (result->ob_item[i+3] == Py_None) {
1497 Py_DECREF(Py_None);
1498 Py_INCREF(result->ob_item[i]);
1499 result->ob_item[i+3] = result->ob_item[i];
1500 }
1501 }
1502 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001503}
1504
1505
1506
1507/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001508static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001509
1510PyDoc_STRVAR(stat_float_times__doc__,
1511"stat_float_times([newval]) -> oldval\n\n\
1512Determine whether os.[lf]stat represents time stamps as float objects.\n\
1513If newval is True, future calls to stat() return floats, if it is False,\n\
1514future calls return ints. \n\
1515If newval is omitted, return the current setting.\n");
1516
1517static PyObject*
1518stat_float_times(PyObject* self, PyObject *args)
1519{
Victor Stinner8c62be82010-05-06 00:08:46 +00001520 int newval = -1;
1521 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1522 return NULL;
1523 if (newval == -1)
1524 /* Return old value */
1525 return PyBool_FromLong(_stat_float_times);
1526 _stat_float_times = newval;
1527 Py_INCREF(Py_None);
1528 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001529}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001530
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001531static void
1532fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1533{
Victor Stinner8c62be82010-05-06 00:08:46 +00001534 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001535#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001536 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001537#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001538 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001539#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001540 if (!ival)
1541 return;
1542 if (_stat_float_times) {
1543 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1544 } else {
1545 fval = ival;
1546 Py_INCREF(fval);
1547 }
1548 PyStructSequence_SET_ITEM(v, index, ival);
1549 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001550}
1551
Tim Peters5aa91602002-01-30 05:46:57 +00001552/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001553 (used by posix_stat() and posix_fstat()) */
1554static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001555_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001556{
Victor Stinner8c62be82010-05-06 00:08:46 +00001557 unsigned long ansec, mnsec, cnsec;
1558 PyObject *v = PyStructSequence_New(&StatResultType);
1559 if (v == NULL)
1560 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001561
Victor Stinner8c62be82010-05-06 00:08:46 +00001562 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001563#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001564 PyStructSequence_SET_ITEM(v, 1,
1565 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001566#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001567 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001568#endif
1569#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001570 PyStructSequence_SET_ITEM(v, 2,
1571 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001572#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001573 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001574#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001575 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1576 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1577 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001578#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001579 PyStructSequence_SET_ITEM(v, 6,
1580 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001581#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001582 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001583#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001584
Martin v. Löwis14694662006-02-03 12:54:16 +00001585#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001586 ansec = st->st_atim.tv_nsec;
1587 mnsec = st->st_mtim.tv_nsec;
1588 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001589#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001590 ansec = st->st_atimespec.tv_nsec;
1591 mnsec = st->st_mtimespec.tv_nsec;
1592 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001593#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001594 ansec = st->st_atime_nsec;
1595 mnsec = st->st_mtime_nsec;
1596 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001597#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001598 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001599#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001600 fill_time(v, 7, st->st_atime, ansec);
1601 fill_time(v, 8, st->st_mtime, mnsec);
1602 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001603
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001604#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001605 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1606 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001607#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001608#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001609 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1610 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001611#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001612#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001613 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1614 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001615#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001616#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001617 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1618 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001619#endif
1620#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001621 {
1622 PyObject *val;
1623 unsigned long bsec,bnsec;
1624 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001625#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001626 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001627#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001628 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001629#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001630 if (_stat_float_times) {
1631 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1632 } else {
1633 val = PyLong_FromLong((long)bsec);
1634 }
1635 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1636 val);
1637 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001638#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001639#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001640 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1641 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001642#endif
Fred Drake699f3522000-06-29 21:12:41 +00001643
Victor Stinner8c62be82010-05-06 00:08:46 +00001644 if (PyErr_Occurred()) {
1645 Py_DECREF(v);
1646 return NULL;
1647 }
Fred Drake699f3522000-06-29 21:12:41 +00001648
Victor Stinner8c62be82010-05-06 00:08:46 +00001649 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001650}
1651
Barry Warsaw53699e91996-12-10 23:23:01 +00001652static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001653posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001654 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001655#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001656 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001657#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001658 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001659#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 char *wformat,
1661 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001662{
Victor Stinner8c62be82010-05-06 00:08:46 +00001663 STRUCT_STAT st;
1664 PyObject *opath;
1665 char *path;
1666 int res;
1667 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001668
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001669#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 PyUnicodeObject *po;
1671 if (PyArg_ParseTuple(args, wformat, &po)) {
1672 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001673
Victor Stinner8c62be82010-05-06 00:08:46 +00001674 Py_BEGIN_ALLOW_THREADS
1675 /* PyUnicode_AS_UNICODE result OK without
1676 thread lock as it is a simple dereference. */
1677 res = wstatfunc(wpath, &st);
1678 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001679
Victor Stinner8c62be82010-05-06 00:08:46 +00001680 if (res != 0)
1681 return win32_error_unicode("stat", wpath);
1682 return _pystat_fromstructstat(&st);
1683 }
1684 /* Drop the argument parsing error as narrow strings
1685 are also valid. */
1686 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001687#endif
1688
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 if (!PyArg_ParseTuple(args, format,
1690 PyUnicode_FSConverter, &opath))
1691 return NULL;
1692 path = PyBytes_AsString(opath);
1693 Py_BEGIN_ALLOW_THREADS
1694 res = (*statfunc)(path, &st);
1695 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001696
Victor Stinner8c62be82010-05-06 00:08:46 +00001697 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001698#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001700#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001701 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001702#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001703 }
1704 else
1705 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001706
Victor Stinner8c62be82010-05-06 00:08:46 +00001707 Py_DECREF(opath);
1708 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001709}
1710
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001711/* POSIX methods */
1712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001713PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001714"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001715Use the real uid/gid to test for access to a path. Note that most\n\
1716operations will use the effective uid/gid, therefore this routine can\n\
1717be used in a suid/sgid environment to test if the invoking user has the\n\
1718specified access to the path. The mode argument can be F_OK to test\n\
1719existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001720
1721static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001722posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001723{
Victor Stinner8c62be82010-05-06 00:08:46 +00001724 PyObject *opath;
1725 char *path;
1726 int mode;
1727
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001728#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001729 DWORD attr;
1730 PyUnicodeObject *po;
1731 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1732 Py_BEGIN_ALLOW_THREADS
1733 /* PyUnicode_AS_UNICODE OK without thread lock as
1734 it is a simple dereference. */
1735 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1736 Py_END_ALLOW_THREADS
1737 goto finish;
1738 }
1739 /* Drop the argument parsing error as narrow strings
1740 are also valid. */
1741 PyErr_Clear();
1742 if (!PyArg_ParseTuple(args, "O&i:access",
1743 PyUnicode_FSConverter, &opath, &mode))
1744 return NULL;
1745 path = PyBytes_AsString(opath);
1746 Py_BEGIN_ALLOW_THREADS
1747 attr = GetFileAttributesA(path);
1748 Py_END_ALLOW_THREADS
1749 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001750finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001751 if (attr == 0xFFFFFFFF)
1752 /* File does not exist, or cannot read attributes */
1753 return PyBool_FromLong(0);
1754 /* Access is possible if either write access wasn't requested, or
1755 the file isn't read-only, or if it's a directory, as there are
1756 no read-only directories on Windows. */
1757 return PyBool_FromLong(!(mode & 2)
1758 || !(attr & FILE_ATTRIBUTE_READONLY)
1759 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001760#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001761 int res;
1762 if (!PyArg_ParseTuple(args, "O&i:access",
1763 PyUnicode_FSConverter, &opath, &mode))
1764 return NULL;
1765 path = PyBytes_AsString(opath);
1766 Py_BEGIN_ALLOW_THREADS
1767 res = access(path, mode);
1768 Py_END_ALLOW_THREADS
1769 Py_DECREF(opath);
1770 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001771#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001772}
1773
Guido van Rossumd371ff11999-01-25 16:12:23 +00001774#ifndef F_OK
1775#define F_OK 0
1776#endif
1777#ifndef R_OK
1778#define R_OK 4
1779#endif
1780#ifndef W_OK
1781#define W_OK 2
1782#endif
1783#ifndef X_OK
1784#define X_OK 1
1785#endif
1786
1787#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001789"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001790Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001791
1792static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001793posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001794{
Victor Stinner8c62be82010-05-06 00:08:46 +00001795 int id;
1796 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001797
Victor Stinner8c62be82010-05-06 00:08:46 +00001798 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1799 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001800
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001801#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001802 /* file descriptor 0 only, the default input device (stdin) */
1803 if (id == 0) {
1804 ret = ttyname();
1805 }
1806 else {
1807 ret = NULL;
1808 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001809#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001810 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001811#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001812 if (ret == NULL)
1813 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001814 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001815}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001816#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001817
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001818#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001820"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001821Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001822
1823static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001824posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001825{
Victor Stinner8c62be82010-05-06 00:08:46 +00001826 char *ret;
1827 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001828
Greg Wardb48bc172000-03-01 21:51:56 +00001829#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001830 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001831#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001832 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001833#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001834 if (ret == NULL)
1835 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001836 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001837}
1838#endif
1839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001840PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001841"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001842Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001843
Barry Warsaw53699e91996-12-10 23:23:01 +00001844static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001845posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001846{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001847#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001848 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001849#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001851#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001852 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001853#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001854 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001855#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001856}
1857
Fred Drake4d1e64b2002-04-15 19:40:07 +00001858#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001859PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001860"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001861Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001862opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001863
1864static PyObject *
1865posix_fchdir(PyObject *self, PyObject *fdobj)
1866{
Victor Stinner8c62be82010-05-06 00:08:46 +00001867 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001868}
1869#endif /* HAVE_FCHDIR */
1870
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001873"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001875
Barry Warsaw53699e91996-12-10 23:23:01 +00001876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001877posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001878{
Victor Stinner8c62be82010-05-06 00:08:46 +00001879 PyObject *opath = NULL;
1880 char *path = NULL;
1881 int i;
1882 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001883#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001884 DWORD attr;
1885 PyUnicodeObject *po;
1886 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1887 Py_BEGIN_ALLOW_THREADS
1888 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1889 if (attr != 0xFFFFFFFF) {
1890 if (i & _S_IWRITE)
1891 attr &= ~FILE_ATTRIBUTE_READONLY;
1892 else
1893 attr |= FILE_ATTRIBUTE_READONLY;
1894 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1895 }
1896 else
1897 res = 0;
1898 Py_END_ALLOW_THREADS
1899 if (!res)
1900 return win32_error_unicode("chmod",
1901 PyUnicode_AS_UNICODE(po));
1902 Py_INCREF(Py_None);
1903 return Py_None;
1904 }
1905 /* Drop the argument parsing error as narrow strings
1906 are also valid. */
1907 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001908
Victor Stinner8c62be82010-05-06 00:08:46 +00001909 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1910 &opath, &i))
1911 return NULL;
1912 path = PyBytes_AsString(opath);
1913 Py_BEGIN_ALLOW_THREADS
1914 attr = GetFileAttributesA(path);
1915 if (attr != 0xFFFFFFFF) {
1916 if (i & _S_IWRITE)
1917 attr &= ~FILE_ATTRIBUTE_READONLY;
1918 else
1919 attr |= FILE_ATTRIBUTE_READONLY;
1920 res = SetFileAttributesA(path, attr);
1921 }
1922 else
1923 res = 0;
1924 Py_END_ALLOW_THREADS
1925 if (!res) {
1926 win32_error("chmod", path);
1927 Py_DECREF(opath);
1928 return NULL;
1929 }
1930 Py_DECREF(opath);
1931 Py_INCREF(Py_None);
1932 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001933#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1935 &opath, &i))
1936 return NULL;
1937 path = PyBytes_AsString(opath);
1938 Py_BEGIN_ALLOW_THREADS
1939 res = chmod(path, i);
1940 Py_END_ALLOW_THREADS
1941 if (res < 0)
1942 return posix_error_with_allocated_filename(opath);
1943 Py_DECREF(opath);
1944 Py_INCREF(Py_None);
1945 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001946#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001947}
1948
Christian Heimes4e30a842007-11-30 22:12:06 +00001949#ifdef HAVE_FCHMOD
1950PyDoc_STRVAR(posix_fchmod__doc__,
1951"fchmod(fd, mode)\n\n\
1952Change the access permissions of the file given by file\n\
1953descriptor fd.");
1954
1955static PyObject *
1956posix_fchmod(PyObject *self, PyObject *args)
1957{
Victor Stinner8c62be82010-05-06 00:08:46 +00001958 int fd, mode, res;
1959 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1960 return NULL;
1961 Py_BEGIN_ALLOW_THREADS
1962 res = fchmod(fd, mode);
1963 Py_END_ALLOW_THREADS
1964 if (res < 0)
1965 return posix_error();
1966 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001967}
1968#endif /* HAVE_FCHMOD */
1969
1970#ifdef HAVE_LCHMOD
1971PyDoc_STRVAR(posix_lchmod__doc__,
1972"lchmod(path, mode)\n\n\
1973Change the access permissions of a file. If path is a symlink, this\n\
1974affects the link itself rather than the target.");
1975
1976static PyObject *
1977posix_lchmod(PyObject *self, PyObject *args)
1978{
Victor Stinner8c62be82010-05-06 00:08:46 +00001979 PyObject *opath;
1980 char *path;
1981 int i;
1982 int res;
1983 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1984 &opath, &i))
1985 return NULL;
1986 path = PyBytes_AsString(opath);
1987 Py_BEGIN_ALLOW_THREADS
1988 res = lchmod(path, i);
1989 Py_END_ALLOW_THREADS
1990 if (res < 0)
1991 return posix_error_with_allocated_filename(opath);
1992 Py_DECREF(opath);
1993 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001994}
1995#endif /* HAVE_LCHMOD */
1996
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001997
Thomas Wouterscf297e42007-02-23 15:07:44 +00001998#ifdef HAVE_CHFLAGS
1999PyDoc_STRVAR(posix_chflags__doc__,
2000"chflags(path, flags)\n\n\
2001Set file flags.");
2002
2003static PyObject *
2004posix_chflags(PyObject *self, PyObject *args)
2005{
Victor Stinner8c62be82010-05-06 00:08:46 +00002006 PyObject *opath;
2007 char *path;
2008 unsigned long flags;
2009 int res;
2010 if (!PyArg_ParseTuple(args, "O&k:chflags",
2011 PyUnicode_FSConverter, &opath, &flags))
2012 return NULL;
2013 path = PyBytes_AsString(opath);
2014 Py_BEGIN_ALLOW_THREADS
2015 res = chflags(path, flags);
2016 Py_END_ALLOW_THREADS
2017 if (res < 0)
2018 return posix_error_with_allocated_filename(opath);
2019 Py_DECREF(opath);
2020 Py_INCREF(Py_None);
2021 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002022}
2023#endif /* HAVE_CHFLAGS */
2024
2025#ifdef HAVE_LCHFLAGS
2026PyDoc_STRVAR(posix_lchflags__doc__,
2027"lchflags(path, flags)\n\n\
2028Set file flags.\n\
2029This function will not follow symbolic links.");
2030
2031static PyObject *
2032posix_lchflags(PyObject *self, PyObject *args)
2033{
Victor Stinner8c62be82010-05-06 00:08:46 +00002034 PyObject *opath;
2035 char *path;
2036 unsigned long flags;
2037 int res;
2038 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2039 PyUnicode_FSConverter, &opath, &flags))
2040 return NULL;
2041 path = PyBytes_AsString(opath);
2042 Py_BEGIN_ALLOW_THREADS
2043 res = lchflags(path, flags);
2044 Py_END_ALLOW_THREADS
2045 if (res < 0)
2046 return posix_error_with_allocated_filename(opath);
2047 Py_DECREF(opath);
2048 Py_INCREF(Py_None);
2049 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002050}
2051#endif /* HAVE_LCHFLAGS */
2052
Martin v. Löwis244edc82001-10-04 22:44:26 +00002053#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002054PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002055"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002056Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002057
2058static PyObject *
2059posix_chroot(PyObject *self, PyObject *args)
2060{
Victor Stinner8c62be82010-05-06 00:08:46 +00002061 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002062}
2063#endif
2064
Guido van Rossum21142a01999-01-08 21:05:37 +00002065#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002066PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002067"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002069
2070static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002071posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002072{
Stefan Krah0e803b32010-11-26 16:16:47 +00002073 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002074}
2075#endif /* HAVE_FSYNC */
2076
2077#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002078
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002079#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002080extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2081#endif
2082
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002083PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002084"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002085force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002086 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002087
2088static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002089posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002090{
Stefan Krah0e803b32010-11-26 16:16:47 +00002091 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002092}
2093#endif /* HAVE_FDATASYNC */
2094
2095
Fredrik Lundh10723342000-07-10 16:38:09 +00002096#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002097PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002098"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002099Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002100
Barry Warsaw53699e91996-12-10 23:23:01 +00002101static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002102posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103{
Victor Stinner8c62be82010-05-06 00:08:46 +00002104 PyObject *opath;
2105 char *path;
2106 long uid, gid;
2107 int res;
2108 if (!PyArg_ParseTuple(args, "O&ll:chown",
2109 PyUnicode_FSConverter, &opath,
2110 &uid, &gid))
2111 return NULL;
2112 path = PyBytes_AsString(opath);
2113 Py_BEGIN_ALLOW_THREADS
2114 res = chown(path, (uid_t) uid, (gid_t) gid);
2115 Py_END_ALLOW_THREADS
2116 if (res < 0)
2117 return posix_error_with_allocated_filename(opath);
2118 Py_DECREF(opath);
2119 Py_INCREF(Py_None);
2120 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002121}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002122#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002123
Christian Heimes4e30a842007-11-30 22:12:06 +00002124#ifdef HAVE_FCHOWN
2125PyDoc_STRVAR(posix_fchown__doc__,
2126"fchown(fd, uid, gid)\n\n\
2127Change the owner and group id of the file given by file descriptor\n\
2128fd to the numeric uid and gid.");
2129
2130static PyObject *
2131posix_fchown(PyObject *self, PyObject *args)
2132{
Victor Stinner8c62be82010-05-06 00:08:46 +00002133 int fd;
2134 long uid, gid;
2135 int res;
2136 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2137 return NULL;
2138 Py_BEGIN_ALLOW_THREADS
2139 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2140 Py_END_ALLOW_THREADS
2141 if (res < 0)
2142 return posix_error();
2143 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002144}
2145#endif /* HAVE_FCHOWN */
2146
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002147#ifdef HAVE_LCHOWN
2148PyDoc_STRVAR(posix_lchown__doc__,
2149"lchown(path, uid, gid)\n\n\
2150Change the owner and group id of path to the numeric uid and gid.\n\
2151This function will not follow symbolic links.");
2152
2153static PyObject *
2154posix_lchown(PyObject *self, PyObject *args)
2155{
Victor Stinner8c62be82010-05-06 00:08:46 +00002156 PyObject *opath;
2157 char *path;
2158 long uid, gid;
2159 int res;
2160 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2161 PyUnicode_FSConverter, &opath,
2162 &uid, &gid))
2163 return NULL;
2164 path = PyBytes_AsString(opath);
2165 Py_BEGIN_ALLOW_THREADS
2166 res = lchown(path, (uid_t) uid, (gid_t) gid);
2167 Py_END_ALLOW_THREADS
2168 if (res < 0)
2169 return posix_error_with_allocated_filename(opath);
2170 Py_DECREF(opath);
2171 Py_INCREF(Py_None);
2172 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002173}
2174#endif /* HAVE_LCHOWN */
2175
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002176
Guido van Rossum36bc6801995-06-14 22:54:23 +00002177#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002178static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002179posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002180{
Victor Stinner8c62be82010-05-06 00:08:46 +00002181 char buf[1026];
2182 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002183
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002184#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002185 if (!use_bytes) {
2186 wchar_t wbuf[1026];
2187 wchar_t *wbuf2 = wbuf;
2188 PyObject *resobj;
2189 DWORD len;
2190 Py_BEGIN_ALLOW_THREADS
2191 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2192 /* If the buffer is large enough, len does not include the
2193 terminating \0. If the buffer is too small, len includes
2194 the space needed for the terminator. */
2195 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2196 wbuf2 = malloc(len * sizeof(wchar_t));
2197 if (wbuf2)
2198 len = GetCurrentDirectoryW(len, wbuf2);
2199 }
2200 Py_END_ALLOW_THREADS
2201 if (!wbuf2) {
2202 PyErr_NoMemory();
2203 return NULL;
2204 }
2205 if (!len) {
2206 if (wbuf2 != wbuf) free(wbuf2);
2207 return win32_error("getcwdu", NULL);
2208 }
2209 resobj = PyUnicode_FromWideChar(wbuf2, len);
2210 if (wbuf2 != wbuf) free(wbuf2);
2211 return resobj;
2212 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002213#endif
2214
Victor Stinner8c62be82010-05-06 00:08:46 +00002215 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002216#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002217 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002218#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002219 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002220#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002221 Py_END_ALLOW_THREADS
2222 if (res == NULL)
2223 return posix_error();
2224 if (use_bytes)
2225 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002226 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002227}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002228
2229PyDoc_STRVAR(posix_getcwd__doc__,
2230"getcwd() -> path\n\n\
2231Return a unicode string representing the current working directory.");
2232
2233static PyObject *
2234posix_getcwd_unicode(PyObject *self)
2235{
2236 return posix_getcwd(0);
2237}
2238
2239PyDoc_STRVAR(posix_getcwdb__doc__,
2240"getcwdb() -> path\n\n\
2241Return a bytes string representing the current working directory.");
2242
2243static PyObject *
2244posix_getcwd_bytes(PyObject *self)
2245{
2246 return posix_getcwd(1);
2247}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002248#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002250
Guido van Rossumb6775db1994-08-01 11:34:53 +00002251#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002252PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002253"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002254Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002255
Barry Warsaw53699e91996-12-10 23:23:01 +00002256static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002257posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002258{
Victor Stinner8c62be82010-05-06 00:08:46 +00002259 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002260}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002261#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002262
Brian Curtin1b9df392010-11-24 20:24:31 +00002263#ifdef MS_WINDOWS
2264PyDoc_STRVAR(win32_link__doc__,
2265"link(src, dst)\n\n\
2266Create a hard link to a file.");
2267
2268static PyObject *
2269win32_link(PyObject *self, PyObject *args)
2270{
2271 PyObject *osrc, *odst;
2272 char *src, *dst;
2273 BOOL rslt;
2274
Brian Curtinfc889c42010-11-28 23:59:46 +00002275 PyUnicodeObject *usrc, *udst;
2276 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2277 Py_BEGIN_ALLOW_THREADS
2278 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2279 PyUnicode_AS_UNICODE(usrc), NULL);
2280 Py_END_ALLOW_THREADS
2281
2282 if (rslt == 0)
2283 return win32_error("link", NULL);
2284
2285 Py_RETURN_NONE;
2286 }
2287
2288 /* Narrow strings also valid. */
2289 PyErr_Clear();
2290
Brian Curtin1b9df392010-11-24 20:24:31 +00002291 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2292 PyUnicode_FSConverter, &odst))
2293 return NULL;
2294
2295 src = PyBytes_AsString(osrc);
2296 dst = PyBytes_AsString(odst);
2297
2298 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002299 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002300 Py_END_ALLOW_THREADS
2301
Stefan Krah30b341f2010-11-27 11:44:18 +00002302 Py_DECREF(osrc);
2303 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002304 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002305 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002306
2307 Py_RETURN_NONE;
2308}
2309#endif /* MS_WINDOWS */
2310
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002312PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002313"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002314Return a list containing the names of the entries in the directory.\n\
2315\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002316 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002317\n\
2318The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002319entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002320
Barry Warsaw53699e91996-12-10 23:23:01 +00002321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002322posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002323{
Victor Stinner8c62be82010-05-06 00:08:46 +00002324 /* XXX Should redo this putting the (now four) versions of opendir
2325 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002326#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002327
Victor Stinner8c62be82010-05-06 00:08:46 +00002328 PyObject *d, *v;
2329 HANDLE hFindFile;
2330 BOOL result;
2331 WIN32_FIND_DATA FileData;
2332 PyObject *opath;
2333 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2334 char *bufptr = namebuf;
2335 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002336
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002337 PyObject *po = NULL;
2338 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002339 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002340 Py_UNICODE *wnamebuf, *po_wchars;
2341
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002342 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002343 po_wchars = L".";
2344 len = 1;
2345 } else {
2346 po_wchars = PyUnicode_AS_UNICODE(po);
2347 len = PyUnicode_GET_SIZE(po);
2348 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002349 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002350 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2351 if (!wnamebuf) {
2352 PyErr_NoMemory();
2353 return NULL;
2354 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002355 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002356 if (len > 0) {
2357 Py_UNICODE wch = wnamebuf[len-1];
2358 if (wch != L'/' && wch != L'\\' && wch != L':')
2359 wnamebuf[len++] = L'\\';
2360 wcscpy(wnamebuf + len, L"*.*");
2361 }
2362 if ((d = PyList_New(0)) == NULL) {
2363 free(wnamebuf);
2364 return NULL;
2365 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002366 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002367 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002368 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002369 if (hFindFile == INVALID_HANDLE_VALUE) {
2370 int error = GetLastError();
2371 if (error == ERROR_FILE_NOT_FOUND) {
2372 free(wnamebuf);
2373 return d;
2374 }
2375 Py_DECREF(d);
2376 win32_error_unicode("FindFirstFileW", wnamebuf);
2377 free(wnamebuf);
2378 return NULL;
2379 }
2380 do {
2381 /* Skip over . and .. */
2382 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2383 wcscmp(wFileData.cFileName, L"..") != 0) {
2384 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2385 if (v == NULL) {
2386 Py_DECREF(d);
2387 d = NULL;
2388 break;
2389 }
2390 if (PyList_Append(d, v) != 0) {
2391 Py_DECREF(v);
2392 Py_DECREF(d);
2393 d = NULL;
2394 break;
2395 }
2396 Py_DECREF(v);
2397 }
2398 Py_BEGIN_ALLOW_THREADS
2399 result = FindNextFileW(hFindFile, &wFileData);
2400 Py_END_ALLOW_THREADS
2401 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2402 it got to the end of the directory. */
2403 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2404 Py_DECREF(d);
2405 win32_error_unicode("FindNextFileW", wnamebuf);
2406 FindClose(hFindFile);
2407 free(wnamebuf);
2408 return NULL;
2409 }
2410 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002411
Victor Stinner8c62be82010-05-06 00:08:46 +00002412 if (FindClose(hFindFile) == FALSE) {
2413 Py_DECREF(d);
2414 win32_error_unicode("FindClose", wnamebuf);
2415 free(wnamebuf);
2416 return NULL;
2417 }
2418 free(wnamebuf);
2419 return d;
2420 }
2421 /* Drop the argument parsing error as narrow strings
2422 are also valid. */
2423 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002424
Victor Stinner8c62be82010-05-06 00:08:46 +00002425 if (!PyArg_ParseTuple(args, "O&:listdir",
2426 PyUnicode_FSConverter, &opath))
2427 return NULL;
2428 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2429 PyErr_SetString(PyExc_ValueError, "path too long");
2430 Py_DECREF(opath);
2431 return NULL;
2432 }
2433 strcpy(namebuf, PyBytes_AsString(opath));
2434 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002435 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002436 if (len > 0) {
2437 char ch = namebuf[len-1];
2438 if (ch != SEP && ch != ALTSEP && ch != ':')
2439 namebuf[len++] = '/';
2440 strcpy(namebuf + len, "*.*");
2441 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002442
Victor Stinner8c62be82010-05-06 00:08:46 +00002443 if ((d = PyList_New(0)) == NULL)
2444 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002445
Antoine Pitroub73caab2010-08-09 23:39:31 +00002446 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002447 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002448 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002449 if (hFindFile == INVALID_HANDLE_VALUE) {
2450 int error = GetLastError();
2451 if (error == ERROR_FILE_NOT_FOUND)
2452 return d;
2453 Py_DECREF(d);
2454 return win32_error("FindFirstFile", namebuf);
2455 }
2456 do {
2457 /* Skip over . and .. */
2458 if (strcmp(FileData.cFileName, ".") != 0 &&
2459 strcmp(FileData.cFileName, "..") != 0) {
2460 v = PyBytes_FromString(FileData.cFileName);
2461 if (v == NULL) {
2462 Py_DECREF(d);
2463 d = NULL;
2464 break;
2465 }
2466 if (PyList_Append(d, v) != 0) {
2467 Py_DECREF(v);
2468 Py_DECREF(d);
2469 d = NULL;
2470 break;
2471 }
2472 Py_DECREF(v);
2473 }
2474 Py_BEGIN_ALLOW_THREADS
2475 result = FindNextFile(hFindFile, &FileData);
2476 Py_END_ALLOW_THREADS
2477 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2478 it got to the end of the directory. */
2479 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2480 Py_DECREF(d);
2481 win32_error("FindNextFile", namebuf);
2482 FindClose(hFindFile);
2483 return NULL;
2484 }
2485 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002486
Victor Stinner8c62be82010-05-06 00:08:46 +00002487 if (FindClose(hFindFile) == FALSE) {
2488 Py_DECREF(d);
2489 return win32_error("FindClose", namebuf);
2490 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002491
Victor Stinner8c62be82010-05-06 00:08:46 +00002492 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002493
Tim Peters0bb44a42000-09-15 07:44:49 +00002494#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002495
2496#ifndef MAX_PATH
2497#define MAX_PATH CCHMAXPATH
2498#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002499 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002500 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002501 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002502 PyObject *d, *v;
2503 char namebuf[MAX_PATH+5];
2504 HDIR hdir = 1;
2505 ULONG srchcnt = 1;
2506 FILEFINDBUF3 ep;
2507 APIRET rc;
2508
Victor Stinner8c62be82010-05-06 00:08:46 +00002509 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002510 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002511 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002512 name = PyBytes_AsString(oname);
2513 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002514 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002515 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002516 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002517 return NULL;
2518 }
2519 strcpy(namebuf, name);
2520 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002521 if (*pt == ALTSEP)
2522 *pt = SEP;
2523 if (namebuf[len-1] != SEP)
2524 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002525 strcpy(namebuf + len, "*.*");
2526
Neal Norwitz6c913782007-10-14 03:23:09 +00002527 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002528 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002529 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002530 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002531
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002532 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2533 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002534 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002535 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2536 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2537 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002538
2539 if (rc != NO_ERROR) {
2540 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002541 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002542 }
2543
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002544 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002545 do {
2546 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002547 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002548 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002549
2550 strcpy(namebuf, ep.achName);
2551
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002552 /* Leave Case of Name Alone -- In Native Form */
2553 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002554
Christian Heimes72b710a2008-05-26 13:28:38 +00002555 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002556 if (v == NULL) {
2557 Py_DECREF(d);
2558 d = NULL;
2559 break;
2560 }
2561 if (PyList_Append(d, v) != 0) {
2562 Py_DECREF(v);
2563 Py_DECREF(d);
2564 d = NULL;
2565 break;
2566 }
2567 Py_DECREF(v);
2568 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2569 }
2570
Victor Stinnerdcb24032010-04-22 12:08:36 +00002571 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002572 return d;
2573#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002574 PyObject *oname;
2575 char *name;
2576 PyObject *d, *v;
2577 DIR *dirp;
2578 struct dirent *ep;
2579 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002580
Victor Stinner8c62be82010-05-06 00:08:46 +00002581 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002582 /* v is never read, so it does not need to be initialized yet. */
2583 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002584 arg_is_unicode = 0;
2585 PyErr_Clear();
2586 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002587 oname = NULL;
2588 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002589 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002590 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002591 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002592 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002593 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002594 Py_BEGIN_ALLOW_THREADS
2595 dirp = opendir(name);
2596 Py_END_ALLOW_THREADS
2597 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002598 return posix_error_with_allocated_filename(oname);
2599 }
2600 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002601 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002602 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002603 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002604 Py_DECREF(oname);
2605 return NULL;
2606 }
2607 for (;;) {
2608 errno = 0;
2609 Py_BEGIN_ALLOW_THREADS
2610 ep = readdir(dirp);
2611 Py_END_ALLOW_THREADS
2612 if (ep == NULL) {
2613 if (errno == 0) {
2614 break;
2615 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002616 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002617 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002618 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002619 Py_DECREF(d);
2620 return posix_error_with_allocated_filename(oname);
2621 }
2622 }
2623 if (ep->d_name[0] == '.' &&
2624 (NAMLEN(ep) == 1 ||
2625 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2626 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002627 if (arg_is_unicode)
2628 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2629 else
2630 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002631 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002632 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002633 break;
2634 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002635 if (PyList_Append(d, v) != 0) {
2636 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002637 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002638 break;
2639 }
2640 Py_DECREF(v);
2641 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002642 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002643 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002644 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002645 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002646
Victor Stinner8c62be82010-05-06 00:08:46 +00002647 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002648
Tim Peters0bb44a42000-09-15 07:44:49 +00002649#endif /* which OS */
2650} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002651
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002652#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002653/* A helper function for abspath on win32 */
2654static PyObject *
2655posix__getfullpathname(PyObject *self, PyObject *args)
2656{
Victor Stinner8c62be82010-05-06 00:08:46 +00002657 PyObject *opath;
2658 char *path;
2659 char outbuf[MAX_PATH*2];
2660 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002661#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002662 PyUnicodeObject *po;
2663 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2664 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2665 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2666 Py_UNICODE *wtemp;
2667 DWORD result;
2668 PyObject *v;
2669 result = GetFullPathNameW(wpath,
2670 sizeof(woutbuf)/sizeof(woutbuf[0]),
2671 woutbuf, &wtemp);
2672 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2673 woutbufp = malloc(result * sizeof(Py_UNICODE));
2674 if (!woutbufp)
2675 return PyErr_NoMemory();
2676 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2677 }
2678 if (result)
2679 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2680 else
2681 v = win32_error_unicode("GetFullPathNameW", wpath);
2682 if (woutbufp != woutbuf)
2683 free(woutbufp);
2684 return v;
2685 }
2686 /* Drop the argument parsing error as narrow strings
2687 are also valid. */
2688 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002689
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002690#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002691 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2692 PyUnicode_FSConverter, &opath))
2693 return NULL;
2694 path = PyBytes_AsString(opath);
2695 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2696 outbuf, &temp)) {
2697 win32_error("GetFullPathName", path);
2698 Py_DECREF(opath);
2699 return NULL;
2700 }
2701 Py_DECREF(opath);
2702 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2703 return PyUnicode_Decode(outbuf, strlen(outbuf),
2704 Py_FileSystemDefaultEncoding, NULL);
2705 }
2706 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002707} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002708
Brian Curtinf5e76d02010-11-24 13:14:05 +00002709/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2710static int has_GetFinalPathNameByHandle = 0;
2711static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2712 DWORD);
2713static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2714 DWORD);
2715static int
2716check_GetFinalPathNameByHandle()
2717{
2718 HINSTANCE hKernel32;
2719 /* only recheck */
2720 if (!has_GetFinalPathNameByHandle)
2721 {
2722 hKernel32 = GetModuleHandle("KERNEL32");
2723 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2724 "GetFinalPathNameByHandleA");
2725 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2726 "GetFinalPathNameByHandleW");
2727 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2728 Py_GetFinalPathNameByHandleW;
2729 }
2730 return has_GetFinalPathNameByHandle;
2731}
2732
Brian Curtind40e6f72010-07-08 21:39:08 +00002733/* A helper function for samepath on windows */
2734static PyObject *
2735posix__getfinalpathname(PyObject *self, PyObject *args)
2736{
2737 HANDLE hFile;
2738 int buf_size;
2739 wchar_t *target_path;
2740 int result_length;
2741 PyObject *result;
2742 wchar_t *path;
2743
Brian Curtin94622b02010-09-24 00:03:39 +00002744 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002745 return NULL;
2746 }
2747
2748 if(!check_GetFinalPathNameByHandle()) {
2749 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2750 NotImplementedError. */
2751 return PyErr_Format(PyExc_NotImplementedError,
2752 "GetFinalPathNameByHandle not available on this platform");
2753 }
2754
2755 hFile = CreateFileW(
2756 path,
2757 0, /* desired access */
2758 0, /* share mode */
2759 NULL, /* security attributes */
2760 OPEN_EXISTING,
2761 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2762 FILE_FLAG_BACKUP_SEMANTICS,
2763 NULL);
2764
2765 if(hFile == INVALID_HANDLE_VALUE) {
2766 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002767 return PyErr_Format(PyExc_RuntimeError,
2768 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002769 }
2770
2771 /* We have a good handle to the target, use it to determine the
2772 target path name. */
2773 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2774
2775 if(!buf_size)
2776 return win32_error_unicode("GetFinalPathNameByHandle", path);
2777
2778 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2779 if(!target_path)
2780 return PyErr_NoMemory();
2781
2782 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2783 buf_size, VOLUME_NAME_DOS);
2784 if(!result_length)
2785 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2786
2787 if(!CloseHandle(hFile))
2788 return win32_error_unicode("GetFinalPathNameByHandle", path);
2789
2790 target_path[result_length] = 0;
2791 result = PyUnicode_FromUnicode(target_path, result_length);
2792 free(target_path);
2793 return result;
2794
2795} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002796
2797static PyObject *
2798posix__getfileinformation(PyObject *self, PyObject *args)
2799{
2800 HANDLE hFile;
2801 BY_HANDLE_FILE_INFORMATION info;
2802 int fd;
2803
2804 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2805 return NULL;
2806
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002807 if (!_PyVerify_fd(fd))
2808 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002809
2810 hFile = (HANDLE)_get_osfhandle(fd);
2811 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002812 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002813
2814 if (!GetFileInformationByHandle(hFile, &info))
2815 return win32_error("_getfileinformation", NULL);
2816
2817 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2818 info.nFileIndexHigh,
2819 info.nFileIndexLow);
2820}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002821#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002822
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002823PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002824"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002825Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002826
Barry Warsaw53699e91996-12-10 23:23:01 +00002827static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002828posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002829{
Victor Stinner8c62be82010-05-06 00:08:46 +00002830 int res;
2831 PyObject *opath;
2832 char *path;
2833 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002834
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002835#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002836 PyUnicodeObject *po;
2837 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2838 Py_BEGIN_ALLOW_THREADS
2839 /* PyUnicode_AS_UNICODE OK without thread lock as
2840 it is a simple dereference. */
2841 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2842 Py_END_ALLOW_THREADS
2843 if (!res)
2844 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2845 Py_INCREF(Py_None);
2846 return Py_None;
2847 }
2848 /* Drop the argument parsing error as narrow strings
2849 are also valid. */
2850 PyErr_Clear();
2851 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2852 PyUnicode_FSConverter, &opath, &mode))
2853 return NULL;
2854 path = PyBytes_AsString(opath);
2855 Py_BEGIN_ALLOW_THREADS
2856 /* PyUnicode_AS_UNICODE OK without thread lock as
2857 it is a simple dereference. */
2858 res = CreateDirectoryA(path, NULL);
2859 Py_END_ALLOW_THREADS
2860 if (!res) {
2861 win32_error("mkdir", path);
2862 Py_DECREF(opath);
2863 return NULL;
2864 }
2865 Py_DECREF(opath);
2866 Py_INCREF(Py_None);
2867 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002868#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002869
Victor Stinner8c62be82010-05-06 00:08:46 +00002870 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2871 PyUnicode_FSConverter, &opath, &mode))
2872 return NULL;
2873 path = PyBytes_AsString(opath);
2874 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002875#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002876 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002877#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002878 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002879#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002880 Py_END_ALLOW_THREADS
2881 if (res < 0)
2882 return posix_error_with_allocated_filename(opath);
2883 Py_DECREF(opath);
2884 Py_INCREF(Py_None);
2885 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002886#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002887}
2888
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002889
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002890/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2891#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002892#include <sys/resource.h>
2893#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002894
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002895
2896#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002897PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002898"nice(inc) -> new_priority\n\n\
2899Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002900
Barry Warsaw53699e91996-12-10 23:23:01 +00002901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002902posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002903{
Victor Stinner8c62be82010-05-06 00:08:46 +00002904 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002905
Victor Stinner8c62be82010-05-06 00:08:46 +00002906 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2907 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002908
Victor Stinner8c62be82010-05-06 00:08:46 +00002909 /* There are two flavours of 'nice': one that returns the new
2910 priority (as required by almost all standards out there) and the
2911 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2912 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002913
Victor Stinner8c62be82010-05-06 00:08:46 +00002914 If we are of the nice family that returns the new priority, we
2915 need to clear errno before the call, and check if errno is filled
2916 before calling posix_error() on a returnvalue of -1, because the
2917 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002918
Victor Stinner8c62be82010-05-06 00:08:46 +00002919 errno = 0;
2920 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002921#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002922 if (value == 0)
2923 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002924#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002925 if (value == -1 && errno != 0)
2926 /* either nice() or getpriority() returned an error */
2927 return posix_error();
2928 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002929}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002930#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002931
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002932PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002933"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002934Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002935
Barry Warsaw53699e91996-12-10 23:23:01 +00002936static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002937posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002938{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002939#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002940 PyObject *o1, *o2;
2941 char *p1, *p2;
2942 BOOL result;
2943 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2944 goto error;
2945 if (!convert_to_unicode(&o1))
2946 goto error;
2947 if (!convert_to_unicode(&o2)) {
2948 Py_DECREF(o1);
2949 goto error;
2950 }
2951 Py_BEGIN_ALLOW_THREADS
2952 result = MoveFileW(PyUnicode_AsUnicode(o1),
2953 PyUnicode_AsUnicode(o2));
2954 Py_END_ALLOW_THREADS
2955 Py_DECREF(o1);
2956 Py_DECREF(o2);
2957 if (!result)
2958 return win32_error("rename", NULL);
2959 Py_INCREF(Py_None);
2960 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002961error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002962 PyErr_Clear();
2963 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2964 return NULL;
2965 Py_BEGIN_ALLOW_THREADS
2966 result = MoveFileA(p1, p2);
2967 Py_END_ALLOW_THREADS
2968 if (!result)
2969 return win32_error("rename", NULL);
2970 Py_INCREF(Py_None);
2971 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002972#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002973 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002974#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002975}
2976
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002978PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002979"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002980Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002981
Barry Warsaw53699e91996-12-10 23:23:01 +00002982static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002983posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002984{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002985#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002986 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002987#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002988 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002989#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002990}
2991
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002992
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002993PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002994"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002995Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002996
Barry Warsaw53699e91996-12-10 23:23:01 +00002997static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002998posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002999{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003000#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003001 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003002#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003003 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003004#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003005}
3006
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003007
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003008#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003009PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003010"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003011Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003012
Barry Warsaw53699e91996-12-10 23:23:01 +00003013static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003014posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003015{
Victor Stinner8c62be82010-05-06 00:08:46 +00003016 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003017#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003018 wchar_t *command;
3019 if (!PyArg_ParseTuple(args, "u:system", &command))
3020 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003021
Victor Stinner8c62be82010-05-06 00:08:46 +00003022 Py_BEGIN_ALLOW_THREADS
3023 sts = _wsystem(command);
3024 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003025#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003026 PyObject *command_obj;
3027 char *command;
3028 if (!PyArg_ParseTuple(args, "O&:system",
3029 PyUnicode_FSConverter, &command_obj))
3030 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003031
Victor Stinner8c62be82010-05-06 00:08:46 +00003032 command = PyBytes_AsString(command_obj);
3033 Py_BEGIN_ALLOW_THREADS
3034 sts = system(command);
3035 Py_END_ALLOW_THREADS
3036 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003037#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003038 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003039}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003040#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003041
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003043PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003044"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003045Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003046
Barry Warsaw53699e91996-12-10 23:23:01 +00003047static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003048posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003049{
Victor Stinner8c62be82010-05-06 00:08:46 +00003050 int i;
3051 if (!PyArg_ParseTuple(args, "i:umask", &i))
3052 return NULL;
3053 i = (int)umask(i);
3054 if (i < 0)
3055 return posix_error();
3056 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003057}
3058
Brian Curtind40e6f72010-07-08 21:39:08 +00003059#ifdef MS_WINDOWS
3060
3061/* override the default DeleteFileW behavior so that directory
3062symlinks can be removed with this function, the same as with
3063Unix symlinks */
3064BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3065{
3066 WIN32_FILE_ATTRIBUTE_DATA info;
3067 WIN32_FIND_DATAW find_data;
3068 HANDLE find_data_handle;
3069 int is_directory = 0;
3070 int is_link = 0;
3071
3072 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3073 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3074
3075 /* Get WIN32_FIND_DATA structure for the path to determine if
3076 it is a symlink */
3077 if(is_directory &&
3078 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3079 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3080
3081 if(find_data_handle != INVALID_HANDLE_VALUE) {
3082 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3083 FindClose(find_data_handle);
3084 }
3085 }
3086 }
3087
3088 if (is_directory && is_link)
3089 return RemoveDirectoryW(lpFileName);
3090
3091 return DeleteFileW(lpFileName);
3092}
3093#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003094
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003095PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003096"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003097Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003099PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003100"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003101Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003102
Barry Warsaw53699e91996-12-10 23:23:01 +00003103static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003104posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003105{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003106#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003107 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3108 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003109#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003110 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003111#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003112}
3113
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003114
Guido van Rossumb6775db1994-08-01 11:34:53 +00003115#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003116PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003117"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003118Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003119
Barry Warsaw53699e91996-12-10 23:23:01 +00003120static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003121posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003122{
Victor Stinner8c62be82010-05-06 00:08:46 +00003123 struct utsname u;
3124 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003125
Victor Stinner8c62be82010-05-06 00:08:46 +00003126 Py_BEGIN_ALLOW_THREADS
3127 res = uname(&u);
3128 Py_END_ALLOW_THREADS
3129 if (res < 0)
3130 return posix_error();
3131 return Py_BuildValue("(sssss)",
3132 u.sysname,
3133 u.nodename,
3134 u.release,
3135 u.version,
3136 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003137}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003138#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003139
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003140static int
3141extract_time(PyObject *t, long* sec, long* usec)
3142{
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 long intval;
3144 if (PyFloat_Check(t)) {
3145 double tval = PyFloat_AsDouble(t);
3146 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
3147 if (!intobj)
3148 return -1;
3149 intval = PyLong_AsLong(intobj);
3150 Py_DECREF(intobj);
3151 if (intval == -1 && PyErr_Occurred())
3152 return -1;
3153 *sec = intval;
3154 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3155 if (*usec < 0)
3156 /* If rounding gave us a negative number,
3157 truncate. */
3158 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003159 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003160 }
3161 intval = PyLong_AsLong(t);
3162 if (intval == -1 && PyErr_Occurred())
3163 return -1;
3164 *sec = intval;
3165 *usec = 0;
3166 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003167}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003169PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003170"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003171utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003172Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003173second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003174
Barry Warsaw53699e91996-12-10 23:23:01 +00003175static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003176posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003177{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003178#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003179 PyObject *arg;
3180 PyUnicodeObject *obwpath;
3181 wchar_t *wpath = NULL;
3182 PyObject *oapath;
3183 char *apath;
3184 HANDLE hFile;
3185 long atimesec, mtimesec, ausec, musec;
3186 FILETIME atime, mtime;
3187 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003188
Victor Stinner8c62be82010-05-06 00:08:46 +00003189 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3190 wpath = PyUnicode_AS_UNICODE(obwpath);
3191 Py_BEGIN_ALLOW_THREADS
3192 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3193 NULL, OPEN_EXISTING,
3194 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3195 Py_END_ALLOW_THREADS
3196 if (hFile == INVALID_HANDLE_VALUE)
3197 return win32_error_unicode("utime", wpath);
3198 } else
3199 /* Drop the argument parsing error as narrow strings
3200 are also valid. */
3201 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003202
Victor Stinner8c62be82010-05-06 00:08:46 +00003203 if (!wpath) {
3204 if (!PyArg_ParseTuple(args, "O&O:utime",
3205 PyUnicode_FSConverter, &oapath, &arg))
3206 return NULL;
3207 apath = PyBytes_AsString(oapath);
3208 Py_BEGIN_ALLOW_THREADS
3209 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3210 NULL, OPEN_EXISTING,
3211 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3212 Py_END_ALLOW_THREADS
3213 if (hFile == INVALID_HANDLE_VALUE) {
3214 win32_error("utime", apath);
3215 Py_DECREF(oapath);
3216 return NULL;
3217 }
3218 Py_DECREF(oapath);
3219 }
3220
3221 if (arg == Py_None) {
3222 SYSTEMTIME now;
3223 GetSystemTime(&now);
3224 if (!SystemTimeToFileTime(&now, &mtime) ||
3225 !SystemTimeToFileTime(&now, &atime)) {
3226 win32_error("utime", NULL);
3227 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003228 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003229 }
3230 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3231 PyErr_SetString(PyExc_TypeError,
3232 "utime() arg 2 must be a tuple (atime, mtime)");
3233 goto done;
3234 }
3235 else {
3236 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3237 &atimesec, &ausec) == -1)
3238 goto done;
3239 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3240 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3241 &mtimesec, &musec) == -1)
3242 goto done;
3243 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3244 }
3245 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3246 /* Avoid putting the file name into the error here,
3247 as that may confuse the user into believing that
3248 something is wrong with the file, when it also
3249 could be the time stamp that gives a problem. */
3250 win32_error("utime", NULL);
3251 }
3252 Py_INCREF(Py_None);
3253 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003254done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003255 CloseHandle(hFile);
3256 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003257#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003258
Victor Stinner8c62be82010-05-06 00:08:46 +00003259 PyObject *opath;
3260 char *path;
3261 long atime, mtime, ausec, musec;
3262 int res;
3263 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003264
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003265#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003266 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003267#define ATIME buf[0].tv_sec
3268#define MTIME buf[1].tv_sec
3269#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003270/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003271 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003272#define ATIME buf.actime
3273#define MTIME buf.modtime
3274#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003275#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003276 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003277#define ATIME buf[0]
3278#define MTIME buf[1]
3279#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003280#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003281
Mark Hammond817c9292003-12-03 01:22:38 +00003282
Victor Stinner8c62be82010-05-06 00:08:46 +00003283 if (!PyArg_ParseTuple(args, "O&O:utime",
3284 PyUnicode_FSConverter, &opath, &arg))
3285 return NULL;
3286 path = PyBytes_AsString(opath);
3287 if (arg == Py_None) {
3288 /* optional time values not given */
3289 Py_BEGIN_ALLOW_THREADS
3290 res = utime(path, NULL);
3291 Py_END_ALLOW_THREADS
3292 }
3293 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3294 PyErr_SetString(PyExc_TypeError,
3295 "utime() arg 2 must be a tuple (atime, mtime)");
3296 Py_DECREF(opath);
3297 return NULL;
3298 }
3299 else {
3300 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3301 &atime, &ausec) == -1) {
3302 Py_DECREF(opath);
3303 return NULL;
3304 }
3305 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3306 &mtime, &musec) == -1) {
3307 Py_DECREF(opath);
3308 return NULL;
3309 }
3310 ATIME = atime;
3311 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003312#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003313 buf[0].tv_usec = ausec;
3314 buf[1].tv_usec = musec;
3315 Py_BEGIN_ALLOW_THREADS
3316 res = utimes(path, buf);
3317 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003318#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003319 Py_BEGIN_ALLOW_THREADS
3320 res = utime(path, UTIME_ARG);
3321 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003322#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003323 }
3324 if (res < 0) {
3325 return posix_error_with_allocated_filename(opath);
3326 }
3327 Py_DECREF(opath);
3328 Py_INCREF(Py_None);
3329 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003330#undef UTIME_ARG
3331#undef ATIME
3332#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003333#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003334}
3335
Guido van Rossum85e3b011991-06-03 12:42:10 +00003336
Guido van Rossum3b066191991-06-04 19:40:25 +00003337/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003339PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003340"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003341Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003342
Barry Warsaw53699e91996-12-10 23:23:01 +00003343static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003344posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003345{
Victor Stinner8c62be82010-05-06 00:08:46 +00003346 int sts;
3347 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3348 return NULL;
3349 _exit(sts);
3350 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003351}
3352
Martin v. Löwis114619e2002-10-07 06:44:21 +00003353#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3354static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003355free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003356{
Victor Stinner8c62be82010-05-06 00:08:46 +00003357 Py_ssize_t i;
3358 for (i = 0; i < count; i++)
3359 PyMem_Free(array[i]);
3360 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003361}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003362
Antoine Pitrou69f71142009-05-24 21:25:49 +00003363static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003364int fsconvert_strdup(PyObject *o, char**out)
3365{
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 PyObject *bytes;
3367 Py_ssize_t size;
3368 if (!PyUnicode_FSConverter(o, &bytes))
3369 return 0;
3370 size = PyBytes_GET_SIZE(bytes);
3371 *out = PyMem_Malloc(size+1);
3372 if (!*out)
3373 return 0;
3374 memcpy(*out, PyBytes_AsString(bytes), size+1);
3375 Py_DECREF(bytes);
3376 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003377}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003378#endif
3379
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003380
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003381#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003382PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003383"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003384Execute an executable path with arguments, replacing current process.\n\
3385\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003386 path: path of executable file\n\
3387 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003388
Barry Warsaw53699e91996-12-10 23:23:01 +00003389static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003390posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003391{
Victor Stinner8c62be82010-05-06 00:08:46 +00003392 PyObject *opath;
3393 char *path;
3394 PyObject *argv;
3395 char **argvlist;
3396 Py_ssize_t i, argc;
3397 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003398
Victor Stinner8c62be82010-05-06 00:08:46 +00003399 /* execv has two arguments: (path, argv), where
3400 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003401
Victor Stinner8c62be82010-05-06 00:08:46 +00003402 if (!PyArg_ParseTuple(args, "O&O:execv",
3403 PyUnicode_FSConverter,
3404 &opath, &argv))
3405 return NULL;
3406 path = PyBytes_AsString(opath);
3407 if (PyList_Check(argv)) {
3408 argc = PyList_Size(argv);
3409 getitem = PyList_GetItem;
3410 }
3411 else if (PyTuple_Check(argv)) {
3412 argc = PyTuple_Size(argv);
3413 getitem = PyTuple_GetItem;
3414 }
3415 else {
3416 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3417 Py_DECREF(opath);
3418 return NULL;
3419 }
3420 if (argc < 1) {
3421 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3422 Py_DECREF(opath);
3423 return NULL;
3424 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003425
Victor Stinner8c62be82010-05-06 00:08:46 +00003426 argvlist = PyMem_NEW(char *, argc+1);
3427 if (argvlist == NULL) {
3428 Py_DECREF(opath);
3429 return PyErr_NoMemory();
3430 }
3431 for (i = 0; i < argc; i++) {
3432 if (!fsconvert_strdup((*getitem)(argv, i),
3433 &argvlist[i])) {
3434 free_string_array(argvlist, i);
3435 PyErr_SetString(PyExc_TypeError,
3436 "execv() arg 2 must contain only strings");
3437 Py_DECREF(opath);
3438 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003439
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 }
3441 }
3442 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003443
Victor Stinner8c62be82010-05-06 00:08:46 +00003444 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003445
Victor Stinner8c62be82010-05-06 00:08:46 +00003446 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003447
Victor Stinner8c62be82010-05-06 00:08:46 +00003448 free_string_array(argvlist, argc);
3449 Py_DECREF(opath);
3450 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003451}
3452
Victor Stinner13bb71c2010-04-23 21:41:56 +00003453static char**
3454parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3455{
Victor Stinner8c62be82010-05-06 00:08:46 +00003456 char **envlist;
3457 Py_ssize_t i, pos, envc;
3458 PyObject *keys=NULL, *vals=NULL;
3459 PyObject *key, *val, *key2, *val2;
3460 char *p, *k, *v;
3461 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003462
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 i = PyMapping_Size(env);
3464 if (i < 0)
3465 return NULL;
3466 envlist = PyMem_NEW(char *, i + 1);
3467 if (envlist == NULL) {
3468 PyErr_NoMemory();
3469 return NULL;
3470 }
3471 envc = 0;
3472 keys = PyMapping_Keys(env);
3473 vals = PyMapping_Values(env);
3474 if (!keys || !vals)
3475 goto error;
3476 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3477 PyErr_Format(PyExc_TypeError,
3478 "env.keys() or env.values() is not a list");
3479 goto error;
3480 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003481
Victor Stinner8c62be82010-05-06 00:08:46 +00003482 for (pos = 0; pos < i; pos++) {
3483 key = PyList_GetItem(keys, pos);
3484 val = PyList_GetItem(vals, pos);
3485 if (!key || !val)
3486 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003487
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 if (PyUnicode_FSConverter(key, &key2) == 0)
3489 goto error;
3490 if (PyUnicode_FSConverter(val, &val2) == 0) {
3491 Py_DECREF(key2);
3492 goto error;
3493 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003494
3495#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003496 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3497 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003498#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 k = PyBytes_AsString(key2);
3500 v = PyBytes_AsString(val2);
3501 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003502
Victor Stinner8c62be82010-05-06 00:08:46 +00003503 p = PyMem_NEW(char, len);
3504 if (p == NULL) {
3505 PyErr_NoMemory();
3506 Py_DECREF(key2);
3507 Py_DECREF(val2);
3508 goto error;
3509 }
3510 PyOS_snprintf(p, len, "%s=%s", k, v);
3511 envlist[envc++] = p;
3512 Py_DECREF(key2);
3513 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003514#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003515 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003516#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003517 }
3518 Py_DECREF(vals);
3519 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003520
Victor Stinner8c62be82010-05-06 00:08:46 +00003521 envlist[envc] = 0;
3522 *envc_ptr = envc;
3523 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003524
3525error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003526 Py_XDECREF(keys);
3527 Py_XDECREF(vals);
3528 while (--envc >= 0)
3529 PyMem_DEL(envlist[envc]);
3530 PyMem_DEL(envlist);
3531 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003532}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003534PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003535"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003536Execute a path with arguments and environment, replacing current process.\n\
3537\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003538 path: path of executable file\n\
3539 args: tuple or list of arguments\n\
3540 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003541
Barry Warsaw53699e91996-12-10 23:23:01 +00003542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003543posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003544{
Victor Stinner8c62be82010-05-06 00:08:46 +00003545 PyObject *opath;
3546 char *path;
3547 PyObject *argv, *env;
3548 char **argvlist;
3549 char **envlist;
3550 Py_ssize_t i, argc, envc;
3551 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3552 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003553
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 /* execve has three arguments: (path, argv, env), where
3555 argv is a list or tuple of strings and env is a dictionary
3556 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003557
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 if (!PyArg_ParseTuple(args, "O&OO:execve",
3559 PyUnicode_FSConverter,
3560 &opath, &argv, &env))
3561 return NULL;
3562 path = PyBytes_AsString(opath);
3563 if (PyList_Check(argv)) {
3564 argc = PyList_Size(argv);
3565 getitem = PyList_GetItem;
3566 }
3567 else if (PyTuple_Check(argv)) {
3568 argc = PyTuple_Size(argv);
3569 getitem = PyTuple_GetItem;
3570 }
3571 else {
3572 PyErr_SetString(PyExc_TypeError,
3573 "execve() arg 2 must be a tuple or list");
3574 goto fail_0;
3575 }
3576 if (!PyMapping_Check(env)) {
3577 PyErr_SetString(PyExc_TypeError,
3578 "execve() arg 3 must be a mapping object");
3579 goto fail_0;
3580 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003581
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 argvlist = PyMem_NEW(char *, argc+1);
3583 if (argvlist == NULL) {
3584 PyErr_NoMemory();
3585 goto fail_0;
3586 }
3587 for (i = 0; i < argc; i++) {
3588 if (!fsconvert_strdup((*getitem)(argv, i),
3589 &argvlist[i]))
3590 {
3591 lastarg = i;
3592 goto fail_1;
3593 }
3594 }
3595 lastarg = argc;
3596 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003597
Victor Stinner8c62be82010-05-06 00:08:46 +00003598 envlist = parse_envlist(env, &envc);
3599 if (envlist == NULL)
3600 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003601
Victor Stinner8c62be82010-05-06 00:08:46 +00003602 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003603
Victor Stinner8c62be82010-05-06 00:08:46 +00003604 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003605
Victor Stinner8c62be82010-05-06 00:08:46 +00003606 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003607
Victor Stinner8c62be82010-05-06 00:08:46 +00003608 while (--envc >= 0)
3609 PyMem_DEL(envlist[envc]);
3610 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003611 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003612 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003613 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 Py_DECREF(opath);
3615 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003616}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003617#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003618
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003619
Guido van Rossuma1065681999-01-25 23:20:23 +00003620#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003621PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003622"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003623Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003624\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 mode: mode of process creation\n\
3626 path: path of executable file\n\
3627 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003628
3629static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003630posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003631{
Victor Stinner8c62be82010-05-06 00:08:46 +00003632 PyObject *opath;
3633 char *path;
3634 PyObject *argv;
3635 char **argvlist;
3636 int mode, i;
3637 Py_ssize_t argc;
3638 Py_intptr_t spawnval;
3639 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003640
Victor Stinner8c62be82010-05-06 00:08:46 +00003641 /* spawnv has three arguments: (mode, path, argv), where
3642 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003643
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3645 PyUnicode_FSConverter,
3646 &opath, &argv))
3647 return NULL;
3648 path = PyBytes_AsString(opath);
3649 if (PyList_Check(argv)) {
3650 argc = PyList_Size(argv);
3651 getitem = PyList_GetItem;
3652 }
3653 else if (PyTuple_Check(argv)) {
3654 argc = PyTuple_Size(argv);
3655 getitem = PyTuple_GetItem;
3656 }
3657 else {
3658 PyErr_SetString(PyExc_TypeError,
3659 "spawnv() arg 2 must be a tuple or list");
3660 Py_DECREF(opath);
3661 return NULL;
3662 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003663
Victor Stinner8c62be82010-05-06 00:08:46 +00003664 argvlist = PyMem_NEW(char *, argc+1);
3665 if (argvlist == NULL) {
3666 Py_DECREF(opath);
3667 return PyErr_NoMemory();
3668 }
3669 for (i = 0; i < argc; i++) {
3670 if (!fsconvert_strdup((*getitem)(argv, i),
3671 &argvlist[i])) {
3672 free_string_array(argvlist, i);
3673 PyErr_SetString(
3674 PyExc_TypeError,
3675 "spawnv() arg 2 must contain only strings");
3676 Py_DECREF(opath);
3677 return NULL;
3678 }
3679 }
3680 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003681
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003682#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003683 Py_BEGIN_ALLOW_THREADS
3684 spawnval = spawnv(mode, path, argvlist);
3685 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003686#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003687 if (mode == _OLD_P_OVERLAY)
3688 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003689
Victor Stinner8c62be82010-05-06 00:08:46 +00003690 Py_BEGIN_ALLOW_THREADS
3691 spawnval = _spawnv(mode, path, argvlist);
3692 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003693#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003694
Victor Stinner8c62be82010-05-06 00:08:46 +00003695 free_string_array(argvlist, argc);
3696 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003697
Victor Stinner8c62be82010-05-06 00:08:46 +00003698 if (spawnval == -1)
3699 return posix_error();
3700 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003701#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003702 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003703#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003705#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003706}
3707
3708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003709PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003710"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003711Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003712\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003713 mode: mode of process creation\n\
3714 path: path of executable file\n\
3715 args: tuple or list of arguments\n\
3716 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003717
3718static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003719posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003720{
Victor Stinner8c62be82010-05-06 00:08:46 +00003721 PyObject *opath;
3722 char *path;
3723 PyObject *argv, *env;
3724 char **argvlist;
3725 char **envlist;
3726 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003727 int mode;
3728 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003729 Py_intptr_t spawnval;
3730 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3731 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003732
Victor Stinner8c62be82010-05-06 00:08:46 +00003733 /* spawnve has four arguments: (mode, path, argv, env), where
3734 argv is a list or tuple of strings and env is a dictionary
3735 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003736
Victor Stinner8c62be82010-05-06 00:08:46 +00003737 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3738 PyUnicode_FSConverter,
3739 &opath, &argv, &env))
3740 return NULL;
3741 path = PyBytes_AsString(opath);
3742 if (PyList_Check(argv)) {
3743 argc = PyList_Size(argv);
3744 getitem = PyList_GetItem;
3745 }
3746 else if (PyTuple_Check(argv)) {
3747 argc = PyTuple_Size(argv);
3748 getitem = PyTuple_GetItem;
3749 }
3750 else {
3751 PyErr_SetString(PyExc_TypeError,
3752 "spawnve() arg 2 must be a tuple or list");
3753 goto fail_0;
3754 }
3755 if (!PyMapping_Check(env)) {
3756 PyErr_SetString(PyExc_TypeError,
3757 "spawnve() arg 3 must be a mapping object");
3758 goto fail_0;
3759 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003760
Victor Stinner8c62be82010-05-06 00:08:46 +00003761 argvlist = PyMem_NEW(char *, argc+1);
3762 if (argvlist == NULL) {
3763 PyErr_NoMemory();
3764 goto fail_0;
3765 }
3766 for (i = 0; i < argc; i++) {
3767 if (!fsconvert_strdup((*getitem)(argv, i),
3768 &argvlist[i]))
3769 {
3770 lastarg = i;
3771 goto fail_1;
3772 }
3773 }
3774 lastarg = argc;
3775 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003776
Victor Stinner8c62be82010-05-06 00:08:46 +00003777 envlist = parse_envlist(env, &envc);
3778 if (envlist == NULL)
3779 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003780
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003781#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003782 Py_BEGIN_ALLOW_THREADS
3783 spawnval = spawnve(mode, path, argvlist, envlist);
3784 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003785#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003786 if (mode == _OLD_P_OVERLAY)
3787 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003788
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 Py_BEGIN_ALLOW_THREADS
3790 spawnval = _spawnve(mode, path, argvlist, envlist);
3791 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003792#endif
Tim Peters25059d32001-12-07 20:35:43 +00003793
Victor Stinner8c62be82010-05-06 00:08:46 +00003794 if (spawnval == -1)
3795 (void) posix_error();
3796 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003797#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003799#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003800 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003801#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003802
Victor Stinner8c62be82010-05-06 00:08:46 +00003803 while (--envc >= 0)
3804 PyMem_DEL(envlist[envc]);
3805 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003806 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003807 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003808 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003809 Py_DECREF(opath);
3810 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003811}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003812
3813/* OS/2 supports spawnvp & spawnvpe natively */
3814#if defined(PYOS_OS2)
3815PyDoc_STRVAR(posix_spawnvp__doc__,
3816"spawnvp(mode, file, args)\n\n\
3817Execute the program 'file' in a new process, using the environment\n\
3818search path to find the file.\n\
3819\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003820 mode: mode of process creation\n\
3821 file: executable file name\n\
3822 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003823
3824static PyObject *
3825posix_spawnvp(PyObject *self, PyObject *args)
3826{
Victor Stinner8c62be82010-05-06 00:08:46 +00003827 PyObject *opath;
3828 char *path;
3829 PyObject *argv;
3830 char **argvlist;
3831 int mode, i, argc;
3832 Py_intptr_t spawnval;
3833 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003834
Victor Stinner8c62be82010-05-06 00:08:46 +00003835 /* spawnvp has three arguments: (mode, path, argv), where
3836 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003837
Victor Stinner8c62be82010-05-06 00:08:46 +00003838 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3839 PyUnicode_FSConverter,
3840 &opath, &argv))
3841 return NULL;
3842 path = PyBytes_AsString(opath);
3843 if (PyList_Check(argv)) {
3844 argc = PyList_Size(argv);
3845 getitem = PyList_GetItem;
3846 }
3847 else if (PyTuple_Check(argv)) {
3848 argc = PyTuple_Size(argv);
3849 getitem = PyTuple_GetItem;
3850 }
3851 else {
3852 PyErr_SetString(PyExc_TypeError,
3853 "spawnvp() arg 2 must be a tuple or list");
3854 Py_DECREF(opath);
3855 return NULL;
3856 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003857
Victor Stinner8c62be82010-05-06 00:08:46 +00003858 argvlist = PyMem_NEW(char *, argc+1);
3859 if (argvlist == NULL) {
3860 Py_DECREF(opath);
3861 return PyErr_NoMemory();
3862 }
3863 for (i = 0; i < argc; i++) {
3864 if (!fsconvert_strdup((*getitem)(argv, i),
3865 &argvlist[i])) {
3866 free_string_array(argvlist, i);
3867 PyErr_SetString(
3868 PyExc_TypeError,
3869 "spawnvp() arg 2 must contain only strings");
3870 Py_DECREF(opath);
3871 return NULL;
3872 }
3873 }
3874 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003875
Victor Stinner8c62be82010-05-06 00:08:46 +00003876 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003877#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003878 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003879#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003880 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003881#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003882 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003883
Victor Stinner8c62be82010-05-06 00:08:46 +00003884 free_string_array(argvlist, argc);
3885 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003886
Victor Stinner8c62be82010-05-06 00:08:46 +00003887 if (spawnval == -1)
3888 return posix_error();
3889 else
3890 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003891}
3892
3893
3894PyDoc_STRVAR(posix_spawnvpe__doc__,
3895"spawnvpe(mode, file, args, env)\n\n\
3896Execute the program 'file' in a new process, using the environment\n\
3897search path to find the file.\n\
3898\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003899 mode: mode of process creation\n\
3900 file: executable file name\n\
3901 args: tuple or list of arguments\n\
3902 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003903
3904static PyObject *
3905posix_spawnvpe(PyObject *self, PyObject *args)
3906{
Victor Stinner8c62be82010-05-06 00:08:46 +00003907 PyObject *opath
3908 char *path;
3909 PyObject *argv, *env;
3910 char **argvlist;
3911 char **envlist;
3912 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003913 int mode;
3914 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003915 Py_intptr_t spawnval;
3916 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3917 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003918
Victor Stinner8c62be82010-05-06 00:08:46 +00003919 /* spawnvpe has four arguments: (mode, path, argv, env), where
3920 argv is a list or tuple of strings and env is a dictionary
3921 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003922
Victor Stinner8c62be82010-05-06 00:08:46 +00003923 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3924 PyUnicode_FSConverter,
3925 &opath, &argv, &env))
3926 return NULL;
3927 path = PyBytes_AsString(opath);
3928 if (PyList_Check(argv)) {
3929 argc = PyList_Size(argv);
3930 getitem = PyList_GetItem;
3931 }
3932 else if (PyTuple_Check(argv)) {
3933 argc = PyTuple_Size(argv);
3934 getitem = PyTuple_GetItem;
3935 }
3936 else {
3937 PyErr_SetString(PyExc_TypeError,
3938 "spawnvpe() arg 2 must be a tuple or list");
3939 goto fail_0;
3940 }
3941 if (!PyMapping_Check(env)) {
3942 PyErr_SetString(PyExc_TypeError,
3943 "spawnvpe() arg 3 must be a mapping object");
3944 goto fail_0;
3945 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003946
Victor Stinner8c62be82010-05-06 00:08:46 +00003947 argvlist = PyMem_NEW(char *, argc+1);
3948 if (argvlist == NULL) {
3949 PyErr_NoMemory();
3950 goto fail_0;
3951 }
3952 for (i = 0; i < argc; i++) {
3953 if (!fsconvert_strdup((*getitem)(argv, i),
3954 &argvlist[i]))
3955 {
3956 lastarg = i;
3957 goto fail_1;
3958 }
3959 }
3960 lastarg = argc;
3961 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003962
Victor Stinner8c62be82010-05-06 00:08:46 +00003963 envlist = parse_envlist(env, &envc);
3964 if (envlist == NULL)
3965 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003966
Victor Stinner8c62be82010-05-06 00:08:46 +00003967 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003968#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003969 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003970#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003971 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003972#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003973 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003974
Victor Stinner8c62be82010-05-06 00:08:46 +00003975 if (spawnval == -1)
3976 (void) posix_error();
3977 else
3978 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003979
Victor Stinner8c62be82010-05-06 00:08:46 +00003980 while (--envc >= 0)
3981 PyMem_DEL(envlist[envc]);
3982 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003983 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003984 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003985 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003986 Py_DECREF(opath);
3987 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003988}
3989#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003990#endif /* HAVE_SPAWNV */
3991
3992
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003993#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003994PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003995"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003996Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3997\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003998Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003999
4000static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004001posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004002{
Victor Stinner8c62be82010-05-06 00:08:46 +00004003 pid_t pid;
4004 int result = 0;
4005 _PyImport_AcquireLock();
4006 pid = fork1();
4007 if (pid == 0) {
4008 /* child: this clobbers and resets the import lock. */
4009 PyOS_AfterFork();
4010 } else {
4011 /* parent: release the import lock. */
4012 result = _PyImport_ReleaseLock();
4013 }
4014 if (pid == -1)
4015 return posix_error();
4016 if (result < 0) {
4017 /* Don't clobber the OSError if the fork failed. */
4018 PyErr_SetString(PyExc_RuntimeError,
4019 "not holding the import lock");
4020 return NULL;
4021 }
4022 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004023}
4024#endif
4025
4026
Guido van Rossumad0ee831995-03-01 10:34:45 +00004027#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004028PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004029"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004030Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004031Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004032
Barry Warsaw53699e91996-12-10 23:23:01 +00004033static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004034posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004035{
Victor Stinner8c62be82010-05-06 00:08:46 +00004036 pid_t pid;
4037 int result = 0;
4038 _PyImport_AcquireLock();
4039 pid = fork();
4040 if (pid == 0) {
4041 /* child: this clobbers and resets the import lock. */
4042 PyOS_AfterFork();
4043 } else {
4044 /* parent: release the import lock. */
4045 result = _PyImport_ReleaseLock();
4046 }
4047 if (pid == -1)
4048 return posix_error();
4049 if (result < 0) {
4050 /* Don't clobber the OSError if the fork failed. */
4051 PyErr_SetString(PyExc_RuntimeError,
4052 "not holding the import lock");
4053 return NULL;
4054 }
4055 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004056}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004057#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004058
Neal Norwitzb59798b2003-03-21 01:43:31 +00004059/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004060/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4061#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004062#define DEV_PTY_FILE "/dev/ptc"
4063#define HAVE_DEV_PTMX
4064#else
4065#define DEV_PTY_FILE "/dev/ptmx"
4066#endif
4067
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004068#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004069#ifdef HAVE_PTY_H
4070#include <pty.h>
4071#else
4072#ifdef HAVE_LIBUTIL_H
4073#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004074#else
4075#ifdef HAVE_UTIL_H
4076#include <util.h>
4077#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004078#endif /* HAVE_LIBUTIL_H */
4079#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004080#ifdef HAVE_STROPTS_H
4081#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004082#endif
4083#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004084
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004085#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004086PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004087"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004088Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004089
4090static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004091posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004092{
Victor Stinner8c62be82010-05-06 00:08:46 +00004093 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004094#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004095 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004096#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004097#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004098 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004099#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004100 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004101#endif
4102#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004103
Thomas Wouters70c21a12000-07-14 14:28:33 +00004104#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004105 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4106 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004107#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004108 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4109 if (slave_name == NULL)
4110 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004111
Victor Stinner8c62be82010-05-06 00:08:46 +00004112 slave_fd = open(slave_name, O_RDWR);
4113 if (slave_fd < 0)
4114 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004115#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004116 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4117 if (master_fd < 0)
4118 return posix_error();
4119 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4120 /* change permission of slave */
4121 if (grantpt(master_fd) < 0) {
4122 PyOS_setsig(SIGCHLD, sig_saved);
4123 return posix_error();
4124 }
4125 /* unlock slave */
4126 if (unlockpt(master_fd) < 0) {
4127 PyOS_setsig(SIGCHLD, sig_saved);
4128 return posix_error();
4129 }
4130 PyOS_setsig(SIGCHLD, sig_saved);
4131 slave_name = ptsname(master_fd); /* get name of slave */
4132 if (slave_name == NULL)
4133 return posix_error();
4134 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4135 if (slave_fd < 0)
4136 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004137#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004138 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4139 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004140#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004141 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004142#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004143#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004144#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004145
Victor Stinner8c62be82010-05-06 00:08:46 +00004146 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004147
Fred Drake8cef4cf2000-06-28 16:40:38 +00004148}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004149#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004150
4151#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004152PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004153"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004154Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4155Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004156To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004157
4158static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004159posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004160{
Victor Stinner8c62be82010-05-06 00:08:46 +00004161 int master_fd = -1, result = 0;
4162 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004163
Victor Stinner8c62be82010-05-06 00:08:46 +00004164 _PyImport_AcquireLock();
4165 pid = forkpty(&master_fd, NULL, NULL, NULL);
4166 if (pid == 0) {
4167 /* child: this clobbers and resets the import lock. */
4168 PyOS_AfterFork();
4169 } else {
4170 /* parent: release the import lock. */
4171 result = _PyImport_ReleaseLock();
4172 }
4173 if (pid == -1)
4174 return posix_error();
4175 if (result < 0) {
4176 /* Don't clobber the OSError if the fork failed. */
4177 PyErr_SetString(PyExc_RuntimeError,
4178 "not holding the import lock");
4179 return NULL;
4180 }
4181 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004182}
4183#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004184
Guido van Rossumad0ee831995-03-01 10:34:45 +00004185#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004186PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004187"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004188Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004189
Barry Warsaw53699e91996-12-10 23:23:01 +00004190static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004191posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004192{
Victor Stinner8c62be82010-05-06 00:08:46 +00004193 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004194}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004195#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004196
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004197
Guido van Rossumad0ee831995-03-01 10:34:45 +00004198#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004199PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004200"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004201Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004202
Barry Warsaw53699e91996-12-10 23:23:01 +00004203static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004204posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004205{
Victor Stinner8c62be82010-05-06 00:08:46 +00004206 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004207}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004208#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004209
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004210
Guido van Rossumad0ee831995-03-01 10:34:45 +00004211#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004212PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004213"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004214Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004215
Barry Warsaw53699e91996-12-10 23:23:01 +00004216static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004217posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004218{
Victor Stinner8c62be82010-05-06 00:08:46 +00004219 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004220}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004221#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004222
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004223
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004224PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004225"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004226Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004227
Barry Warsaw53699e91996-12-10 23:23:01 +00004228static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004229posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004230{
Victor Stinner8c62be82010-05-06 00:08:46 +00004231 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004232}
4233
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004234
Fred Drakec9680921999-12-13 16:37:25 +00004235#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004236PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004237"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004238Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004239
4240static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004241posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004242{
4243 PyObject *result = NULL;
4244
Fred Drakec9680921999-12-13 16:37:25 +00004245#ifdef NGROUPS_MAX
4246#define MAX_GROUPS NGROUPS_MAX
4247#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004248 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004249#define MAX_GROUPS 64
4250#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004251 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004252
4253 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4254 * This is a helper variable to store the intermediate result when
4255 * that happens.
4256 *
4257 * To keep the code readable the OSX behaviour is unconditional,
4258 * according to the POSIX spec this should be safe on all unix-y
4259 * systems.
4260 */
4261 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004262 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004263
Victor Stinner8c62be82010-05-06 00:08:46 +00004264 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004265 if (n < 0) {
4266 if (errno == EINVAL) {
4267 n = getgroups(0, NULL);
4268 if (n == -1) {
4269 return posix_error();
4270 }
4271 if (n == 0) {
4272 /* Avoid malloc(0) */
4273 alt_grouplist = grouplist;
4274 } else {
4275 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4276 if (alt_grouplist == NULL) {
4277 errno = EINVAL;
4278 return posix_error();
4279 }
4280 n = getgroups(n, alt_grouplist);
4281 if (n == -1) {
4282 PyMem_Free(alt_grouplist);
4283 return posix_error();
4284 }
4285 }
4286 } else {
4287 return posix_error();
4288 }
4289 }
4290 result = PyList_New(n);
4291 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004292 int i;
4293 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004294 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004295 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004296 Py_DECREF(result);
4297 result = NULL;
4298 break;
Fred Drakec9680921999-12-13 16:37:25 +00004299 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004300 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004301 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004302 }
4303
4304 if (alt_grouplist != grouplist) {
4305 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004306 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004307
Fred Drakec9680921999-12-13 16:37:25 +00004308 return result;
4309}
4310#endif
4311
Antoine Pitroub7572f02009-12-02 20:46:48 +00004312#ifdef HAVE_INITGROUPS
4313PyDoc_STRVAR(posix_initgroups__doc__,
4314"initgroups(username, gid) -> None\n\n\
4315Call the system initgroups() to initialize the group access list with all of\n\
4316the groups of which the specified username is a member, plus the specified\n\
4317group id.");
4318
4319static PyObject *
4320posix_initgroups(PyObject *self, PyObject *args)
4321{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004322 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004323 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004324 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004325 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004326
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004327 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4328 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004329 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004330 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004331
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004332 res = initgroups(username, (gid_t) gid);
4333 Py_DECREF(oname);
4334 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004335 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004336
Victor Stinner8c62be82010-05-06 00:08:46 +00004337 Py_INCREF(Py_None);
4338 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004339}
4340#endif
4341
Martin v. Löwis606edc12002-06-13 21:09:11 +00004342#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004343PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004344"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004345Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004346
4347static PyObject *
4348posix_getpgid(PyObject *self, PyObject *args)
4349{
Victor Stinner8c62be82010-05-06 00:08:46 +00004350 pid_t pid, pgid;
4351 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4352 return NULL;
4353 pgid = getpgid(pid);
4354 if (pgid < 0)
4355 return posix_error();
4356 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004357}
4358#endif /* HAVE_GETPGID */
4359
4360
Guido van Rossumb6775db1994-08-01 11:34:53 +00004361#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004362PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004363"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004364Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004365
Barry Warsaw53699e91996-12-10 23:23:01 +00004366static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004367posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004368{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004369#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004370 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004371#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004372 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004373#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004374}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004375#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004376
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004377
Guido van Rossumb6775db1994-08-01 11:34:53 +00004378#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004379PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004380"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004381Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004382
Barry Warsaw53699e91996-12-10 23:23:01 +00004383static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004384posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004385{
Guido van Rossum64933891994-10-20 21:56:42 +00004386#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004388#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004389 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004390#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004391 return posix_error();
4392 Py_INCREF(Py_None);
4393 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004394}
4395
Guido van Rossumb6775db1994-08-01 11:34:53 +00004396#endif /* HAVE_SETPGRP */
4397
Guido van Rossumad0ee831995-03-01 10:34:45 +00004398#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004399
4400#ifdef MS_WINDOWS
4401#include <tlhelp32.h>
4402
4403static PyObject*
4404win32_getppid()
4405{
4406 HANDLE snapshot;
4407 pid_t mypid;
4408 PyObject* result = NULL;
4409 BOOL have_record;
4410 PROCESSENTRY32 pe;
4411
4412 mypid = getpid(); /* This function never fails */
4413
4414 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4415 if (snapshot == INVALID_HANDLE_VALUE)
4416 return PyErr_SetFromWindowsErr(GetLastError());
4417
4418 pe.dwSize = sizeof(pe);
4419 have_record = Process32First(snapshot, &pe);
4420 while (have_record) {
4421 if (mypid == (pid_t)pe.th32ProcessID) {
4422 /* We could cache the ulong value in a static variable. */
4423 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4424 break;
4425 }
4426
4427 have_record = Process32Next(snapshot, &pe);
4428 }
4429
4430 /* If our loop exits and our pid was not found (result will be NULL)
4431 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4432 * error anyway, so let's raise it. */
4433 if (!result)
4434 result = PyErr_SetFromWindowsErr(GetLastError());
4435
4436 CloseHandle(snapshot);
4437
4438 return result;
4439}
4440#endif /*MS_WINDOWS*/
4441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004442PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004443"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004444Return the parent's process id. If the parent process has already exited,\n\
4445Windows machines will still return its id; others systems will return the id\n\
4446of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004447
Barry Warsaw53699e91996-12-10 23:23:01 +00004448static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004449posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004450{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004451#ifdef MS_WINDOWS
4452 return win32_getppid();
4453#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004454 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004455#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004456}
4457#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004458
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004459
Fred Drake12c6e2d1999-12-14 21:25:03 +00004460#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004461PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004462"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004463Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004464
4465static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004466posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004467{
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004469#ifdef MS_WINDOWS
4470 wchar_t user_name[UNLEN + 1];
4471 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4472
4473 if (GetUserNameW(user_name, &num_chars)) {
4474 /* num_chars is the number of unicode chars plus null terminator */
4475 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4476 }
4477 else
4478 result = PyErr_SetFromWindowsErr(GetLastError());
4479#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004480 char *name;
4481 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004482
Victor Stinner8c62be82010-05-06 00:08:46 +00004483 errno = 0;
4484 name = getlogin();
4485 if (name == NULL) {
4486 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004487 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004488 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004489 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004490 }
4491 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004492 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004493 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004494#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004495 return result;
4496}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004497#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004498
Guido van Rossumad0ee831995-03-01 10:34:45 +00004499#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004500PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004501"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004502Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004503
Barry Warsaw53699e91996-12-10 23:23:01 +00004504static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004505posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004506{
Victor Stinner8c62be82010-05-06 00:08:46 +00004507 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004508}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004509#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004510
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004511
Guido van Rossumad0ee831995-03-01 10:34:45 +00004512#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004513PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004514"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004515Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004516
Barry Warsaw53699e91996-12-10 23:23:01 +00004517static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004518posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004519{
Victor Stinner8c62be82010-05-06 00:08:46 +00004520 pid_t pid;
4521 int sig;
4522 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4523 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004524#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004525 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4526 APIRET rc;
4527 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004528 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004529
4530 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4531 APIRET rc;
4532 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004533 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004534
4535 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004536 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004537#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004538 if (kill(pid, sig) == -1)
4539 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004540#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 Py_INCREF(Py_None);
4542 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004543}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004544#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004545
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004546#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004547PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004548"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004549Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004550
4551static PyObject *
4552posix_killpg(PyObject *self, PyObject *args)
4553{
Victor Stinner8c62be82010-05-06 00:08:46 +00004554 int sig;
4555 pid_t pgid;
4556 /* XXX some man pages make the `pgid` parameter an int, others
4557 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4558 take the same type. Moreover, pid_t is always at least as wide as
4559 int (else compilation of this module fails), which is safe. */
4560 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4561 return NULL;
4562 if (killpg(pgid, sig) == -1)
4563 return posix_error();
4564 Py_INCREF(Py_None);
4565 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004566}
4567#endif
4568
Brian Curtineb24d742010-04-12 17:16:38 +00004569#ifdef MS_WINDOWS
4570PyDoc_STRVAR(win32_kill__doc__,
4571"kill(pid, sig)\n\n\
4572Kill a process with a signal.");
4573
4574static PyObject *
4575win32_kill(PyObject *self, PyObject *args)
4576{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004577 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004578 DWORD pid, sig, err;
4579 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004580
Victor Stinner8c62be82010-05-06 00:08:46 +00004581 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4582 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004583
Victor Stinner8c62be82010-05-06 00:08:46 +00004584 /* Console processes which share a common console can be sent CTRL+C or
4585 CTRL+BREAK events, provided they handle said events. */
4586 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4587 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4588 err = GetLastError();
4589 PyErr_SetFromWindowsErr(err);
4590 }
4591 else
4592 Py_RETURN_NONE;
4593 }
Brian Curtineb24d742010-04-12 17:16:38 +00004594
Victor Stinner8c62be82010-05-06 00:08:46 +00004595 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4596 attempt to open and terminate the process. */
4597 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4598 if (handle == NULL) {
4599 err = GetLastError();
4600 return PyErr_SetFromWindowsErr(err);
4601 }
Brian Curtineb24d742010-04-12 17:16:38 +00004602
Victor Stinner8c62be82010-05-06 00:08:46 +00004603 if (TerminateProcess(handle, sig) == 0) {
4604 err = GetLastError();
4605 result = PyErr_SetFromWindowsErr(err);
4606 } else {
4607 Py_INCREF(Py_None);
4608 result = Py_None;
4609 }
Brian Curtineb24d742010-04-12 17:16:38 +00004610
Victor Stinner8c62be82010-05-06 00:08:46 +00004611 CloseHandle(handle);
4612 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004613}
4614#endif /* MS_WINDOWS */
4615
Guido van Rossumc0125471996-06-28 18:55:32 +00004616#ifdef HAVE_PLOCK
4617
4618#ifdef HAVE_SYS_LOCK_H
4619#include <sys/lock.h>
4620#endif
4621
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004622PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004623"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004624Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004625
Barry Warsaw53699e91996-12-10 23:23:01 +00004626static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004627posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004628{
Victor Stinner8c62be82010-05-06 00:08:46 +00004629 int op;
4630 if (!PyArg_ParseTuple(args, "i:plock", &op))
4631 return NULL;
4632 if (plock(op) == -1)
4633 return posix_error();
4634 Py_INCREF(Py_None);
4635 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004636}
4637#endif
4638
Guido van Rossumb6775db1994-08-01 11:34:53 +00004639#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004640PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004641"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004642Set the current process's user id.");
4643
Barry Warsaw53699e91996-12-10 23:23:01 +00004644static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004645posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004646{
Victor Stinner8c62be82010-05-06 00:08:46 +00004647 long uid_arg;
4648 uid_t uid;
4649 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4650 return NULL;
4651 uid = uid_arg;
4652 if (uid != uid_arg) {
4653 PyErr_SetString(PyExc_OverflowError, "user id too big");
4654 return NULL;
4655 }
4656 if (setuid(uid) < 0)
4657 return posix_error();
4658 Py_INCREF(Py_None);
4659 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004660}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004661#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004662
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004663
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004664#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004665PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004666"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004667Set the current process's effective user id.");
4668
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004669static PyObject *
4670posix_seteuid (PyObject *self, PyObject *args)
4671{
Victor Stinner8c62be82010-05-06 00:08:46 +00004672 long euid_arg;
4673 uid_t euid;
4674 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4675 return NULL;
4676 euid = euid_arg;
4677 if (euid != euid_arg) {
4678 PyErr_SetString(PyExc_OverflowError, "user id too big");
4679 return NULL;
4680 }
4681 if (seteuid(euid) < 0) {
4682 return posix_error();
4683 } else {
4684 Py_INCREF(Py_None);
4685 return Py_None;
4686 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004687}
4688#endif /* HAVE_SETEUID */
4689
4690#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004691PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004692"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004693Set the current process's effective group id.");
4694
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004695static PyObject *
4696posix_setegid (PyObject *self, PyObject *args)
4697{
Victor Stinner8c62be82010-05-06 00:08:46 +00004698 long egid_arg;
4699 gid_t egid;
4700 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4701 return NULL;
4702 egid = egid_arg;
4703 if (egid != egid_arg) {
4704 PyErr_SetString(PyExc_OverflowError, "group id too big");
4705 return NULL;
4706 }
4707 if (setegid(egid) < 0) {
4708 return posix_error();
4709 } else {
4710 Py_INCREF(Py_None);
4711 return Py_None;
4712 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004713}
4714#endif /* HAVE_SETEGID */
4715
4716#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004717PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004718"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004719Set the current process's real and effective user ids.");
4720
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004721static PyObject *
4722posix_setreuid (PyObject *self, PyObject *args)
4723{
Victor Stinner8c62be82010-05-06 00:08:46 +00004724 long ruid_arg, euid_arg;
4725 uid_t ruid, euid;
4726 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4727 return NULL;
4728 if (ruid_arg == -1)
4729 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4730 else
4731 ruid = ruid_arg; /* otherwise, assign from our long */
4732 if (euid_arg == -1)
4733 euid = (uid_t)-1;
4734 else
4735 euid = euid_arg;
4736 if ((euid_arg != -1 && euid != euid_arg) ||
4737 (ruid_arg != -1 && ruid != ruid_arg)) {
4738 PyErr_SetString(PyExc_OverflowError, "user id too big");
4739 return NULL;
4740 }
4741 if (setreuid(ruid, euid) < 0) {
4742 return posix_error();
4743 } else {
4744 Py_INCREF(Py_None);
4745 return Py_None;
4746 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004747}
4748#endif /* HAVE_SETREUID */
4749
4750#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004751PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004752"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004753Set the current process's real and effective group ids.");
4754
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004755static PyObject *
4756posix_setregid (PyObject *self, PyObject *args)
4757{
Victor Stinner8c62be82010-05-06 00:08:46 +00004758 long rgid_arg, egid_arg;
4759 gid_t rgid, egid;
4760 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4761 return NULL;
4762 if (rgid_arg == -1)
4763 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4764 else
4765 rgid = rgid_arg; /* otherwise, assign from our long */
4766 if (egid_arg == -1)
4767 egid = (gid_t)-1;
4768 else
4769 egid = egid_arg;
4770 if ((egid_arg != -1 && egid != egid_arg) ||
4771 (rgid_arg != -1 && rgid != rgid_arg)) {
4772 PyErr_SetString(PyExc_OverflowError, "group id too big");
4773 return NULL;
4774 }
4775 if (setregid(rgid, egid) < 0) {
4776 return posix_error();
4777 } else {
4778 Py_INCREF(Py_None);
4779 return Py_None;
4780 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004781}
4782#endif /* HAVE_SETREGID */
4783
Guido van Rossumb6775db1994-08-01 11:34:53 +00004784#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004785PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004786"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004787Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004788
Barry Warsaw53699e91996-12-10 23:23:01 +00004789static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004790posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004791{
Victor Stinner8c62be82010-05-06 00:08:46 +00004792 long gid_arg;
4793 gid_t gid;
4794 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4795 return NULL;
4796 gid = gid_arg;
4797 if (gid != gid_arg) {
4798 PyErr_SetString(PyExc_OverflowError, "group id too big");
4799 return NULL;
4800 }
4801 if (setgid(gid) < 0)
4802 return posix_error();
4803 Py_INCREF(Py_None);
4804 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004805}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004806#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004807
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004808#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004809PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004810"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004811Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004812
4813static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004814posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004815{
Victor Stinner8c62be82010-05-06 00:08:46 +00004816 int i, len;
4817 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004818
Victor Stinner8c62be82010-05-06 00:08:46 +00004819 if (!PySequence_Check(groups)) {
4820 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4821 return NULL;
4822 }
4823 len = PySequence_Size(groups);
4824 if (len > MAX_GROUPS) {
4825 PyErr_SetString(PyExc_ValueError, "too many groups");
4826 return NULL;
4827 }
4828 for(i = 0; i < len; i++) {
4829 PyObject *elem;
4830 elem = PySequence_GetItem(groups, i);
4831 if (!elem)
4832 return NULL;
4833 if (!PyLong_Check(elem)) {
4834 PyErr_SetString(PyExc_TypeError,
4835 "groups must be integers");
4836 Py_DECREF(elem);
4837 return NULL;
4838 } else {
4839 unsigned long x = PyLong_AsUnsignedLong(elem);
4840 if (PyErr_Occurred()) {
4841 PyErr_SetString(PyExc_TypeError,
4842 "group id too big");
4843 Py_DECREF(elem);
4844 return NULL;
4845 }
4846 grouplist[i] = x;
4847 /* read back the value to see if it fitted in gid_t */
4848 if (grouplist[i] != x) {
4849 PyErr_SetString(PyExc_TypeError,
4850 "group id too big");
4851 Py_DECREF(elem);
4852 return NULL;
4853 }
4854 }
4855 Py_DECREF(elem);
4856 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004857
Victor Stinner8c62be82010-05-06 00:08:46 +00004858 if (setgroups(len, grouplist) < 0)
4859 return posix_error();
4860 Py_INCREF(Py_None);
4861 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004862}
4863#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004864
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004865#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4866static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004867wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004868{
Victor Stinner8c62be82010-05-06 00:08:46 +00004869 PyObject *result;
4870 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004871
Victor Stinner8c62be82010-05-06 00:08:46 +00004872 if (pid == -1)
4873 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004874
Victor Stinner8c62be82010-05-06 00:08:46 +00004875 if (struct_rusage == NULL) {
4876 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4877 if (m == NULL)
4878 return NULL;
4879 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4880 Py_DECREF(m);
4881 if (struct_rusage == NULL)
4882 return NULL;
4883 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004884
Victor Stinner8c62be82010-05-06 00:08:46 +00004885 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4886 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4887 if (!result)
4888 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004889
4890#ifndef doubletime
4891#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4892#endif
4893
Victor Stinner8c62be82010-05-06 00:08:46 +00004894 PyStructSequence_SET_ITEM(result, 0,
4895 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4896 PyStructSequence_SET_ITEM(result, 1,
4897 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004898#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004899 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4900 SET_INT(result, 2, ru->ru_maxrss);
4901 SET_INT(result, 3, ru->ru_ixrss);
4902 SET_INT(result, 4, ru->ru_idrss);
4903 SET_INT(result, 5, ru->ru_isrss);
4904 SET_INT(result, 6, ru->ru_minflt);
4905 SET_INT(result, 7, ru->ru_majflt);
4906 SET_INT(result, 8, ru->ru_nswap);
4907 SET_INT(result, 9, ru->ru_inblock);
4908 SET_INT(result, 10, ru->ru_oublock);
4909 SET_INT(result, 11, ru->ru_msgsnd);
4910 SET_INT(result, 12, ru->ru_msgrcv);
4911 SET_INT(result, 13, ru->ru_nsignals);
4912 SET_INT(result, 14, ru->ru_nvcsw);
4913 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004914#undef SET_INT
4915
Victor Stinner8c62be82010-05-06 00:08:46 +00004916 if (PyErr_Occurred()) {
4917 Py_DECREF(result);
4918 return NULL;
4919 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004920
Victor Stinner8c62be82010-05-06 00:08:46 +00004921 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004922}
4923#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4924
4925#ifdef HAVE_WAIT3
4926PyDoc_STRVAR(posix_wait3__doc__,
4927"wait3(options) -> (pid, status, rusage)\n\n\
4928Wait for completion of a child process.");
4929
4930static PyObject *
4931posix_wait3(PyObject *self, PyObject *args)
4932{
Victor Stinner8c62be82010-05-06 00:08:46 +00004933 pid_t pid;
4934 int options;
4935 struct rusage ru;
4936 WAIT_TYPE status;
4937 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004938
Victor Stinner8c62be82010-05-06 00:08:46 +00004939 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4940 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004941
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 Py_BEGIN_ALLOW_THREADS
4943 pid = wait3(&status, options, &ru);
4944 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004945
Victor Stinner8c62be82010-05-06 00:08:46 +00004946 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004947}
4948#endif /* HAVE_WAIT3 */
4949
4950#ifdef HAVE_WAIT4
4951PyDoc_STRVAR(posix_wait4__doc__,
4952"wait4(pid, options) -> (pid, status, rusage)\n\n\
4953Wait for completion of a given child process.");
4954
4955static PyObject *
4956posix_wait4(PyObject *self, PyObject *args)
4957{
Victor Stinner8c62be82010-05-06 00:08:46 +00004958 pid_t pid;
4959 int options;
4960 struct rusage ru;
4961 WAIT_TYPE status;
4962 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004963
Victor Stinner8c62be82010-05-06 00:08:46 +00004964 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4965 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004966
Victor Stinner8c62be82010-05-06 00:08:46 +00004967 Py_BEGIN_ALLOW_THREADS
4968 pid = wait4(pid, &status, options, &ru);
4969 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004970
Victor Stinner8c62be82010-05-06 00:08:46 +00004971 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004972}
4973#endif /* HAVE_WAIT4 */
4974
Guido van Rossumb6775db1994-08-01 11:34:53 +00004975#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004976PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004977"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004978Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004979
Barry Warsaw53699e91996-12-10 23:23:01 +00004980static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004981posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004982{
Victor Stinner8c62be82010-05-06 00:08:46 +00004983 pid_t pid;
4984 int options;
4985 WAIT_TYPE status;
4986 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004987
Victor Stinner8c62be82010-05-06 00:08:46 +00004988 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4989 return NULL;
4990 Py_BEGIN_ALLOW_THREADS
4991 pid = waitpid(pid, &status, options);
4992 Py_END_ALLOW_THREADS
4993 if (pid == -1)
4994 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004995
Victor Stinner8c62be82010-05-06 00:08:46 +00004996 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004997}
4998
Tim Petersab034fa2002-02-01 11:27:43 +00004999#elif defined(HAVE_CWAIT)
5000
5001/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005002PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005003"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005004"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005005
5006static PyObject *
5007posix_waitpid(PyObject *self, PyObject *args)
5008{
Victor Stinner8c62be82010-05-06 00:08:46 +00005009 Py_intptr_t pid;
5010 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005011
Victor Stinner8c62be82010-05-06 00:08:46 +00005012 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5013 return NULL;
5014 Py_BEGIN_ALLOW_THREADS
5015 pid = _cwait(&status, pid, options);
5016 Py_END_ALLOW_THREADS
5017 if (pid == -1)
5018 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005019
Victor Stinner8c62be82010-05-06 00:08:46 +00005020 /* shift the status left a byte so this is more like the POSIX waitpid */
5021 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005022}
5023#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005024
Guido van Rossumad0ee831995-03-01 10:34:45 +00005025#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005026PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005027"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005028Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005029
Barry Warsaw53699e91996-12-10 23:23:01 +00005030static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005031posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005032{
Victor Stinner8c62be82010-05-06 00:08:46 +00005033 pid_t pid;
5034 WAIT_TYPE status;
5035 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005036
Victor Stinner8c62be82010-05-06 00:08:46 +00005037 Py_BEGIN_ALLOW_THREADS
5038 pid = wait(&status);
5039 Py_END_ALLOW_THREADS
5040 if (pid == -1)
5041 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005042
Victor Stinner8c62be82010-05-06 00:08:46 +00005043 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005044}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005045#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005046
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005047
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005048PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005049"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005050Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005051
Barry Warsaw53699e91996-12-10 23:23:01 +00005052static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005053posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005054{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005055#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005056 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005057#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005058#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005059 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5060 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005061#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005062 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005063#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005064#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005065}
5066
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005067
Guido van Rossumb6775db1994-08-01 11:34:53 +00005068#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005069PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005070"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005071Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005072
Barry Warsaw53699e91996-12-10 23:23:01 +00005073static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005074posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005075{
Victor Stinner8c62be82010-05-06 00:08:46 +00005076 PyObject* v;
5077 char buf[MAXPATHLEN];
5078 PyObject *opath;
5079 char *path;
5080 int n;
5081 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005082
Victor Stinner8c62be82010-05-06 00:08:46 +00005083 if (!PyArg_ParseTuple(args, "O&:readlink",
5084 PyUnicode_FSConverter, &opath))
5085 return NULL;
5086 path = PyBytes_AsString(opath);
5087 v = PySequence_GetItem(args, 0);
5088 if (v == NULL) {
5089 Py_DECREF(opath);
5090 return NULL;
5091 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005092
Victor Stinner8c62be82010-05-06 00:08:46 +00005093 if (PyUnicode_Check(v)) {
5094 arg_is_unicode = 1;
5095 }
5096 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005097
Victor Stinner8c62be82010-05-06 00:08:46 +00005098 Py_BEGIN_ALLOW_THREADS
5099 n = readlink(path, buf, (int) sizeof buf);
5100 Py_END_ALLOW_THREADS
5101 if (n < 0)
5102 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005103
Victor Stinner8c62be82010-05-06 00:08:46 +00005104 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005105 if (arg_is_unicode)
5106 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5107 else
5108 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005109}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005110#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005111
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005112
Brian Curtin52173d42010-12-02 18:29:18 +00005113#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005114PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005115"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005116Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005117
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005119posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005120{
Victor Stinner8c62be82010-05-06 00:08:46 +00005121 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005122}
5123#endif /* HAVE_SYMLINK */
5124
Brian Curtind40e6f72010-07-08 21:39:08 +00005125#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5126
5127PyDoc_STRVAR(win_readlink__doc__,
5128"readlink(path) -> path\n\n\
5129Return a string representing the path to which the symbolic link points.");
5130
Brian Curtind40e6f72010-07-08 21:39:08 +00005131/* Windows readlink implementation */
5132static PyObject *
5133win_readlink(PyObject *self, PyObject *args)
5134{
5135 wchar_t *path;
5136 DWORD n_bytes_returned;
5137 DWORD io_result;
5138 PyObject *result;
5139 HANDLE reparse_point_handle;
5140
5141 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5142 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5143 wchar_t *print_name;
5144
5145 if (!PyArg_ParseTuple(args,
5146 "u:readlink",
5147 &path))
5148 return NULL;
5149
5150 /* First get a handle to the reparse point */
5151 Py_BEGIN_ALLOW_THREADS
5152 reparse_point_handle = CreateFileW(
5153 path,
5154 0,
5155 0,
5156 0,
5157 OPEN_EXISTING,
5158 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5159 0);
5160 Py_END_ALLOW_THREADS
5161
5162 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5163 {
5164 return win32_error_unicode("readlink", path);
5165 }
5166
5167 Py_BEGIN_ALLOW_THREADS
5168 /* New call DeviceIoControl to read the reparse point */
5169 io_result = DeviceIoControl(
5170 reparse_point_handle,
5171 FSCTL_GET_REPARSE_POINT,
5172 0, 0, /* in buffer */
5173 target_buffer, sizeof(target_buffer),
5174 &n_bytes_returned,
5175 0 /* we're not using OVERLAPPED_IO */
5176 );
5177 CloseHandle(reparse_point_handle);
5178 Py_END_ALLOW_THREADS
5179
5180 if (io_result==0)
5181 {
5182 return win32_error_unicode("readlink", path);
5183 }
5184
5185 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5186 {
5187 PyErr_SetString(PyExc_ValueError,
5188 "not a symbolic link");
5189 return NULL;
5190 }
Brian Curtin74e45612010-07-09 15:58:59 +00005191 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5192 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5193
5194 result = PyUnicode_FromWideChar(print_name,
5195 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005196 return result;
5197}
5198
5199#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5200
Brian Curtin52173d42010-12-02 18:29:18 +00005201#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005202
5203/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5204static int has_CreateSymbolicLinkW = 0;
5205static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5206static int
5207check_CreateSymbolicLinkW()
5208{
5209 HINSTANCE hKernel32;
5210 /* only recheck */
5211 if (has_CreateSymbolicLinkW)
5212 return has_CreateSymbolicLinkW;
5213 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005214 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5215 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005216 if (Py_CreateSymbolicLinkW)
5217 has_CreateSymbolicLinkW = 1;
5218 return has_CreateSymbolicLinkW;
5219}
5220
5221PyDoc_STRVAR(win_symlink__doc__,
5222"symlink(src, dst, target_is_directory=False)\n\n\
5223Create a symbolic link pointing to src named dst.\n\
5224target_is_directory is required if the target is to be interpreted as\n\
5225a directory.\n\
5226This function requires Windows 6.0 or greater, and raises a\n\
5227NotImplementedError otherwise.");
5228
5229static PyObject *
5230win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5231{
5232 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5233 PyObject *src, *dest;
5234 int target_is_directory = 0;
5235 DWORD res;
5236 WIN32_FILE_ATTRIBUTE_DATA src_info;
5237
5238 if (!check_CreateSymbolicLinkW())
5239 {
5240 /* raise NotImplementedError */
5241 return PyErr_Format(PyExc_NotImplementedError,
5242 "CreateSymbolicLinkW not found");
5243 }
5244 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5245 kwlist, &src, &dest, &target_is_directory))
5246 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005247
5248 if (win32_can_symlink == 0)
5249 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5250
Brian Curtind40e6f72010-07-08 21:39:08 +00005251 if (!convert_to_unicode(&src)) { return NULL; }
5252 if (!convert_to_unicode(&dest)) {
5253 Py_DECREF(src);
5254 return NULL;
5255 }
5256
5257 /* if src is a directory, ensure target_is_directory==1 */
5258 if(
5259 GetFileAttributesExW(
5260 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5261 ))
5262 {
5263 target_is_directory = target_is_directory ||
5264 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5265 }
5266
5267 Py_BEGIN_ALLOW_THREADS
5268 res = Py_CreateSymbolicLinkW(
5269 PyUnicode_AsUnicode(dest),
5270 PyUnicode_AsUnicode(src),
5271 target_is_directory);
5272 Py_END_ALLOW_THREADS
5273 Py_DECREF(src);
5274 Py_DECREF(dest);
5275 if (!res)
5276 {
5277 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5278 }
5279
5280 Py_INCREF(Py_None);
5281 return Py_None;
5282}
Brian Curtin52173d42010-12-02 18:29:18 +00005283#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005284
5285#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005286#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5287static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005288system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005289{
5290 ULONG value = 0;
5291
5292 Py_BEGIN_ALLOW_THREADS
5293 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5294 Py_END_ALLOW_THREADS
5295
5296 return value;
5297}
5298
5299static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005300posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005301{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005302 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005303 return Py_BuildValue("ddddd",
5304 (double)0 /* t.tms_utime / HZ */,
5305 (double)0 /* t.tms_stime / HZ */,
5306 (double)0 /* t.tms_cutime / HZ */,
5307 (double)0 /* t.tms_cstime / HZ */,
5308 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005309}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005310#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005311#define NEED_TICKS_PER_SECOND
5312static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005313static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005314posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005315{
Victor Stinner8c62be82010-05-06 00:08:46 +00005316 struct tms t;
5317 clock_t c;
5318 errno = 0;
5319 c = times(&t);
5320 if (c == (clock_t) -1)
5321 return posix_error();
5322 return Py_BuildValue("ddddd",
5323 (double)t.tms_utime / ticks_per_second,
5324 (double)t.tms_stime / ticks_per_second,
5325 (double)t.tms_cutime / ticks_per_second,
5326 (double)t.tms_cstime / ticks_per_second,
5327 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005328}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005329#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005330#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005331
5332
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005333#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005334#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005335static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005336posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005337{
Victor Stinner8c62be82010-05-06 00:08:46 +00005338 FILETIME create, exit, kernel, user;
5339 HANDLE hProc;
5340 hProc = GetCurrentProcess();
5341 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5342 /* The fields of a FILETIME structure are the hi and lo part
5343 of a 64-bit value expressed in 100 nanosecond units.
5344 1e7 is one second in such units; 1e-7 the inverse.
5345 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5346 */
5347 return Py_BuildValue(
5348 "ddddd",
5349 (double)(user.dwHighDateTime*429.4967296 +
5350 user.dwLowDateTime*1e-7),
5351 (double)(kernel.dwHighDateTime*429.4967296 +
5352 kernel.dwLowDateTime*1e-7),
5353 (double)0,
5354 (double)0,
5355 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005356}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005357#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005358
5359#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005360PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005361"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005362Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005363#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005364
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005365
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005366#ifdef HAVE_GETSID
5367PyDoc_STRVAR(posix_getsid__doc__,
5368"getsid(pid) -> sid\n\n\
5369Call the system call getsid().");
5370
5371static PyObject *
5372posix_getsid(PyObject *self, PyObject *args)
5373{
Victor Stinner8c62be82010-05-06 00:08:46 +00005374 pid_t pid;
5375 int sid;
5376 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5377 return NULL;
5378 sid = getsid(pid);
5379 if (sid < 0)
5380 return posix_error();
5381 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005382}
5383#endif /* HAVE_GETSID */
5384
5385
Guido van Rossumb6775db1994-08-01 11:34:53 +00005386#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005387PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005388"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005389Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005390
Barry Warsaw53699e91996-12-10 23:23:01 +00005391static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005392posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005393{
Victor Stinner8c62be82010-05-06 00:08:46 +00005394 if (setsid() < 0)
5395 return posix_error();
5396 Py_INCREF(Py_None);
5397 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005398}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005399#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005400
Guido van Rossumb6775db1994-08-01 11:34:53 +00005401#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005402PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005403"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005404Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005405
Barry Warsaw53699e91996-12-10 23:23:01 +00005406static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005407posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005408{
Victor Stinner8c62be82010-05-06 00:08:46 +00005409 pid_t pid;
5410 int pgrp;
5411 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5412 return NULL;
5413 if (setpgid(pid, pgrp) < 0)
5414 return posix_error();
5415 Py_INCREF(Py_None);
5416 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005417}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005418#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005419
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005420
Guido van Rossumb6775db1994-08-01 11:34:53 +00005421#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005422PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005423"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005424Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005425
Barry Warsaw53699e91996-12-10 23:23:01 +00005426static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005427posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005428{
Victor Stinner8c62be82010-05-06 00:08:46 +00005429 int fd;
5430 pid_t pgid;
5431 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5432 return NULL;
5433 pgid = tcgetpgrp(fd);
5434 if (pgid < 0)
5435 return posix_error();
5436 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005437}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005438#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005439
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005440
Guido van Rossumb6775db1994-08-01 11:34:53 +00005441#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005442PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005443"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005444Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005445
Barry Warsaw53699e91996-12-10 23:23:01 +00005446static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005447posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005448{
Victor Stinner8c62be82010-05-06 00:08:46 +00005449 int fd;
5450 pid_t pgid;
5451 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5452 return NULL;
5453 if (tcsetpgrp(fd, pgid) < 0)
5454 return posix_error();
5455 Py_INCREF(Py_None);
5456 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005457}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005458#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005459
Guido van Rossum687dd131993-05-17 08:34:16 +00005460/* Functions acting on file descriptors */
5461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005462PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005463"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005464Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005465
Barry Warsaw53699e91996-12-10 23:23:01 +00005466static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005467posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005468{
Victor Stinner8c62be82010-05-06 00:08:46 +00005469 PyObject *ofile;
5470 char *file;
5471 int flag;
5472 int mode = 0777;
5473 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005474
5475#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005476 PyUnicodeObject *po;
5477 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5478 Py_BEGIN_ALLOW_THREADS
5479 /* PyUnicode_AS_UNICODE OK without thread
5480 lock as it is a simple dereference. */
5481 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5482 Py_END_ALLOW_THREADS
5483 if (fd < 0)
5484 return posix_error();
5485 return PyLong_FromLong((long)fd);
5486 }
5487 /* Drop the argument parsing error as narrow strings
5488 are also valid. */
5489 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005490#endif
5491
Victor Stinner8c62be82010-05-06 00:08:46 +00005492 if (!PyArg_ParseTuple(args, "O&i|i",
5493 PyUnicode_FSConverter, &ofile,
5494 &flag, &mode))
5495 return NULL;
5496 file = PyBytes_AsString(ofile);
5497 Py_BEGIN_ALLOW_THREADS
5498 fd = open(file, flag, mode);
5499 Py_END_ALLOW_THREADS
5500 if (fd < 0)
5501 return posix_error_with_allocated_filename(ofile);
5502 Py_DECREF(ofile);
5503 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005504}
5505
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005507PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005508"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005509Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005510
Barry Warsaw53699e91996-12-10 23:23:01 +00005511static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005512posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005513{
Victor Stinner8c62be82010-05-06 00:08:46 +00005514 int fd, res;
5515 if (!PyArg_ParseTuple(args, "i:close", &fd))
5516 return NULL;
5517 if (!_PyVerify_fd(fd))
5518 return posix_error();
5519 Py_BEGIN_ALLOW_THREADS
5520 res = close(fd);
5521 Py_END_ALLOW_THREADS
5522 if (res < 0)
5523 return posix_error();
5524 Py_INCREF(Py_None);
5525 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005526}
5527
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005528
Victor Stinner8c62be82010-05-06 00:08:46 +00005529PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005530"closerange(fd_low, fd_high)\n\n\
5531Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5532
5533static PyObject *
5534posix_closerange(PyObject *self, PyObject *args)
5535{
Victor Stinner8c62be82010-05-06 00:08:46 +00005536 int fd_from, fd_to, i;
5537 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5538 return NULL;
5539 Py_BEGIN_ALLOW_THREADS
5540 for (i = fd_from; i < fd_to; i++)
5541 if (_PyVerify_fd(i))
5542 close(i);
5543 Py_END_ALLOW_THREADS
5544 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005545}
5546
5547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005548PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005549"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005550Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005551
Barry Warsaw53699e91996-12-10 23:23:01 +00005552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005553posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005554{
Victor Stinner8c62be82010-05-06 00:08:46 +00005555 int fd;
5556 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5557 return NULL;
5558 if (!_PyVerify_fd(fd))
5559 return posix_error();
5560 Py_BEGIN_ALLOW_THREADS
5561 fd = dup(fd);
5562 Py_END_ALLOW_THREADS
5563 if (fd < 0)
5564 return posix_error();
5565 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005566}
5567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005568
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005569PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005570"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005571Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005572
Barry Warsaw53699e91996-12-10 23:23:01 +00005573static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005574posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005575{
Victor Stinner8c62be82010-05-06 00:08:46 +00005576 int fd, fd2, res;
5577 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5578 return NULL;
5579 if (!_PyVerify_fd_dup2(fd, fd2))
5580 return posix_error();
5581 Py_BEGIN_ALLOW_THREADS
5582 res = dup2(fd, fd2);
5583 Py_END_ALLOW_THREADS
5584 if (res < 0)
5585 return posix_error();
5586 Py_INCREF(Py_None);
5587 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005588}
5589
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005591PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005592"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005593Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005594
Barry Warsaw53699e91996-12-10 23:23:01 +00005595static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005596posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005597{
Victor Stinner8c62be82010-05-06 00:08:46 +00005598 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005599#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005600 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005601#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005602 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005603#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005604 PyObject *posobj;
5605 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5606 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005607#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005608 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5609 switch (how) {
5610 case 0: how = SEEK_SET; break;
5611 case 1: how = SEEK_CUR; break;
5612 case 2: how = SEEK_END; break;
5613 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005614#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005615
5616#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005617 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005618#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005619 pos = PyLong_Check(posobj) ?
5620 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005621#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005622 if (PyErr_Occurred())
5623 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005624
Victor Stinner8c62be82010-05-06 00:08:46 +00005625 if (!_PyVerify_fd(fd))
5626 return posix_error();
5627 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005628#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005629 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005630#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005631 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005632#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005633 Py_END_ALLOW_THREADS
5634 if (res < 0)
5635 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005636
5637#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005638 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005639#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005640 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005641#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005642}
5643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005645PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005646"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005647Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005648
Barry Warsaw53699e91996-12-10 23:23:01 +00005649static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005650posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005651{
Victor Stinner8c62be82010-05-06 00:08:46 +00005652 int fd, size;
5653 Py_ssize_t n;
5654 PyObject *buffer;
5655 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5656 return NULL;
5657 if (size < 0) {
5658 errno = EINVAL;
5659 return posix_error();
5660 }
5661 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5662 if (buffer == NULL)
5663 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005664 if (!_PyVerify_fd(fd)) {
5665 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005666 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005667 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005668 Py_BEGIN_ALLOW_THREADS
5669 n = read(fd, PyBytes_AS_STRING(buffer), size);
5670 Py_END_ALLOW_THREADS
5671 if (n < 0) {
5672 Py_DECREF(buffer);
5673 return posix_error();
5674 }
5675 if (n != size)
5676 _PyBytes_Resize(&buffer, n);
5677 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005678}
5679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005681PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005682"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005683Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005684
Barry Warsaw53699e91996-12-10 23:23:01 +00005685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005686posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005687{
Victor Stinner8c62be82010-05-06 00:08:46 +00005688 Py_buffer pbuf;
5689 int fd;
5690 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005691
Victor Stinner8c62be82010-05-06 00:08:46 +00005692 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5693 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005694 if (!_PyVerify_fd(fd)) {
5695 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005696 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005697 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005698 Py_BEGIN_ALLOW_THREADS
5699 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5700 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005701 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005702 if (size < 0)
5703 return posix_error();
5704 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005705}
5706
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005708PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005709"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005710Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005711
Barry Warsaw53699e91996-12-10 23:23:01 +00005712static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005713posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005714{
Victor Stinner8c62be82010-05-06 00:08:46 +00005715 int fd;
5716 STRUCT_STAT st;
5717 int res;
5718 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5719 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005720#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005721 /* on OpenVMS we must ensure that all bytes are written to the file */
5722 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005723#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005724 if (!_PyVerify_fd(fd))
5725 return posix_error();
5726 Py_BEGIN_ALLOW_THREADS
5727 res = FSTAT(fd, &st);
5728 Py_END_ALLOW_THREADS
5729 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005730#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005731 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005732#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005733 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005734#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005735 }
Tim Peters5aa91602002-01-30 05:46:57 +00005736
Victor Stinner8c62be82010-05-06 00:08:46 +00005737 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005738}
5739
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005740PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005741"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005742Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005743connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005744
5745static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005746posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005747{
Victor Stinner8c62be82010-05-06 00:08:46 +00005748 int fd;
5749 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5750 return NULL;
5751 if (!_PyVerify_fd(fd))
5752 return PyBool_FromLong(0);
5753 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005754}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005755
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005756#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005757PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005758"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005759Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005760
Barry Warsaw53699e91996-12-10 23:23:01 +00005761static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005762posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005763{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005764#if defined(PYOS_OS2)
5765 HFILE read, write;
5766 APIRET rc;
5767
Victor Stinner8c62be82010-05-06 00:08:46 +00005768 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005769 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005770 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005771 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005772 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005773
5774 return Py_BuildValue("(ii)", read, write);
5775#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005776#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005777 int fds[2];
5778 int res;
5779 Py_BEGIN_ALLOW_THREADS
5780 res = pipe(fds);
5781 Py_END_ALLOW_THREADS
5782 if (res != 0)
5783 return posix_error();
5784 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005785#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005786 HANDLE read, write;
5787 int read_fd, write_fd;
5788 BOOL ok;
5789 Py_BEGIN_ALLOW_THREADS
5790 ok = CreatePipe(&read, &write, NULL, 0);
5791 Py_END_ALLOW_THREADS
5792 if (!ok)
5793 return win32_error("CreatePipe", NULL);
5794 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5795 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5796 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005797#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005798#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005799}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005800#endif /* HAVE_PIPE */
5801
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005802
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005803#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005804PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005805"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005806Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005807
Barry Warsaw53699e91996-12-10 23:23:01 +00005808static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005809posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005810{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005811 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005812 char *filename;
5813 int mode = 0666;
5814 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005815 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5816 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005817 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005818 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005819 Py_BEGIN_ALLOW_THREADS
5820 res = mkfifo(filename, mode);
5821 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005822 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005823 if (res < 0)
5824 return posix_error();
5825 Py_INCREF(Py_None);
5826 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005827}
5828#endif
5829
5830
Neal Norwitz11690112002-07-30 01:08:28 +00005831#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005832PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005833"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005834Create a filesystem node (file, device special file or named pipe)\n\
5835named filename. mode specifies both the permissions to use and the\n\
5836type of node to be created, being combined (bitwise OR) with one of\n\
5837S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005838device defines the newly created device special file (probably using\n\
5839os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005840
5841
5842static PyObject *
5843posix_mknod(PyObject *self, PyObject *args)
5844{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005845 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005846 char *filename;
5847 int mode = 0600;
5848 int device = 0;
5849 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005850 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5851 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005852 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005853 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005854 Py_BEGIN_ALLOW_THREADS
5855 res = mknod(filename, mode, device);
5856 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005857 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005858 if (res < 0)
5859 return posix_error();
5860 Py_INCREF(Py_None);
5861 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005862}
5863#endif
5864
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005865#ifdef HAVE_DEVICE_MACROS
5866PyDoc_STRVAR(posix_major__doc__,
5867"major(device) -> major number\n\
5868Extracts a device major number from a raw device number.");
5869
5870static PyObject *
5871posix_major(PyObject *self, PyObject *args)
5872{
Victor Stinner8c62be82010-05-06 00:08:46 +00005873 int device;
5874 if (!PyArg_ParseTuple(args, "i:major", &device))
5875 return NULL;
5876 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005877}
5878
5879PyDoc_STRVAR(posix_minor__doc__,
5880"minor(device) -> minor number\n\
5881Extracts a device minor number from a raw device number.");
5882
5883static PyObject *
5884posix_minor(PyObject *self, PyObject *args)
5885{
Victor Stinner8c62be82010-05-06 00:08:46 +00005886 int device;
5887 if (!PyArg_ParseTuple(args, "i:minor", &device))
5888 return NULL;
5889 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005890}
5891
5892PyDoc_STRVAR(posix_makedev__doc__,
5893"makedev(major, minor) -> device number\n\
5894Composes a raw device number from the major and minor device numbers.");
5895
5896static PyObject *
5897posix_makedev(PyObject *self, PyObject *args)
5898{
Victor Stinner8c62be82010-05-06 00:08:46 +00005899 int major, minor;
5900 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5901 return NULL;
5902 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005903}
5904#endif /* device macros */
5905
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005906
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005907#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005908PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005909"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005910Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005911
Barry Warsaw53699e91996-12-10 23:23:01 +00005912static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005913posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005914{
Victor Stinner8c62be82010-05-06 00:08:46 +00005915 int fd;
5916 off_t length;
5917 int res;
5918 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005919
Victor Stinner8c62be82010-05-06 00:08:46 +00005920 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5921 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005922
5923#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005924 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005925#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005926 length = PyLong_Check(lenobj) ?
5927 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005928#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 if (PyErr_Occurred())
5930 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005931
Victor Stinner8c62be82010-05-06 00:08:46 +00005932 Py_BEGIN_ALLOW_THREADS
5933 res = ftruncate(fd, length);
5934 Py_END_ALLOW_THREADS
5935 if (res < 0)
5936 return posix_error();
5937 Py_INCREF(Py_None);
5938 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005939}
5940#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005941
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005942#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005943PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005944"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005945Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005946
Fred Drake762e2061999-08-26 17:23:54 +00005947/* Save putenv() parameters as values here, so we can collect them when they
5948 * get re-set with another call for the same key. */
5949static PyObject *posix_putenv_garbage;
5950
Tim Peters5aa91602002-01-30 05:46:57 +00005951static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005952posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005953{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005954#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005955 wchar_t *s1, *s2;
5956 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005957#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005958 PyObject *os1, *os2;
5959 char *s1, *s2;
5960 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005961#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005962 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005963 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005964
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005965#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005966 if (!PyArg_ParseTuple(args,
5967 "uu:putenv",
5968 &s1, &s2))
5969 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005970#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005971 if (!PyArg_ParseTuple(args,
5972 "O&O&:putenv",
5973 PyUnicode_FSConverter, &os1,
5974 PyUnicode_FSConverter, &os2))
5975 return NULL;
5976 s1 = PyBytes_AsString(os1);
5977 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005978#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005979
5980#if defined(PYOS_OS2)
5981 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5982 APIRET rc;
5983
Guido van Rossumd48f2521997-12-05 22:19:34 +00005984 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005985 if (rc != NO_ERROR) {
5986 os2_error(rc);
5987 goto error;
5988 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005989
5990 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5991 APIRET rc;
5992
Guido van Rossumd48f2521997-12-05 22:19:34 +00005993 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005994 if (rc != NO_ERROR) {
5995 os2_error(rc);
5996 goto error;
5997 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005998 } else {
5999#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006000 /* XXX This can leak memory -- not easy to fix :-( */
6001 /* len includes space for a trailing \0; the size arg to
6002 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006003#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 len = wcslen(s1) + wcslen(s2) + 2;
6005 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006006#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006007 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006008 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006009#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006010 if (newstr == NULL) {
6011 PyErr_NoMemory();
6012 goto error;
6013 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006014#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 newenv = PyUnicode_AsUnicode(newstr);
6016 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6017 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006018 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006019 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006020 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006021#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006022 newenv = PyBytes_AS_STRING(newstr);
6023 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6024 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006025 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006026 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006027 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006028#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006029
Victor Stinner8c62be82010-05-06 00:08:46 +00006030 /* Install the first arg and newstr in posix_putenv_garbage;
6031 * this will cause previous value to be collected. This has to
6032 * happen after the real putenv() call because the old value
6033 * was still accessible until then. */
6034 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006035#ifdef MS_WINDOWS
6036 PyTuple_GET_ITEM(args, 0),
6037#else
6038 os1,
6039#endif
6040 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006041 /* really not much we can do; just leak */
6042 PyErr_Clear();
6043 }
6044 else {
6045 Py_DECREF(newstr);
6046 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006047
6048#if defined(PYOS_OS2)
6049 }
6050#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006051
Martin v. Löwis011e8422009-05-05 04:43:17 +00006052#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006053 Py_DECREF(os1);
6054 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006055#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006056 Py_RETURN_NONE;
6057
6058error:
6059#ifndef MS_WINDOWS
6060 Py_DECREF(os1);
6061 Py_DECREF(os2);
6062#endif
6063 Py_XDECREF(newstr);
6064 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006065}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006066#endif /* putenv */
6067
Guido van Rossumc524d952001-10-19 01:31:59 +00006068#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006069PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006070"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006071Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006072
6073static PyObject *
6074posix_unsetenv(PyObject *self, PyObject *args)
6075{
Victor Stinner84ae1182010-05-06 22:05:07 +00006076#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006077 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006078
Victor Stinner8c62be82010-05-06 00:08:46 +00006079 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6080 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006081#else
6082 PyObject *os1;
6083 char *s1;
6084
6085 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6086 PyUnicode_FSConverter, &os1))
6087 return NULL;
6088 s1 = PyBytes_AsString(os1);
6089#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006090
Victor Stinner8c62be82010-05-06 00:08:46 +00006091 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006092
Victor Stinner8c62be82010-05-06 00:08:46 +00006093 /* Remove the key from posix_putenv_garbage;
6094 * this will cause it to be collected. This has to
6095 * happen after the real unsetenv() call because the
6096 * old value was still accessible until then.
6097 */
6098 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006099#ifdef MS_WINDOWS
6100 PyTuple_GET_ITEM(args, 0)
6101#else
6102 os1
6103#endif
6104 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006105 /* really not much we can do; just leak */
6106 PyErr_Clear();
6107 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006108
Victor Stinner84ae1182010-05-06 22:05:07 +00006109#ifndef MS_WINDOWS
6110 Py_DECREF(os1);
6111#endif
6112 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006113}
6114#endif /* unsetenv */
6115
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006116PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006117"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006118Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006119
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006120static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006121posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006122{
Victor Stinner8c62be82010-05-06 00:08:46 +00006123 int code;
6124 char *message;
6125 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6126 return NULL;
6127 message = strerror(code);
6128 if (message == NULL) {
6129 PyErr_SetString(PyExc_ValueError,
6130 "strerror() argument out of range");
6131 return NULL;
6132 }
6133 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006134}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006135
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006136
Guido van Rossumc9641791998-08-04 15:26:23 +00006137#ifdef HAVE_SYS_WAIT_H
6138
Fred Drake106c1a02002-04-23 15:58:02 +00006139#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006140PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006141"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006142Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006143
6144static PyObject *
6145posix_WCOREDUMP(PyObject *self, PyObject *args)
6146{
Victor Stinner8c62be82010-05-06 00:08:46 +00006147 WAIT_TYPE status;
6148 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006149
Victor Stinner8c62be82010-05-06 00:08:46 +00006150 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6151 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006152
Victor Stinner8c62be82010-05-06 00:08:46 +00006153 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006154}
6155#endif /* WCOREDUMP */
6156
6157#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006158PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006159"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006160Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006161job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006162
6163static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006164posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006165{
Victor Stinner8c62be82010-05-06 00:08:46 +00006166 WAIT_TYPE status;
6167 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006168
Victor Stinner8c62be82010-05-06 00:08:46 +00006169 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6170 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006171
Victor Stinner8c62be82010-05-06 00:08:46 +00006172 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006173}
6174#endif /* WIFCONTINUED */
6175
Guido van Rossumc9641791998-08-04 15:26:23 +00006176#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006177PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006178"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006179Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006180
6181static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006182posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006183{
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 WAIT_TYPE status;
6185 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006186
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6188 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006189
Victor Stinner8c62be82010-05-06 00:08:46 +00006190 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006191}
6192#endif /* WIFSTOPPED */
6193
6194#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006195PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006196"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006197Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006198
6199static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006200posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006201{
Victor Stinner8c62be82010-05-06 00:08:46 +00006202 WAIT_TYPE status;
6203 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006204
Victor Stinner8c62be82010-05-06 00:08:46 +00006205 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6206 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006207
Victor Stinner8c62be82010-05-06 00:08:46 +00006208 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006209}
6210#endif /* WIFSIGNALED */
6211
6212#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006213PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006214"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006215Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006216system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006217
6218static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006219posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006220{
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 WAIT_TYPE status;
6222 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006223
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6225 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006226
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006228}
6229#endif /* WIFEXITED */
6230
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006231#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006232PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006233"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006234Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006235
6236static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006237posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006238{
Victor Stinner8c62be82010-05-06 00:08:46 +00006239 WAIT_TYPE status;
6240 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006241
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6243 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006244
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006246}
6247#endif /* WEXITSTATUS */
6248
6249#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006250PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006251"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006252Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006253value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006254
6255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006256posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006257{
Victor Stinner8c62be82010-05-06 00:08:46 +00006258 WAIT_TYPE status;
6259 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006260
Victor Stinner8c62be82010-05-06 00:08:46 +00006261 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6262 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006263
Victor Stinner8c62be82010-05-06 00:08:46 +00006264 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006265}
6266#endif /* WTERMSIG */
6267
6268#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006269PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006270"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006271Return the signal that stopped the process that provided\n\
6272the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006273
6274static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006275posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006276{
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 WAIT_TYPE status;
6278 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006279
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6281 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006282
Victor Stinner8c62be82010-05-06 00:08:46 +00006283 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006284}
6285#endif /* WSTOPSIG */
6286
6287#endif /* HAVE_SYS_WAIT_H */
6288
6289
Thomas Wouters477c8d52006-05-27 19:21:47 +00006290#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006291#ifdef _SCO_DS
6292/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6293 needed definitions in sys/statvfs.h */
6294#define _SVID3
6295#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006296#include <sys/statvfs.h>
6297
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006298static PyObject*
6299_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6301 if (v == NULL)
6302 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006303
6304#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006305 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6306 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6307 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6308 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6309 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6310 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6311 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6312 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6313 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6314 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006315#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6317 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6318 PyStructSequence_SET_ITEM(v, 2,
6319 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6320 PyStructSequence_SET_ITEM(v, 3,
6321 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6322 PyStructSequence_SET_ITEM(v, 4,
6323 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6324 PyStructSequence_SET_ITEM(v, 5,
6325 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6326 PyStructSequence_SET_ITEM(v, 6,
6327 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6328 PyStructSequence_SET_ITEM(v, 7,
6329 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6330 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6331 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006332#endif
6333
Victor Stinner8c62be82010-05-06 00:08:46 +00006334 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006335}
6336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006337PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006338"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006339Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006340
6341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006342posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006343{
Victor Stinner8c62be82010-05-06 00:08:46 +00006344 int fd, res;
6345 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006346
Victor Stinner8c62be82010-05-06 00:08:46 +00006347 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6348 return NULL;
6349 Py_BEGIN_ALLOW_THREADS
6350 res = fstatvfs(fd, &st);
6351 Py_END_ALLOW_THREADS
6352 if (res != 0)
6353 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006354
Victor Stinner8c62be82010-05-06 00:08:46 +00006355 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006356}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006357#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006358
6359
Thomas Wouters477c8d52006-05-27 19:21:47 +00006360#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006361#include <sys/statvfs.h>
6362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006363PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006364"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006365Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006366
6367static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006368posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006369{
Victor Stinner8c62be82010-05-06 00:08:46 +00006370 char *path;
6371 int res;
6372 struct statvfs st;
6373 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6374 return NULL;
6375 Py_BEGIN_ALLOW_THREADS
6376 res = statvfs(path, &st);
6377 Py_END_ALLOW_THREADS
6378 if (res != 0)
6379 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006380
Victor Stinner8c62be82010-05-06 00:08:46 +00006381 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006382}
6383#endif /* HAVE_STATVFS */
6384
Fred Drakec9680921999-12-13 16:37:25 +00006385/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6386 * It maps strings representing configuration variable names to
6387 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006388 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006389 * rarely-used constants. There are three separate tables that use
6390 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006391 *
6392 * This code is always included, even if none of the interfaces that
6393 * need it are included. The #if hackery needed to avoid it would be
6394 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006395 */
6396struct constdef {
6397 char *name;
6398 long value;
6399};
6400
Fred Drake12c6e2d1999-12-14 21:25:03 +00006401static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006402conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006403 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006404{
Christian Heimes217cfd12007-12-02 14:31:20 +00006405 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006406 *valuep = PyLong_AS_LONG(arg);
6407 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006408 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006409 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006410 /* look up the value in the table using a binary search */
6411 size_t lo = 0;
6412 size_t mid;
6413 size_t hi = tablesize;
6414 int cmp;
6415 const char *confname;
6416 if (!PyUnicode_Check(arg)) {
6417 PyErr_SetString(PyExc_TypeError,
6418 "configuration names must be strings or integers");
6419 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006420 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006421 confname = _PyUnicode_AsString(arg);
6422 if (confname == NULL)
6423 return 0;
6424 while (lo < hi) {
6425 mid = (lo + hi) / 2;
6426 cmp = strcmp(confname, table[mid].name);
6427 if (cmp < 0)
6428 hi = mid;
6429 else if (cmp > 0)
6430 lo = mid + 1;
6431 else {
6432 *valuep = table[mid].value;
6433 return 1;
6434 }
6435 }
6436 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6437 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006438 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006439}
6440
6441
6442#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6443static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006444#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006445 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006446#endif
6447#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006448 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006449#endif
Fred Drakec9680921999-12-13 16:37:25 +00006450#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006451 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006452#endif
6453#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006454 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006455#endif
6456#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006457 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006458#endif
6459#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006460 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006461#endif
6462#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006463 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006464#endif
6465#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006466 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006467#endif
6468#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006469 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006470#endif
6471#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006473#endif
6474#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006475 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006476#endif
6477#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006479#endif
6480#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006482#endif
6483#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006484 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006485#endif
6486#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006488#endif
6489#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006490 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006491#endif
6492#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006493 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006494#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006495#ifdef _PC_ACL_ENABLED
6496 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6497#endif
6498#ifdef _PC_MIN_HOLE_SIZE
6499 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6500#endif
6501#ifdef _PC_ALLOC_SIZE_MIN
6502 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6503#endif
6504#ifdef _PC_REC_INCR_XFER_SIZE
6505 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6506#endif
6507#ifdef _PC_REC_MAX_XFER_SIZE
6508 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6509#endif
6510#ifdef _PC_REC_MIN_XFER_SIZE
6511 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6512#endif
6513#ifdef _PC_REC_XFER_ALIGN
6514 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6515#endif
6516#ifdef _PC_SYMLINK_MAX
6517 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6518#endif
6519#ifdef _PC_XATTR_ENABLED
6520 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6521#endif
6522#ifdef _PC_XATTR_EXISTS
6523 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6524#endif
6525#ifdef _PC_TIMESTAMP_RESOLUTION
6526 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6527#endif
Fred Drakec9680921999-12-13 16:37:25 +00006528};
6529
Fred Drakec9680921999-12-13 16:37:25 +00006530static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006531conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006532{
6533 return conv_confname(arg, valuep, posix_constants_pathconf,
6534 sizeof(posix_constants_pathconf)
6535 / sizeof(struct constdef));
6536}
6537#endif
6538
6539#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006540PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006541"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006542Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006543If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006544
6545static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006546posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006547{
6548 PyObject *result = NULL;
6549 int name, fd;
6550
Fred Drake12c6e2d1999-12-14 21:25:03 +00006551 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6552 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006553 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006554
Stefan Krah0e803b32010-11-26 16:16:47 +00006555 errno = 0;
6556 limit = fpathconf(fd, name);
6557 if (limit == -1 && errno != 0)
6558 posix_error();
6559 else
6560 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006561 }
6562 return result;
6563}
6564#endif
6565
6566
6567#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006568PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006569"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006570Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006571If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006572
6573static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006574posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006575{
6576 PyObject *result = NULL;
6577 int name;
6578 char *path;
6579
6580 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6581 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006583
Victor Stinner8c62be82010-05-06 00:08:46 +00006584 errno = 0;
6585 limit = pathconf(path, name);
6586 if (limit == -1 && errno != 0) {
6587 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006588 /* could be a path or name problem */
6589 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006590 else
Stefan Krah99439262010-11-26 12:58:05 +00006591 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 }
6593 else
6594 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006595 }
6596 return result;
6597}
6598#endif
6599
6600#ifdef HAVE_CONFSTR
6601static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006602#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006603 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006604#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006605#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006606 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006607#endif
6608#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006609 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006610#endif
Fred Draked86ed291999-12-15 15:34:33 +00006611#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006612 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006613#endif
6614#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006615 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006616#endif
6617#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006618 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006619#endif
6620#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006621 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006622#endif
Fred Drakec9680921999-12-13 16:37:25 +00006623#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006624 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006625#endif
6626#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006627 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006628#endif
6629#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006630 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006631#endif
6632#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006633 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006634#endif
6635#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006637#endif
6638#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006639 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006640#endif
6641#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006642 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006643#endif
6644#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006646#endif
Fred Draked86ed291999-12-15 15:34:33 +00006647#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006648 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006649#endif
Fred Drakec9680921999-12-13 16:37:25 +00006650#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006651 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006652#endif
Fred Draked86ed291999-12-15 15:34:33 +00006653#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006654 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006655#endif
6656#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006657 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006658#endif
6659#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006660 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006661#endif
6662#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006663 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006664#endif
Fred Drakec9680921999-12-13 16:37:25 +00006665#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006666 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006667#endif
6668#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006669 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006670#endif
6671#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006672 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006673#endif
6674#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006675 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006676#endif
6677#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006678 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006679#endif
6680#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006681 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006682#endif
6683#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006684 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006685#endif
6686#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006687 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006688#endif
6689#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006690 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006691#endif
6692#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006693 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006694#endif
6695#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006696 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006697#endif
6698#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006700#endif
6701#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006702 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006703#endif
6704#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006705 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006706#endif
6707#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006709#endif
6710#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006711 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006712#endif
Fred Draked86ed291999-12-15 15:34:33 +00006713#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006714 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006715#endif
6716#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006718#endif
6719#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006721#endif
6722#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006724#endif
6725#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006727#endif
6728#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006729 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006730#endif
6731#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006732 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006733#endif
6734#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006735 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006736#endif
6737#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006738 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006739#endif
6740#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006741 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006742#endif
6743#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006744 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006745#endif
6746#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006748#endif
6749#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006750 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006751#endif
Fred Drakec9680921999-12-13 16:37:25 +00006752};
6753
6754static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006755conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006756{
6757 return conv_confname(arg, valuep, posix_constants_confstr,
6758 sizeof(posix_constants_confstr)
6759 / sizeof(struct constdef));
6760}
6761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006762PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006763"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006764Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006765
6766static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006767posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006768{
6769 PyObject *result = NULL;
6770 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006771 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006772 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006773
Victor Stinnercb043522010-09-10 23:49:04 +00006774 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6775 return NULL;
6776
6777 errno = 0;
6778 len = confstr(name, buffer, sizeof(buffer));
6779 if (len == 0) {
6780 if (errno) {
6781 posix_error();
6782 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006783 }
6784 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006785 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006786 }
6787 }
Victor Stinnercb043522010-09-10 23:49:04 +00006788
6789 if ((unsigned int)len >= sizeof(buffer)) {
6790 char *buf = PyMem_Malloc(len);
6791 if (buf == NULL)
6792 return PyErr_NoMemory();
6793 confstr(name, buf, len);
6794 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6795 PyMem_Free(buf);
6796 }
6797 else
6798 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006799 return result;
6800}
6801#endif
6802
6803
6804#ifdef HAVE_SYSCONF
6805static struct constdef posix_constants_sysconf[] = {
6806#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006807 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006808#endif
6809#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006810 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006811#endif
6812#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006813 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006814#endif
6815#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006816 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006817#endif
6818#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006820#endif
6821#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006823#endif
6824#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006825 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006826#endif
6827#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006829#endif
6830#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006831 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006832#endif
6833#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006834 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006835#endif
Fred Draked86ed291999-12-15 15:34:33 +00006836#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006838#endif
6839#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006840 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006841#endif
Fred Drakec9680921999-12-13 16:37:25 +00006842#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006844#endif
Fred Drakec9680921999-12-13 16:37:25 +00006845#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006847#endif
6848#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006849 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006850#endif
6851#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006852 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006853#endif
6854#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006856#endif
6857#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006858 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006859#endif
Fred Draked86ed291999-12-15 15:34:33 +00006860#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006861 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006862#endif
Fred Drakec9680921999-12-13 16:37:25 +00006863#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006865#endif
6866#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006868#endif
6869#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006871#endif
6872#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006873 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006874#endif
6875#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006876 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006877#endif
Fred Draked86ed291999-12-15 15:34:33 +00006878#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006880#endif
Fred Drakec9680921999-12-13 16:37:25 +00006881#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006882 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006883#endif
6884#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006885 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006886#endif
6887#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006888 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006889#endif
6890#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006891 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006892#endif
6893#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006894 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006895#endif
6896#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006897 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006898#endif
6899#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006900 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006901#endif
6902#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006903 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006904#endif
6905#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006906 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006907#endif
6908#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006909 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006910#endif
6911#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006913#endif
6914#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006916#endif
6917#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006919#endif
6920#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006922#endif
6923#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006924 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006925#endif
6926#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006928#endif
6929#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006930 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006931#endif
6932#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006934#endif
6935#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006936 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006937#endif
6938#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006939 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006940#endif
6941#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006942 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006943#endif
6944#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006945 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006946#endif
6947#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006948 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006949#endif
Fred Draked86ed291999-12-15 15:34:33 +00006950#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006951 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006952#endif
Fred Drakec9680921999-12-13 16:37:25 +00006953#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006954 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006955#endif
6956#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006958#endif
6959#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006961#endif
Fred Draked86ed291999-12-15 15:34:33 +00006962#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006963 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006964#endif
Fred Drakec9680921999-12-13 16:37:25 +00006965#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006967#endif
Fred Draked86ed291999-12-15 15:34:33 +00006968#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006969 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006970#endif
6971#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006972 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006973#endif
Fred Drakec9680921999-12-13 16:37:25 +00006974#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006975 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006976#endif
6977#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006978 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006979#endif
6980#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006981 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006982#endif
6983#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006985#endif
Fred Draked86ed291999-12-15 15:34:33 +00006986#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006988#endif
Fred Drakec9680921999-12-13 16:37:25 +00006989#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006991#endif
6992#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006993 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006994#endif
6995#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006996 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006997#endif
6998#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006999 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007000#endif
7001#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007002 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007003#endif
7004#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007006#endif
7007#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007009#endif
Fred Draked86ed291999-12-15 15:34:33 +00007010#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007011 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007012#endif
Fred Drakec9680921999-12-13 16:37:25 +00007013#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007014 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007015#endif
7016#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007017 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007018#endif
Fred Draked86ed291999-12-15 15:34:33 +00007019#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007020 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007021#endif
Fred Drakec9680921999-12-13 16:37:25 +00007022#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007024#endif
7025#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007026 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007027#endif
7028#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007030#endif
7031#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007033#endif
7034#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007035 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007036#endif
7037#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007038 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007039#endif
7040#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007042#endif
7043#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007044 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007045#endif
7046#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007047 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007048#endif
Fred Draked86ed291999-12-15 15:34:33 +00007049#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007050 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007051#endif
7052#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007053 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007054#endif
Fred Drakec9680921999-12-13 16:37:25 +00007055#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007057#endif
7058#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007060#endif
7061#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007063#endif
7064#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007065 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007066#endif
7067#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007069#endif
7070#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007071 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007072#endif
7073#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007075#endif
7076#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007078#endif
7079#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007081#endif
7082#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007084#endif
7085#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007087#endif
7088#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007090#endif
7091#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007093#endif
7094#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007095 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007096#endif
7097#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007099#endif
7100#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007102#endif
7103#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007104 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007105#endif
7106#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007107 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007108#endif
7109#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007111#endif
7112#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007114#endif
7115#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007116 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007117#endif
7118#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007119 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007120#endif
7121#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007123#endif
7124#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007126#endif
7127#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007128 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007129#endif
7130#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007131 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007132#endif
7133#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007134 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007135#endif
7136#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007137 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007138#endif
7139#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007140 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007141#endif
7142#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007143 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007144#endif
7145#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007147#endif
7148#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007149 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007150#endif
7151#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007153#endif
7154#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007155 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007156#endif
7157#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007158 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007159#endif
Fred Draked86ed291999-12-15 15:34:33 +00007160#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007161 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007162#endif
Fred Drakec9680921999-12-13 16:37:25 +00007163#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007164 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007165#endif
7166#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007168#endif
7169#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007171#endif
7172#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007173 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007174#endif
7175#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007176 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007177#endif
7178#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007180#endif
7181#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007182 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007183#endif
7184#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007186#endif
7187#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007188 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007189#endif
7190#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007191 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007192#endif
7193#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007194 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007195#endif
7196#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007197 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007198#endif
7199#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007201#endif
7202#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007204#endif
7205#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007206 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007207#endif
7208#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007209 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007210#endif
7211#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007213#endif
7214#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007215 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007216#endif
7217#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007218 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007219#endif
7220#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007221 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007222#endif
7223#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007225#endif
7226#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007227 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007228#endif
7229#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007230 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007231#endif
7232#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007233 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007234#endif
7235#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007236 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007237#endif
7238#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007240#endif
7241#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007242 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007243#endif
7244#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007246#endif
7247#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007249#endif
7250#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007252#endif
7253#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007255#endif
7256#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007258#endif
7259#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007261#endif
7262#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007263 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007264#endif
7265#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007267#endif
7268#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007269 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007270#endif
7271#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007272 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007273#endif
7274#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007276#endif
7277#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007278 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007279#endif
7280#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007281 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007282#endif
7283#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007284 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007285#endif
7286#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007287 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007288#endif
7289#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007290 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007291#endif
7292#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007293 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007294#endif
7295#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007296 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007297#endif
7298};
7299
7300static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007301conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007302{
7303 return conv_confname(arg, valuep, posix_constants_sysconf,
7304 sizeof(posix_constants_sysconf)
7305 / sizeof(struct constdef));
7306}
7307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007308PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007309"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007310Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007311
7312static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007313posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007314{
7315 PyObject *result = NULL;
7316 int name;
7317
7318 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7319 int value;
7320
7321 errno = 0;
7322 value = sysconf(name);
7323 if (value == -1 && errno != 0)
7324 posix_error();
7325 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007326 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007327 }
7328 return result;
7329}
7330#endif
7331
7332
Fred Drakebec628d1999-12-15 18:31:10 +00007333/* This code is used to ensure that the tables of configuration value names
7334 * are in sorted order as required by conv_confname(), and also to build the
7335 * the exported dictionaries that are used to publish information about the
7336 * names available on the host platform.
7337 *
7338 * Sorting the table at runtime ensures that the table is properly ordered
7339 * when used, even for platforms we're not able to test on. It also makes
7340 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007341 */
Fred Drakebec628d1999-12-15 18:31:10 +00007342
7343static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007344cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007345{
7346 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007348 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007349 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007350
7351 return strcmp(c1->name, c2->name);
7352}
7353
7354static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007355setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007357{
Fred Drakebec628d1999-12-15 18:31:10 +00007358 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007359 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007360
7361 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7362 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007363 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007364 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007365
Barry Warsaw3155db32000-04-13 15:20:40 +00007366 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007367 PyObject *o = PyLong_FromLong(table[i].value);
7368 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7369 Py_XDECREF(o);
7370 Py_DECREF(d);
7371 return -1;
7372 }
7373 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007374 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007375 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007376}
7377
Fred Drakebec628d1999-12-15 18:31:10 +00007378/* Return -1 on failure, 0 on success. */
7379static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007380setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007381{
7382#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007383 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007384 sizeof(posix_constants_pathconf)
7385 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007386 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007387 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007388#endif
7389#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007390 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007391 sizeof(posix_constants_confstr)
7392 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007393 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007394 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007395#endif
7396#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007397 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007398 sizeof(posix_constants_sysconf)
7399 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007400 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007401 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007402#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007403 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007404}
Fred Draked86ed291999-12-15 15:34:33 +00007405
7406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007407PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007408"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007409Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007410in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007411
7412static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007413posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007414{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007415 abort();
7416 /*NOTREACHED*/
7417 Py_FatalError("abort() called from Python code didn't abort!");
7418 return NULL;
7419}
Fred Drakebec628d1999-12-15 18:31:10 +00007420
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007421#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007422PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007423"startfile(filepath [, operation]) - Start a file with its associated\n\
7424application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007425\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007426When \"operation\" is not specified or \"open\", this acts like\n\
7427double-clicking the file in Explorer, or giving the file name as an\n\
7428argument to the DOS \"start\" command: the file is opened with whatever\n\
7429application (if any) its extension is associated.\n\
7430When another \"operation\" is given, it specifies what should be done with\n\
7431the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007432\n\
7433startfile returns as soon as the associated application is launched.\n\
7434There is no option to wait for the application to close, and no way\n\
7435to retrieve the application's exit status.\n\
7436\n\
7437The filepath is relative to the current directory. If you want to use\n\
7438an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007439the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007440
7441static PyObject *
7442win32_startfile(PyObject *self, PyObject *args)
7443{
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 PyObject *ofilepath;
7445 char *filepath;
7446 char *operation = NULL;
7447 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007448
Victor Stinner8c62be82010-05-06 00:08:46 +00007449 PyObject *unipath, *woperation = NULL;
7450 if (!PyArg_ParseTuple(args, "U|s:startfile",
7451 &unipath, &operation)) {
7452 PyErr_Clear();
7453 goto normal;
7454 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007455
Victor Stinner8c62be82010-05-06 00:08:46 +00007456 if (operation) {
7457 woperation = PyUnicode_DecodeASCII(operation,
7458 strlen(operation), NULL);
7459 if (!woperation) {
7460 PyErr_Clear();
7461 operation = NULL;
7462 goto normal;
7463 }
7464 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007465
Victor Stinner8c62be82010-05-06 00:08:46 +00007466 Py_BEGIN_ALLOW_THREADS
7467 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7468 PyUnicode_AS_UNICODE(unipath),
7469 NULL, NULL, SW_SHOWNORMAL);
7470 Py_END_ALLOW_THREADS
7471
7472 Py_XDECREF(woperation);
7473 if (rc <= (HINSTANCE)32) {
7474 PyObject *errval = win32_error_unicode("startfile",
7475 PyUnicode_AS_UNICODE(unipath));
7476 return errval;
7477 }
7478 Py_INCREF(Py_None);
7479 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007480
7481normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007482 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7483 PyUnicode_FSConverter, &ofilepath,
7484 &operation))
7485 return NULL;
7486 filepath = PyBytes_AsString(ofilepath);
7487 Py_BEGIN_ALLOW_THREADS
7488 rc = ShellExecute((HWND)0, operation, filepath,
7489 NULL, NULL, SW_SHOWNORMAL);
7490 Py_END_ALLOW_THREADS
7491 if (rc <= (HINSTANCE)32) {
7492 PyObject *errval = win32_error("startfile", filepath);
7493 Py_DECREF(ofilepath);
7494 return errval;
7495 }
7496 Py_DECREF(ofilepath);
7497 Py_INCREF(Py_None);
7498 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007499}
7500#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007501
Martin v. Löwis438b5342002-12-27 10:16:42 +00007502#ifdef HAVE_GETLOADAVG
7503PyDoc_STRVAR(posix_getloadavg__doc__,
7504"getloadavg() -> (float, float, float)\n\n\
7505Return the number of processes in the system run queue averaged over\n\
7506the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7507was unobtainable");
7508
7509static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007510posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007511{
7512 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007513 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007514 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7515 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007516 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007517 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007518}
7519#endif
7520
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007521#ifdef MS_WINDOWS
7522
7523PyDoc_STRVAR(win32_urandom__doc__,
7524"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007525Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007526
7527typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7528 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7529 DWORD dwFlags );
7530typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7531 BYTE *pbBuffer );
7532
7533static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007534/* This handle is never explicitly released. Instead, the operating
7535 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007536static HCRYPTPROV hCryptProv = 0;
7537
Tim Peters4ad82172004-08-30 17:02:04 +00007538static PyObject*
7539win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007540{
Victor Stinner8c62be82010-05-06 00:08:46 +00007541 int howMany;
7542 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007543
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 /* Read arguments */
7545 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7546 return NULL;
7547 if (howMany < 0)
7548 return PyErr_Format(PyExc_ValueError,
7549 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007550
Victor Stinner8c62be82010-05-06 00:08:46 +00007551 if (hCryptProv == 0) {
7552 HINSTANCE hAdvAPI32 = NULL;
7553 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007554
Victor Stinner8c62be82010-05-06 00:08:46 +00007555 /* Obtain handle to the DLL containing CryptoAPI
7556 This should not fail */
7557 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7558 if(hAdvAPI32 == NULL)
7559 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007560
Victor Stinner8c62be82010-05-06 00:08:46 +00007561 /* Obtain pointers to the CryptoAPI functions
7562 This will fail on some early versions of Win95 */
7563 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7564 hAdvAPI32,
7565 "CryptAcquireContextA");
7566 if (pCryptAcquireContext == NULL)
7567 return PyErr_Format(PyExc_NotImplementedError,
7568 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007569
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7571 hAdvAPI32, "CryptGenRandom");
7572 if (pCryptGenRandom == NULL)
7573 return PyErr_Format(PyExc_NotImplementedError,
7574 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007575
Victor Stinner8c62be82010-05-06 00:08:46 +00007576 /* Acquire context */
7577 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7578 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7579 return win32_error("CryptAcquireContext", NULL);
7580 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007581
Victor Stinner8c62be82010-05-06 00:08:46 +00007582 /* Allocate bytes */
7583 result = PyBytes_FromStringAndSize(NULL, howMany);
7584 if (result != NULL) {
7585 /* Get random data */
7586 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7587 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7588 PyBytes_AS_STRING(result))) {
7589 Py_DECREF(result);
7590 return win32_error("CryptGenRandom", NULL);
7591 }
7592 }
7593 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007594}
7595#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007596
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007597PyDoc_STRVAR(device_encoding__doc__,
7598"device_encoding(fd) -> str\n\n\
7599Return a string describing the encoding of the device\n\
7600if the output is a terminal; else return None.");
7601
7602static PyObject *
7603device_encoding(PyObject *self, PyObject *args)
7604{
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 int fd;
7606 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7607 return NULL;
7608 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7609 Py_INCREF(Py_None);
7610 return Py_None;
7611 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007612#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 if (fd == 0) {
7614 char buf[100];
7615 sprintf(buf, "cp%d", GetConsoleCP());
7616 return PyUnicode_FromString(buf);
7617 }
7618 if (fd == 1 || fd == 2) {
7619 char buf[100];
7620 sprintf(buf, "cp%d", GetConsoleOutputCP());
7621 return PyUnicode_FromString(buf);
7622 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007623#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007624 {
7625 char *codeset = nl_langinfo(CODESET);
7626 if (codeset != NULL && codeset[0] != 0)
7627 return PyUnicode_FromString(codeset);
7628 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007629#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 Py_INCREF(Py_None);
7631 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007632}
7633
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007634#ifdef __VMS
7635/* Use openssl random routine */
7636#include <openssl/rand.h>
7637PyDoc_STRVAR(vms_urandom__doc__,
7638"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007639Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007640
7641static PyObject*
7642vms_urandom(PyObject *self, PyObject *args)
7643{
Victor Stinner8c62be82010-05-06 00:08:46 +00007644 int howMany;
7645 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007646
Victor Stinner8c62be82010-05-06 00:08:46 +00007647 /* Read arguments */
7648 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7649 return NULL;
7650 if (howMany < 0)
7651 return PyErr_Format(PyExc_ValueError,
7652 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007653
Victor Stinner8c62be82010-05-06 00:08:46 +00007654 /* Allocate bytes */
7655 result = PyBytes_FromStringAndSize(NULL, howMany);
7656 if (result != NULL) {
7657 /* Get random data */
7658 if (RAND_pseudo_bytes((unsigned char*)
7659 PyBytes_AS_STRING(result),
7660 howMany) < 0) {
7661 Py_DECREF(result);
7662 return PyErr_Format(PyExc_ValueError,
7663 "RAND_pseudo_bytes");
7664 }
7665 }
7666 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007667}
7668#endif
7669
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007670#ifdef HAVE_SETRESUID
7671PyDoc_STRVAR(posix_setresuid__doc__,
7672"setresuid(ruid, euid, suid)\n\n\
7673Set the current process's real, effective, and saved user ids.");
7674
7675static PyObject*
7676posix_setresuid (PyObject *self, PyObject *args)
7677{
Victor Stinner8c62be82010-05-06 00:08:46 +00007678 /* We assume uid_t is no larger than a long. */
7679 long ruid, euid, suid;
7680 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7681 return NULL;
7682 if (setresuid(ruid, euid, suid) < 0)
7683 return posix_error();
7684 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007685}
7686#endif
7687
7688#ifdef HAVE_SETRESGID
7689PyDoc_STRVAR(posix_setresgid__doc__,
7690"setresgid(rgid, egid, sgid)\n\n\
7691Set the current process's real, effective, and saved group ids.");
7692
7693static PyObject*
7694posix_setresgid (PyObject *self, PyObject *args)
7695{
Victor Stinner8c62be82010-05-06 00:08:46 +00007696 /* We assume uid_t is no larger than a long. */
7697 long rgid, egid, sgid;
7698 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7699 return NULL;
7700 if (setresgid(rgid, egid, sgid) < 0)
7701 return posix_error();
7702 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007703}
7704#endif
7705
7706#ifdef HAVE_GETRESUID
7707PyDoc_STRVAR(posix_getresuid__doc__,
7708"getresuid() -> (ruid, euid, suid)\n\n\
7709Get tuple of the current process's real, effective, and saved user ids.");
7710
7711static PyObject*
7712posix_getresuid (PyObject *self, PyObject *noargs)
7713{
Victor Stinner8c62be82010-05-06 00:08:46 +00007714 uid_t ruid, euid, suid;
7715 long l_ruid, l_euid, l_suid;
7716 if (getresuid(&ruid, &euid, &suid) < 0)
7717 return posix_error();
7718 /* Force the values into long's as we don't know the size of uid_t. */
7719 l_ruid = ruid;
7720 l_euid = euid;
7721 l_suid = suid;
7722 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007723}
7724#endif
7725
7726#ifdef HAVE_GETRESGID
7727PyDoc_STRVAR(posix_getresgid__doc__,
7728"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007729Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007730
7731static PyObject*
7732posix_getresgid (PyObject *self, PyObject *noargs)
7733{
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 uid_t rgid, egid, sgid;
7735 long l_rgid, l_egid, l_sgid;
7736 if (getresgid(&rgid, &egid, &sgid) < 0)
7737 return posix_error();
7738 /* Force the values into long's as we don't know the size of uid_t. */
7739 l_rgid = rgid;
7740 l_egid = egid;
7741 l_sgid = sgid;
7742 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007743}
7744#endif
7745
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007746static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007747 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007748#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007750#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007752#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007753 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007754#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007755 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007756#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007758#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007759#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007761#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007762#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007764#endif /* HAVE_LCHMOD */
7765#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007767#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007768#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007770#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007771#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007773#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007774#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007775 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007776#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007777#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007779#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007780#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7782 METH_NOARGS, posix_getcwd__doc__},
7783 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7784 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007785#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007786#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007788#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007789 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7790 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7791 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007792#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007794#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007795#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007796 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007797#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007798#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007799 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007800#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007801 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7802 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7803 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007805#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007807#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007808#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007809 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007810 win_symlink__doc__},
7811#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007812#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007813 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007814#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007816#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007818#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007819 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7820 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7821 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007822#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007824#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007825 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007826#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7828 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007829#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007830#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7832 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007833#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7835 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007836#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007837#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007838#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007840#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007841#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007843#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007844#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007846#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007847#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007849#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007850#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007852#endif /* HAVE_GETEGID */
7853#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007855#endif /* HAVE_GETEUID */
7856#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007858#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007859#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007860 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007861#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007863#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007865#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007866#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007868#endif /* HAVE_GETPPID */
7869#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007871#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007872#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007874#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007875#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007877#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007878#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007880#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007881#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007882 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007883#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007884#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007885 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7886 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007887 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007888#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007889#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007890 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007891#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007892#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007893 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007894#endif /* HAVE_SETEUID */
7895#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007896 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007897#endif /* HAVE_SETEGID */
7898#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007900#endif /* HAVE_SETREUID */
7901#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007903#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007904#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007905 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007906#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007907#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007908 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007909#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007910#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007911 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007912#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007913#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007914 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007915#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007916#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007917 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007918#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007919#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007920 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007921#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007922#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007923 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007924#endif /* HAVE_WAIT3 */
7925#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007926 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007927#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007928#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007929 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007930#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007931#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007932 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007933#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007934#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007935 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007936#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007937#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007938 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007939#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007940#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007942#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007943#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007944 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007945#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007946 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7947 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7948 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7949 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7950 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7951 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7952 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7953 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7954 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7955 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7956 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007957#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007959#endif
7960#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007962#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007963#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007964 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007965#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007966#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007967 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7968 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7969 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007970#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007971#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007973#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007974#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007976#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007977#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007979#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007981#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007983#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007984#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007985 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007986#endif
7987#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007988 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007989#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007990#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007991#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007992 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007993#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007994#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007996#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007997#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007999#endif /* WIFSTOPPED */
8000#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008002#endif /* WIFSIGNALED */
8003#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008005#endif /* WIFEXITED */
8006#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008008#endif /* WEXITSTATUS */
8009#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008011#endif /* WTERMSIG */
8012#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008014#endif /* WSTOPSIG */
8015#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008016#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008018#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008019#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008021#endif
Fred Drakec9680921999-12-13 16:37:25 +00008022#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008023 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008024#endif
8025#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008027#endif
8028#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008030#endif
8031#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008033#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008034 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008035#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008037 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008038 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008039#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008040#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008041 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008042#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008043 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008044 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008045 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008046 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008047 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008048 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008049#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008051#endif
8052#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008054#endif
8055#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008057#endif
8058#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008060#endif
8061
Victor Stinner8c62be82010-05-06 00:08:46 +00008062 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008063};
8064
8065
Barry Warsaw4a342091996-12-19 23:50:02 +00008066static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008067ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008068{
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008070}
8071
Guido van Rossumd48f2521997-12-05 22:19:34 +00008072#if defined(PYOS_OS2)
8073/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008074static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008075{
8076 APIRET rc;
8077 ULONG values[QSV_MAX+1];
8078 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008079 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008080
8081 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008082 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008083 Py_END_ALLOW_THREADS
8084
8085 if (rc != NO_ERROR) {
8086 os2_error(rc);
8087 return -1;
8088 }
8089
Fred Drake4d1e64b2002-04-15 19:40:07 +00008090 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8091 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8092 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8093 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8094 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8095 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8096 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008097
8098 switch (values[QSV_VERSION_MINOR]) {
8099 case 0: ver = "2.00"; break;
8100 case 10: ver = "2.10"; break;
8101 case 11: ver = "2.11"; break;
8102 case 30: ver = "3.00"; break;
8103 case 40: ver = "4.00"; break;
8104 case 50: ver = "5.00"; break;
8105 default:
Tim Peters885d4572001-11-28 20:27:42 +00008106 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008107 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008108 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008109 ver = &tmp[0];
8110 }
8111
8112 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008113 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008114 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008115
8116 /* Add Indicator of Which Drive was Used to Boot the System */
8117 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8118 tmp[1] = ':';
8119 tmp[2] = '\0';
8120
Fred Drake4d1e64b2002-04-15 19:40:07 +00008121 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008122}
8123#endif
8124
Brian Curtin52173d42010-12-02 18:29:18 +00008125#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008126static int
Brian Curtin52173d42010-12-02 18:29:18 +00008127enable_symlink()
8128{
8129 HANDLE tok;
8130 TOKEN_PRIVILEGES tok_priv;
8131 LUID luid;
8132 int meth_idx = 0;
8133
8134 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008135 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008136
8137 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008138 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008139
8140 tok_priv.PrivilegeCount = 1;
8141 tok_priv.Privileges[0].Luid = luid;
8142 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8143
8144 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8145 sizeof(TOKEN_PRIVILEGES),
8146 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008147 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008148
Brian Curtin3b4499c2010-12-28 14:31:47 +00008149 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8150 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008151}
8152#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8153
Barry Warsaw4a342091996-12-19 23:50:02 +00008154static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008155all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008156{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008157#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008158 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008159#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008160#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008161 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008162#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008163#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008164 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008165#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008166#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008167 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008168#endif
Fred Drakec9680921999-12-13 16:37:25 +00008169#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008170 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008171#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008172#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008173 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008174#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008175#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008176 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008177#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008178#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008179 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008180#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008181#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008182 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008183#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008184#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008185 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008186#endif
8187#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008188 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008189#endif
8190#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008191 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008192#endif
8193#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008194 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008195#endif
8196#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008197 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008198#endif
8199#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008200 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008201#endif
8202#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008203 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008204#endif
8205#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008206 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008207#endif
8208#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008209 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008210#endif
8211#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008212 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008213#endif
8214#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008215 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008216#endif
8217#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008218 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008219#endif
8220#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008221 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008222#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008223#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008224 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008225#endif
8226#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008227 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008228#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008229#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008230 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008231#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008232#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008233 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008234#endif
8235#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008236 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008237#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008238
Tim Peters5aa91602002-01-30 05:46:57 +00008239/* MS Windows */
8240#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 /* Don't inherit in child processes. */
8242 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008243#endif
8244#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008245 /* Optimize for short life (keep in memory). */
8246 /* MS forgot to define this one with a non-underscore form too. */
8247 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008248#endif
8249#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 /* Automatically delete when last handle is closed. */
8251 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008252#endif
8253#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 /* Optimize for random access. */
8255 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008256#endif
8257#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008258 /* Optimize for sequential access. */
8259 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008260#endif
8261
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008262/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008263#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 /* Send a SIGIO signal whenever input or output
8265 becomes available on file descriptor */
8266 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008267#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008268#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 /* Direct disk access. */
8270 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008271#endif
8272#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 /* Must be a directory. */
8274 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008275#endif
8276#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 /* Do not follow links. */
8278 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008279#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008280#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008281 /* Do not update the access time. */
8282 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008283#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008284
Victor Stinner8c62be82010-05-06 00:08:46 +00008285 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008286#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008288#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008289#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008291#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008292#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008294#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008295#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008297#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008298#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008299 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008300#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008301#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008302 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008303#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008304#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008306#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008307#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008308 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008309#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008310#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008312#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008313#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008315#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008316#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008317 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008318#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008319#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008320 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008321#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008322#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008323 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008324#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008325#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008327#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008328#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008329 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008330#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008331#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008332 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008333#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008334#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008336#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008337
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008338 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008339#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008340 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008341#endif /* ST_RDONLY */
8342#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008343 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008344#endif /* ST_NOSUID */
8345
Guido van Rossum246bc171999-02-01 23:54:31 +00008346#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008347#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008348 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8349 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8350 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8351 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8352 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8353 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8354 if (ins(d, "P_PM", (long)P_PM)) return -1;
8355 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8356 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8357 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8358 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8359 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8360 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8361 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8362 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8363 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8364 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8365 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8366 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8367 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008368#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008369 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8370 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8371 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8372 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8373 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008374#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008375#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008376
Guido van Rossumd48f2521997-12-05 22:19:34 +00008377#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008378 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008379#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008380 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008381}
8382
8383
Tim Peters5aa91602002-01-30 05:46:57 +00008384#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008385#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008386#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008387
8388#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008389#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008390#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008391
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008392#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008393#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008394#define MODNAME "posix"
8395#endif
8396
Martin v. Löwis1a214512008-06-11 05:26:20 +00008397static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008398 PyModuleDef_HEAD_INIT,
8399 MODNAME,
8400 posix__doc__,
8401 -1,
8402 posix_methods,
8403 NULL,
8404 NULL,
8405 NULL,
8406 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008407};
8408
8409
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008410PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008411INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008412{
Victor Stinner8c62be82010-05-06 00:08:46 +00008413 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008414
Brian Curtin52173d42010-12-02 18:29:18 +00008415#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008416 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008417#endif
8418
Victor Stinner8c62be82010-05-06 00:08:46 +00008419 m = PyModule_Create(&posixmodule);
8420 if (m == NULL)
8421 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008422
Victor Stinner8c62be82010-05-06 00:08:46 +00008423 /* Initialize environ dictionary */
8424 v = convertenviron();
8425 Py_XINCREF(v);
8426 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8427 return NULL;
8428 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008429
Victor Stinner8c62be82010-05-06 00:08:46 +00008430 if (all_ins(m))
8431 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008432
Victor Stinner8c62be82010-05-06 00:08:46 +00008433 if (setup_confname_tables(m))
8434 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008435
Victor Stinner8c62be82010-05-06 00:08:46 +00008436 Py_INCREF(PyExc_OSError);
8437 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008438
Guido van Rossumb3d39562000-01-31 18:41:26 +00008439#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008440 if (posix_putenv_garbage == NULL)
8441 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008442#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008443
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 if (!initialized) {
8445 stat_result_desc.name = MODNAME ".stat_result";
8446 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8447 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8448 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8449 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8450 structseq_new = StatResultType.tp_new;
8451 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008452
Victor Stinner8c62be82010-05-06 00:08:46 +00008453 statvfs_result_desc.name = MODNAME ".statvfs_result";
8454 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008455#ifdef NEED_TICKS_PER_SECOND
8456# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008457 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008458# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008460# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008461 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008462# endif
8463#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008464 }
8465 Py_INCREF((PyObject*) &StatResultType);
8466 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8467 Py_INCREF((PyObject*) &StatVFSResultType);
8468 PyModule_AddObject(m, "statvfs_result",
8469 (PyObject*) &StatVFSResultType);
8470 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008471
8472#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 /*
8474 * Step 2 of weak-linking support on Mac OS X.
8475 *
8476 * The code below removes functions that are not available on the
8477 * currently active platform.
8478 *
8479 * This block allow one to use a python binary that was build on
8480 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8481 * OSX 10.4.
8482 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008483#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008484 if (fstatvfs == NULL) {
8485 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8486 return NULL;
8487 }
8488 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008489#endif /* HAVE_FSTATVFS */
8490
8491#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 if (statvfs == NULL) {
8493 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8494 return NULL;
8495 }
8496 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008497#endif /* HAVE_STATVFS */
8498
8499# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 if (lchown == NULL) {
8501 if (PyObject_DelAttrString(m, "lchown") == -1) {
8502 return NULL;
8503 }
8504 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008505#endif /* HAVE_LCHOWN */
8506
8507
8508#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008510
Guido van Rossumb6775db1994-08-01 11:34:53 +00008511}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008512
8513#ifdef __cplusplus
8514}
8515#endif