blob: c7d5c80dcfdb06b62d70b5acb9159cbc4a097de2 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#endif /* defined(__VMS) */
35
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#ifdef __cplusplus
37extern "C" {
38#endif
39
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000041"This module provides access to operating system functionality that is\n\
42standardized by the C Standard and the POSIX standard (a thinly\n\
43disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000046
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#if defined(PYOS_OS2)
48#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_TYPES_H */
65
66#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000069
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000071#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000072#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000073
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000075#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000077
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#ifdef HAVE_FCNTL_H
79#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000080#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000081
Guido van Rossuma6535fd2001-10-18 19:44:10 +000082#ifdef HAVE_GRP_H
83#include <grp.h>
84#endif
85
Barry Warsaw5676bd12003-01-07 20:57:09 +000086#ifdef HAVE_SYSEXITS_H
87#include <sysexits.h>
88#endif /* HAVE_SYSEXITS_H */
89
Anthony Baxter8a560de2004-10-13 15:30:56 +000090#ifdef HAVE_SYS_LOADAVG_H
91#include <sys/loadavg.h>
92#endif
93
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000094#ifdef HAVE_LANGINFO_H
95#include <langinfo.h>
96#endif
97
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000099/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000100#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#include <process.h>
102#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000103#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000104#define HAVE_GETCWD 1
105#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000106#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#if defined(__OS2__)
108#define HAVE_EXECV 1
109#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000110#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000111#include <process.h>
112#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000113#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_EXECV 1
115#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_OPENDIR 1
117#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000118#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_WAIT 1
120#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000121#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000122#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000123#define HAVE_GETPPID 1
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000124#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000128#define HAVE_SYSTEM 1
129#define HAVE_CWAIT 1
130#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000132#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000133#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
134/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136/* Unix functions that the configure script doesn't check for */
137#define HAVE_EXECV 1
138#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#define HAVE_FORK1 1
141#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_GETCWD 1
143#define HAVE_GETEGID 1
144#define HAVE_GETEUID 1
145#define HAVE_GETGID 1
146#define HAVE_GETPPID 1
147#define HAVE_GETUID 1
148#define HAVE_KILL 1
149#define HAVE_OPENDIR 1
150#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000154#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#endif /* _MSC_VER */
156#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000157#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000159
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000161
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000162#if defined(__sgi)&&_COMPILER_VERSION>=700
163/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 (default) */
165extern char *ctermid_r(char *);
166#endif
167
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000168#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000170extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000172#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000177#endif
178#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int chdir(char *);
180extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000182extern int chdir(const char *);
183extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000185#ifdef __BORLANDC__
186extern int chmod(const char *, int);
187#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000188extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000190/*#ifdef HAVE_FCHMOD
191extern int fchmod(int, mode_t);
192#endif*/
193/*#ifdef HAVE_LCHMOD
194extern int lchmod(const char *, mode_t);
195#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int chown(const char *, uid_t, gid_t);
197extern char *getcwd(char *, int);
198extern char *strerror(int);
199extern int link(const char *, const char *);
200extern int rename(const char *, const char *);
201extern int stat(const char *, struct stat *);
202extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000205#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000207extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000211#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#ifdef HAVE_UTIME_H
214#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000215#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000217#ifdef HAVE_SYS_UTIME_H
218#include <sys/utime.h>
219#define HAVE_UTIME_H /* pretend we do for the rest of this file */
220#endif /* HAVE_SYS_UTIME_H */
221
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#ifdef HAVE_SYS_TIMES_H
223#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000224#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
226#ifdef HAVE_SYS_PARAM_H
227#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000228#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229
230#ifdef HAVE_SYS_UTSNAME_H
231#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000232#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000234#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) strlen((dirent)->d_name)
237#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000238#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#include <direct.h>
240#define NAMLEN(dirent) strlen((dirent)->d_name)
241#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000247#endif
248#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#endif
251#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#endif
254#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259#endif
260#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262#endif
263#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000266#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000267#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000268#endif
269#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000270#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000271#endif
272#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000273#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000274#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000276#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000278#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000279#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000280#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
281#define HAVE_SYMLINK
282#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000283#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284
Guido van Rossumd48f2521997-12-05 22:19:34 +0000285#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000287#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288
Tim Petersbc2e10e2002-03-03 23:17:02 +0000289#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000290#if defined(PATH_MAX) && PATH_MAX > 1024
291#define MAXPATHLEN PATH_MAX
292#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000293#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000294#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000295#endif /* MAXPATHLEN */
296
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000297#ifdef UNION_WAIT
298/* Emulate some macros on systems that have a union instead of macros */
299
300#ifndef WIFEXITED
301#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
302#endif
303
304#ifndef WEXITSTATUS
305#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
306#endif
307
308#ifndef WTERMSIG
309#define WTERMSIG(u_wait) ((u_wait).w_termsig)
310#endif
311
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312#define WAIT_TYPE union wait
313#define WAIT_STATUS_INT(s) (s.w_status)
314
315#else /* !UNION_WAIT */
316#define WAIT_TYPE int
317#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000318#endif /* UNION_WAIT */
319
Greg Wardb48bc172000-03-01 21:51:56 +0000320/* Don't use the "_r" form if we don't need it (also, won't have a
321 prototype for it, at least on Solaris -- maybe others as well?). */
322#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
323#define USE_CTERMID_R
324#endif
325
Fred Drake699f3522000-06-29 21:12:41 +0000326/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000327#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000328#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000329# define STAT win32_stat
330# define FSTAT win32_fstat
331# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000332#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000333# define STAT stat
334# define FSTAT fstat
335# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000336#endif
337
Tim Peters11b23062003-04-23 02:39:17 +0000338#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000339#include <sys/mkdev.h>
340#else
341#if defined(MAJOR_IN_SYSMACROS)
342#include <sys/sysmacros.h>
343#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000344#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
345#include <sys/mkdev.h>
346#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000347#endif
Fred Drake699f3522000-06-29 21:12:41 +0000348
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000349#if defined _MSC_VER && _MSC_VER >= 1400
350/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
351 * valid and throw an assertion if it isn't.
352 * Normally, an invalid fd is likely to be a C program error and therefore
353 * an assertion can be useful, but it does contradict the POSIX standard
354 * which for write(2) states:
355 * "Otherwise, -1 shall be returned and errno set to indicate the error."
356 * "[EBADF] The fildes argument is not a valid file descriptor open for
357 * writing."
358 * Furthermore, python allows the user to enter any old integer
359 * as a fd and should merely raise a python exception on error.
360 * The Microsoft CRT doesn't provide an official way to check for the
361 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000362 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000363 * internal structures involved.
364 * The structures below must be updated for each version of visual studio
365 * according to the file internal.h in the CRT source, until MS comes
366 * up with a less hacky way to do this.
367 * (all of this is to avoid globally modifying the CRT behaviour using
368 * _set_invalid_parameter_handler() and _CrtSetReportMode())
369 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000370/* The actual size of the structure is determined at runtime.
371 * Only the first items must be present.
372 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000373typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000374 intptr_t osfhnd;
375 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000376} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000377
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000378extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000379#define IOINFO_L2E 5
380#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
381#define IOINFO_ARRAYS 64
382#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
383#define FOPEN 0x01
384#define _NO_CONSOLE_FILENO (intptr_t)-2
385
386/* This function emulates what the windows CRT does to validate file handles */
387int
388_PyVerify_fd(int fd)
389{
Victor Stinner8c62be82010-05-06 00:08:46 +0000390 const int i1 = fd >> IOINFO_L2E;
391 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000392
Antoine Pitrou22e41552010-08-15 18:07:50 +0000393 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000394
Victor Stinner8c62be82010-05-06 00:08:46 +0000395 /* Determine the actual size of the ioinfo structure,
396 * as used by the CRT loaded in memory
397 */
398 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
399 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
400 }
401 if (sizeof_ioinfo == 0) {
402 /* This should not happen... */
403 goto fail;
404 }
405
406 /* See that it isn't a special CLEAR fileno */
407 if (fd != _NO_CONSOLE_FILENO) {
408 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
409 * we check pointer validity and other info
410 */
411 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
412 /* finally, check that the file is open */
413 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
414 if (info->osfile & FOPEN) {
415 return 1;
416 }
417 }
418 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000419 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000420 errno = EBADF;
421 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422}
423
424/* the special case of checking dup2. The target fd must be in a sensible range */
425static int
426_PyVerify_fd_dup2(int fd1, int fd2)
427{
Victor Stinner8c62be82010-05-06 00:08:46 +0000428 if (!_PyVerify_fd(fd1))
429 return 0;
430 if (fd2 == _NO_CONSOLE_FILENO)
431 return 0;
432 if ((unsigned)fd2 < _NHANDLE_)
433 return 1;
434 else
435 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000436}
437#else
438/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
439#define _PyVerify_fd_dup2(A, B) (1)
440#endif
441
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000442#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000443/* The following structure was copied from
444 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
445 include doesn't seem to be present in the Windows SDK (at least as included
446 with Visual Studio Express). */
447typedef struct _REPARSE_DATA_BUFFER {
448 ULONG ReparseTag;
449 USHORT ReparseDataLength;
450 USHORT Reserved;
451 union {
452 struct {
453 USHORT SubstituteNameOffset;
454 USHORT SubstituteNameLength;
455 USHORT PrintNameOffset;
456 USHORT PrintNameLength;
457 ULONG Flags;
458 WCHAR PathBuffer[1];
459 } SymbolicLinkReparseBuffer;
460
461 struct {
462 USHORT SubstituteNameOffset;
463 USHORT SubstituteNameLength;
464 USHORT PrintNameOffset;
465 USHORT PrintNameLength;
466 WCHAR PathBuffer[1];
467 } MountPointReparseBuffer;
468
469 struct {
470 UCHAR DataBuffer[1];
471 } GenericReparseBuffer;
472 };
473} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
474
475#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
476 GenericReparseBuffer)
477#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
478
479static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000480win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000481{
482 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
483 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
484 DWORD n_bytes_returned;
485 const wchar_t *ptr;
486 wchar_t *buf;
487 size_t len;
488
489 if (0 == DeviceIoControl(
490 reparse_point_handle,
491 FSCTL_GET_REPARSE_POINT,
492 NULL, 0, /* in buffer */
493 target_buffer, sizeof(target_buffer),
494 &n_bytes_returned,
495 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000496 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000497
498 if (reparse_tag)
499 *reparse_tag = rdb->ReparseTag;
500
501 if (target_path) {
502 switch (rdb->ReparseTag) {
503 case IO_REPARSE_TAG_SYMLINK:
504 /* XXX: Maybe should use SubstituteName? */
505 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
506 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
507 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
508 break;
509 case IO_REPARSE_TAG_MOUNT_POINT:
510 ptr = rdb->MountPointReparseBuffer.PathBuffer +
511 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
512 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
513 break;
514 default:
515 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000516 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000517 }
518 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
519 if (!buf) {
520 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000521 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000522 }
523 wcsncpy(buf, ptr, len);
524 buf[len] = L'\0';
525 if (wcsncmp(buf, L"\\??\\", 4) == 0)
526 buf[1] = L'\\';
527 *target_path = buf;
528 }
529
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000530 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000531}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000532#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000533
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000535#ifdef WITH_NEXT_FRAMEWORK
536/* On Darwin/MacOSX a shared library or framework has no access to
537** environ directly, we must obtain it with _NSGetEnviron().
538*/
539#include <crt_externs.h>
540static char **environ;
541#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000543#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544
Barry Warsaw53699e91996-12-10 23:23:01 +0000545static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000546convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547{
Victor Stinner8c62be82010-05-06 00:08:46 +0000548 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000549#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000550 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000551#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000552 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000553#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000554#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000555 APIRET rc;
556 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
557#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000558
Victor Stinner8c62be82010-05-06 00:08:46 +0000559 d = PyDict_New();
560 if (d == NULL)
561 return NULL;
562#ifdef WITH_NEXT_FRAMEWORK
563 if (environ == NULL)
564 environ = *_NSGetEnviron();
565#endif
566#ifdef MS_WINDOWS
567 /* _wenviron must be initialized in this way if the program is started
568 through main() instead of wmain(). */
569 _wgetenv(L"");
570 if (_wenviron == NULL)
571 return d;
572 /* This part ignores errors */
573 for (e = _wenviron; *e != NULL; e++) {
574 PyObject *k;
575 PyObject *v;
576 wchar_t *p = wcschr(*e, L'=');
577 if (p == NULL)
578 continue;
579 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
580 if (k == NULL) {
581 PyErr_Clear();
582 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000583 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000584 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
585 if (v == NULL) {
586 PyErr_Clear();
587 Py_DECREF(k);
588 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000589 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000590 if (PyDict_GetItem(d, k) == NULL) {
591 if (PyDict_SetItem(d, k, v) != 0)
592 PyErr_Clear();
593 }
594 Py_DECREF(k);
595 Py_DECREF(v);
596 }
597#else
598 if (environ == NULL)
599 return d;
600 /* This part ignores errors */
601 for (e = environ; *e != NULL; e++) {
602 PyObject *k;
603 PyObject *v;
604 char *p = strchr(*e, '=');
605 if (p == NULL)
606 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000607 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000608 if (k == NULL) {
609 PyErr_Clear();
610 continue;
611 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000612 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000613 if (v == NULL) {
614 PyErr_Clear();
615 Py_DECREF(k);
616 continue;
617 }
618 if (PyDict_GetItem(d, k) == NULL) {
619 if (PyDict_SetItem(d, k, v) != 0)
620 PyErr_Clear();
621 }
622 Py_DECREF(k);
623 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000624 }
625#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000626#if defined(PYOS_OS2)
627 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
628 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
629 PyObject *v = PyBytes_FromString(buffer);
630 PyDict_SetItemString(d, "BEGINLIBPATH", v);
631 Py_DECREF(v);
632 }
633 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
634 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
635 PyObject *v = PyBytes_FromString(buffer);
636 PyDict_SetItemString(d, "ENDLIBPATH", v);
637 Py_DECREF(v);
638 }
639#endif
640 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641}
642
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643/* Set a POSIX-specific error from errno, and return NULL */
644
Barry Warsawd58d7641998-07-23 16:14:40 +0000645static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000646posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000647{
Victor Stinner8c62be82010-05-06 00:08:46 +0000648 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649}
Barry Warsawd58d7641998-07-23 16:14:40 +0000650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000651posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000652{
Victor Stinner8c62be82010-05-06 00:08:46 +0000653 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000654}
655
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000656
Mark Hammondef8b6542001-05-13 08:04:26 +0000657static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000658posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000659{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000660 PyObject *name_str, *rc;
661 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
662 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000663 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000664 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
665 name_str);
666 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000667 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000668}
669
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000670#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000671static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000672win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000673{
Victor Stinner8c62be82010-05-06 00:08:46 +0000674 /* XXX We should pass the function name along in the future.
675 (winreg.c also wants to pass the function name.)
676 This would however require an additional param to the
677 Windows error object, which is non-trivial.
678 */
679 errno = GetLastError();
680 if (filename)
681 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
682 else
683 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000684}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000685
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686static PyObject *
687win32_error_unicode(char* function, Py_UNICODE* filename)
688{
Victor Stinner8c62be82010-05-06 00:08:46 +0000689 /* XXX - see win32_error for comments on 'function' */
690 errno = GetLastError();
691 if (filename)
692 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
693 else
694 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000695}
696
Thomas Wouters477c8d52006-05-27 19:21:47 +0000697static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000698convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699{
Victor Stinner8c62be82010-05-06 00:08:46 +0000700 if (PyUnicode_CheckExact(*param))
701 Py_INCREF(*param);
702 else if (PyUnicode_Check(*param))
703 /* For a Unicode subtype that's not a Unicode object,
704 return a true Unicode object with the same data. */
705 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
706 PyUnicode_GET_SIZE(*param));
707 else
708 *param = PyUnicode_FromEncodedObject(*param,
709 Py_FileSystemDefaultEncoding,
710 "strict");
711 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000712}
713
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000714#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715
Guido van Rossumd48f2521997-12-05 22:19:34 +0000716#if defined(PYOS_OS2)
717/**********************************************************************
718 * Helper Function to Trim and Format OS/2 Messages
719 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000720static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000721os2_formatmsg(char *msgbuf, int msglen, char *reason)
722{
723 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
724
725 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
726 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
727
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000728 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000729 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
730 }
731
732 /* Add Optional Reason Text */
733 if (reason) {
734 strcat(msgbuf, " : ");
735 strcat(msgbuf, reason);
736 }
737}
738
739/**********************************************************************
740 * Decode an OS/2 Operating System Error Code
741 *
742 * A convenience function to lookup an OS/2 error code and return a
743 * text message we can use to raise a Python exception.
744 *
745 * Notes:
746 * The messages for errors returned from the OS/2 kernel reside in
747 * the file OSO001.MSG in the \OS2 directory hierarchy.
748 *
749 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000750static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000751os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
752{
753 APIRET rc;
754 ULONG msglen;
755
756 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
757 Py_BEGIN_ALLOW_THREADS
758 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
759 errorcode, "oso001.msg", &msglen);
760 Py_END_ALLOW_THREADS
761
762 if (rc == NO_ERROR)
763 os2_formatmsg(msgbuf, msglen, reason);
764 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000765 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000766 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000767
768 return msgbuf;
769}
770
771/* Set an OS/2-specific error and return NULL. OS/2 kernel
772 errors are not in a global variable e.g. 'errno' nor are
773 they congruent with posix error numbers. */
774
Victor Stinner8c62be82010-05-06 00:08:46 +0000775static PyObject *
776os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000777{
778 char text[1024];
779 PyObject *v;
780
781 os2_strerror(text, sizeof(text), code, "");
782
783 v = Py_BuildValue("(is)", code, text);
784 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000785 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000786 Py_DECREF(v);
787 }
788 return NULL; /* Signal to Python that an Exception is Pending */
789}
790
791#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792
793/* POSIX generic methods */
794
Barry Warsaw53699e91996-12-10 23:23:01 +0000795static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000796posix_fildes(PyObject *fdobj, int (*func)(int))
797{
Victor Stinner8c62be82010-05-06 00:08:46 +0000798 int fd;
799 int res;
800 fd = PyObject_AsFileDescriptor(fdobj);
801 if (fd < 0)
802 return NULL;
803 if (!_PyVerify_fd(fd))
804 return posix_error();
805 Py_BEGIN_ALLOW_THREADS
806 res = (*func)(fd);
807 Py_END_ALLOW_THREADS
808 if (res < 0)
809 return posix_error();
810 Py_INCREF(Py_None);
811 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000812}
Guido van Rossum21142a01999-01-08 21:05:37 +0000813
814static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816{
Victor Stinner8c62be82010-05-06 00:08:46 +0000817 PyObject *opath1 = NULL;
818 char *path1;
819 int res;
820 if (!PyArg_ParseTuple(args, format,
821 PyUnicode_FSConverter, &opath1))
822 return NULL;
823 path1 = PyBytes_AsString(opath1);
824 Py_BEGIN_ALLOW_THREADS
825 res = (*func)(path1);
826 Py_END_ALLOW_THREADS
827 if (res < 0)
828 return posix_error_with_allocated_filename(opath1);
829 Py_DECREF(opath1);
830 Py_INCREF(Py_None);
831 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000832}
833
Barry Warsaw53699e91996-12-10 23:23:01 +0000834static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000835posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000836 char *format,
837 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838{
Victor Stinner8c62be82010-05-06 00:08:46 +0000839 PyObject *opath1 = NULL, *opath2 = NULL;
840 char *path1, *path2;
841 int res;
842 if (!PyArg_ParseTuple(args, format,
843 PyUnicode_FSConverter, &opath1,
844 PyUnicode_FSConverter, &opath2)) {
845 return NULL;
846 }
847 path1 = PyBytes_AsString(opath1);
848 path2 = PyBytes_AsString(opath2);
849 Py_BEGIN_ALLOW_THREADS
850 res = (*func)(path1, path2);
851 Py_END_ALLOW_THREADS
852 Py_DECREF(opath1);
853 Py_DECREF(opath2);
854 if (res != 0)
855 /* XXX how to report both path1 and path2??? */
856 return posix_error();
857 Py_INCREF(Py_None);
858 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859}
860
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000861#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000863win32_1str(PyObject* args, char* func,
864 char* format, BOOL (__stdcall *funcA)(LPCSTR),
865 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000866{
Victor Stinner8c62be82010-05-06 00:08:46 +0000867 PyObject *uni;
868 char *ansi;
869 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000870
Victor Stinner8c62be82010-05-06 00:08:46 +0000871 if (!PyArg_ParseTuple(args, wformat, &uni))
872 PyErr_Clear();
873 else {
874 Py_BEGIN_ALLOW_THREADS
875 result = funcW(PyUnicode_AsUnicode(uni));
876 Py_END_ALLOW_THREADS
877 if (!result)
878 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
879 Py_INCREF(Py_None);
880 return Py_None;
881 }
882 if (!PyArg_ParseTuple(args, format, &ansi))
883 return NULL;
884 Py_BEGIN_ALLOW_THREADS
885 result = funcA(ansi);
886 Py_END_ALLOW_THREADS
887 if (!result)
888 return win32_error(func, ansi);
889 Py_INCREF(Py_None);
890 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891
892}
893
894/* This is a reimplementation of the C library's chdir function,
895 but one that produces Win32 errors instead of DOS error codes.
896 chdir is essentially a wrapper around SetCurrentDirectory; however,
897 it also needs to set "magic" environment variables indicating
898 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000899static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900win32_chdir(LPCSTR path)
901{
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 char new_path[MAX_PATH+1];
903 int result;
904 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 if(!SetCurrentDirectoryA(path))
907 return FALSE;
908 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
909 if (!result)
910 return FALSE;
911 /* In the ANSI API, there should not be any paths longer
912 than MAX_PATH. */
913 assert(result <= MAX_PATH+1);
914 if (strncmp(new_path, "\\\\", 2) == 0 ||
915 strncmp(new_path, "//", 2) == 0)
916 /* UNC path, nothing to do. */
917 return TRUE;
918 env[1] = new_path[0];
919 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920}
921
922/* The Unicode version differs from the ANSI version
923 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000924static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000925win32_wchdir(LPCWSTR path)
926{
Victor Stinner8c62be82010-05-06 00:08:46 +0000927 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
928 int result;
929 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930
Victor Stinner8c62be82010-05-06 00:08:46 +0000931 if(!SetCurrentDirectoryW(path))
932 return FALSE;
933 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
934 if (!result)
935 return FALSE;
936 if (result > MAX_PATH+1) {
937 new_path = malloc(result * sizeof(wchar_t));
938 if (!new_path) {
939 SetLastError(ERROR_OUTOFMEMORY);
940 return FALSE;
941 }
942 result = GetCurrentDirectoryW(result, new_path);
943 if (!result) {
944 free(new_path);
945 return FALSE;
946 }
947 }
948 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
949 wcsncmp(new_path, L"//", 2) == 0)
950 /* UNC path, nothing to do. */
951 return TRUE;
952 env[1] = new_path[0];
953 result = SetEnvironmentVariableW(env, new_path);
954 if (new_path != _new_path)
955 free(new_path);
956 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957}
958#endif
959
Martin v. Löwis14694662006-02-03 12:54:16 +0000960#ifdef MS_WINDOWS
961/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
962 - time stamps are restricted to second resolution
963 - file modification times suffer from forth-and-back conversions between
964 UTC and local time
965 Therefore, we implement our own stat, based on the Win32 API directly.
966*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000967#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000968
969struct win32_stat{
970 int st_dev;
971 __int64 st_ino;
972 unsigned short st_mode;
973 int st_nlink;
974 int st_uid;
975 int st_gid;
976 int st_rdev;
977 __int64 st_size;
978 int st_atime;
979 int st_atime_nsec;
980 int st_mtime;
981 int st_mtime_nsec;
982 int st_ctime;
983 int st_ctime_nsec;
984};
985
986static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
987
988static void
989FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
990{
Victor Stinner8c62be82010-05-06 00:08:46 +0000991 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
992 /* Cannot simply cast and dereference in_ptr,
993 since it might not be aligned properly */
994 __int64 in;
995 memcpy(&in, in_ptr, sizeof(in));
996 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
997 /* XXX Win32 supports time stamps past 2038; we currently don't */
998 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000999}
1000
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001static void
1002time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
1003{
Victor Stinner8c62be82010-05-06 00:08:46 +00001004 /* XXX endianness */
1005 __int64 out;
1006 out = time_in + secs_between_epochs;
1007 out = out * 10000000 + nsec_in / 100;
1008 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001009}
1010
Martin v. Löwis14694662006-02-03 12:54:16 +00001011/* Below, we *know* that ugo+r is 0444 */
1012#if _S_IREAD != 0400
1013#error Unsupported C library
1014#endif
1015static int
1016attributes_to_mode(DWORD attr)
1017{
Victor Stinner8c62be82010-05-06 00:08:46 +00001018 int m = 0;
1019 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1020 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1021 else
1022 m |= _S_IFREG;
1023 if (attr & FILE_ATTRIBUTE_READONLY)
1024 m |= 0444;
1025 else
1026 m |= 0666;
1027 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001028}
1029
1030static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001031attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001032{
Victor Stinner8c62be82010-05-06 00:08:46 +00001033 memset(result, 0, sizeof(*result));
1034 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1035 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1036 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1037 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1038 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001039 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001040 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001041 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1042 /* first clear the S_IFMT bits */
1043 result->st_mode ^= (result->st_mode & 0170000);
1044 /* now set the bits that make this a symlink */
1045 result->st_mode |= 0120000;
1046 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001047
Victor Stinner8c62be82010-05-06 00:08:46 +00001048 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001049}
1050
Guido van Rossumd8faa362007-04-27 19:54:29 +00001051static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001052attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001053{
Victor Stinner8c62be82010-05-06 00:08:46 +00001054 HANDLE hFindFile;
1055 WIN32_FIND_DATAA FileData;
1056 hFindFile = FindFirstFileA(pszFile, &FileData);
1057 if (hFindFile == INVALID_HANDLE_VALUE)
1058 return FALSE;
1059 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001060 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001061 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001062 info->dwFileAttributes = FileData.dwFileAttributes;
1063 info->ftCreationTime = FileData.ftCreationTime;
1064 info->ftLastAccessTime = FileData.ftLastAccessTime;
1065 info->ftLastWriteTime = FileData.ftLastWriteTime;
1066 info->nFileSizeHigh = FileData.nFileSizeHigh;
1067 info->nFileSizeLow = FileData.nFileSizeLow;
1068/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001069 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1070 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001071 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001072}
1073
1074static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001075attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001076{
Victor Stinner8c62be82010-05-06 00:08:46 +00001077 HANDLE hFindFile;
1078 WIN32_FIND_DATAW FileData;
1079 hFindFile = FindFirstFileW(pszFile, &FileData);
1080 if (hFindFile == INVALID_HANDLE_VALUE)
1081 return FALSE;
1082 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001083 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001084 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001085 info->dwFileAttributes = FileData.dwFileAttributes;
1086 info->ftCreationTime = FileData.ftCreationTime;
1087 info->ftLastAccessTime = FileData.ftLastAccessTime;
1088 info->ftLastWriteTime = FileData.ftLastWriteTime;
1089 info->nFileSizeHigh = FileData.nFileSizeHigh;
1090 info->nFileSizeLow = FileData.nFileSizeLow;
1091/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001092 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1093 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001094 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001095}
1096
Brian Curtinf5e76d02010-11-24 13:14:05 +00001097#ifndef SYMLOOP_MAX
1098#define SYMLOOP_MAX ( 88 )
1099#endif
1100
1101static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001102win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001103
1104static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001105win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001106{
1107 int code;
1108 HANDLE hFile;
1109 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001110 ULONG reparse_tag = 0;
1111 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001112 const char *dot;
1113
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001114 if (depth > SYMLOOP_MAX) {
1115 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1116 return -1;
1117 }
1118
Brian Curtinf5e76d02010-11-24 13:14:05 +00001119 hFile = CreateFileA(
1120 path,
1121 0, /* desired access */
1122 0, /* share mode */
1123 NULL, /* security attributes */
1124 OPEN_EXISTING,
1125 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1126 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1127 NULL);
1128
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001129 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001130 /* Either the target doesn't exist, or we don't have access to
1131 get a handle to it. If the former, we need to return an error.
1132 If the latter, we can use attributes_from_dir. */
1133 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001134 return -1;
1135 /* Could not get attributes on open file. Fall back to
1136 reading the directory. */
1137 if (!attributes_from_dir(path, &info, &reparse_tag))
1138 /* Very strange. This should not fail now */
1139 return -1;
1140 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1141 if (traverse) {
1142 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001143 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001144 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001145 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001146 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001147 } else {
1148 if (!GetFileInformationByHandle(hFile, &info)) {
1149 CloseHandle(hFile);
1150 return -1;;
1151 }
1152 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1153 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1154 CloseHandle(hFile);
1155 if (code < 0)
1156 return code;
1157 if (traverse) {
1158 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1159 free(target_path);
1160 return code;
1161 }
1162 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001163 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001164 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001165 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001166
1167 /* Set S_IEXEC if it is an .exe, .bat, ... */
1168 dot = strrchr(path, '.');
1169 if (dot) {
1170 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1171 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1172 result->st_mode |= 0111;
1173 }
1174 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001175}
1176
1177static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001178win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001179{
1180 int code;
1181 HANDLE hFile;
1182 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001183 ULONG reparse_tag = 0;
1184 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001185 const wchar_t *dot;
1186
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001187 if (depth > SYMLOOP_MAX) {
1188 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1189 return -1;
1190 }
1191
Brian Curtinf5e76d02010-11-24 13:14:05 +00001192 hFile = CreateFileW(
1193 path,
1194 0, /* desired access */
1195 0, /* share mode */
1196 NULL, /* security attributes */
1197 OPEN_EXISTING,
1198 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1199 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1200 NULL);
1201
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001202 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001203 /* Either the target doesn't exist, or we don't have access to
1204 get a handle to it. If the former, we need to return an error.
1205 If the latter, we can use attributes_from_dir. */
1206 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001207 return -1;
1208 /* Could not get attributes on open file. Fall back to
1209 reading the directory. */
1210 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1211 /* Very strange. This should not fail now */
1212 return -1;
1213 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1214 if (traverse) {
1215 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001216 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001217 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001218 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001219 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001220 } else {
1221 if (!GetFileInformationByHandle(hFile, &info)) {
1222 CloseHandle(hFile);
1223 return -1;;
1224 }
1225 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1226 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1227 CloseHandle(hFile);
1228 if (code < 0)
1229 return code;
1230 if (traverse) {
1231 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1232 free(target_path);
1233 return code;
1234 }
1235 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001236 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001237 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001238 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001239
1240 /* Set S_IEXEC if it is an .exe, .bat, ... */
1241 dot = wcsrchr(path, '.');
1242 if (dot) {
1243 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1244 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1245 result->st_mode |= 0111;
1246 }
1247 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248}
1249
1250static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001251win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001252{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001253 /* Protocol violation: we explicitly clear errno, instead of
1254 setting it to a POSIX error. Callers should use GetLastError. */
1255 int code = win32_xstat_impl(path, result, traverse, 0);
1256 errno = 0;
1257 return code;
1258}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001259
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001260static int
1261win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1262{
1263 /* Protocol violation: we explicitly clear errno, instead of
1264 setting it to a POSIX error. Callers should use GetLastError. */
1265 int code = win32_xstat_impl_w(path, result, traverse, 0);
1266 errno = 0;
1267 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268}
1269
Brian Curtind40e6f72010-07-08 21:39:08 +00001270/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1271 win32_stat_w
1272
1273 In Posix, stat automatically traverses symlinks and returns the stat
1274 structure for the target. In Windows, the equivalent GetFileAttributes by
1275 default does not traverse symlinks and instead returns attributes for
1276 the symlink.
1277
1278 Therefore, win32_lstat will get the attributes traditionally, and
1279 win32_stat will first explicitly resolve the symlink target and then will
1280 call win32_lstat on that result.
1281
1282 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1283
1284static int
1285win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001286{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001287 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001288}
1289
Victor Stinner8c62be82010-05-06 00:08:46 +00001290static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001291win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001292{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001293 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001294}
1295
1296static int
1297win32_stat(const char* path, struct win32_stat *result)
1298{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001299 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001300}
1301
1302static int
1303win32_stat_w(const wchar_t* path, struct win32_stat *result)
1304{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001305 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001306}
1307
1308static int
1309win32_fstat(int file_number, struct win32_stat *result)
1310{
Victor Stinner8c62be82010-05-06 00:08:46 +00001311 BY_HANDLE_FILE_INFORMATION info;
1312 HANDLE h;
1313 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001314
Victor Stinner8c62be82010-05-06 00:08:46 +00001315 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001316
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 /* Protocol violation: we explicitly clear errno, instead of
1318 setting it to a POSIX error. Callers should use GetLastError. */
1319 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001320
Victor Stinner8c62be82010-05-06 00:08:46 +00001321 if (h == INVALID_HANDLE_VALUE) {
1322 /* This is really a C library error (invalid file handle).
1323 We set the Win32 error to the closes one matching. */
1324 SetLastError(ERROR_INVALID_HANDLE);
1325 return -1;
1326 }
1327 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001328
Victor Stinner8c62be82010-05-06 00:08:46 +00001329 type = GetFileType(h);
1330 if (type == FILE_TYPE_UNKNOWN) {
1331 DWORD error = GetLastError();
1332 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001333 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001334 }
1335 /* else: valid but unknown file */
1336 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001337
Victor Stinner8c62be82010-05-06 00:08:46 +00001338 if (type != FILE_TYPE_DISK) {
1339 if (type == FILE_TYPE_CHAR)
1340 result->st_mode = _S_IFCHR;
1341 else if (type == FILE_TYPE_PIPE)
1342 result->st_mode = _S_IFIFO;
1343 return 0;
1344 }
1345
1346 if (!GetFileInformationByHandle(h, &info)) {
1347 return -1;
1348 }
1349
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001350 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001351 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001352 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1353 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001354}
1355
1356#endif /* MS_WINDOWS */
1357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001358PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359"stat_result: Result from stat or lstat.\n\n\
1360This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001361 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001362or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1363\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001364Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1365or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001366\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001367See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001368
1369static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001370 {"st_mode", "protection bits"},
1371 {"st_ino", "inode"},
1372 {"st_dev", "device"},
1373 {"st_nlink", "number of hard links"},
1374 {"st_uid", "user ID of owner"},
1375 {"st_gid", "group ID of owner"},
1376 {"st_size", "total size, in bytes"},
1377 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1378 {NULL, "integer time of last access"},
1379 {NULL, "integer time of last modification"},
1380 {NULL, "integer time of last change"},
1381 {"st_atime", "time of last access"},
1382 {"st_mtime", "time of last modification"},
1383 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001384#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001385 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001386#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001387#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001388 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001389#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001390#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001391 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001392#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001393#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001394 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001395#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001396#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001397 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001398#endif
1399#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001400 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001401#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001402 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001403};
1404
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001405#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001406#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001407#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001408#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001409#endif
1410
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001411#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001412#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1413#else
1414#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1415#endif
1416
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001417#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001418#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1419#else
1420#define ST_RDEV_IDX ST_BLOCKS_IDX
1421#endif
1422
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001423#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1424#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1425#else
1426#define ST_FLAGS_IDX ST_RDEV_IDX
1427#endif
1428
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001429#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001430#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001431#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001432#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001433#endif
1434
1435#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1436#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1437#else
1438#define ST_BIRTHTIME_IDX ST_GEN_IDX
1439#endif
1440
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001441static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001442 "stat_result", /* name */
1443 stat_result__doc__, /* doc */
1444 stat_result_fields,
1445 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001446};
1447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001448PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001449"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1450This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001451 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001452or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001453\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001454See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001455
1456static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001457 {"f_bsize", },
1458 {"f_frsize", },
1459 {"f_blocks", },
1460 {"f_bfree", },
1461 {"f_bavail", },
1462 {"f_files", },
1463 {"f_ffree", },
1464 {"f_favail", },
1465 {"f_flag", },
1466 {"f_namemax",},
1467 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001468};
1469
1470static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 "statvfs_result", /* name */
1472 statvfs_result__doc__, /* doc */
1473 statvfs_result_fields,
1474 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001475};
1476
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001478static PyTypeObject StatResultType;
1479static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001480static newfunc structseq_new;
1481
1482static PyObject *
1483statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1484{
Victor Stinner8c62be82010-05-06 00:08:46 +00001485 PyStructSequence *result;
1486 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001487
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 result = (PyStructSequence*)structseq_new(type, args, kwds);
1489 if (!result)
1490 return NULL;
1491 /* If we have been initialized from a tuple,
1492 st_?time might be set to None. Initialize it
1493 from the int slots. */
1494 for (i = 7; i <= 9; i++) {
1495 if (result->ob_item[i+3] == Py_None) {
1496 Py_DECREF(Py_None);
1497 Py_INCREF(result->ob_item[i]);
1498 result->ob_item[i+3] = result->ob_item[i];
1499 }
1500 }
1501 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001502}
1503
1504
1505
1506/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001507static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001508
1509PyDoc_STRVAR(stat_float_times__doc__,
1510"stat_float_times([newval]) -> oldval\n\n\
1511Determine whether os.[lf]stat represents time stamps as float objects.\n\
1512If newval is True, future calls to stat() return floats, if it is False,\n\
1513future calls return ints. \n\
1514If newval is omitted, return the current setting.\n");
1515
1516static PyObject*
1517stat_float_times(PyObject* self, PyObject *args)
1518{
Victor Stinner8c62be82010-05-06 00:08:46 +00001519 int newval = -1;
1520 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1521 return NULL;
1522 if (newval == -1)
1523 /* Return old value */
1524 return PyBool_FromLong(_stat_float_times);
1525 _stat_float_times = newval;
1526 Py_INCREF(Py_None);
1527 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001528}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001529
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001530static void
1531fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1532{
Victor Stinner8c62be82010-05-06 00:08:46 +00001533 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001534#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001536#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001537 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001538#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001539 if (!ival)
1540 return;
1541 if (_stat_float_times) {
1542 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1543 } else {
1544 fval = ival;
1545 Py_INCREF(fval);
1546 }
1547 PyStructSequence_SET_ITEM(v, index, ival);
1548 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001549}
1550
Tim Peters5aa91602002-01-30 05:46:57 +00001551/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001552 (used by posix_stat() and posix_fstat()) */
1553static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001554_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001555{
Victor Stinner8c62be82010-05-06 00:08:46 +00001556 unsigned long ansec, mnsec, cnsec;
1557 PyObject *v = PyStructSequence_New(&StatResultType);
1558 if (v == NULL)
1559 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001560
Victor Stinner8c62be82010-05-06 00:08:46 +00001561 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001562#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 PyStructSequence_SET_ITEM(v, 1,
1564 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001565#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001566 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001567#endif
1568#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001569 PyStructSequence_SET_ITEM(v, 2,
1570 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001571#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001572 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001573#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001574 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1575 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1576 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001577#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 PyStructSequence_SET_ITEM(v, 6,
1579 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001580#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001581 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001582#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001583
Martin v. Löwis14694662006-02-03 12:54:16 +00001584#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001585 ansec = st->st_atim.tv_nsec;
1586 mnsec = st->st_mtim.tv_nsec;
1587 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001588#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 ansec = st->st_atimespec.tv_nsec;
1590 mnsec = st->st_mtimespec.tv_nsec;
1591 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001592#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 ansec = st->st_atime_nsec;
1594 mnsec = st->st_mtime_nsec;
1595 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001596#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001597 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001598#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 fill_time(v, 7, st->st_atime, ansec);
1600 fill_time(v, 8, st->st_mtime, mnsec);
1601 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001602
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001603#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1605 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001606#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001607#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1609 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001610#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001611#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001612 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1613 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001614#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001615#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001616 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1617 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001618#endif
1619#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001620 {
1621 PyObject *val;
1622 unsigned long bsec,bnsec;
1623 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001624#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001625 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001626#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001628#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001629 if (_stat_float_times) {
1630 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1631 } else {
1632 val = PyLong_FromLong((long)bsec);
1633 }
1634 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1635 val);
1636 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001637#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001638#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001639 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1640 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001641#endif
Fred Drake699f3522000-06-29 21:12:41 +00001642
Victor Stinner8c62be82010-05-06 00:08:46 +00001643 if (PyErr_Occurred()) {
1644 Py_DECREF(v);
1645 return NULL;
1646 }
Fred Drake699f3522000-06-29 21:12:41 +00001647
Victor Stinner8c62be82010-05-06 00:08:46 +00001648 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001649}
1650
Barry Warsaw53699e91996-12-10 23:23:01 +00001651static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001652posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001653 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001654#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001656#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001657 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001658#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001659 char *wformat,
1660 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001661{
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 STRUCT_STAT st;
1663 PyObject *opath;
1664 char *path;
1665 int res;
1666 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001667
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001668#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001669 PyUnicodeObject *po;
1670 if (PyArg_ParseTuple(args, wformat, &po)) {
1671 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001672
Victor Stinner8c62be82010-05-06 00:08:46 +00001673 Py_BEGIN_ALLOW_THREADS
1674 /* PyUnicode_AS_UNICODE result OK without
1675 thread lock as it is a simple dereference. */
1676 res = wstatfunc(wpath, &st);
1677 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001678
Victor Stinner8c62be82010-05-06 00:08:46 +00001679 if (res != 0)
1680 return win32_error_unicode("stat", wpath);
1681 return _pystat_fromstructstat(&st);
1682 }
1683 /* Drop the argument parsing error as narrow strings
1684 are also valid. */
1685 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001686#endif
1687
Victor Stinner8c62be82010-05-06 00:08:46 +00001688 if (!PyArg_ParseTuple(args, format,
1689 PyUnicode_FSConverter, &opath))
1690 return NULL;
1691 path = PyBytes_AsString(opath);
1692 Py_BEGIN_ALLOW_THREADS
1693 res = (*statfunc)(path, &st);
1694 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001695
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001697#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001699#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001701#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 }
1703 else
1704 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001705
Victor Stinner8c62be82010-05-06 00:08:46 +00001706 Py_DECREF(opath);
1707 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001708}
1709
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001710/* POSIX methods */
1711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001712PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001713"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001714Use the real uid/gid to test for access to a path. Note that most\n\
1715operations will use the effective uid/gid, therefore this routine can\n\
1716be used in a suid/sgid environment to test if the invoking user has the\n\
1717specified access to the path. The mode argument can be F_OK to test\n\
1718existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001719
1720static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001721posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001722{
Victor Stinner8c62be82010-05-06 00:08:46 +00001723 PyObject *opath;
1724 char *path;
1725 int mode;
1726
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001727#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 DWORD attr;
1729 PyUnicodeObject *po;
1730 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1731 Py_BEGIN_ALLOW_THREADS
1732 /* PyUnicode_AS_UNICODE OK without thread lock as
1733 it is a simple dereference. */
1734 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1735 Py_END_ALLOW_THREADS
1736 goto finish;
1737 }
1738 /* Drop the argument parsing error as narrow strings
1739 are also valid. */
1740 PyErr_Clear();
1741 if (!PyArg_ParseTuple(args, "O&i:access",
1742 PyUnicode_FSConverter, &opath, &mode))
1743 return NULL;
1744 path = PyBytes_AsString(opath);
1745 Py_BEGIN_ALLOW_THREADS
1746 attr = GetFileAttributesA(path);
1747 Py_END_ALLOW_THREADS
1748 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001749finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001750 if (attr == 0xFFFFFFFF)
1751 /* File does not exist, or cannot read attributes */
1752 return PyBool_FromLong(0);
1753 /* Access is possible if either write access wasn't requested, or
1754 the file isn't read-only, or if it's a directory, as there are
1755 no read-only directories on Windows. */
1756 return PyBool_FromLong(!(mode & 2)
1757 || !(attr & FILE_ATTRIBUTE_READONLY)
1758 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001759#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 int res;
1761 if (!PyArg_ParseTuple(args, "O&i:access",
1762 PyUnicode_FSConverter, &opath, &mode))
1763 return NULL;
1764 path = PyBytes_AsString(opath);
1765 Py_BEGIN_ALLOW_THREADS
1766 res = access(path, mode);
1767 Py_END_ALLOW_THREADS
1768 Py_DECREF(opath);
1769 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001770#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001771}
1772
Guido van Rossumd371ff11999-01-25 16:12:23 +00001773#ifndef F_OK
1774#define F_OK 0
1775#endif
1776#ifndef R_OK
1777#define R_OK 4
1778#endif
1779#ifndef W_OK
1780#define W_OK 2
1781#endif
1782#ifndef X_OK
1783#define X_OK 1
1784#endif
1785
1786#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001787PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001788"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001790
1791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001792posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001793{
Victor Stinner8c62be82010-05-06 00:08:46 +00001794 int id;
1795 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001796
Victor Stinner8c62be82010-05-06 00:08:46 +00001797 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1798 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001799
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001800#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001801 /* file descriptor 0 only, the default input device (stdin) */
1802 if (id == 0) {
1803 ret = ttyname();
1804 }
1805 else {
1806 ret = NULL;
1807 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001808#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001809 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001810#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001811 if (ret == NULL)
1812 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001813 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001814}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001815#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001816
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001817#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001819"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001821
1822static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001823posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001824{
Victor Stinner8c62be82010-05-06 00:08:46 +00001825 char *ret;
1826 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001827
Greg Wardb48bc172000-03-01 21:51:56 +00001828#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001829 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001830#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001832#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 if (ret == NULL)
1834 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001835 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001836}
1837#endif
1838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001839PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001840"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Barry Warsaw53699e91996-12-10 23:23:01 +00001843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001844posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001847 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001848#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001849 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001850#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001851 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001852#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001853 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001854#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001855}
1856
Fred Drake4d1e64b2002-04-15 19:40:07 +00001857#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001859"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001860Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001862
1863static PyObject *
1864posix_fchdir(PyObject *self, PyObject *fdobj)
1865{
Victor Stinner8c62be82010-05-06 00:08:46 +00001866 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001867}
1868#endif /* HAVE_FCHDIR */
1869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001872"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001874
Barry Warsaw53699e91996-12-10 23:23:01 +00001875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001876posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001877{
Victor Stinner8c62be82010-05-06 00:08:46 +00001878 PyObject *opath = NULL;
1879 char *path = NULL;
1880 int i;
1881 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001882#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 DWORD attr;
1884 PyUnicodeObject *po;
1885 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1886 Py_BEGIN_ALLOW_THREADS
1887 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1888 if (attr != 0xFFFFFFFF) {
1889 if (i & _S_IWRITE)
1890 attr &= ~FILE_ATTRIBUTE_READONLY;
1891 else
1892 attr |= FILE_ATTRIBUTE_READONLY;
1893 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1894 }
1895 else
1896 res = 0;
1897 Py_END_ALLOW_THREADS
1898 if (!res)
1899 return win32_error_unicode("chmod",
1900 PyUnicode_AS_UNICODE(po));
1901 Py_INCREF(Py_None);
1902 return Py_None;
1903 }
1904 /* Drop the argument parsing error as narrow strings
1905 are also valid. */
1906 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001907
Victor Stinner8c62be82010-05-06 00:08:46 +00001908 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1909 &opath, &i))
1910 return NULL;
1911 path = PyBytes_AsString(opath);
1912 Py_BEGIN_ALLOW_THREADS
1913 attr = GetFileAttributesA(path);
1914 if (attr != 0xFFFFFFFF) {
1915 if (i & _S_IWRITE)
1916 attr &= ~FILE_ATTRIBUTE_READONLY;
1917 else
1918 attr |= FILE_ATTRIBUTE_READONLY;
1919 res = SetFileAttributesA(path, attr);
1920 }
1921 else
1922 res = 0;
1923 Py_END_ALLOW_THREADS
1924 if (!res) {
1925 win32_error("chmod", path);
1926 Py_DECREF(opath);
1927 return NULL;
1928 }
1929 Py_DECREF(opath);
1930 Py_INCREF(Py_None);
1931 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001932#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001933 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1934 &opath, &i))
1935 return NULL;
1936 path = PyBytes_AsString(opath);
1937 Py_BEGIN_ALLOW_THREADS
1938 res = chmod(path, i);
1939 Py_END_ALLOW_THREADS
1940 if (res < 0)
1941 return posix_error_with_allocated_filename(opath);
1942 Py_DECREF(opath);
1943 Py_INCREF(Py_None);
1944 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001945#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001946}
1947
Christian Heimes4e30a842007-11-30 22:12:06 +00001948#ifdef HAVE_FCHMOD
1949PyDoc_STRVAR(posix_fchmod__doc__,
1950"fchmod(fd, mode)\n\n\
1951Change the access permissions of the file given by file\n\
1952descriptor fd.");
1953
1954static PyObject *
1955posix_fchmod(PyObject *self, PyObject *args)
1956{
Victor Stinner8c62be82010-05-06 00:08:46 +00001957 int fd, mode, res;
1958 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1959 return NULL;
1960 Py_BEGIN_ALLOW_THREADS
1961 res = fchmod(fd, mode);
1962 Py_END_ALLOW_THREADS
1963 if (res < 0)
1964 return posix_error();
1965 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001966}
1967#endif /* HAVE_FCHMOD */
1968
1969#ifdef HAVE_LCHMOD
1970PyDoc_STRVAR(posix_lchmod__doc__,
1971"lchmod(path, mode)\n\n\
1972Change the access permissions of a file. If path is a symlink, this\n\
1973affects the link itself rather than the target.");
1974
1975static PyObject *
1976posix_lchmod(PyObject *self, PyObject *args)
1977{
Victor Stinner8c62be82010-05-06 00:08:46 +00001978 PyObject *opath;
1979 char *path;
1980 int i;
1981 int res;
1982 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1983 &opath, &i))
1984 return NULL;
1985 path = PyBytes_AsString(opath);
1986 Py_BEGIN_ALLOW_THREADS
1987 res = lchmod(path, i);
1988 Py_END_ALLOW_THREADS
1989 if (res < 0)
1990 return posix_error_with_allocated_filename(opath);
1991 Py_DECREF(opath);
1992 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001993}
1994#endif /* HAVE_LCHMOD */
1995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001996
Thomas Wouterscf297e42007-02-23 15:07:44 +00001997#ifdef HAVE_CHFLAGS
1998PyDoc_STRVAR(posix_chflags__doc__,
1999"chflags(path, flags)\n\n\
2000Set file flags.");
2001
2002static PyObject *
2003posix_chflags(PyObject *self, PyObject *args)
2004{
Victor Stinner8c62be82010-05-06 00:08:46 +00002005 PyObject *opath;
2006 char *path;
2007 unsigned long flags;
2008 int res;
2009 if (!PyArg_ParseTuple(args, "O&k:chflags",
2010 PyUnicode_FSConverter, &opath, &flags))
2011 return NULL;
2012 path = PyBytes_AsString(opath);
2013 Py_BEGIN_ALLOW_THREADS
2014 res = chflags(path, flags);
2015 Py_END_ALLOW_THREADS
2016 if (res < 0)
2017 return posix_error_with_allocated_filename(opath);
2018 Py_DECREF(opath);
2019 Py_INCREF(Py_None);
2020 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002021}
2022#endif /* HAVE_CHFLAGS */
2023
2024#ifdef HAVE_LCHFLAGS
2025PyDoc_STRVAR(posix_lchflags__doc__,
2026"lchflags(path, flags)\n\n\
2027Set file flags.\n\
2028This function will not follow symbolic links.");
2029
2030static PyObject *
2031posix_lchflags(PyObject *self, PyObject *args)
2032{
Victor Stinner8c62be82010-05-06 00:08:46 +00002033 PyObject *opath;
2034 char *path;
2035 unsigned long flags;
2036 int res;
2037 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2038 PyUnicode_FSConverter, &opath, &flags))
2039 return NULL;
2040 path = PyBytes_AsString(opath);
2041 Py_BEGIN_ALLOW_THREADS
2042 res = lchflags(path, flags);
2043 Py_END_ALLOW_THREADS
2044 if (res < 0)
2045 return posix_error_with_allocated_filename(opath);
2046 Py_DECREF(opath);
2047 Py_INCREF(Py_None);
2048 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002049}
2050#endif /* HAVE_LCHFLAGS */
2051
Martin v. Löwis244edc82001-10-04 22:44:26 +00002052#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002053PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002054"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002056
2057static PyObject *
2058posix_chroot(PyObject *self, PyObject *args)
2059{
Victor Stinner8c62be82010-05-06 00:08:46 +00002060 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002061}
2062#endif
2063
Guido van Rossum21142a01999-01-08 21:05:37 +00002064#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002066"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002068
2069static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002070posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002071{
Stefan Krah0e803b32010-11-26 16:16:47 +00002072 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002073}
2074#endif /* HAVE_FSYNC */
2075
2076#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002077
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002078#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002079extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2080#endif
2081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002083"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002084force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002086
2087static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002088posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002089{
Stefan Krah0e803b32010-11-26 16:16:47 +00002090 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002091}
2092#endif /* HAVE_FDATASYNC */
2093
2094
Fredrik Lundh10723342000-07-10 16:38:09 +00002095#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002096PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002097"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002098Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002099
Barry Warsaw53699e91996-12-10 23:23:01 +00002100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002101posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002102{
Victor Stinner8c62be82010-05-06 00:08:46 +00002103 PyObject *opath;
2104 char *path;
2105 long uid, gid;
2106 int res;
2107 if (!PyArg_ParseTuple(args, "O&ll:chown",
2108 PyUnicode_FSConverter, &opath,
2109 &uid, &gid))
2110 return NULL;
2111 path = PyBytes_AsString(opath);
2112 Py_BEGIN_ALLOW_THREADS
2113 res = chown(path, (uid_t) uid, (gid_t) gid);
2114 Py_END_ALLOW_THREADS
2115 if (res < 0)
2116 return posix_error_with_allocated_filename(opath);
2117 Py_DECREF(opath);
2118 Py_INCREF(Py_None);
2119 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002120}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002121#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002122
Christian Heimes4e30a842007-11-30 22:12:06 +00002123#ifdef HAVE_FCHOWN
2124PyDoc_STRVAR(posix_fchown__doc__,
2125"fchown(fd, uid, gid)\n\n\
2126Change the owner and group id of the file given by file descriptor\n\
2127fd to the numeric uid and gid.");
2128
2129static PyObject *
2130posix_fchown(PyObject *self, PyObject *args)
2131{
Victor Stinner8c62be82010-05-06 00:08:46 +00002132 int fd;
2133 long uid, gid;
2134 int res;
2135 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2136 return NULL;
2137 Py_BEGIN_ALLOW_THREADS
2138 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2139 Py_END_ALLOW_THREADS
2140 if (res < 0)
2141 return posix_error();
2142 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002143}
2144#endif /* HAVE_FCHOWN */
2145
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002146#ifdef HAVE_LCHOWN
2147PyDoc_STRVAR(posix_lchown__doc__,
2148"lchown(path, uid, gid)\n\n\
2149Change the owner and group id of path to the numeric uid and gid.\n\
2150This function will not follow symbolic links.");
2151
2152static PyObject *
2153posix_lchown(PyObject *self, PyObject *args)
2154{
Victor Stinner8c62be82010-05-06 00:08:46 +00002155 PyObject *opath;
2156 char *path;
2157 long uid, gid;
2158 int res;
2159 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2160 PyUnicode_FSConverter, &opath,
2161 &uid, &gid))
2162 return NULL;
2163 path = PyBytes_AsString(opath);
2164 Py_BEGIN_ALLOW_THREADS
2165 res = lchown(path, (uid_t) uid, (gid_t) gid);
2166 Py_END_ALLOW_THREADS
2167 if (res < 0)
2168 return posix_error_with_allocated_filename(opath);
2169 Py_DECREF(opath);
2170 Py_INCREF(Py_None);
2171 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002172}
2173#endif /* HAVE_LCHOWN */
2174
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002175
Guido van Rossum36bc6801995-06-14 22:54:23 +00002176#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002177static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002178posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002179{
Victor Stinner8c62be82010-05-06 00:08:46 +00002180 char buf[1026];
2181 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002182
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002183#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002184 if (!use_bytes) {
2185 wchar_t wbuf[1026];
2186 wchar_t *wbuf2 = wbuf;
2187 PyObject *resobj;
2188 DWORD len;
2189 Py_BEGIN_ALLOW_THREADS
2190 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2191 /* If the buffer is large enough, len does not include the
2192 terminating \0. If the buffer is too small, len includes
2193 the space needed for the terminator. */
2194 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2195 wbuf2 = malloc(len * sizeof(wchar_t));
2196 if (wbuf2)
2197 len = GetCurrentDirectoryW(len, wbuf2);
2198 }
2199 Py_END_ALLOW_THREADS
2200 if (!wbuf2) {
2201 PyErr_NoMemory();
2202 return NULL;
2203 }
2204 if (!len) {
2205 if (wbuf2 != wbuf) free(wbuf2);
2206 return win32_error("getcwdu", NULL);
2207 }
2208 resobj = PyUnicode_FromWideChar(wbuf2, len);
2209 if (wbuf2 != wbuf) free(wbuf2);
2210 return resobj;
2211 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002212#endif
2213
Victor Stinner8c62be82010-05-06 00:08:46 +00002214 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002215#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002216 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002217#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002218 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002219#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002220 Py_END_ALLOW_THREADS
2221 if (res == NULL)
2222 return posix_error();
2223 if (use_bytes)
2224 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002225 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002226}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002227
2228PyDoc_STRVAR(posix_getcwd__doc__,
2229"getcwd() -> path\n\n\
2230Return a unicode string representing the current working directory.");
2231
2232static PyObject *
2233posix_getcwd_unicode(PyObject *self)
2234{
2235 return posix_getcwd(0);
2236}
2237
2238PyDoc_STRVAR(posix_getcwdb__doc__,
2239"getcwdb() -> path\n\n\
2240Return a bytes string representing the current working directory.");
2241
2242static PyObject *
2243posix_getcwd_bytes(PyObject *self)
2244{
2245 return posix_getcwd(1);
2246}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002247#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002248
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002249
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002251PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002252"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002253Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002254
Barry Warsaw53699e91996-12-10 23:23:01 +00002255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002256posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002257{
Victor Stinner8c62be82010-05-06 00:08:46 +00002258 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002259}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002260#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002261
Brian Curtin1b9df392010-11-24 20:24:31 +00002262#ifdef MS_WINDOWS
2263PyDoc_STRVAR(win32_link__doc__,
2264"link(src, dst)\n\n\
2265Create a hard link to a file.");
2266
2267static PyObject *
2268win32_link(PyObject *self, PyObject *args)
2269{
2270 PyObject *osrc, *odst;
2271 char *src, *dst;
2272 BOOL rslt;
2273
Brian Curtinfc889c42010-11-28 23:59:46 +00002274 PyUnicodeObject *usrc, *udst;
2275 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2276 Py_BEGIN_ALLOW_THREADS
2277 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2278 PyUnicode_AS_UNICODE(usrc), NULL);
2279 Py_END_ALLOW_THREADS
2280
2281 if (rslt == 0)
2282 return win32_error("link", NULL);
2283
2284 Py_RETURN_NONE;
2285 }
2286
2287 /* Narrow strings also valid. */
2288 PyErr_Clear();
2289
Brian Curtin1b9df392010-11-24 20:24:31 +00002290 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2291 PyUnicode_FSConverter, &odst))
2292 return NULL;
2293
2294 src = PyBytes_AsString(osrc);
2295 dst = PyBytes_AsString(odst);
2296
2297 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002298 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002299 Py_END_ALLOW_THREADS
2300
Stefan Krah30b341f2010-11-27 11:44:18 +00002301 Py_DECREF(osrc);
2302 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002303 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002304 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002305
2306 Py_RETURN_NONE;
2307}
2308#endif /* MS_WINDOWS */
2309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002311PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002312"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002313Return a list containing the names of the entries in the directory.\n\
2314\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002315 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002316\n\
2317The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002318entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002319
Barry Warsaw53699e91996-12-10 23:23:01 +00002320static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002321posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002322{
Victor Stinner8c62be82010-05-06 00:08:46 +00002323 /* XXX Should redo this putting the (now four) versions of opendir
2324 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002325#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002326
Victor Stinner8c62be82010-05-06 00:08:46 +00002327 PyObject *d, *v;
2328 HANDLE hFindFile;
2329 BOOL result;
2330 WIN32_FIND_DATA FileData;
2331 PyObject *opath;
2332 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2333 char *bufptr = namebuf;
2334 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002335
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002336 PyObject *po = NULL;
2337 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002338 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002339 Py_UNICODE *wnamebuf, *po_wchars;
2340
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002341 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002342 po_wchars = L".";
2343 len = 1;
2344 } else {
2345 po_wchars = PyUnicode_AS_UNICODE(po);
2346 len = PyUnicode_GET_SIZE(po);
2347 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002348 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002349 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2350 if (!wnamebuf) {
2351 PyErr_NoMemory();
2352 return NULL;
2353 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002354 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002355 if (len > 0) {
2356 Py_UNICODE wch = wnamebuf[len-1];
2357 if (wch != L'/' && wch != L'\\' && wch != L':')
2358 wnamebuf[len++] = L'\\';
2359 wcscpy(wnamebuf + len, L"*.*");
2360 }
2361 if ((d = PyList_New(0)) == NULL) {
2362 free(wnamebuf);
2363 return NULL;
2364 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002365 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002367 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 if (hFindFile == INVALID_HANDLE_VALUE) {
2369 int error = GetLastError();
2370 if (error == ERROR_FILE_NOT_FOUND) {
2371 free(wnamebuf);
2372 return d;
2373 }
2374 Py_DECREF(d);
2375 win32_error_unicode("FindFirstFileW", wnamebuf);
2376 free(wnamebuf);
2377 return NULL;
2378 }
2379 do {
2380 /* Skip over . and .. */
2381 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2382 wcscmp(wFileData.cFileName, L"..") != 0) {
2383 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2384 if (v == NULL) {
2385 Py_DECREF(d);
2386 d = NULL;
2387 break;
2388 }
2389 if (PyList_Append(d, v) != 0) {
2390 Py_DECREF(v);
2391 Py_DECREF(d);
2392 d = NULL;
2393 break;
2394 }
2395 Py_DECREF(v);
2396 }
2397 Py_BEGIN_ALLOW_THREADS
2398 result = FindNextFileW(hFindFile, &wFileData);
2399 Py_END_ALLOW_THREADS
2400 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2401 it got to the end of the directory. */
2402 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2403 Py_DECREF(d);
2404 win32_error_unicode("FindNextFileW", wnamebuf);
2405 FindClose(hFindFile);
2406 free(wnamebuf);
2407 return NULL;
2408 }
2409 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002410
Victor Stinner8c62be82010-05-06 00:08:46 +00002411 if (FindClose(hFindFile) == FALSE) {
2412 Py_DECREF(d);
2413 win32_error_unicode("FindClose", wnamebuf);
2414 free(wnamebuf);
2415 return NULL;
2416 }
2417 free(wnamebuf);
2418 return d;
2419 }
2420 /* Drop the argument parsing error as narrow strings
2421 are also valid. */
2422 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002423
Victor Stinner8c62be82010-05-06 00:08:46 +00002424 if (!PyArg_ParseTuple(args, "O&:listdir",
2425 PyUnicode_FSConverter, &opath))
2426 return NULL;
2427 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2428 PyErr_SetString(PyExc_ValueError, "path too long");
2429 Py_DECREF(opath);
2430 return NULL;
2431 }
2432 strcpy(namebuf, PyBytes_AsString(opath));
2433 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002434 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002435 if (len > 0) {
2436 char ch = namebuf[len-1];
2437 if (ch != SEP && ch != ALTSEP && ch != ':')
2438 namebuf[len++] = '/';
2439 strcpy(namebuf + len, "*.*");
2440 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002441
Victor Stinner8c62be82010-05-06 00:08:46 +00002442 if ((d = PyList_New(0)) == NULL)
2443 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002444
Antoine Pitroub73caab2010-08-09 23:39:31 +00002445 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002446 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002447 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002448 if (hFindFile == INVALID_HANDLE_VALUE) {
2449 int error = GetLastError();
2450 if (error == ERROR_FILE_NOT_FOUND)
2451 return d;
2452 Py_DECREF(d);
2453 return win32_error("FindFirstFile", namebuf);
2454 }
2455 do {
2456 /* Skip over . and .. */
2457 if (strcmp(FileData.cFileName, ".") != 0 &&
2458 strcmp(FileData.cFileName, "..") != 0) {
2459 v = PyBytes_FromString(FileData.cFileName);
2460 if (v == NULL) {
2461 Py_DECREF(d);
2462 d = NULL;
2463 break;
2464 }
2465 if (PyList_Append(d, v) != 0) {
2466 Py_DECREF(v);
2467 Py_DECREF(d);
2468 d = NULL;
2469 break;
2470 }
2471 Py_DECREF(v);
2472 }
2473 Py_BEGIN_ALLOW_THREADS
2474 result = FindNextFile(hFindFile, &FileData);
2475 Py_END_ALLOW_THREADS
2476 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2477 it got to the end of the directory. */
2478 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2479 Py_DECREF(d);
2480 win32_error("FindNextFile", namebuf);
2481 FindClose(hFindFile);
2482 return NULL;
2483 }
2484 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002485
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 if (FindClose(hFindFile) == FALSE) {
2487 Py_DECREF(d);
2488 return win32_error("FindClose", namebuf);
2489 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002490
Victor Stinner8c62be82010-05-06 00:08:46 +00002491 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002492
Tim Peters0bb44a42000-09-15 07:44:49 +00002493#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002494
2495#ifndef MAX_PATH
2496#define MAX_PATH CCHMAXPATH
2497#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002498 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002499 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002500 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002501 PyObject *d, *v;
2502 char namebuf[MAX_PATH+5];
2503 HDIR hdir = 1;
2504 ULONG srchcnt = 1;
2505 FILEFINDBUF3 ep;
2506 APIRET rc;
2507
Victor Stinner8c62be82010-05-06 00:08:46 +00002508 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002509 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002510 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002511 name = PyBytes_AsString(oname);
2512 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002513 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002514 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002515 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002516 return NULL;
2517 }
2518 strcpy(namebuf, name);
2519 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002520 if (*pt == ALTSEP)
2521 *pt = SEP;
2522 if (namebuf[len-1] != SEP)
2523 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002524 strcpy(namebuf + len, "*.*");
2525
Neal Norwitz6c913782007-10-14 03:23:09 +00002526 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002527 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002528 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002529 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002530
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002531 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2532 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002533 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002534 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2535 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2536 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002537
2538 if (rc != NO_ERROR) {
2539 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002540 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002541 }
2542
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002543 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002544 do {
2545 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002546 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002547 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002548
2549 strcpy(namebuf, ep.achName);
2550
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002551 /* Leave Case of Name Alone -- In Native Form */
2552 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002553
Christian Heimes72b710a2008-05-26 13:28:38 +00002554 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002555 if (v == NULL) {
2556 Py_DECREF(d);
2557 d = NULL;
2558 break;
2559 }
2560 if (PyList_Append(d, v) != 0) {
2561 Py_DECREF(v);
2562 Py_DECREF(d);
2563 d = NULL;
2564 break;
2565 }
2566 Py_DECREF(v);
2567 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2568 }
2569
Victor Stinnerdcb24032010-04-22 12:08:36 +00002570 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002571 return d;
2572#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002573 PyObject *oname;
2574 char *name;
2575 PyObject *d, *v;
2576 DIR *dirp;
2577 struct dirent *ep;
2578 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002579
Victor Stinner8c62be82010-05-06 00:08:46 +00002580 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002581 /* v is never read, so it does not need to be initialized yet. */
2582 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 arg_is_unicode = 0;
2584 PyErr_Clear();
2585 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002586 oname = NULL;
2587 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002588 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002589 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002590 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002591 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002592 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002593 Py_BEGIN_ALLOW_THREADS
2594 dirp = opendir(name);
2595 Py_END_ALLOW_THREADS
2596 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002597 return posix_error_with_allocated_filename(oname);
2598 }
2599 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002600 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002601 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002602 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002603 Py_DECREF(oname);
2604 return NULL;
2605 }
2606 for (;;) {
2607 errno = 0;
2608 Py_BEGIN_ALLOW_THREADS
2609 ep = readdir(dirp);
2610 Py_END_ALLOW_THREADS
2611 if (ep == NULL) {
2612 if (errno == 0) {
2613 break;
2614 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002615 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002616 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002617 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002618 Py_DECREF(d);
2619 return posix_error_with_allocated_filename(oname);
2620 }
2621 }
2622 if (ep->d_name[0] == '.' &&
2623 (NAMLEN(ep) == 1 ||
2624 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2625 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002626 if (arg_is_unicode)
2627 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2628 else
2629 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002630 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002631 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 break;
2633 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 if (PyList_Append(d, v) != 0) {
2635 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002636 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002637 break;
2638 }
2639 Py_DECREF(v);
2640 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002641 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002642 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002643 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002645
Victor Stinner8c62be82010-05-06 00:08:46 +00002646 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002647
Tim Peters0bb44a42000-09-15 07:44:49 +00002648#endif /* which OS */
2649} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002650
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002651#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002652/* A helper function for abspath on win32 */
2653static PyObject *
2654posix__getfullpathname(PyObject *self, PyObject *args)
2655{
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 PyObject *opath;
2657 char *path;
2658 char outbuf[MAX_PATH*2];
2659 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002660#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002661 PyUnicodeObject *po;
2662 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2663 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2664 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2665 Py_UNICODE *wtemp;
2666 DWORD result;
2667 PyObject *v;
2668 result = GetFullPathNameW(wpath,
2669 sizeof(woutbuf)/sizeof(woutbuf[0]),
2670 woutbuf, &wtemp);
2671 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2672 woutbufp = malloc(result * sizeof(Py_UNICODE));
2673 if (!woutbufp)
2674 return PyErr_NoMemory();
2675 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2676 }
2677 if (result)
2678 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2679 else
2680 v = win32_error_unicode("GetFullPathNameW", wpath);
2681 if (woutbufp != woutbuf)
2682 free(woutbufp);
2683 return v;
2684 }
2685 /* Drop the argument parsing error as narrow strings
2686 are also valid. */
2687 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002688
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002689#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002690 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2691 PyUnicode_FSConverter, &opath))
2692 return NULL;
2693 path = PyBytes_AsString(opath);
2694 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2695 outbuf, &temp)) {
2696 win32_error("GetFullPathName", path);
2697 Py_DECREF(opath);
2698 return NULL;
2699 }
2700 Py_DECREF(opath);
2701 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2702 return PyUnicode_Decode(outbuf, strlen(outbuf),
2703 Py_FileSystemDefaultEncoding, NULL);
2704 }
2705 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002706} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002707
Brian Curtinf5e76d02010-11-24 13:14:05 +00002708/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2709static int has_GetFinalPathNameByHandle = 0;
2710static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2711 DWORD);
2712static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2713 DWORD);
2714static int
2715check_GetFinalPathNameByHandle()
2716{
2717 HINSTANCE hKernel32;
2718 /* only recheck */
2719 if (!has_GetFinalPathNameByHandle)
2720 {
2721 hKernel32 = GetModuleHandle("KERNEL32");
2722 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2723 "GetFinalPathNameByHandleA");
2724 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2725 "GetFinalPathNameByHandleW");
2726 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2727 Py_GetFinalPathNameByHandleW;
2728 }
2729 return has_GetFinalPathNameByHandle;
2730}
2731
Brian Curtind40e6f72010-07-08 21:39:08 +00002732/* A helper function for samepath on windows */
2733static PyObject *
2734posix__getfinalpathname(PyObject *self, PyObject *args)
2735{
2736 HANDLE hFile;
2737 int buf_size;
2738 wchar_t *target_path;
2739 int result_length;
2740 PyObject *result;
2741 wchar_t *path;
2742
Brian Curtin94622b02010-09-24 00:03:39 +00002743 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002744 return NULL;
2745 }
2746
2747 if(!check_GetFinalPathNameByHandle()) {
2748 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2749 NotImplementedError. */
2750 return PyErr_Format(PyExc_NotImplementedError,
2751 "GetFinalPathNameByHandle not available on this platform");
2752 }
2753
2754 hFile = CreateFileW(
2755 path,
2756 0, /* desired access */
2757 0, /* share mode */
2758 NULL, /* security attributes */
2759 OPEN_EXISTING,
2760 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2761 FILE_FLAG_BACKUP_SEMANTICS,
2762 NULL);
2763
2764 if(hFile == INVALID_HANDLE_VALUE) {
2765 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002766 return PyErr_Format(PyExc_RuntimeError,
2767 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002768 }
2769
2770 /* We have a good handle to the target, use it to determine the
2771 target path name. */
2772 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2773
2774 if(!buf_size)
2775 return win32_error_unicode("GetFinalPathNameByHandle", path);
2776
2777 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2778 if(!target_path)
2779 return PyErr_NoMemory();
2780
2781 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2782 buf_size, VOLUME_NAME_DOS);
2783 if(!result_length)
2784 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2785
2786 if(!CloseHandle(hFile))
2787 return win32_error_unicode("GetFinalPathNameByHandle", path);
2788
2789 target_path[result_length] = 0;
2790 result = PyUnicode_FromUnicode(target_path, result_length);
2791 free(target_path);
2792 return result;
2793
2794} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002795
2796static PyObject *
2797posix__getfileinformation(PyObject *self, PyObject *args)
2798{
2799 HANDLE hFile;
2800 BY_HANDLE_FILE_INFORMATION info;
2801 int fd;
2802
2803 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2804 return NULL;
2805
2806 if (!_PyVerify_fd(fd)) {
2807 PyErr_SetString(PyExc_ValueError, "received invalid file descriptor");
2808 return NULL;
2809 }
2810
2811 hFile = (HANDLE)_get_osfhandle(fd);
2812 if (hFile == INVALID_HANDLE_VALUE)
2813 return win32_error("_getfileinformation", NULL);
2814
2815 if (!GetFileInformationByHandle(hFile, &info))
2816 return win32_error("_getfileinformation", NULL);
2817
2818 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2819 info.nFileIndexHigh,
2820 info.nFileIndexLow);
2821}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002822#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002823
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002824PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002825"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002826Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002827
Barry Warsaw53699e91996-12-10 23:23:01 +00002828static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002829posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002830{
Victor Stinner8c62be82010-05-06 00:08:46 +00002831 int res;
2832 PyObject *opath;
2833 char *path;
2834 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002835
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002836#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002837 PyUnicodeObject *po;
2838 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2839 Py_BEGIN_ALLOW_THREADS
2840 /* PyUnicode_AS_UNICODE OK without thread lock as
2841 it is a simple dereference. */
2842 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2843 Py_END_ALLOW_THREADS
2844 if (!res)
2845 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2846 Py_INCREF(Py_None);
2847 return Py_None;
2848 }
2849 /* Drop the argument parsing error as narrow strings
2850 are also valid. */
2851 PyErr_Clear();
2852 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2853 PyUnicode_FSConverter, &opath, &mode))
2854 return NULL;
2855 path = PyBytes_AsString(opath);
2856 Py_BEGIN_ALLOW_THREADS
2857 /* PyUnicode_AS_UNICODE OK without thread lock as
2858 it is a simple dereference. */
2859 res = CreateDirectoryA(path, NULL);
2860 Py_END_ALLOW_THREADS
2861 if (!res) {
2862 win32_error("mkdir", path);
2863 Py_DECREF(opath);
2864 return NULL;
2865 }
2866 Py_DECREF(opath);
2867 Py_INCREF(Py_None);
2868 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002869#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002870
Victor Stinner8c62be82010-05-06 00:08:46 +00002871 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2872 PyUnicode_FSConverter, &opath, &mode))
2873 return NULL;
2874 path = PyBytes_AsString(opath);
2875 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002876#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002877 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002878#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002880#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002881 Py_END_ALLOW_THREADS
2882 if (res < 0)
2883 return posix_error_with_allocated_filename(opath);
2884 Py_DECREF(opath);
2885 Py_INCREF(Py_None);
2886 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002887#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002888}
2889
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002890
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002891/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2892#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002893#include <sys/resource.h>
2894#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002895
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002896
2897#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002898PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002899"nice(inc) -> new_priority\n\n\
2900Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002901
Barry Warsaw53699e91996-12-10 23:23:01 +00002902static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002903posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002904{
Victor Stinner8c62be82010-05-06 00:08:46 +00002905 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002906
Victor Stinner8c62be82010-05-06 00:08:46 +00002907 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2908 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002909
Victor Stinner8c62be82010-05-06 00:08:46 +00002910 /* There are two flavours of 'nice': one that returns the new
2911 priority (as required by almost all standards out there) and the
2912 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2913 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002914
Victor Stinner8c62be82010-05-06 00:08:46 +00002915 If we are of the nice family that returns the new priority, we
2916 need to clear errno before the call, and check if errno is filled
2917 before calling posix_error() on a returnvalue of -1, because the
2918 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002919
Victor Stinner8c62be82010-05-06 00:08:46 +00002920 errno = 0;
2921 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002922#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002923 if (value == 0)
2924 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002925#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002926 if (value == -1 && errno != 0)
2927 /* either nice() or getpriority() returned an error */
2928 return posix_error();
2929 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002930}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002931#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002932
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002933PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002934"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002935Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002936
Barry Warsaw53699e91996-12-10 23:23:01 +00002937static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002938posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002939{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002940#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002941 PyObject *o1, *o2;
2942 char *p1, *p2;
2943 BOOL result;
2944 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2945 goto error;
2946 if (!convert_to_unicode(&o1))
2947 goto error;
2948 if (!convert_to_unicode(&o2)) {
2949 Py_DECREF(o1);
2950 goto error;
2951 }
2952 Py_BEGIN_ALLOW_THREADS
2953 result = MoveFileW(PyUnicode_AsUnicode(o1),
2954 PyUnicode_AsUnicode(o2));
2955 Py_END_ALLOW_THREADS
2956 Py_DECREF(o1);
2957 Py_DECREF(o2);
2958 if (!result)
2959 return win32_error("rename", NULL);
2960 Py_INCREF(Py_None);
2961 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002962error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002963 PyErr_Clear();
2964 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2965 return NULL;
2966 Py_BEGIN_ALLOW_THREADS
2967 result = MoveFileA(p1, p2);
2968 Py_END_ALLOW_THREADS
2969 if (!result)
2970 return win32_error("rename", NULL);
2971 Py_INCREF(Py_None);
2972 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002973#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002974 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002975#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002976}
2977
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002979PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002980"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002981Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002982
Barry Warsaw53699e91996-12-10 23:23:01 +00002983static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002984posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002985{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002986#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002987 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002988#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002989 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002990#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002991}
2992
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002994PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002995"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002996Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002997
Barry Warsaw53699e91996-12-10 23:23:01 +00002998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002999posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003000{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003001#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003002 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003003#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003004 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003005#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003006}
3007
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003008
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003009#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003011"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003013
Barry Warsaw53699e91996-12-10 23:23:01 +00003014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003015posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003016{
Victor Stinner8c62be82010-05-06 00:08:46 +00003017 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003018#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003019 wchar_t *command;
3020 if (!PyArg_ParseTuple(args, "u:system", &command))
3021 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003022
Victor Stinner8c62be82010-05-06 00:08:46 +00003023 Py_BEGIN_ALLOW_THREADS
3024 sts = _wsystem(command);
3025 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003026#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003027 PyObject *command_obj;
3028 char *command;
3029 if (!PyArg_ParseTuple(args, "O&:system",
3030 PyUnicode_FSConverter, &command_obj))
3031 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003032
Victor Stinner8c62be82010-05-06 00:08:46 +00003033 command = PyBytes_AsString(command_obj);
3034 Py_BEGIN_ALLOW_THREADS
3035 sts = system(command);
3036 Py_END_ALLOW_THREADS
3037 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003038#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003039 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003040}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003041#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003042
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003044PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003045"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003046Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003047
Barry Warsaw53699e91996-12-10 23:23:01 +00003048static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003049posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003050{
Victor Stinner8c62be82010-05-06 00:08:46 +00003051 int i;
3052 if (!PyArg_ParseTuple(args, "i:umask", &i))
3053 return NULL;
3054 i = (int)umask(i);
3055 if (i < 0)
3056 return posix_error();
3057 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003058}
3059
Brian Curtind40e6f72010-07-08 21:39:08 +00003060#ifdef MS_WINDOWS
3061
3062/* override the default DeleteFileW behavior so that directory
3063symlinks can be removed with this function, the same as with
3064Unix symlinks */
3065BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3066{
3067 WIN32_FILE_ATTRIBUTE_DATA info;
3068 WIN32_FIND_DATAW find_data;
3069 HANDLE find_data_handle;
3070 int is_directory = 0;
3071 int is_link = 0;
3072
3073 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3074 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3075
3076 /* Get WIN32_FIND_DATA structure for the path to determine if
3077 it is a symlink */
3078 if(is_directory &&
3079 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3080 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3081
3082 if(find_data_handle != INVALID_HANDLE_VALUE) {
3083 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3084 FindClose(find_data_handle);
3085 }
3086 }
3087 }
3088
3089 if (is_directory && is_link)
3090 return RemoveDirectoryW(lpFileName);
3091
3092 return DeleteFileW(lpFileName);
3093}
3094#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003097"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003101"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003102Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003103
Barry Warsaw53699e91996-12-10 23:23:01 +00003104static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003105posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003106{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003107#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003108 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3109 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003110#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003111 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003112#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003113}
3114
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003115
Guido van Rossumb6775db1994-08-01 11:34:53 +00003116#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003117PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003118"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003119Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003120
Barry Warsaw53699e91996-12-10 23:23:01 +00003121static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003122posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003123{
Victor Stinner8c62be82010-05-06 00:08:46 +00003124 struct utsname u;
3125 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003126
Victor Stinner8c62be82010-05-06 00:08:46 +00003127 Py_BEGIN_ALLOW_THREADS
3128 res = uname(&u);
3129 Py_END_ALLOW_THREADS
3130 if (res < 0)
3131 return posix_error();
3132 return Py_BuildValue("(sssss)",
3133 u.sysname,
3134 u.nodename,
3135 u.release,
3136 u.version,
3137 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003138}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003139#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003140
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003141static int
3142extract_time(PyObject *t, long* sec, long* usec)
3143{
Victor Stinner8c62be82010-05-06 00:08:46 +00003144 long intval;
3145 if (PyFloat_Check(t)) {
3146 double tval = PyFloat_AsDouble(t);
3147 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
3148 if (!intobj)
3149 return -1;
3150 intval = PyLong_AsLong(intobj);
3151 Py_DECREF(intobj);
3152 if (intval == -1 && PyErr_Occurred())
3153 return -1;
3154 *sec = intval;
3155 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3156 if (*usec < 0)
3157 /* If rounding gave us a negative number,
3158 truncate. */
3159 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003160 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003161 }
3162 intval = PyLong_AsLong(t);
3163 if (intval == -1 && PyErr_Occurred())
3164 return -1;
3165 *sec = intval;
3166 *usec = 0;
3167 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003168}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003169
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003170PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003171"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003172utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003173Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003174second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003175
Barry Warsaw53699e91996-12-10 23:23:01 +00003176static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003177posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003178{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003179#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003180 PyObject *arg;
3181 PyUnicodeObject *obwpath;
3182 wchar_t *wpath = NULL;
3183 PyObject *oapath;
3184 char *apath;
3185 HANDLE hFile;
3186 long atimesec, mtimesec, ausec, musec;
3187 FILETIME atime, mtime;
3188 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003189
Victor Stinner8c62be82010-05-06 00:08:46 +00003190 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3191 wpath = PyUnicode_AS_UNICODE(obwpath);
3192 Py_BEGIN_ALLOW_THREADS
3193 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3194 NULL, OPEN_EXISTING,
3195 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3196 Py_END_ALLOW_THREADS
3197 if (hFile == INVALID_HANDLE_VALUE)
3198 return win32_error_unicode("utime", wpath);
3199 } else
3200 /* Drop the argument parsing error as narrow strings
3201 are also valid. */
3202 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003203
Victor Stinner8c62be82010-05-06 00:08:46 +00003204 if (!wpath) {
3205 if (!PyArg_ParseTuple(args, "O&O:utime",
3206 PyUnicode_FSConverter, &oapath, &arg))
3207 return NULL;
3208 apath = PyBytes_AsString(oapath);
3209 Py_BEGIN_ALLOW_THREADS
3210 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3211 NULL, OPEN_EXISTING,
3212 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3213 Py_END_ALLOW_THREADS
3214 if (hFile == INVALID_HANDLE_VALUE) {
3215 win32_error("utime", apath);
3216 Py_DECREF(oapath);
3217 return NULL;
3218 }
3219 Py_DECREF(oapath);
3220 }
3221
3222 if (arg == Py_None) {
3223 SYSTEMTIME now;
3224 GetSystemTime(&now);
3225 if (!SystemTimeToFileTime(&now, &mtime) ||
3226 !SystemTimeToFileTime(&now, &atime)) {
3227 win32_error("utime", NULL);
3228 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003229 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003230 }
3231 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3232 PyErr_SetString(PyExc_TypeError,
3233 "utime() arg 2 must be a tuple (atime, mtime)");
3234 goto done;
3235 }
3236 else {
3237 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3238 &atimesec, &ausec) == -1)
3239 goto done;
3240 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3241 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3242 &mtimesec, &musec) == -1)
3243 goto done;
3244 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3245 }
3246 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3247 /* Avoid putting the file name into the error here,
3248 as that may confuse the user into believing that
3249 something is wrong with the file, when it also
3250 could be the time stamp that gives a problem. */
3251 win32_error("utime", NULL);
3252 }
3253 Py_INCREF(Py_None);
3254 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003255done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003256 CloseHandle(hFile);
3257 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003258#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003259
Victor Stinner8c62be82010-05-06 00:08:46 +00003260 PyObject *opath;
3261 char *path;
3262 long atime, mtime, ausec, musec;
3263 int res;
3264 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003265
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003266#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003267 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003268#define ATIME buf[0].tv_sec
3269#define MTIME buf[1].tv_sec
3270#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003271/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003272 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003273#define ATIME buf.actime
3274#define MTIME buf.modtime
3275#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003276#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003277 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003278#define ATIME buf[0]
3279#define MTIME buf[1]
3280#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003281#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003282
Mark Hammond817c9292003-12-03 01:22:38 +00003283
Victor Stinner8c62be82010-05-06 00:08:46 +00003284 if (!PyArg_ParseTuple(args, "O&O:utime",
3285 PyUnicode_FSConverter, &opath, &arg))
3286 return NULL;
3287 path = PyBytes_AsString(opath);
3288 if (arg == Py_None) {
3289 /* optional time values not given */
3290 Py_BEGIN_ALLOW_THREADS
3291 res = utime(path, NULL);
3292 Py_END_ALLOW_THREADS
3293 }
3294 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3295 PyErr_SetString(PyExc_TypeError,
3296 "utime() arg 2 must be a tuple (atime, mtime)");
3297 Py_DECREF(opath);
3298 return NULL;
3299 }
3300 else {
3301 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3302 &atime, &ausec) == -1) {
3303 Py_DECREF(opath);
3304 return NULL;
3305 }
3306 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3307 &mtime, &musec) == -1) {
3308 Py_DECREF(opath);
3309 return NULL;
3310 }
3311 ATIME = atime;
3312 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003313#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 buf[0].tv_usec = ausec;
3315 buf[1].tv_usec = musec;
3316 Py_BEGIN_ALLOW_THREADS
3317 res = utimes(path, buf);
3318 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003319#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003320 Py_BEGIN_ALLOW_THREADS
3321 res = utime(path, UTIME_ARG);
3322 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003323#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 }
3325 if (res < 0) {
3326 return posix_error_with_allocated_filename(opath);
3327 }
3328 Py_DECREF(opath);
3329 Py_INCREF(Py_None);
3330 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003331#undef UTIME_ARG
3332#undef ATIME
3333#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003334#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003335}
3336
Guido van Rossum85e3b011991-06-03 12:42:10 +00003337
Guido van Rossum3b066191991-06-04 19:40:25 +00003338/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003339
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003340PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003341"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003342Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003343
Barry Warsaw53699e91996-12-10 23:23:01 +00003344static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003345posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003346{
Victor Stinner8c62be82010-05-06 00:08:46 +00003347 int sts;
3348 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3349 return NULL;
3350 _exit(sts);
3351 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003352}
3353
Martin v. Löwis114619e2002-10-07 06:44:21 +00003354#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3355static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003356free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003357{
Victor Stinner8c62be82010-05-06 00:08:46 +00003358 Py_ssize_t i;
3359 for (i = 0; i < count; i++)
3360 PyMem_Free(array[i]);
3361 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003362}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003363
Antoine Pitrou69f71142009-05-24 21:25:49 +00003364static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003365int fsconvert_strdup(PyObject *o, char**out)
3366{
Victor Stinner8c62be82010-05-06 00:08:46 +00003367 PyObject *bytes;
3368 Py_ssize_t size;
3369 if (!PyUnicode_FSConverter(o, &bytes))
3370 return 0;
3371 size = PyBytes_GET_SIZE(bytes);
3372 *out = PyMem_Malloc(size+1);
3373 if (!*out)
3374 return 0;
3375 memcpy(*out, PyBytes_AsString(bytes), size+1);
3376 Py_DECREF(bytes);
3377 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003378}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003379#endif
3380
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003381
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003382#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003383PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003384"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003385Execute an executable path with arguments, replacing current process.\n\
3386\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003387 path: path of executable file\n\
3388 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003389
Barry Warsaw53699e91996-12-10 23:23:01 +00003390static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003391posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003392{
Victor Stinner8c62be82010-05-06 00:08:46 +00003393 PyObject *opath;
3394 char *path;
3395 PyObject *argv;
3396 char **argvlist;
3397 Py_ssize_t i, argc;
3398 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003399
Victor Stinner8c62be82010-05-06 00:08:46 +00003400 /* execv has two arguments: (path, argv), where
3401 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003402
Victor Stinner8c62be82010-05-06 00:08:46 +00003403 if (!PyArg_ParseTuple(args, "O&O:execv",
3404 PyUnicode_FSConverter,
3405 &opath, &argv))
3406 return NULL;
3407 path = PyBytes_AsString(opath);
3408 if (PyList_Check(argv)) {
3409 argc = PyList_Size(argv);
3410 getitem = PyList_GetItem;
3411 }
3412 else if (PyTuple_Check(argv)) {
3413 argc = PyTuple_Size(argv);
3414 getitem = PyTuple_GetItem;
3415 }
3416 else {
3417 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3418 Py_DECREF(opath);
3419 return NULL;
3420 }
3421 if (argc < 1) {
3422 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3423 Py_DECREF(opath);
3424 return NULL;
3425 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003426
Victor Stinner8c62be82010-05-06 00:08:46 +00003427 argvlist = PyMem_NEW(char *, argc+1);
3428 if (argvlist == NULL) {
3429 Py_DECREF(opath);
3430 return PyErr_NoMemory();
3431 }
3432 for (i = 0; i < argc; i++) {
3433 if (!fsconvert_strdup((*getitem)(argv, i),
3434 &argvlist[i])) {
3435 free_string_array(argvlist, i);
3436 PyErr_SetString(PyExc_TypeError,
3437 "execv() arg 2 must contain only strings");
3438 Py_DECREF(opath);
3439 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003440
Victor Stinner8c62be82010-05-06 00:08:46 +00003441 }
3442 }
3443 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003444
Victor Stinner8c62be82010-05-06 00:08:46 +00003445 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003446
Victor Stinner8c62be82010-05-06 00:08:46 +00003447 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003448
Victor Stinner8c62be82010-05-06 00:08:46 +00003449 free_string_array(argvlist, argc);
3450 Py_DECREF(opath);
3451 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003452}
3453
Victor Stinner13bb71c2010-04-23 21:41:56 +00003454static char**
3455parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3456{
Victor Stinner8c62be82010-05-06 00:08:46 +00003457 char **envlist;
3458 Py_ssize_t i, pos, envc;
3459 PyObject *keys=NULL, *vals=NULL;
3460 PyObject *key, *val, *key2, *val2;
3461 char *p, *k, *v;
3462 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003463
Victor Stinner8c62be82010-05-06 00:08:46 +00003464 i = PyMapping_Size(env);
3465 if (i < 0)
3466 return NULL;
3467 envlist = PyMem_NEW(char *, i + 1);
3468 if (envlist == NULL) {
3469 PyErr_NoMemory();
3470 return NULL;
3471 }
3472 envc = 0;
3473 keys = PyMapping_Keys(env);
3474 vals = PyMapping_Values(env);
3475 if (!keys || !vals)
3476 goto error;
3477 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3478 PyErr_Format(PyExc_TypeError,
3479 "env.keys() or env.values() is not a list");
3480 goto error;
3481 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003482
Victor Stinner8c62be82010-05-06 00:08:46 +00003483 for (pos = 0; pos < i; pos++) {
3484 key = PyList_GetItem(keys, pos);
3485 val = PyList_GetItem(vals, pos);
3486 if (!key || !val)
3487 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003488
Victor Stinner8c62be82010-05-06 00:08:46 +00003489 if (PyUnicode_FSConverter(key, &key2) == 0)
3490 goto error;
3491 if (PyUnicode_FSConverter(val, &val2) == 0) {
3492 Py_DECREF(key2);
3493 goto error;
3494 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003495
3496#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003497 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3498 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003499#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 k = PyBytes_AsString(key2);
3501 v = PyBytes_AsString(val2);
3502 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003503
Victor Stinner8c62be82010-05-06 00:08:46 +00003504 p = PyMem_NEW(char, len);
3505 if (p == NULL) {
3506 PyErr_NoMemory();
3507 Py_DECREF(key2);
3508 Py_DECREF(val2);
3509 goto error;
3510 }
3511 PyOS_snprintf(p, len, "%s=%s", k, v);
3512 envlist[envc++] = p;
3513 Py_DECREF(key2);
3514 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003515#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003516 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003517#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003518 }
3519 Py_DECREF(vals);
3520 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003521
Victor Stinner8c62be82010-05-06 00:08:46 +00003522 envlist[envc] = 0;
3523 *envc_ptr = envc;
3524 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003525
3526error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003527 Py_XDECREF(keys);
3528 Py_XDECREF(vals);
3529 while (--envc >= 0)
3530 PyMem_DEL(envlist[envc]);
3531 PyMem_DEL(envlist);
3532 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003533}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003534
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003535PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003536"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003537Execute a path with arguments and environment, replacing current process.\n\
3538\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003539 path: path of executable file\n\
3540 args: tuple or list of arguments\n\
3541 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003542
Barry Warsaw53699e91996-12-10 23:23:01 +00003543static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003544posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003545{
Victor Stinner8c62be82010-05-06 00:08:46 +00003546 PyObject *opath;
3547 char *path;
3548 PyObject *argv, *env;
3549 char **argvlist;
3550 char **envlist;
3551 Py_ssize_t i, argc, envc;
3552 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3553 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003554
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 /* execve has three arguments: (path, argv, env), where
3556 argv is a list or tuple of strings and env is a dictionary
3557 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003558
Victor Stinner8c62be82010-05-06 00:08:46 +00003559 if (!PyArg_ParseTuple(args, "O&OO:execve",
3560 PyUnicode_FSConverter,
3561 &opath, &argv, &env))
3562 return NULL;
3563 path = PyBytes_AsString(opath);
3564 if (PyList_Check(argv)) {
3565 argc = PyList_Size(argv);
3566 getitem = PyList_GetItem;
3567 }
3568 else if (PyTuple_Check(argv)) {
3569 argc = PyTuple_Size(argv);
3570 getitem = PyTuple_GetItem;
3571 }
3572 else {
3573 PyErr_SetString(PyExc_TypeError,
3574 "execve() arg 2 must be a tuple or list");
3575 goto fail_0;
3576 }
3577 if (!PyMapping_Check(env)) {
3578 PyErr_SetString(PyExc_TypeError,
3579 "execve() arg 3 must be a mapping object");
3580 goto fail_0;
3581 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003582
Victor Stinner8c62be82010-05-06 00:08:46 +00003583 argvlist = PyMem_NEW(char *, argc+1);
3584 if (argvlist == NULL) {
3585 PyErr_NoMemory();
3586 goto fail_0;
3587 }
3588 for (i = 0; i < argc; i++) {
3589 if (!fsconvert_strdup((*getitem)(argv, i),
3590 &argvlist[i]))
3591 {
3592 lastarg = i;
3593 goto fail_1;
3594 }
3595 }
3596 lastarg = argc;
3597 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003598
Victor Stinner8c62be82010-05-06 00:08:46 +00003599 envlist = parse_envlist(env, &envc);
3600 if (envlist == NULL)
3601 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003602
Victor Stinner8c62be82010-05-06 00:08:46 +00003603 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003604
Victor Stinner8c62be82010-05-06 00:08:46 +00003605 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003606
Victor Stinner8c62be82010-05-06 00:08:46 +00003607 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003608
Victor Stinner8c62be82010-05-06 00:08:46 +00003609 while (--envc >= 0)
3610 PyMem_DEL(envlist[envc]);
3611 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003612 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003613 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003614 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003615 Py_DECREF(opath);
3616 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003617}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003618#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003619
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003620
Guido van Rossuma1065681999-01-25 23:20:23 +00003621#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003622PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003623"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003624Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003625\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003626 mode: mode of process creation\n\
3627 path: path of executable file\n\
3628 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003629
3630static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003631posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003632{
Victor Stinner8c62be82010-05-06 00:08:46 +00003633 PyObject *opath;
3634 char *path;
3635 PyObject *argv;
3636 char **argvlist;
3637 int mode, i;
3638 Py_ssize_t argc;
3639 Py_intptr_t spawnval;
3640 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003641
Victor Stinner8c62be82010-05-06 00:08:46 +00003642 /* spawnv has three arguments: (mode, path, argv), where
3643 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003644
Victor Stinner8c62be82010-05-06 00:08:46 +00003645 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3646 PyUnicode_FSConverter,
3647 &opath, &argv))
3648 return NULL;
3649 path = PyBytes_AsString(opath);
3650 if (PyList_Check(argv)) {
3651 argc = PyList_Size(argv);
3652 getitem = PyList_GetItem;
3653 }
3654 else if (PyTuple_Check(argv)) {
3655 argc = PyTuple_Size(argv);
3656 getitem = PyTuple_GetItem;
3657 }
3658 else {
3659 PyErr_SetString(PyExc_TypeError,
3660 "spawnv() arg 2 must be a tuple or list");
3661 Py_DECREF(opath);
3662 return NULL;
3663 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003664
Victor Stinner8c62be82010-05-06 00:08:46 +00003665 argvlist = PyMem_NEW(char *, argc+1);
3666 if (argvlist == NULL) {
3667 Py_DECREF(opath);
3668 return PyErr_NoMemory();
3669 }
3670 for (i = 0; i < argc; i++) {
3671 if (!fsconvert_strdup((*getitem)(argv, i),
3672 &argvlist[i])) {
3673 free_string_array(argvlist, i);
3674 PyErr_SetString(
3675 PyExc_TypeError,
3676 "spawnv() arg 2 must contain only strings");
3677 Py_DECREF(opath);
3678 return NULL;
3679 }
3680 }
3681 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003682
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003683#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003684 Py_BEGIN_ALLOW_THREADS
3685 spawnval = spawnv(mode, path, argvlist);
3686 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003687#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003688 if (mode == _OLD_P_OVERLAY)
3689 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003690
Victor Stinner8c62be82010-05-06 00:08:46 +00003691 Py_BEGIN_ALLOW_THREADS
3692 spawnval = _spawnv(mode, path, argvlist);
3693 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003694#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003695
Victor Stinner8c62be82010-05-06 00:08:46 +00003696 free_string_array(argvlist, argc);
3697 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003698
Victor Stinner8c62be82010-05-06 00:08:46 +00003699 if (spawnval == -1)
3700 return posix_error();
3701 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003702#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003703 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003704#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003705 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003706#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003707}
3708
3709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003710PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003711"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003712Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003713\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003714 mode: mode of process creation\n\
3715 path: path of executable file\n\
3716 args: tuple or list of arguments\n\
3717 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003718
3719static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003720posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003721{
Victor Stinner8c62be82010-05-06 00:08:46 +00003722 PyObject *opath;
3723 char *path;
3724 PyObject *argv, *env;
3725 char **argvlist;
3726 char **envlist;
3727 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003728 int mode;
3729 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003730 Py_intptr_t spawnval;
3731 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3732 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003733
Victor Stinner8c62be82010-05-06 00:08:46 +00003734 /* spawnve has four arguments: (mode, path, argv, env), where
3735 argv is a list or tuple of strings and env is a dictionary
3736 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003737
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3739 PyUnicode_FSConverter,
3740 &opath, &argv, &env))
3741 return NULL;
3742 path = PyBytes_AsString(opath);
3743 if (PyList_Check(argv)) {
3744 argc = PyList_Size(argv);
3745 getitem = PyList_GetItem;
3746 }
3747 else if (PyTuple_Check(argv)) {
3748 argc = PyTuple_Size(argv);
3749 getitem = PyTuple_GetItem;
3750 }
3751 else {
3752 PyErr_SetString(PyExc_TypeError,
3753 "spawnve() arg 2 must be a tuple or list");
3754 goto fail_0;
3755 }
3756 if (!PyMapping_Check(env)) {
3757 PyErr_SetString(PyExc_TypeError,
3758 "spawnve() arg 3 must be a mapping object");
3759 goto fail_0;
3760 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003761
Victor Stinner8c62be82010-05-06 00:08:46 +00003762 argvlist = PyMem_NEW(char *, argc+1);
3763 if (argvlist == NULL) {
3764 PyErr_NoMemory();
3765 goto fail_0;
3766 }
3767 for (i = 0; i < argc; i++) {
3768 if (!fsconvert_strdup((*getitem)(argv, i),
3769 &argvlist[i]))
3770 {
3771 lastarg = i;
3772 goto fail_1;
3773 }
3774 }
3775 lastarg = argc;
3776 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003777
Victor Stinner8c62be82010-05-06 00:08:46 +00003778 envlist = parse_envlist(env, &envc);
3779 if (envlist == NULL)
3780 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003781
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003782#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003783 Py_BEGIN_ALLOW_THREADS
3784 spawnval = spawnve(mode, path, argvlist, envlist);
3785 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003786#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003787 if (mode == _OLD_P_OVERLAY)
3788 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003789
Victor Stinner8c62be82010-05-06 00:08:46 +00003790 Py_BEGIN_ALLOW_THREADS
3791 spawnval = _spawnve(mode, path, argvlist, envlist);
3792 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003793#endif
Tim Peters25059d32001-12-07 20:35:43 +00003794
Victor Stinner8c62be82010-05-06 00:08:46 +00003795 if (spawnval == -1)
3796 (void) posix_error();
3797 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003798#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003799 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003800#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003801 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003802#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003803
Victor Stinner8c62be82010-05-06 00:08:46 +00003804 while (--envc >= 0)
3805 PyMem_DEL(envlist[envc]);
3806 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003807 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003808 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003809 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003810 Py_DECREF(opath);
3811 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003812}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003813
3814/* OS/2 supports spawnvp & spawnvpe natively */
3815#if defined(PYOS_OS2)
3816PyDoc_STRVAR(posix_spawnvp__doc__,
3817"spawnvp(mode, file, args)\n\n\
3818Execute the program 'file' in a new process, using the environment\n\
3819search path to find the file.\n\
3820\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003821 mode: mode of process creation\n\
3822 file: executable file name\n\
3823 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003824
3825static PyObject *
3826posix_spawnvp(PyObject *self, PyObject *args)
3827{
Victor Stinner8c62be82010-05-06 00:08:46 +00003828 PyObject *opath;
3829 char *path;
3830 PyObject *argv;
3831 char **argvlist;
3832 int mode, i, argc;
3833 Py_intptr_t spawnval;
3834 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003835
Victor Stinner8c62be82010-05-06 00:08:46 +00003836 /* spawnvp has three arguments: (mode, path, argv), where
3837 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003838
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3840 PyUnicode_FSConverter,
3841 &opath, &argv))
3842 return NULL;
3843 path = PyBytes_AsString(opath);
3844 if (PyList_Check(argv)) {
3845 argc = PyList_Size(argv);
3846 getitem = PyList_GetItem;
3847 }
3848 else if (PyTuple_Check(argv)) {
3849 argc = PyTuple_Size(argv);
3850 getitem = PyTuple_GetItem;
3851 }
3852 else {
3853 PyErr_SetString(PyExc_TypeError,
3854 "spawnvp() arg 2 must be a tuple or list");
3855 Py_DECREF(opath);
3856 return NULL;
3857 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003858
Victor Stinner8c62be82010-05-06 00:08:46 +00003859 argvlist = PyMem_NEW(char *, argc+1);
3860 if (argvlist == NULL) {
3861 Py_DECREF(opath);
3862 return PyErr_NoMemory();
3863 }
3864 for (i = 0; i < argc; i++) {
3865 if (!fsconvert_strdup((*getitem)(argv, i),
3866 &argvlist[i])) {
3867 free_string_array(argvlist, i);
3868 PyErr_SetString(
3869 PyExc_TypeError,
3870 "spawnvp() arg 2 must contain only strings");
3871 Py_DECREF(opath);
3872 return NULL;
3873 }
3874 }
3875 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003876
Victor Stinner8c62be82010-05-06 00:08:46 +00003877 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003878#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003879 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003880#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003881 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003882#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003883 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003884
Victor Stinner8c62be82010-05-06 00:08:46 +00003885 free_string_array(argvlist, argc);
3886 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003887
Victor Stinner8c62be82010-05-06 00:08:46 +00003888 if (spawnval == -1)
3889 return posix_error();
3890 else
3891 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003892}
3893
3894
3895PyDoc_STRVAR(posix_spawnvpe__doc__,
3896"spawnvpe(mode, file, args, env)\n\n\
3897Execute the program 'file' in a new process, using the environment\n\
3898search path to find the file.\n\
3899\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003900 mode: mode of process creation\n\
3901 file: executable file name\n\
3902 args: tuple or list of arguments\n\
3903 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003904
3905static PyObject *
3906posix_spawnvpe(PyObject *self, PyObject *args)
3907{
Victor Stinner8c62be82010-05-06 00:08:46 +00003908 PyObject *opath
3909 char *path;
3910 PyObject *argv, *env;
3911 char **argvlist;
3912 char **envlist;
3913 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003914 int mode;
3915 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003916 Py_intptr_t spawnval;
3917 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3918 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003919
Victor Stinner8c62be82010-05-06 00:08:46 +00003920 /* spawnvpe has four arguments: (mode, path, argv, env), where
3921 argv is a list or tuple of strings and env is a dictionary
3922 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003923
Victor Stinner8c62be82010-05-06 00:08:46 +00003924 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3925 PyUnicode_FSConverter,
3926 &opath, &argv, &env))
3927 return NULL;
3928 path = PyBytes_AsString(opath);
3929 if (PyList_Check(argv)) {
3930 argc = PyList_Size(argv);
3931 getitem = PyList_GetItem;
3932 }
3933 else if (PyTuple_Check(argv)) {
3934 argc = PyTuple_Size(argv);
3935 getitem = PyTuple_GetItem;
3936 }
3937 else {
3938 PyErr_SetString(PyExc_TypeError,
3939 "spawnvpe() arg 2 must be a tuple or list");
3940 goto fail_0;
3941 }
3942 if (!PyMapping_Check(env)) {
3943 PyErr_SetString(PyExc_TypeError,
3944 "spawnvpe() arg 3 must be a mapping object");
3945 goto fail_0;
3946 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003947
Victor Stinner8c62be82010-05-06 00:08:46 +00003948 argvlist = PyMem_NEW(char *, argc+1);
3949 if (argvlist == NULL) {
3950 PyErr_NoMemory();
3951 goto fail_0;
3952 }
3953 for (i = 0; i < argc; i++) {
3954 if (!fsconvert_strdup((*getitem)(argv, i),
3955 &argvlist[i]))
3956 {
3957 lastarg = i;
3958 goto fail_1;
3959 }
3960 }
3961 lastarg = argc;
3962 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003963
Victor Stinner8c62be82010-05-06 00:08:46 +00003964 envlist = parse_envlist(env, &envc);
3965 if (envlist == NULL)
3966 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003967
Victor Stinner8c62be82010-05-06 00:08:46 +00003968 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003969#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003970 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003971#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003972 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003973#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003974 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003975
Victor Stinner8c62be82010-05-06 00:08:46 +00003976 if (spawnval == -1)
3977 (void) posix_error();
3978 else
3979 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003980
Victor Stinner8c62be82010-05-06 00:08:46 +00003981 while (--envc >= 0)
3982 PyMem_DEL(envlist[envc]);
3983 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003984 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003985 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003986 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003987 Py_DECREF(opath);
3988 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003989}
3990#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003991#endif /* HAVE_SPAWNV */
3992
3993
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003994#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003995PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003996"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003997Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3998\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003999Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004000
4001static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004002posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004003{
Victor Stinner8c62be82010-05-06 00:08:46 +00004004 pid_t pid;
4005 int result = 0;
4006 _PyImport_AcquireLock();
4007 pid = fork1();
4008 if (pid == 0) {
4009 /* child: this clobbers and resets the import lock. */
4010 PyOS_AfterFork();
4011 } else {
4012 /* parent: release the import lock. */
4013 result = _PyImport_ReleaseLock();
4014 }
4015 if (pid == -1)
4016 return posix_error();
4017 if (result < 0) {
4018 /* Don't clobber the OSError if the fork failed. */
4019 PyErr_SetString(PyExc_RuntimeError,
4020 "not holding the import lock");
4021 return NULL;
4022 }
4023 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004024}
4025#endif
4026
4027
Guido van Rossumad0ee831995-03-01 10:34:45 +00004028#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004029PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004030"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004031Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004032Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004033
Barry Warsaw53699e91996-12-10 23:23:01 +00004034static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004035posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004036{
Victor Stinner8c62be82010-05-06 00:08:46 +00004037 pid_t pid;
4038 int result = 0;
4039 _PyImport_AcquireLock();
4040 pid = fork();
4041 if (pid == 0) {
4042 /* child: this clobbers and resets the import lock. */
4043 PyOS_AfterFork();
4044 } else {
4045 /* parent: release the import lock. */
4046 result = _PyImport_ReleaseLock();
4047 }
4048 if (pid == -1)
4049 return posix_error();
4050 if (result < 0) {
4051 /* Don't clobber the OSError if the fork failed. */
4052 PyErr_SetString(PyExc_RuntimeError,
4053 "not holding the import lock");
4054 return NULL;
4055 }
4056 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004057}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004058#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004059
Neal Norwitzb59798b2003-03-21 01:43:31 +00004060/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004061/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4062#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004063#define DEV_PTY_FILE "/dev/ptc"
4064#define HAVE_DEV_PTMX
4065#else
4066#define DEV_PTY_FILE "/dev/ptmx"
4067#endif
4068
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004069#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004070#ifdef HAVE_PTY_H
4071#include <pty.h>
4072#else
4073#ifdef HAVE_LIBUTIL_H
4074#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004075#else
4076#ifdef HAVE_UTIL_H
4077#include <util.h>
4078#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004079#endif /* HAVE_LIBUTIL_H */
4080#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004081#ifdef HAVE_STROPTS_H
4082#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004083#endif
4084#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004085
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004086#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004087PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004088"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004089Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004090
4091static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004092posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004093{
Victor Stinner8c62be82010-05-06 00:08:46 +00004094 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004095#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004096 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004097#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004098#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004099 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004100#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004101 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004102#endif
4103#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004104
Thomas Wouters70c21a12000-07-14 14:28:33 +00004105#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004106 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4107 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004108#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004109 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4110 if (slave_name == NULL)
4111 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004112
Victor Stinner8c62be82010-05-06 00:08:46 +00004113 slave_fd = open(slave_name, O_RDWR);
4114 if (slave_fd < 0)
4115 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004116#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004117 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4118 if (master_fd < 0)
4119 return posix_error();
4120 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4121 /* change permission of slave */
4122 if (grantpt(master_fd) < 0) {
4123 PyOS_setsig(SIGCHLD, sig_saved);
4124 return posix_error();
4125 }
4126 /* unlock slave */
4127 if (unlockpt(master_fd) < 0) {
4128 PyOS_setsig(SIGCHLD, sig_saved);
4129 return posix_error();
4130 }
4131 PyOS_setsig(SIGCHLD, sig_saved);
4132 slave_name = ptsname(master_fd); /* get name of slave */
4133 if (slave_name == NULL)
4134 return posix_error();
4135 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4136 if (slave_fd < 0)
4137 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004138#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004139 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4140 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004141#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004142 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004143#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004144#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004145#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004146
Victor Stinner8c62be82010-05-06 00:08:46 +00004147 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004148
Fred Drake8cef4cf2000-06-28 16:40:38 +00004149}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004150#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004151
4152#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004153PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004154"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004155Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4156Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004157To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004158
4159static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004160posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004161{
Victor Stinner8c62be82010-05-06 00:08:46 +00004162 int master_fd = -1, result = 0;
4163 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004164
Victor Stinner8c62be82010-05-06 00:08:46 +00004165 _PyImport_AcquireLock();
4166 pid = forkpty(&master_fd, NULL, NULL, NULL);
4167 if (pid == 0) {
4168 /* child: this clobbers and resets the import lock. */
4169 PyOS_AfterFork();
4170 } else {
4171 /* parent: release the import lock. */
4172 result = _PyImport_ReleaseLock();
4173 }
4174 if (pid == -1)
4175 return posix_error();
4176 if (result < 0) {
4177 /* Don't clobber the OSError if the fork failed. */
4178 PyErr_SetString(PyExc_RuntimeError,
4179 "not holding the import lock");
4180 return NULL;
4181 }
4182 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004183}
4184#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004185
Guido van Rossumad0ee831995-03-01 10:34:45 +00004186#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004187PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004188"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004189Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004190
Barry Warsaw53699e91996-12-10 23:23:01 +00004191static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004192posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004193{
Victor Stinner8c62be82010-05-06 00:08:46 +00004194 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004195}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004196#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004197
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004198
Guido van Rossumad0ee831995-03-01 10:34:45 +00004199#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004200PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004201"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004202Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004203
Barry Warsaw53699e91996-12-10 23:23:01 +00004204static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004205posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004206{
Victor Stinner8c62be82010-05-06 00:08:46 +00004207 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004208}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004209#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004211
Guido van Rossumad0ee831995-03-01 10:34:45 +00004212#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004213PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004214"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004215Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004216
Barry Warsaw53699e91996-12-10 23:23:01 +00004217static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004218posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004219{
Victor Stinner8c62be82010-05-06 00:08:46 +00004220 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004221}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004222#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004225PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004226"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004227Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004228
Barry Warsaw53699e91996-12-10 23:23:01 +00004229static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004230posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004231{
Victor Stinner8c62be82010-05-06 00:08:46 +00004232 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004233}
4234
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004235
Fred Drakec9680921999-12-13 16:37:25 +00004236#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004237PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004238"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004239Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004240
4241static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004242posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004243{
4244 PyObject *result = NULL;
4245
Fred Drakec9680921999-12-13 16:37:25 +00004246#ifdef NGROUPS_MAX
4247#define MAX_GROUPS NGROUPS_MAX
4248#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004249 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004250#define MAX_GROUPS 64
4251#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004252 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004253
4254 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4255 * This is a helper variable to store the intermediate result when
4256 * that happens.
4257 *
4258 * To keep the code readable the OSX behaviour is unconditional,
4259 * according to the POSIX spec this should be safe on all unix-y
4260 * systems.
4261 */
4262 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004263 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004264
Victor Stinner8c62be82010-05-06 00:08:46 +00004265 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004266 if (n < 0) {
4267 if (errno == EINVAL) {
4268 n = getgroups(0, NULL);
4269 if (n == -1) {
4270 return posix_error();
4271 }
4272 if (n == 0) {
4273 /* Avoid malloc(0) */
4274 alt_grouplist = grouplist;
4275 } else {
4276 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4277 if (alt_grouplist == NULL) {
4278 errno = EINVAL;
4279 return posix_error();
4280 }
4281 n = getgroups(n, alt_grouplist);
4282 if (n == -1) {
4283 PyMem_Free(alt_grouplist);
4284 return posix_error();
4285 }
4286 }
4287 } else {
4288 return posix_error();
4289 }
4290 }
4291 result = PyList_New(n);
4292 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004293 int i;
4294 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004295 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004296 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004297 Py_DECREF(result);
4298 result = NULL;
4299 break;
Fred Drakec9680921999-12-13 16:37:25 +00004300 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004301 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004302 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004303 }
4304
4305 if (alt_grouplist != grouplist) {
4306 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004307 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004308
Fred Drakec9680921999-12-13 16:37:25 +00004309 return result;
4310}
4311#endif
4312
Antoine Pitroub7572f02009-12-02 20:46:48 +00004313#ifdef HAVE_INITGROUPS
4314PyDoc_STRVAR(posix_initgroups__doc__,
4315"initgroups(username, gid) -> None\n\n\
4316Call the system initgroups() to initialize the group access list with all of\n\
4317the groups of which the specified username is a member, plus the specified\n\
4318group id.");
4319
4320static PyObject *
4321posix_initgroups(PyObject *self, PyObject *args)
4322{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004323 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004324 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004325 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004326 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004327
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004328 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4329 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004330 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004331 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004332
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004333 res = initgroups(username, (gid_t) gid);
4334 Py_DECREF(oname);
4335 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004336 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004337
Victor Stinner8c62be82010-05-06 00:08:46 +00004338 Py_INCREF(Py_None);
4339 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004340}
4341#endif
4342
Martin v. Löwis606edc12002-06-13 21:09:11 +00004343#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004344PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004345"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004346Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004347
4348static PyObject *
4349posix_getpgid(PyObject *self, PyObject *args)
4350{
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 pid_t pid, pgid;
4352 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4353 return NULL;
4354 pgid = getpgid(pid);
4355 if (pgid < 0)
4356 return posix_error();
4357 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004358}
4359#endif /* HAVE_GETPGID */
4360
4361
Guido van Rossumb6775db1994-08-01 11:34:53 +00004362#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004363PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004364"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004365Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004366
Barry Warsaw53699e91996-12-10 23:23:01 +00004367static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004368posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004369{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004370#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004372#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004374#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004375}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004376#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004377
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004378
Guido van Rossumb6775db1994-08-01 11:34:53 +00004379#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004380PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004381"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004382Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004383
Barry Warsaw53699e91996-12-10 23:23:01 +00004384static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004385posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004386{
Guido van Rossum64933891994-10-20 21:56:42 +00004387#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004388 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004389#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004391#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004392 return posix_error();
4393 Py_INCREF(Py_None);
4394 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004395}
4396
Guido van Rossumb6775db1994-08-01 11:34:53 +00004397#endif /* HAVE_SETPGRP */
4398
Guido van Rossumad0ee831995-03-01 10:34:45 +00004399#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004400
4401#ifdef MS_WINDOWS
4402#include <tlhelp32.h>
4403
4404static PyObject*
4405win32_getppid()
4406{
4407 HANDLE snapshot;
4408 pid_t mypid;
4409 PyObject* result = NULL;
4410 BOOL have_record;
4411 PROCESSENTRY32 pe;
4412
4413 mypid = getpid(); /* This function never fails */
4414
4415 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4416 if (snapshot == INVALID_HANDLE_VALUE)
4417 return PyErr_SetFromWindowsErr(GetLastError());
4418
4419 pe.dwSize = sizeof(pe);
4420 have_record = Process32First(snapshot, &pe);
4421 while (have_record) {
4422 if (mypid == (pid_t)pe.th32ProcessID) {
4423 /* We could cache the ulong value in a static variable. */
4424 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4425 break;
4426 }
4427
4428 have_record = Process32Next(snapshot, &pe);
4429 }
4430
4431 /* If our loop exits and our pid was not found (result will be NULL)
4432 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4433 * error anyway, so let's raise it. */
4434 if (!result)
4435 result = PyErr_SetFromWindowsErr(GetLastError());
4436
4437 CloseHandle(snapshot);
4438
4439 return result;
4440}
4441#endif /*MS_WINDOWS*/
4442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004443PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004444"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004445Return the parent's process id. If the parent process has already exited,\n\
4446Windows machines will still return its id; others systems will return the id\n\
4447of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004448
Barry Warsaw53699e91996-12-10 23:23:01 +00004449static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004450posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004451{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004452#ifdef MS_WINDOWS
4453 return win32_getppid();
4454#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004455 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004456#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004457}
4458#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004459
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004460
Fred Drake12c6e2d1999-12-14 21:25:03 +00004461#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004462PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004463"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004464Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004465
4466static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004467posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004468{
Victor Stinner8c62be82010-05-06 00:08:46 +00004469 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004470#ifdef MS_WINDOWS
4471 wchar_t user_name[UNLEN + 1];
4472 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4473
4474 if (GetUserNameW(user_name, &num_chars)) {
4475 /* num_chars is the number of unicode chars plus null terminator */
4476 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4477 }
4478 else
4479 result = PyErr_SetFromWindowsErr(GetLastError());
4480#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004481 char *name;
4482 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004483
Victor Stinner8c62be82010-05-06 00:08:46 +00004484 errno = 0;
4485 name = getlogin();
4486 if (name == NULL) {
4487 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004488 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004489 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004490 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004491 }
4492 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004493 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004494 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004495#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004496 return result;
4497}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004498#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004499
Guido van Rossumad0ee831995-03-01 10:34:45 +00004500#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004501PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004502"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004503Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004504
Barry Warsaw53699e91996-12-10 23:23:01 +00004505static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004506posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004507{
Victor Stinner8c62be82010-05-06 00:08:46 +00004508 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004509}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004510#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004511
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004512
Guido van Rossumad0ee831995-03-01 10:34:45 +00004513#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004514PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004515"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004516Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004517
Barry Warsaw53699e91996-12-10 23:23:01 +00004518static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004519posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004520{
Victor Stinner8c62be82010-05-06 00:08:46 +00004521 pid_t pid;
4522 int sig;
4523 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4524 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004525#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004526 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4527 APIRET rc;
4528 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004529 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004530
4531 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4532 APIRET rc;
4533 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004534 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004535
4536 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004537 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004538#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004539 if (kill(pid, sig) == -1)
4540 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004541#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004542 Py_INCREF(Py_None);
4543 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004544}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004545#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004546
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004547#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004548PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004549"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004550Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004551
4552static PyObject *
4553posix_killpg(PyObject *self, PyObject *args)
4554{
Victor Stinner8c62be82010-05-06 00:08:46 +00004555 int sig;
4556 pid_t pgid;
4557 /* XXX some man pages make the `pgid` parameter an int, others
4558 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4559 take the same type. Moreover, pid_t is always at least as wide as
4560 int (else compilation of this module fails), which is safe. */
4561 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4562 return NULL;
4563 if (killpg(pgid, sig) == -1)
4564 return posix_error();
4565 Py_INCREF(Py_None);
4566 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004567}
4568#endif
4569
Brian Curtineb24d742010-04-12 17:16:38 +00004570#ifdef MS_WINDOWS
4571PyDoc_STRVAR(win32_kill__doc__,
4572"kill(pid, sig)\n\n\
4573Kill a process with a signal.");
4574
4575static PyObject *
4576win32_kill(PyObject *self, PyObject *args)
4577{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004578 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004579 DWORD pid, sig, err;
4580 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004581
Victor Stinner8c62be82010-05-06 00:08:46 +00004582 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4583 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004584
Victor Stinner8c62be82010-05-06 00:08:46 +00004585 /* Console processes which share a common console can be sent CTRL+C or
4586 CTRL+BREAK events, provided they handle said events. */
4587 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4588 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4589 err = GetLastError();
4590 PyErr_SetFromWindowsErr(err);
4591 }
4592 else
4593 Py_RETURN_NONE;
4594 }
Brian Curtineb24d742010-04-12 17:16:38 +00004595
Victor Stinner8c62be82010-05-06 00:08:46 +00004596 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4597 attempt to open and terminate the process. */
4598 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4599 if (handle == NULL) {
4600 err = GetLastError();
4601 return PyErr_SetFromWindowsErr(err);
4602 }
Brian Curtineb24d742010-04-12 17:16:38 +00004603
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 if (TerminateProcess(handle, sig) == 0) {
4605 err = GetLastError();
4606 result = PyErr_SetFromWindowsErr(err);
4607 } else {
4608 Py_INCREF(Py_None);
4609 result = Py_None;
4610 }
Brian Curtineb24d742010-04-12 17:16:38 +00004611
Victor Stinner8c62be82010-05-06 00:08:46 +00004612 CloseHandle(handle);
4613 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004614}
4615#endif /* MS_WINDOWS */
4616
Guido van Rossumc0125471996-06-28 18:55:32 +00004617#ifdef HAVE_PLOCK
4618
4619#ifdef HAVE_SYS_LOCK_H
4620#include <sys/lock.h>
4621#endif
4622
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004623PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004624"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004625Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004626
Barry Warsaw53699e91996-12-10 23:23:01 +00004627static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004628posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004629{
Victor Stinner8c62be82010-05-06 00:08:46 +00004630 int op;
4631 if (!PyArg_ParseTuple(args, "i:plock", &op))
4632 return NULL;
4633 if (plock(op) == -1)
4634 return posix_error();
4635 Py_INCREF(Py_None);
4636 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004637}
4638#endif
4639
Guido van Rossumb6775db1994-08-01 11:34:53 +00004640#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004641PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004642"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004643Set the current process's user id.");
4644
Barry Warsaw53699e91996-12-10 23:23:01 +00004645static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004646posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004647{
Victor Stinner8c62be82010-05-06 00:08:46 +00004648 long uid_arg;
4649 uid_t uid;
4650 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4651 return NULL;
4652 uid = uid_arg;
4653 if (uid != uid_arg) {
4654 PyErr_SetString(PyExc_OverflowError, "user id too big");
4655 return NULL;
4656 }
4657 if (setuid(uid) < 0)
4658 return posix_error();
4659 Py_INCREF(Py_None);
4660 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004661}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004662#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004663
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004664
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004665#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004666PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004667"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004668Set the current process's effective user id.");
4669
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004670static PyObject *
4671posix_seteuid (PyObject *self, PyObject *args)
4672{
Victor Stinner8c62be82010-05-06 00:08:46 +00004673 long euid_arg;
4674 uid_t euid;
4675 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4676 return NULL;
4677 euid = euid_arg;
4678 if (euid != euid_arg) {
4679 PyErr_SetString(PyExc_OverflowError, "user id too big");
4680 return NULL;
4681 }
4682 if (seteuid(euid) < 0) {
4683 return posix_error();
4684 } else {
4685 Py_INCREF(Py_None);
4686 return Py_None;
4687 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004688}
4689#endif /* HAVE_SETEUID */
4690
4691#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004692PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004693"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004694Set the current process's effective group id.");
4695
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004696static PyObject *
4697posix_setegid (PyObject *self, PyObject *args)
4698{
Victor Stinner8c62be82010-05-06 00:08:46 +00004699 long egid_arg;
4700 gid_t egid;
4701 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4702 return NULL;
4703 egid = egid_arg;
4704 if (egid != egid_arg) {
4705 PyErr_SetString(PyExc_OverflowError, "group id too big");
4706 return NULL;
4707 }
4708 if (setegid(egid) < 0) {
4709 return posix_error();
4710 } else {
4711 Py_INCREF(Py_None);
4712 return Py_None;
4713 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004714}
4715#endif /* HAVE_SETEGID */
4716
4717#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004718PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004719"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004720Set the current process's real and effective user ids.");
4721
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004722static PyObject *
4723posix_setreuid (PyObject *self, PyObject *args)
4724{
Victor Stinner8c62be82010-05-06 00:08:46 +00004725 long ruid_arg, euid_arg;
4726 uid_t ruid, euid;
4727 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4728 return NULL;
4729 if (ruid_arg == -1)
4730 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4731 else
4732 ruid = ruid_arg; /* otherwise, assign from our long */
4733 if (euid_arg == -1)
4734 euid = (uid_t)-1;
4735 else
4736 euid = euid_arg;
4737 if ((euid_arg != -1 && euid != euid_arg) ||
4738 (ruid_arg != -1 && ruid != ruid_arg)) {
4739 PyErr_SetString(PyExc_OverflowError, "user id too big");
4740 return NULL;
4741 }
4742 if (setreuid(ruid, euid) < 0) {
4743 return posix_error();
4744 } else {
4745 Py_INCREF(Py_None);
4746 return Py_None;
4747 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004748}
4749#endif /* HAVE_SETREUID */
4750
4751#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004752PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004753"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004754Set the current process's real and effective group ids.");
4755
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004756static PyObject *
4757posix_setregid (PyObject *self, PyObject *args)
4758{
Victor Stinner8c62be82010-05-06 00:08:46 +00004759 long rgid_arg, egid_arg;
4760 gid_t rgid, egid;
4761 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4762 return NULL;
4763 if (rgid_arg == -1)
4764 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4765 else
4766 rgid = rgid_arg; /* otherwise, assign from our long */
4767 if (egid_arg == -1)
4768 egid = (gid_t)-1;
4769 else
4770 egid = egid_arg;
4771 if ((egid_arg != -1 && egid != egid_arg) ||
4772 (rgid_arg != -1 && rgid != rgid_arg)) {
4773 PyErr_SetString(PyExc_OverflowError, "group id too big");
4774 return NULL;
4775 }
4776 if (setregid(rgid, egid) < 0) {
4777 return posix_error();
4778 } else {
4779 Py_INCREF(Py_None);
4780 return Py_None;
4781 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004782}
4783#endif /* HAVE_SETREGID */
4784
Guido van Rossumb6775db1994-08-01 11:34:53 +00004785#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004786PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004787"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004788Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004789
Barry Warsaw53699e91996-12-10 23:23:01 +00004790static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004791posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004792{
Victor Stinner8c62be82010-05-06 00:08:46 +00004793 long gid_arg;
4794 gid_t gid;
4795 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4796 return NULL;
4797 gid = gid_arg;
4798 if (gid != gid_arg) {
4799 PyErr_SetString(PyExc_OverflowError, "group id too big");
4800 return NULL;
4801 }
4802 if (setgid(gid) < 0)
4803 return posix_error();
4804 Py_INCREF(Py_None);
4805 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004806}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004807#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004808
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004809#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004810PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004811"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004812Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004813
4814static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004815posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004816{
Victor Stinner8c62be82010-05-06 00:08:46 +00004817 int i, len;
4818 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004819
Victor Stinner8c62be82010-05-06 00:08:46 +00004820 if (!PySequence_Check(groups)) {
4821 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4822 return NULL;
4823 }
4824 len = PySequence_Size(groups);
4825 if (len > MAX_GROUPS) {
4826 PyErr_SetString(PyExc_ValueError, "too many groups");
4827 return NULL;
4828 }
4829 for(i = 0; i < len; i++) {
4830 PyObject *elem;
4831 elem = PySequence_GetItem(groups, i);
4832 if (!elem)
4833 return NULL;
4834 if (!PyLong_Check(elem)) {
4835 PyErr_SetString(PyExc_TypeError,
4836 "groups must be integers");
4837 Py_DECREF(elem);
4838 return NULL;
4839 } else {
4840 unsigned long x = PyLong_AsUnsignedLong(elem);
4841 if (PyErr_Occurred()) {
4842 PyErr_SetString(PyExc_TypeError,
4843 "group id too big");
4844 Py_DECREF(elem);
4845 return NULL;
4846 }
4847 grouplist[i] = x;
4848 /* read back the value to see if it fitted in gid_t */
4849 if (grouplist[i] != x) {
4850 PyErr_SetString(PyExc_TypeError,
4851 "group id too big");
4852 Py_DECREF(elem);
4853 return NULL;
4854 }
4855 }
4856 Py_DECREF(elem);
4857 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004858
Victor Stinner8c62be82010-05-06 00:08:46 +00004859 if (setgroups(len, grouplist) < 0)
4860 return posix_error();
4861 Py_INCREF(Py_None);
4862 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004863}
4864#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004865
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004866#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4867static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004868wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004869{
Victor Stinner8c62be82010-05-06 00:08:46 +00004870 PyObject *result;
4871 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004872
Victor Stinner8c62be82010-05-06 00:08:46 +00004873 if (pid == -1)
4874 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004875
Victor Stinner8c62be82010-05-06 00:08:46 +00004876 if (struct_rusage == NULL) {
4877 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4878 if (m == NULL)
4879 return NULL;
4880 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4881 Py_DECREF(m);
4882 if (struct_rusage == NULL)
4883 return NULL;
4884 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004885
Victor Stinner8c62be82010-05-06 00:08:46 +00004886 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4887 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4888 if (!result)
4889 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004890
4891#ifndef doubletime
4892#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4893#endif
4894
Victor Stinner8c62be82010-05-06 00:08:46 +00004895 PyStructSequence_SET_ITEM(result, 0,
4896 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4897 PyStructSequence_SET_ITEM(result, 1,
4898 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004899#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004900 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4901 SET_INT(result, 2, ru->ru_maxrss);
4902 SET_INT(result, 3, ru->ru_ixrss);
4903 SET_INT(result, 4, ru->ru_idrss);
4904 SET_INT(result, 5, ru->ru_isrss);
4905 SET_INT(result, 6, ru->ru_minflt);
4906 SET_INT(result, 7, ru->ru_majflt);
4907 SET_INT(result, 8, ru->ru_nswap);
4908 SET_INT(result, 9, ru->ru_inblock);
4909 SET_INT(result, 10, ru->ru_oublock);
4910 SET_INT(result, 11, ru->ru_msgsnd);
4911 SET_INT(result, 12, ru->ru_msgrcv);
4912 SET_INT(result, 13, ru->ru_nsignals);
4913 SET_INT(result, 14, ru->ru_nvcsw);
4914 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004915#undef SET_INT
4916
Victor Stinner8c62be82010-05-06 00:08:46 +00004917 if (PyErr_Occurred()) {
4918 Py_DECREF(result);
4919 return NULL;
4920 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004921
Victor Stinner8c62be82010-05-06 00:08:46 +00004922 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004923}
4924#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4925
4926#ifdef HAVE_WAIT3
4927PyDoc_STRVAR(posix_wait3__doc__,
4928"wait3(options) -> (pid, status, rusage)\n\n\
4929Wait for completion of a child process.");
4930
4931static PyObject *
4932posix_wait3(PyObject *self, PyObject *args)
4933{
Victor Stinner8c62be82010-05-06 00:08:46 +00004934 pid_t pid;
4935 int options;
4936 struct rusage ru;
4937 WAIT_TYPE status;
4938 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004939
Victor Stinner8c62be82010-05-06 00:08:46 +00004940 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4941 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004942
Victor Stinner8c62be82010-05-06 00:08:46 +00004943 Py_BEGIN_ALLOW_THREADS
4944 pid = wait3(&status, options, &ru);
4945 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004946
Victor Stinner8c62be82010-05-06 00:08:46 +00004947 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004948}
4949#endif /* HAVE_WAIT3 */
4950
4951#ifdef HAVE_WAIT4
4952PyDoc_STRVAR(posix_wait4__doc__,
4953"wait4(pid, options) -> (pid, status, rusage)\n\n\
4954Wait for completion of a given child process.");
4955
4956static PyObject *
4957posix_wait4(PyObject *self, PyObject *args)
4958{
Victor Stinner8c62be82010-05-06 00:08:46 +00004959 pid_t pid;
4960 int options;
4961 struct rusage ru;
4962 WAIT_TYPE status;
4963 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004964
Victor Stinner8c62be82010-05-06 00:08:46 +00004965 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4966 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004967
Victor Stinner8c62be82010-05-06 00:08:46 +00004968 Py_BEGIN_ALLOW_THREADS
4969 pid = wait4(pid, &status, options, &ru);
4970 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004971
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004973}
4974#endif /* HAVE_WAIT4 */
4975
Guido van Rossumb6775db1994-08-01 11:34:53 +00004976#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004977PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004978"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004979Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004980
Barry Warsaw53699e91996-12-10 23:23:01 +00004981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004982posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004983{
Victor Stinner8c62be82010-05-06 00:08:46 +00004984 pid_t pid;
4985 int options;
4986 WAIT_TYPE status;
4987 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004988
Victor Stinner8c62be82010-05-06 00:08:46 +00004989 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4990 return NULL;
4991 Py_BEGIN_ALLOW_THREADS
4992 pid = waitpid(pid, &status, options);
4993 Py_END_ALLOW_THREADS
4994 if (pid == -1)
4995 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004996
Victor Stinner8c62be82010-05-06 00:08:46 +00004997 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004998}
4999
Tim Petersab034fa2002-02-01 11:27:43 +00005000#elif defined(HAVE_CWAIT)
5001
5002/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005003PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005004"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005005"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005006
5007static PyObject *
5008posix_waitpid(PyObject *self, PyObject *args)
5009{
Victor Stinner8c62be82010-05-06 00:08:46 +00005010 Py_intptr_t pid;
5011 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005012
Victor Stinner8c62be82010-05-06 00:08:46 +00005013 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5014 return NULL;
5015 Py_BEGIN_ALLOW_THREADS
5016 pid = _cwait(&status, pid, options);
5017 Py_END_ALLOW_THREADS
5018 if (pid == -1)
5019 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005020
Victor Stinner8c62be82010-05-06 00:08:46 +00005021 /* shift the status left a byte so this is more like the POSIX waitpid */
5022 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005023}
5024#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005025
Guido van Rossumad0ee831995-03-01 10:34:45 +00005026#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005027PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005028"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005029Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005030
Barry Warsaw53699e91996-12-10 23:23:01 +00005031static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005032posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005033{
Victor Stinner8c62be82010-05-06 00:08:46 +00005034 pid_t pid;
5035 WAIT_TYPE status;
5036 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005037
Victor Stinner8c62be82010-05-06 00:08:46 +00005038 Py_BEGIN_ALLOW_THREADS
5039 pid = wait(&status);
5040 Py_END_ALLOW_THREADS
5041 if (pid == -1)
5042 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005043
Victor Stinner8c62be82010-05-06 00:08:46 +00005044 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005045}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005046#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005047
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005048
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005049PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005050"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005051Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005052
Barry Warsaw53699e91996-12-10 23:23:01 +00005053static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005054posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005055{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005056#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005057 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005058#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005059#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005060 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5061 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005062#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005063 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005064#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005065#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005066}
5067
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005068
Guido van Rossumb6775db1994-08-01 11:34:53 +00005069#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005070PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005071"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005072Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005073
Barry Warsaw53699e91996-12-10 23:23:01 +00005074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005075posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005076{
Victor Stinner8c62be82010-05-06 00:08:46 +00005077 PyObject* v;
5078 char buf[MAXPATHLEN];
5079 PyObject *opath;
5080 char *path;
5081 int n;
5082 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005083
Victor Stinner8c62be82010-05-06 00:08:46 +00005084 if (!PyArg_ParseTuple(args, "O&:readlink",
5085 PyUnicode_FSConverter, &opath))
5086 return NULL;
5087 path = PyBytes_AsString(opath);
5088 v = PySequence_GetItem(args, 0);
5089 if (v == NULL) {
5090 Py_DECREF(opath);
5091 return NULL;
5092 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005093
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 if (PyUnicode_Check(v)) {
5095 arg_is_unicode = 1;
5096 }
5097 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005098
Victor Stinner8c62be82010-05-06 00:08:46 +00005099 Py_BEGIN_ALLOW_THREADS
5100 n = readlink(path, buf, (int) sizeof buf);
5101 Py_END_ALLOW_THREADS
5102 if (n < 0)
5103 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005104
Victor Stinner8c62be82010-05-06 00:08:46 +00005105 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005106 if (arg_is_unicode)
5107 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5108 else
5109 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005110}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005111#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005112
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005113
Brian Curtin52173d42010-12-02 18:29:18 +00005114#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005115PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005116"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005117Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005118
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005119static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005120posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005121{
Victor Stinner8c62be82010-05-06 00:08:46 +00005122 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005123}
5124#endif /* HAVE_SYMLINK */
5125
Brian Curtind40e6f72010-07-08 21:39:08 +00005126#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5127
5128PyDoc_STRVAR(win_readlink__doc__,
5129"readlink(path) -> path\n\n\
5130Return a string representing the path to which the symbolic link points.");
5131
Brian Curtind40e6f72010-07-08 21:39:08 +00005132/* Windows readlink implementation */
5133static PyObject *
5134win_readlink(PyObject *self, PyObject *args)
5135{
5136 wchar_t *path;
5137 DWORD n_bytes_returned;
5138 DWORD io_result;
5139 PyObject *result;
5140 HANDLE reparse_point_handle;
5141
5142 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5143 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5144 wchar_t *print_name;
5145
5146 if (!PyArg_ParseTuple(args,
5147 "u:readlink",
5148 &path))
5149 return NULL;
5150
5151 /* First get a handle to the reparse point */
5152 Py_BEGIN_ALLOW_THREADS
5153 reparse_point_handle = CreateFileW(
5154 path,
5155 0,
5156 0,
5157 0,
5158 OPEN_EXISTING,
5159 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5160 0);
5161 Py_END_ALLOW_THREADS
5162
5163 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5164 {
5165 return win32_error_unicode("readlink", path);
5166 }
5167
5168 Py_BEGIN_ALLOW_THREADS
5169 /* New call DeviceIoControl to read the reparse point */
5170 io_result = DeviceIoControl(
5171 reparse_point_handle,
5172 FSCTL_GET_REPARSE_POINT,
5173 0, 0, /* in buffer */
5174 target_buffer, sizeof(target_buffer),
5175 &n_bytes_returned,
5176 0 /* we're not using OVERLAPPED_IO */
5177 );
5178 CloseHandle(reparse_point_handle);
5179 Py_END_ALLOW_THREADS
5180
5181 if (io_result==0)
5182 {
5183 return win32_error_unicode("readlink", path);
5184 }
5185
5186 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5187 {
5188 PyErr_SetString(PyExc_ValueError,
5189 "not a symbolic link");
5190 return NULL;
5191 }
Brian Curtin74e45612010-07-09 15:58:59 +00005192 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5193 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5194
5195 result = PyUnicode_FromWideChar(print_name,
5196 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005197 return result;
5198}
5199
5200#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5201
Brian Curtin52173d42010-12-02 18:29:18 +00005202#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005203
5204/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5205static int has_CreateSymbolicLinkW = 0;
5206static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5207static int
5208check_CreateSymbolicLinkW()
5209{
5210 HINSTANCE hKernel32;
5211 /* only recheck */
5212 if (has_CreateSymbolicLinkW)
5213 return has_CreateSymbolicLinkW;
5214 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005215 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5216 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005217 if (Py_CreateSymbolicLinkW)
5218 has_CreateSymbolicLinkW = 1;
5219 return has_CreateSymbolicLinkW;
5220}
5221
5222PyDoc_STRVAR(win_symlink__doc__,
5223"symlink(src, dst, target_is_directory=False)\n\n\
5224Create a symbolic link pointing to src named dst.\n\
5225target_is_directory is required if the target is to be interpreted as\n\
5226a directory.\n\
5227This function requires Windows 6.0 or greater, and raises a\n\
5228NotImplementedError otherwise.");
5229
5230static PyObject *
5231win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5232{
5233 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5234 PyObject *src, *dest;
5235 int target_is_directory = 0;
5236 DWORD res;
5237 WIN32_FILE_ATTRIBUTE_DATA src_info;
5238
5239 if (!check_CreateSymbolicLinkW())
5240 {
5241 /* raise NotImplementedError */
5242 return PyErr_Format(PyExc_NotImplementedError,
5243 "CreateSymbolicLinkW not found");
5244 }
5245 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5246 kwlist, &src, &dest, &target_is_directory))
5247 return NULL;
5248 if (!convert_to_unicode(&src)) { return NULL; }
5249 if (!convert_to_unicode(&dest)) {
5250 Py_DECREF(src);
5251 return NULL;
5252 }
5253
5254 /* if src is a directory, ensure target_is_directory==1 */
5255 if(
5256 GetFileAttributesExW(
5257 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5258 ))
5259 {
5260 target_is_directory = target_is_directory ||
5261 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5262 }
5263
5264 Py_BEGIN_ALLOW_THREADS
5265 res = Py_CreateSymbolicLinkW(
5266 PyUnicode_AsUnicode(dest),
5267 PyUnicode_AsUnicode(src),
5268 target_is_directory);
5269 Py_END_ALLOW_THREADS
5270 Py_DECREF(src);
5271 Py_DECREF(dest);
5272 if (!res)
5273 {
5274 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5275 }
5276
5277 Py_INCREF(Py_None);
5278 return Py_None;
5279}
Brian Curtin52173d42010-12-02 18:29:18 +00005280#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005281
5282#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005283#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5284static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005285system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005286{
5287 ULONG value = 0;
5288
5289 Py_BEGIN_ALLOW_THREADS
5290 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5291 Py_END_ALLOW_THREADS
5292
5293 return value;
5294}
5295
5296static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005297posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005298{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005299 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005300 return Py_BuildValue("ddddd",
5301 (double)0 /* t.tms_utime / HZ */,
5302 (double)0 /* t.tms_stime / HZ */,
5303 (double)0 /* t.tms_cutime / HZ */,
5304 (double)0 /* t.tms_cstime / HZ */,
5305 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005306}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005307#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005308#define NEED_TICKS_PER_SECOND
5309static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005310static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005311posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005312{
Victor Stinner8c62be82010-05-06 00:08:46 +00005313 struct tms t;
5314 clock_t c;
5315 errno = 0;
5316 c = times(&t);
5317 if (c == (clock_t) -1)
5318 return posix_error();
5319 return Py_BuildValue("ddddd",
5320 (double)t.tms_utime / ticks_per_second,
5321 (double)t.tms_stime / ticks_per_second,
5322 (double)t.tms_cutime / ticks_per_second,
5323 (double)t.tms_cstime / ticks_per_second,
5324 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005325}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005326#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005327#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005328
5329
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005330#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005331#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005332static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005333posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005334{
Victor Stinner8c62be82010-05-06 00:08:46 +00005335 FILETIME create, exit, kernel, user;
5336 HANDLE hProc;
5337 hProc = GetCurrentProcess();
5338 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5339 /* The fields of a FILETIME structure are the hi and lo part
5340 of a 64-bit value expressed in 100 nanosecond units.
5341 1e7 is one second in such units; 1e-7 the inverse.
5342 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5343 */
5344 return Py_BuildValue(
5345 "ddddd",
5346 (double)(user.dwHighDateTime*429.4967296 +
5347 user.dwLowDateTime*1e-7),
5348 (double)(kernel.dwHighDateTime*429.4967296 +
5349 kernel.dwLowDateTime*1e-7),
5350 (double)0,
5351 (double)0,
5352 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005353}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005354#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005355
5356#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005357PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005358"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005359Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005360#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005361
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005362
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005363#ifdef HAVE_GETSID
5364PyDoc_STRVAR(posix_getsid__doc__,
5365"getsid(pid) -> sid\n\n\
5366Call the system call getsid().");
5367
5368static PyObject *
5369posix_getsid(PyObject *self, PyObject *args)
5370{
Victor Stinner8c62be82010-05-06 00:08:46 +00005371 pid_t pid;
5372 int sid;
5373 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5374 return NULL;
5375 sid = getsid(pid);
5376 if (sid < 0)
5377 return posix_error();
5378 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005379}
5380#endif /* HAVE_GETSID */
5381
5382
Guido van Rossumb6775db1994-08-01 11:34:53 +00005383#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005384PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005385"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005386Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005387
Barry Warsaw53699e91996-12-10 23:23:01 +00005388static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005389posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005390{
Victor Stinner8c62be82010-05-06 00:08:46 +00005391 if (setsid() < 0)
5392 return posix_error();
5393 Py_INCREF(Py_None);
5394 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005395}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005396#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005397
Guido van Rossumb6775db1994-08-01 11:34:53 +00005398#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005399PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005400"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005401Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005402
Barry Warsaw53699e91996-12-10 23:23:01 +00005403static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005404posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005405{
Victor Stinner8c62be82010-05-06 00:08:46 +00005406 pid_t pid;
5407 int pgrp;
5408 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5409 return NULL;
5410 if (setpgid(pid, pgrp) < 0)
5411 return posix_error();
5412 Py_INCREF(Py_None);
5413 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005414}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005415#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005416
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005417
Guido van Rossumb6775db1994-08-01 11:34:53 +00005418#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005419PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005420"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005421Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005422
Barry Warsaw53699e91996-12-10 23:23:01 +00005423static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005424posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005425{
Victor Stinner8c62be82010-05-06 00:08:46 +00005426 int fd;
5427 pid_t pgid;
5428 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5429 return NULL;
5430 pgid = tcgetpgrp(fd);
5431 if (pgid < 0)
5432 return posix_error();
5433 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005434}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005435#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005436
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005437
Guido van Rossumb6775db1994-08-01 11:34:53 +00005438#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005439PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005440"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005441Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005442
Barry Warsaw53699e91996-12-10 23:23:01 +00005443static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005444posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005445{
Victor Stinner8c62be82010-05-06 00:08:46 +00005446 int fd;
5447 pid_t pgid;
5448 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5449 return NULL;
5450 if (tcsetpgrp(fd, pgid) < 0)
5451 return posix_error();
5452 Py_INCREF(Py_None);
5453 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005454}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005455#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005456
Guido van Rossum687dd131993-05-17 08:34:16 +00005457/* Functions acting on file descriptors */
5458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005459PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005460"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005461Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005462
Barry Warsaw53699e91996-12-10 23:23:01 +00005463static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005464posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005465{
Victor Stinner8c62be82010-05-06 00:08:46 +00005466 PyObject *ofile;
5467 char *file;
5468 int flag;
5469 int mode = 0777;
5470 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005471
5472#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005473 PyUnicodeObject *po;
5474 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5475 Py_BEGIN_ALLOW_THREADS
5476 /* PyUnicode_AS_UNICODE OK without thread
5477 lock as it is a simple dereference. */
5478 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5479 Py_END_ALLOW_THREADS
5480 if (fd < 0)
5481 return posix_error();
5482 return PyLong_FromLong((long)fd);
5483 }
5484 /* Drop the argument parsing error as narrow strings
5485 are also valid. */
5486 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005487#endif
5488
Victor Stinner8c62be82010-05-06 00:08:46 +00005489 if (!PyArg_ParseTuple(args, "O&i|i",
5490 PyUnicode_FSConverter, &ofile,
5491 &flag, &mode))
5492 return NULL;
5493 file = PyBytes_AsString(ofile);
5494 Py_BEGIN_ALLOW_THREADS
5495 fd = open(file, flag, mode);
5496 Py_END_ALLOW_THREADS
5497 if (fd < 0)
5498 return posix_error_with_allocated_filename(ofile);
5499 Py_DECREF(ofile);
5500 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005501}
5502
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005505"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005506Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005507
Barry Warsaw53699e91996-12-10 23:23:01 +00005508static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005509posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005510{
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 int fd, res;
5512 if (!PyArg_ParseTuple(args, "i:close", &fd))
5513 return NULL;
5514 if (!_PyVerify_fd(fd))
5515 return posix_error();
5516 Py_BEGIN_ALLOW_THREADS
5517 res = close(fd);
5518 Py_END_ALLOW_THREADS
5519 if (res < 0)
5520 return posix_error();
5521 Py_INCREF(Py_None);
5522 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005523}
5524
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005525
Victor Stinner8c62be82010-05-06 00:08:46 +00005526PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005527"closerange(fd_low, fd_high)\n\n\
5528Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5529
5530static PyObject *
5531posix_closerange(PyObject *self, PyObject *args)
5532{
Victor Stinner8c62be82010-05-06 00:08:46 +00005533 int fd_from, fd_to, i;
5534 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5535 return NULL;
5536 Py_BEGIN_ALLOW_THREADS
5537 for (i = fd_from; i < fd_to; i++)
5538 if (_PyVerify_fd(i))
5539 close(i);
5540 Py_END_ALLOW_THREADS
5541 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005542}
5543
5544
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005545PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005546"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005547Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005548
Barry Warsaw53699e91996-12-10 23:23:01 +00005549static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005550posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005551{
Victor Stinner8c62be82010-05-06 00:08:46 +00005552 int fd;
5553 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5554 return NULL;
5555 if (!_PyVerify_fd(fd))
5556 return posix_error();
5557 Py_BEGIN_ALLOW_THREADS
5558 fd = dup(fd);
5559 Py_END_ALLOW_THREADS
5560 if (fd < 0)
5561 return posix_error();
5562 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005563}
5564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005565
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005567"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005568Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005569
Barry Warsaw53699e91996-12-10 23:23:01 +00005570static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005571posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005572{
Victor Stinner8c62be82010-05-06 00:08:46 +00005573 int fd, fd2, res;
5574 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5575 return NULL;
5576 if (!_PyVerify_fd_dup2(fd, fd2))
5577 return posix_error();
5578 Py_BEGIN_ALLOW_THREADS
5579 res = dup2(fd, fd2);
5580 Py_END_ALLOW_THREADS
5581 if (res < 0)
5582 return posix_error();
5583 Py_INCREF(Py_None);
5584 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005585}
5586
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005588PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005589"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005590Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005591
Barry Warsaw53699e91996-12-10 23:23:01 +00005592static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005593posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005594{
Victor Stinner8c62be82010-05-06 00:08:46 +00005595 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005596#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005597 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005598#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005599 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005600#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005601 PyObject *posobj;
5602 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5603 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005604#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005605 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5606 switch (how) {
5607 case 0: how = SEEK_SET; break;
5608 case 1: how = SEEK_CUR; break;
5609 case 2: how = SEEK_END; break;
5610 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005611#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005612
5613#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005615#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005616 pos = PyLong_Check(posobj) ?
5617 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005618#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005619 if (PyErr_Occurred())
5620 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005621
Victor Stinner8c62be82010-05-06 00:08:46 +00005622 if (!_PyVerify_fd(fd))
5623 return posix_error();
5624 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005625#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005626 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005627#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005628 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005629#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005630 Py_END_ALLOW_THREADS
5631 if (res < 0)
5632 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005633
5634#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005636#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005637 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005638#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005639}
5640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005642PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005643"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005644Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005645
Barry Warsaw53699e91996-12-10 23:23:01 +00005646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005647posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005648{
Victor Stinner8c62be82010-05-06 00:08:46 +00005649 int fd, size;
5650 Py_ssize_t n;
5651 PyObject *buffer;
5652 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5653 return NULL;
5654 if (size < 0) {
5655 errno = EINVAL;
5656 return posix_error();
5657 }
5658 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5659 if (buffer == NULL)
5660 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005661 if (!_PyVerify_fd(fd)) {
5662 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005663 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005664 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005665 Py_BEGIN_ALLOW_THREADS
5666 n = read(fd, PyBytes_AS_STRING(buffer), size);
5667 Py_END_ALLOW_THREADS
5668 if (n < 0) {
5669 Py_DECREF(buffer);
5670 return posix_error();
5671 }
5672 if (n != size)
5673 _PyBytes_Resize(&buffer, n);
5674 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005675}
5676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005678PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005679"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005680Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005681
Barry Warsaw53699e91996-12-10 23:23:01 +00005682static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005683posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005684{
Victor Stinner8c62be82010-05-06 00:08:46 +00005685 Py_buffer pbuf;
5686 int fd;
5687 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005688
Victor Stinner8c62be82010-05-06 00:08:46 +00005689 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5690 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005691 if (!_PyVerify_fd(fd)) {
5692 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005693 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005694 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005695 Py_BEGIN_ALLOW_THREADS
5696 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5697 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005698 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005699 if (size < 0)
5700 return posix_error();
5701 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005702}
5703
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005705PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005706"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005707Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005708
Barry Warsaw53699e91996-12-10 23:23:01 +00005709static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005710posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005711{
Victor Stinner8c62be82010-05-06 00:08:46 +00005712 int fd;
5713 STRUCT_STAT st;
5714 int res;
5715 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5716 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005717#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005718 /* on OpenVMS we must ensure that all bytes are written to the file */
5719 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005720#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005721 if (!_PyVerify_fd(fd))
5722 return posix_error();
5723 Py_BEGIN_ALLOW_THREADS
5724 res = FSTAT(fd, &st);
5725 Py_END_ALLOW_THREADS
5726 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005727#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005728 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005729#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005730 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005731#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005732 }
Tim Peters5aa91602002-01-30 05:46:57 +00005733
Victor Stinner8c62be82010-05-06 00:08:46 +00005734 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005735}
5736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005737PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005738"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005739Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005740connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005741
5742static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005743posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005744{
Victor Stinner8c62be82010-05-06 00:08:46 +00005745 int fd;
5746 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5747 return NULL;
5748 if (!_PyVerify_fd(fd))
5749 return PyBool_FromLong(0);
5750 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005751}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005752
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005753#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005754PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005755"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005756Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005757
Barry Warsaw53699e91996-12-10 23:23:01 +00005758static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005759posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005760{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005761#if defined(PYOS_OS2)
5762 HFILE read, write;
5763 APIRET rc;
5764
Victor Stinner8c62be82010-05-06 00:08:46 +00005765 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005766 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005767 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005768 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005769 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005770
5771 return Py_BuildValue("(ii)", read, write);
5772#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005773#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005774 int fds[2];
5775 int res;
5776 Py_BEGIN_ALLOW_THREADS
5777 res = pipe(fds);
5778 Py_END_ALLOW_THREADS
5779 if (res != 0)
5780 return posix_error();
5781 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005782#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005783 HANDLE read, write;
5784 int read_fd, write_fd;
5785 BOOL ok;
5786 Py_BEGIN_ALLOW_THREADS
5787 ok = CreatePipe(&read, &write, NULL, 0);
5788 Py_END_ALLOW_THREADS
5789 if (!ok)
5790 return win32_error("CreatePipe", NULL);
5791 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5792 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5793 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005794#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005795#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005796}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005797#endif /* HAVE_PIPE */
5798
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005799
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005800#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005802"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005803Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005804
Barry Warsaw53699e91996-12-10 23:23:01 +00005805static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005806posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005807{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005808 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005809 char *filename;
5810 int mode = 0666;
5811 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005812 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5813 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005814 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005815 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005816 Py_BEGIN_ALLOW_THREADS
5817 res = mkfifo(filename, mode);
5818 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005819 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005820 if (res < 0)
5821 return posix_error();
5822 Py_INCREF(Py_None);
5823 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005824}
5825#endif
5826
5827
Neal Norwitz11690112002-07-30 01:08:28 +00005828#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005829PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005830"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005831Create a filesystem node (file, device special file or named pipe)\n\
5832named filename. mode specifies both the permissions to use and the\n\
5833type of node to be created, being combined (bitwise OR) with one of\n\
5834S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005835device defines the newly created device special file (probably using\n\
5836os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005837
5838
5839static PyObject *
5840posix_mknod(PyObject *self, PyObject *args)
5841{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005842 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005843 char *filename;
5844 int mode = 0600;
5845 int device = 0;
5846 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005847 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5848 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005849 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005850 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005851 Py_BEGIN_ALLOW_THREADS
5852 res = mknod(filename, mode, device);
5853 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005854 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005855 if (res < 0)
5856 return posix_error();
5857 Py_INCREF(Py_None);
5858 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005859}
5860#endif
5861
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005862#ifdef HAVE_DEVICE_MACROS
5863PyDoc_STRVAR(posix_major__doc__,
5864"major(device) -> major number\n\
5865Extracts a device major number from a raw device number.");
5866
5867static PyObject *
5868posix_major(PyObject *self, PyObject *args)
5869{
Victor Stinner8c62be82010-05-06 00:08:46 +00005870 int device;
5871 if (!PyArg_ParseTuple(args, "i:major", &device))
5872 return NULL;
5873 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005874}
5875
5876PyDoc_STRVAR(posix_minor__doc__,
5877"minor(device) -> minor number\n\
5878Extracts a device minor number from a raw device number.");
5879
5880static PyObject *
5881posix_minor(PyObject *self, PyObject *args)
5882{
Victor Stinner8c62be82010-05-06 00:08:46 +00005883 int device;
5884 if (!PyArg_ParseTuple(args, "i:minor", &device))
5885 return NULL;
5886 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005887}
5888
5889PyDoc_STRVAR(posix_makedev__doc__,
5890"makedev(major, minor) -> device number\n\
5891Composes a raw device number from the major and minor device numbers.");
5892
5893static PyObject *
5894posix_makedev(PyObject *self, PyObject *args)
5895{
Victor Stinner8c62be82010-05-06 00:08:46 +00005896 int major, minor;
5897 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5898 return NULL;
5899 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005900}
5901#endif /* device macros */
5902
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005903
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005904#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005905PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005906"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005907Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005908
Barry Warsaw53699e91996-12-10 23:23:01 +00005909static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005910posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005911{
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 int fd;
5913 off_t length;
5914 int res;
5915 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005916
Victor Stinner8c62be82010-05-06 00:08:46 +00005917 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5918 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005919
5920#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005922#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005923 length = PyLong_Check(lenobj) ?
5924 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005925#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005926 if (PyErr_Occurred())
5927 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005928
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 Py_BEGIN_ALLOW_THREADS
5930 res = ftruncate(fd, length);
5931 Py_END_ALLOW_THREADS
5932 if (res < 0)
5933 return posix_error();
5934 Py_INCREF(Py_None);
5935 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005936}
5937#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005938
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005939#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005940PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005941"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005942Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005943
Fred Drake762e2061999-08-26 17:23:54 +00005944/* Save putenv() parameters as values here, so we can collect them when they
5945 * get re-set with another call for the same key. */
5946static PyObject *posix_putenv_garbage;
5947
Tim Peters5aa91602002-01-30 05:46:57 +00005948static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005949posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005950{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005951#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005952 wchar_t *s1, *s2;
5953 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005954#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005955 PyObject *os1, *os2;
5956 char *s1, *s2;
5957 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005958#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005959 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005960 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005961
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005962#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005963 if (!PyArg_ParseTuple(args,
5964 "uu:putenv",
5965 &s1, &s2))
5966 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005967#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005968 if (!PyArg_ParseTuple(args,
5969 "O&O&:putenv",
5970 PyUnicode_FSConverter, &os1,
5971 PyUnicode_FSConverter, &os2))
5972 return NULL;
5973 s1 = PyBytes_AsString(os1);
5974 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005975#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005976
5977#if defined(PYOS_OS2)
5978 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5979 APIRET rc;
5980
Guido van Rossumd48f2521997-12-05 22:19:34 +00005981 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005982 if (rc != NO_ERROR) {
5983 os2_error(rc);
5984 goto error;
5985 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005986
5987 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5988 APIRET rc;
5989
Guido van Rossumd48f2521997-12-05 22:19:34 +00005990 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005991 if (rc != NO_ERROR) {
5992 os2_error(rc);
5993 goto error;
5994 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005995 } else {
5996#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005997 /* XXX This can leak memory -- not easy to fix :-( */
5998 /* len includes space for a trailing \0; the size arg to
5999 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006000#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006001 len = wcslen(s1) + wcslen(s2) + 2;
6002 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006003#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006004 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006006#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006007 if (newstr == NULL) {
6008 PyErr_NoMemory();
6009 goto error;
6010 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006011#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006012 newenv = PyUnicode_AsUnicode(newstr);
6013 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6014 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006016 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006017 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006018#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 newenv = PyBytes_AS_STRING(newstr);
6020 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6021 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006022 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006023 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006024 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006025#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006026
Victor Stinner8c62be82010-05-06 00:08:46 +00006027 /* Install the first arg and newstr in posix_putenv_garbage;
6028 * this will cause previous value to be collected. This has to
6029 * happen after the real putenv() call because the old value
6030 * was still accessible until then. */
6031 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006032#ifdef MS_WINDOWS
6033 PyTuple_GET_ITEM(args, 0),
6034#else
6035 os1,
6036#endif
6037 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006038 /* really not much we can do; just leak */
6039 PyErr_Clear();
6040 }
6041 else {
6042 Py_DECREF(newstr);
6043 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006044
6045#if defined(PYOS_OS2)
6046 }
6047#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006048
Martin v. Löwis011e8422009-05-05 04:43:17 +00006049#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006050 Py_DECREF(os1);
6051 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006052#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006053 Py_RETURN_NONE;
6054
6055error:
6056#ifndef MS_WINDOWS
6057 Py_DECREF(os1);
6058 Py_DECREF(os2);
6059#endif
6060 Py_XDECREF(newstr);
6061 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006062}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006063#endif /* putenv */
6064
Guido van Rossumc524d952001-10-19 01:31:59 +00006065#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006066PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006067"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006068Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006069
6070static PyObject *
6071posix_unsetenv(PyObject *self, PyObject *args)
6072{
Victor Stinner84ae1182010-05-06 22:05:07 +00006073#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006074 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006075
Victor Stinner8c62be82010-05-06 00:08:46 +00006076 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6077 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006078#else
6079 PyObject *os1;
6080 char *s1;
6081
6082 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6083 PyUnicode_FSConverter, &os1))
6084 return NULL;
6085 s1 = PyBytes_AsString(os1);
6086#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006087
Victor Stinner8c62be82010-05-06 00:08:46 +00006088 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006089
Victor Stinner8c62be82010-05-06 00:08:46 +00006090 /* Remove the key from posix_putenv_garbage;
6091 * this will cause it to be collected. This has to
6092 * happen after the real unsetenv() call because the
6093 * old value was still accessible until then.
6094 */
6095 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006096#ifdef MS_WINDOWS
6097 PyTuple_GET_ITEM(args, 0)
6098#else
6099 os1
6100#endif
6101 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006102 /* really not much we can do; just leak */
6103 PyErr_Clear();
6104 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006105
Victor Stinner84ae1182010-05-06 22:05:07 +00006106#ifndef MS_WINDOWS
6107 Py_DECREF(os1);
6108#endif
6109 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006110}
6111#endif /* unsetenv */
6112
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006113PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006114"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006115Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006116
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006118posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006119{
Victor Stinner8c62be82010-05-06 00:08:46 +00006120 int code;
6121 char *message;
6122 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6123 return NULL;
6124 message = strerror(code);
6125 if (message == NULL) {
6126 PyErr_SetString(PyExc_ValueError,
6127 "strerror() argument out of range");
6128 return NULL;
6129 }
6130 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006131}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006132
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006133
Guido van Rossumc9641791998-08-04 15:26:23 +00006134#ifdef HAVE_SYS_WAIT_H
6135
Fred Drake106c1a02002-04-23 15:58:02 +00006136#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006137PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006138"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006139Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006140
6141static PyObject *
6142posix_WCOREDUMP(PyObject *self, PyObject *args)
6143{
Victor Stinner8c62be82010-05-06 00:08:46 +00006144 WAIT_TYPE status;
6145 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006146
Victor Stinner8c62be82010-05-06 00:08:46 +00006147 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6148 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006149
Victor Stinner8c62be82010-05-06 00:08:46 +00006150 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006151}
6152#endif /* WCOREDUMP */
6153
6154#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006155PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006156"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006157Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006158job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006159
6160static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006161posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006162{
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 WAIT_TYPE status;
6164 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006165
Victor Stinner8c62be82010-05-06 00:08:46 +00006166 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6167 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006168
Victor Stinner8c62be82010-05-06 00:08:46 +00006169 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006170}
6171#endif /* WIFCONTINUED */
6172
Guido van Rossumc9641791998-08-04 15:26:23 +00006173#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006174PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006175"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006176Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006177
6178static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006179posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006180{
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 WAIT_TYPE status;
6182 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006183
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6185 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006186
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006188}
6189#endif /* WIFSTOPPED */
6190
6191#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006192PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006193"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006194Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006195
6196static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006197posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006198{
Victor Stinner8c62be82010-05-06 00:08:46 +00006199 WAIT_TYPE status;
6200 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006201
Victor Stinner8c62be82010-05-06 00:08:46 +00006202 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6203 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006204
Victor Stinner8c62be82010-05-06 00:08:46 +00006205 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006206}
6207#endif /* WIFSIGNALED */
6208
6209#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006210PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006211"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006212Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006213system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006214
6215static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006216posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006217{
Victor Stinner8c62be82010-05-06 00:08:46 +00006218 WAIT_TYPE status;
6219 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006220
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6222 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006223
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006225}
6226#endif /* WIFEXITED */
6227
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006228#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006229PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006230"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006231Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006232
6233static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006234posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006235{
Victor Stinner8c62be82010-05-06 00:08:46 +00006236 WAIT_TYPE status;
6237 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006238
Victor Stinner8c62be82010-05-06 00:08:46 +00006239 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6240 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006241
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006243}
6244#endif /* WEXITSTATUS */
6245
6246#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006247PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006248"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006249Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006250value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006251
6252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006253posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006254{
Victor Stinner8c62be82010-05-06 00:08:46 +00006255 WAIT_TYPE status;
6256 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006257
Victor Stinner8c62be82010-05-06 00:08:46 +00006258 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6259 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006260
Victor Stinner8c62be82010-05-06 00:08:46 +00006261 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006262}
6263#endif /* WTERMSIG */
6264
6265#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006266PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006267"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006268Return the signal that stopped the process that provided\n\
6269the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006270
6271static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006272posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006273{
Victor Stinner8c62be82010-05-06 00:08:46 +00006274 WAIT_TYPE status;
6275 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006276
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6278 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006279
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006281}
6282#endif /* WSTOPSIG */
6283
6284#endif /* HAVE_SYS_WAIT_H */
6285
6286
Thomas Wouters477c8d52006-05-27 19:21:47 +00006287#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006288#ifdef _SCO_DS
6289/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6290 needed definitions in sys/statvfs.h */
6291#define _SVID3
6292#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006293#include <sys/statvfs.h>
6294
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006295static PyObject*
6296_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006297 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6298 if (v == NULL)
6299 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006300
6301#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006302 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6303 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6304 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6305 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6306 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6307 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6308 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6309 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6310 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6311 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006312#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6314 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6315 PyStructSequence_SET_ITEM(v, 2,
6316 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6317 PyStructSequence_SET_ITEM(v, 3,
6318 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6319 PyStructSequence_SET_ITEM(v, 4,
6320 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6321 PyStructSequence_SET_ITEM(v, 5,
6322 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6323 PyStructSequence_SET_ITEM(v, 6,
6324 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6325 PyStructSequence_SET_ITEM(v, 7,
6326 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6327 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6328 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006329#endif
6330
Victor Stinner8c62be82010-05-06 00:08:46 +00006331 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006332}
6333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006334PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006335"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006336Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006337
6338static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006339posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006340{
Victor Stinner8c62be82010-05-06 00:08:46 +00006341 int fd, res;
6342 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006343
Victor Stinner8c62be82010-05-06 00:08:46 +00006344 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6345 return NULL;
6346 Py_BEGIN_ALLOW_THREADS
6347 res = fstatvfs(fd, &st);
6348 Py_END_ALLOW_THREADS
6349 if (res != 0)
6350 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006351
Victor Stinner8c62be82010-05-06 00:08:46 +00006352 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006353}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006354#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006355
6356
Thomas Wouters477c8d52006-05-27 19:21:47 +00006357#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006358#include <sys/statvfs.h>
6359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006360PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006361"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006362Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006363
6364static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006365posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006366{
Victor Stinner8c62be82010-05-06 00:08:46 +00006367 char *path;
6368 int res;
6369 struct statvfs st;
6370 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6371 return NULL;
6372 Py_BEGIN_ALLOW_THREADS
6373 res = statvfs(path, &st);
6374 Py_END_ALLOW_THREADS
6375 if (res != 0)
6376 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006377
Victor Stinner8c62be82010-05-06 00:08:46 +00006378 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006379}
6380#endif /* HAVE_STATVFS */
6381
Fred Drakec9680921999-12-13 16:37:25 +00006382/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6383 * It maps strings representing configuration variable names to
6384 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006385 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006386 * rarely-used constants. There are three separate tables that use
6387 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006388 *
6389 * This code is always included, even if none of the interfaces that
6390 * need it are included. The #if hackery needed to avoid it would be
6391 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006392 */
6393struct constdef {
6394 char *name;
6395 long value;
6396};
6397
Fred Drake12c6e2d1999-12-14 21:25:03 +00006398static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006399conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006400 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006401{
Christian Heimes217cfd12007-12-02 14:31:20 +00006402 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006403 *valuep = PyLong_AS_LONG(arg);
6404 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006405 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006406 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006407 /* look up the value in the table using a binary search */
6408 size_t lo = 0;
6409 size_t mid;
6410 size_t hi = tablesize;
6411 int cmp;
6412 const char *confname;
6413 if (!PyUnicode_Check(arg)) {
6414 PyErr_SetString(PyExc_TypeError,
6415 "configuration names must be strings or integers");
6416 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006417 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006418 confname = _PyUnicode_AsString(arg);
6419 if (confname == NULL)
6420 return 0;
6421 while (lo < hi) {
6422 mid = (lo + hi) / 2;
6423 cmp = strcmp(confname, table[mid].name);
6424 if (cmp < 0)
6425 hi = mid;
6426 else if (cmp > 0)
6427 lo = mid + 1;
6428 else {
6429 *valuep = table[mid].value;
6430 return 1;
6431 }
6432 }
6433 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6434 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006435 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006436}
6437
6438
6439#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6440static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006441#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006442 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006443#endif
6444#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006445 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006446#endif
Fred Drakec9680921999-12-13 16:37:25 +00006447#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006448 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006449#endif
6450#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006451 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006452#endif
6453#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006454 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006455#endif
6456#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006457 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006458#endif
6459#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006460 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006461#endif
6462#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006463 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006464#endif
6465#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006466 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006467#endif
6468#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006469 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006470#endif
6471#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006473#endif
6474#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006475 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006476#endif
6477#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006479#endif
6480#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006482#endif
6483#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006484 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006485#endif
6486#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006488#endif
6489#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006490 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006491#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006492#ifdef _PC_ACL_ENABLED
6493 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6494#endif
6495#ifdef _PC_MIN_HOLE_SIZE
6496 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6497#endif
6498#ifdef _PC_ALLOC_SIZE_MIN
6499 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6500#endif
6501#ifdef _PC_REC_INCR_XFER_SIZE
6502 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6503#endif
6504#ifdef _PC_REC_MAX_XFER_SIZE
6505 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6506#endif
6507#ifdef _PC_REC_MIN_XFER_SIZE
6508 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6509#endif
6510#ifdef _PC_REC_XFER_ALIGN
6511 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6512#endif
6513#ifdef _PC_SYMLINK_MAX
6514 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6515#endif
6516#ifdef _PC_XATTR_ENABLED
6517 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6518#endif
6519#ifdef _PC_XATTR_EXISTS
6520 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6521#endif
6522#ifdef _PC_TIMESTAMP_RESOLUTION
6523 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6524#endif
Fred Drakec9680921999-12-13 16:37:25 +00006525};
6526
Fred Drakec9680921999-12-13 16:37:25 +00006527static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006528conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006529{
6530 return conv_confname(arg, valuep, posix_constants_pathconf,
6531 sizeof(posix_constants_pathconf)
6532 / sizeof(struct constdef));
6533}
6534#endif
6535
6536#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006537PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006538"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006539Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006540If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006541
6542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006543posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006544{
6545 PyObject *result = NULL;
6546 int name, fd;
6547
Fred Drake12c6e2d1999-12-14 21:25:03 +00006548 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6549 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006550 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006551
Stefan Krah0e803b32010-11-26 16:16:47 +00006552 errno = 0;
6553 limit = fpathconf(fd, name);
6554 if (limit == -1 && errno != 0)
6555 posix_error();
6556 else
6557 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006558 }
6559 return result;
6560}
6561#endif
6562
6563
6564#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006565PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006566"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006567Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006568If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006569
6570static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006571posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006572{
6573 PyObject *result = NULL;
6574 int name;
6575 char *path;
6576
6577 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6578 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006579 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006580
Victor Stinner8c62be82010-05-06 00:08:46 +00006581 errno = 0;
6582 limit = pathconf(path, name);
6583 if (limit == -1 && errno != 0) {
6584 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006585 /* could be a path or name problem */
6586 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006587 else
Stefan Krah99439262010-11-26 12:58:05 +00006588 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006589 }
6590 else
6591 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006592 }
6593 return result;
6594}
6595#endif
6596
6597#ifdef HAVE_CONFSTR
6598static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006599#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006600 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006601#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006602#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006603 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006604#endif
6605#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006606 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006607#endif
Fred Draked86ed291999-12-15 15:34:33 +00006608#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006609 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006610#endif
6611#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006612 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006613#endif
6614#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006615 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006616#endif
6617#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006618 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006619#endif
Fred Drakec9680921999-12-13 16:37:25 +00006620#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006621 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006622#endif
6623#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006624 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006625#endif
6626#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006627 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006628#endif
6629#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006630 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006631#endif
6632#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006633 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006634#endif
6635#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006637#endif
6638#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006639 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006640#endif
6641#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006642 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006643#endif
Fred Draked86ed291999-12-15 15:34:33 +00006644#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006646#endif
Fred Drakec9680921999-12-13 16:37:25 +00006647#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006648 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006649#endif
Fred Draked86ed291999-12-15 15:34:33 +00006650#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006651 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006652#endif
6653#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006654 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006655#endif
6656#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006657 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006658#endif
6659#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006660 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006661#endif
Fred Drakec9680921999-12-13 16:37:25 +00006662#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006663 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006664#endif
6665#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006666 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006667#endif
6668#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006669 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006670#endif
6671#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006672 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006673#endif
6674#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006675 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006676#endif
6677#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006678 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006679#endif
6680#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006681 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006682#endif
6683#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006684 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006685#endif
6686#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006687 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006688#endif
6689#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006690 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006691#endif
6692#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006693 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006694#endif
6695#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006696 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006697#endif
6698#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006700#endif
6701#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006702 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006703#endif
6704#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006705 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006706#endif
6707#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006709#endif
Fred Draked86ed291999-12-15 15:34:33 +00006710#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006711 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006712#endif
6713#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006714 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006715#endif
6716#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006718#endif
6719#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006721#endif
6722#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006724#endif
6725#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006727#endif
6728#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006729 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006730#endif
6731#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006732 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006733#endif
6734#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006735 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006736#endif
6737#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006738 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006739#endif
6740#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006741 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006742#endif
6743#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006744 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006745#endif
6746#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006748#endif
Fred Drakec9680921999-12-13 16:37:25 +00006749};
6750
6751static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006752conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006753{
6754 return conv_confname(arg, valuep, posix_constants_confstr,
6755 sizeof(posix_constants_confstr)
6756 / sizeof(struct constdef));
6757}
6758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006759PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006760"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006761Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006762
6763static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006764posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006765{
6766 PyObject *result = NULL;
6767 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006768 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006769 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006770
Victor Stinnercb043522010-09-10 23:49:04 +00006771 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6772 return NULL;
6773
6774 errno = 0;
6775 len = confstr(name, buffer, sizeof(buffer));
6776 if (len == 0) {
6777 if (errno) {
6778 posix_error();
6779 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006780 }
6781 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006782 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006783 }
6784 }
Victor Stinnercb043522010-09-10 23:49:04 +00006785
6786 if ((unsigned int)len >= sizeof(buffer)) {
6787 char *buf = PyMem_Malloc(len);
6788 if (buf == NULL)
6789 return PyErr_NoMemory();
6790 confstr(name, buf, len);
6791 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6792 PyMem_Free(buf);
6793 }
6794 else
6795 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006796 return result;
6797}
6798#endif
6799
6800
6801#ifdef HAVE_SYSCONF
6802static struct constdef posix_constants_sysconf[] = {
6803#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006805#endif
6806#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006807 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006808#endif
6809#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006810 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006811#endif
6812#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006813 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006814#endif
6815#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006816 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006817#endif
6818#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006820#endif
6821#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006823#endif
6824#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006825 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006826#endif
6827#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006829#endif
6830#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006831 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006832#endif
Fred Draked86ed291999-12-15 15:34:33 +00006833#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006834 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006835#endif
6836#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006838#endif
Fred Drakec9680921999-12-13 16:37:25 +00006839#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006840 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006841#endif
Fred Drakec9680921999-12-13 16:37:25 +00006842#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006844#endif
6845#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006847#endif
6848#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006849 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006850#endif
6851#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006852 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006853#endif
6854#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006856#endif
Fred Draked86ed291999-12-15 15:34:33 +00006857#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006858 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006859#endif
Fred Drakec9680921999-12-13 16:37:25 +00006860#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006861 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006862#endif
6863#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006865#endif
6866#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006868#endif
6869#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006871#endif
6872#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006873 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006874#endif
Fred Draked86ed291999-12-15 15:34:33 +00006875#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006876 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006877#endif
Fred Drakec9680921999-12-13 16:37:25 +00006878#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006880#endif
6881#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006882 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006883#endif
6884#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006885 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006886#endif
6887#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006888 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006889#endif
6890#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006891 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006892#endif
6893#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006894 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006895#endif
6896#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006897 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006898#endif
6899#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006900 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006901#endif
6902#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006903 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006904#endif
6905#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006906 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006907#endif
6908#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006909 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006910#endif
6911#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006913#endif
6914#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006916#endif
6917#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006919#endif
6920#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006922#endif
6923#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006924 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006925#endif
6926#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006928#endif
6929#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006930 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006931#endif
6932#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006934#endif
6935#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006936 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006937#endif
6938#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006939 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006940#endif
6941#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006942 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006943#endif
6944#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006945 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006946#endif
Fred Draked86ed291999-12-15 15:34:33 +00006947#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006948 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006949#endif
Fred Drakec9680921999-12-13 16:37:25 +00006950#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006951 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006952#endif
6953#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006954 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006955#endif
6956#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006958#endif
Fred Draked86ed291999-12-15 15:34:33 +00006959#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006961#endif
Fred Drakec9680921999-12-13 16:37:25 +00006962#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006963 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006964#endif
Fred Draked86ed291999-12-15 15:34:33 +00006965#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006967#endif
6968#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006969 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006970#endif
Fred Drakec9680921999-12-13 16:37:25 +00006971#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006972 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006973#endif
6974#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006975 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006976#endif
6977#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006978 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006979#endif
6980#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006981 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006982#endif
Fred Draked86ed291999-12-15 15:34:33 +00006983#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006985#endif
Fred Drakec9680921999-12-13 16:37:25 +00006986#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006988#endif
6989#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006991#endif
6992#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006993 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006994#endif
6995#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006996 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006997#endif
6998#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006999 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007000#endif
7001#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007002 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007003#endif
7004#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007006#endif
Fred Draked86ed291999-12-15 15:34:33 +00007007#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007009#endif
Fred Drakec9680921999-12-13 16:37:25 +00007010#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007011 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007012#endif
7013#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007014 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007015#endif
Fred Draked86ed291999-12-15 15:34:33 +00007016#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007017 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007018#endif
Fred Drakec9680921999-12-13 16:37:25 +00007019#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007020 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007021#endif
7022#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007024#endif
7025#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007026 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007027#endif
7028#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007030#endif
7031#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007033#endif
7034#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007035 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007036#endif
7037#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007038 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007039#endif
7040#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007042#endif
7043#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007044 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007045#endif
Fred Draked86ed291999-12-15 15:34:33 +00007046#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007047 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007048#endif
7049#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007050 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007051#endif
Fred Drakec9680921999-12-13 16:37:25 +00007052#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007053 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007054#endif
7055#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007057#endif
7058#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007060#endif
7061#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007063#endif
7064#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007065 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007066#endif
7067#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007069#endif
7070#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007071 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007072#endif
7073#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007075#endif
7076#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007078#endif
7079#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007081#endif
7082#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007084#endif
7085#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007087#endif
7088#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007090#endif
7091#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007093#endif
7094#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007095 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007096#endif
7097#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007099#endif
7100#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007102#endif
7103#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007104 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007105#endif
7106#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007107 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007108#endif
7109#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007111#endif
7112#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007114#endif
7115#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007116 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007117#endif
7118#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007119 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007120#endif
7121#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007123#endif
7124#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007126#endif
7127#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007128 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007129#endif
7130#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007131 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007132#endif
7133#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007134 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007135#endif
7136#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007137 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007138#endif
7139#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007140 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007141#endif
7142#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007143 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007144#endif
7145#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007147#endif
7148#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007149 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007150#endif
7151#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007153#endif
7154#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007155 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007156#endif
Fred Draked86ed291999-12-15 15:34:33 +00007157#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007158 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007159#endif
Fred Drakec9680921999-12-13 16:37:25 +00007160#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007161 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007162#endif
7163#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007164 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007165#endif
7166#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007168#endif
7169#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007171#endif
7172#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007173 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007174#endif
7175#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007176 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007177#endif
7178#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007180#endif
7181#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007182 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007183#endif
7184#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007186#endif
7187#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007188 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007189#endif
7190#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007191 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007192#endif
7193#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007194 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007195#endif
7196#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007197 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007198#endif
7199#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007201#endif
7202#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007204#endif
7205#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007206 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007207#endif
7208#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007209 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007210#endif
7211#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007213#endif
7214#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007215 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007216#endif
7217#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007218 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007219#endif
7220#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007221 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007222#endif
7223#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007225#endif
7226#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007227 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007228#endif
7229#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007230 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007231#endif
7232#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007233 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007234#endif
7235#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007236 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007237#endif
7238#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007240#endif
7241#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007242 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007243#endif
7244#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007246#endif
7247#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007249#endif
7250#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007252#endif
7253#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007255#endif
7256#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007258#endif
7259#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007261#endif
7262#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007263 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007264#endif
7265#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007267#endif
7268#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007269 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007270#endif
7271#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007272 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007273#endif
7274#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007276#endif
7277#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007278 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007279#endif
7280#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007281 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007282#endif
7283#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007284 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007285#endif
7286#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007287 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007288#endif
7289#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007290 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007291#endif
7292#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007293 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007294#endif
7295};
7296
7297static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007298conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007299{
7300 return conv_confname(arg, valuep, posix_constants_sysconf,
7301 sizeof(posix_constants_sysconf)
7302 / sizeof(struct constdef));
7303}
7304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007305PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007306"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007307Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007308
7309static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007310posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007311{
7312 PyObject *result = NULL;
7313 int name;
7314
7315 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7316 int value;
7317
7318 errno = 0;
7319 value = sysconf(name);
7320 if (value == -1 && errno != 0)
7321 posix_error();
7322 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007323 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007324 }
7325 return result;
7326}
7327#endif
7328
7329
Fred Drakebec628d1999-12-15 18:31:10 +00007330/* This code is used to ensure that the tables of configuration value names
7331 * are in sorted order as required by conv_confname(), and also to build the
7332 * the exported dictionaries that are used to publish information about the
7333 * names available on the host platform.
7334 *
7335 * Sorting the table at runtime ensures that the table is properly ordered
7336 * when used, even for platforms we're not able to test on. It also makes
7337 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007338 */
Fred Drakebec628d1999-12-15 18:31:10 +00007339
7340static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007341cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007342{
7343 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007345 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007346 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007347
7348 return strcmp(c1->name, c2->name);
7349}
7350
7351static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007352setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007353 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007354{
Fred Drakebec628d1999-12-15 18:31:10 +00007355 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007356 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007357
7358 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7359 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007360 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007361 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007362
Barry Warsaw3155db32000-04-13 15:20:40 +00007363 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007364 PyObject *o = PyLong_FromLong(table[i].value);
7365 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7366 Py_XDECREF(o);
7367 Py_DECREF(d);
7368 return -1;
7369 }
7370 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007371 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007372 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007373}
7374
Fred Drakebec628d1999-12-15 18:31:10 +00007375/* Return -1 on failure, 0 on success. */
7376static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007377setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007378{
7379#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007380 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007381 sizeof(posix_constants_pathconf)
7382 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007383 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007384 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007385#endif
7386#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007387 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007388 sizeof(posix_constants_confstr)
7389 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007390 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007391 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007392#endif
7393#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007394 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007395 sizeof(posix_constants_sysconf)
7396 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007397 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007398 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007399#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007400 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007401}
Fred Draked86ed291999-12-15 15:34:33 +00007402
7403
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007404PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007405"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007406Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007407in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007408
7409static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007410posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007411{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007412 abort();
7413 /*NOTREACHED*/
7414 Py_FatalError("abort() called from Python code didn't abort!");
7415 return NULL;
7416}
Fred Drakebec628d1999-12-15 18:31:10 +00007417
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007418#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007419PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007420"startfile(filepath [, operation]) - Start a file with its associated\n\
7421application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007422\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007423When \"operation\" is not specified or \"open\", this acts like\n\
7424double-clicking the file in Explorer, or giving the file name as an\n\
7425argument to the DOS \"start\" command: the file is opened with whatever\n\
7426application (if any) its extension is associated.\n\
7427When another \"operation\" is given, it specifies what should be done with\n\
7428the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007429\n\
7430startfile returns as soon as the associated application is launched.\n\
7431There is no option to wait for the application to close, and no way\n\
7432to retrieve the application's exit status.\n\
7433\n\
7434The filepath is relative to the current directory. If you want to use\n\
7435an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007436the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007437
7438static PyObject *
7439win32_startfile(PyObject *self, PyObject *args)
7440{
Victor Stinner8c62be82010-05-06 00:08:46 +00007441 PyObject *ofilepath;
7442 char *filepath;
7443 char *operation = NULL;
7444 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007445
Victor Stinner8c62be82010-05-06 00:08:46 +00007446 PyObject *unipath, *woperation = NULL;
7447 if (!PyArg_ParseTuple(args, "U|s:startfile",
7448 &unipath, &operation)) {
7449 PyErr_Clear();
7450 goto normal;
7451 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007452
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 if (operation) {
7454 woperation = PyUnicode_DecodeASCII(operation,
7455 strlen(operation), NULL);
7456 if (!woperation) {
7457 PyErr_Clear();
7458 operation = NULL;
7459 goto normal;
7460 }
7461 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007462
Victor Stinner8c62be82010-05-06 00:08:46 +00007463 Py_BEGIN_ALLOW_THREADS
7464 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7465 PyUnicode_AS_UNICODE(unipath),
7466 NULL, NULL, SW_SHOWNORMAL);
7467 Py_END_ALLOW_THREADS
7468
7469 Py_XDECREF(woperation);
7470 if (rc <= (HINSTANCE)32) {
7471 PyObject *errval = win32_error_unicode("startfile",
7472 PyUnicode_AS_UNICODE(unipath));
7473 return errval;
7474 }
7475 Py_INCREF(Py_None);
7476 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007477
7478normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7480 PyUnicode_FSConverter, &ofilepath,
7481 &operation))
7482 return NULL;
7483 filepath = PyBytes_AsString(ofilepath);
7484 Py_BEGIN_ALLOW_THREADS
7485 rc = ShellExecute((HWND)0, operation, filepath,
7486 NULL, NULL, SW_SHOWNORMAL);
7487 Py_END_ALLOW_THREADS
7488 if (rc <= (HINSTANCE)32) {
7489 PyObject *errval = win32_error("startfile", filepath);
7490 Py_DECREF(ofilepath);
7491 return errval;
7492 }
7493 Py_DECREF(ofilepath);
7494 Py_INCREF(Py_None);
7495 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007496}
7497#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007498
Martin v. Löwis438b5342002-12-27 10:16:42 +00007499#ifdef HAVE_GETLOADAVG
7500PyDoc_STRVAR(posix_getloadavg__doc__,
7501"getloadavg() -> (float, float, float)\n\n\
7502Return the number of processes in the system run queue averaged over\n\
7503the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7504was unobtainable");
7505
7506static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007507posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007508{
7509 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007510 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007511 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7512 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007513 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007514 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007515}
7516#endif
7517
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007518#ifdef MS_WINDOWS
7519
7520PyDoc_STRVAR(win32_urandom__doc__,
7521"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007522Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007523
7524typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7525 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7526 DWORD dwFlags );
7527typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7528 BYTE *pbBuffer );
7529
7530static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007531/* This handle is never explicitly released. Instead, the operating
7532 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007533static HCRYPTPROV hCryptProv = 0;
7534
Tim Peters4ad82172004-08-30 17:02:04 +00007535static PyObject*
7536win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007537{
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 int howMany;
7539 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007540
Victor Stinner8c62be82010-05-06 00:08:46 +00007541 /* Read arguments */
7542 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7543 return NULL;
7544 if (howMany < 0)
7545 return PyErr_Format(PyExc_ValueError,
7546 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007547
Victor Stinner8c62be82010-05-06 00:08:46 +00007548 if (hCryptProv == 0) {
7549 HINSTANCE hAdvAPI32 = NULL;
7550 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007551
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 /* Obtain handle to the DLL containing CryptoAPI
7553 This should not fail */
7554 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7555 if(hAdvAPI32 == NULL)
7556 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007557
Victor Stinner8c62be82010-05-06 00:08:46 +00007558 /* Obtain pointers to the CryptoAPI functions
7559 This will fail on some early versions of Win95 */
7560 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7561 hAdvAPI32,
7562 "CryptAcquireContextA");
7563 if (pCryptAcquireContext == NULL)
7564 return PyErr_Format(PyExc_NotImplementedError,
7565 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007566
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7568 hAdvAPI32, "CryptGenRandom");
7569 if (pCryptGenRandom == NULL)
7570 return PyErr_Format(PyExc_NotImplementedError,
7571 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007572
Victor Stinner8c62be82010-05-06 00:08:46 +00007573 /* Acquire context */
7574 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7575 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7576 return win32_error("CryptAcquireContext", NULL);
7577 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007578
Victor Stinner8c62be82010-05-06 00:08:46 +00007579 /* Allocate bytes */
7580 result = PyBytes_FromStringAndSize(NULL, howMany);
7581 if (result != NULL) {
7582 /* Get random data */
7583 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7584 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7585 PyBytes_AS_STRING(result))) {
7586 Py_DECREF(result);
7587 return win32_error("CryptGenRandom", NULL);
7588 }
7589 }
7590 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007591}
7592#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007593
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007594PyDoc_STRVAR(device_encoding__doc__,
7595"device_encoding(fd) -> str\n\n\
7596Return a string describing the encoding of the device\n\
7597if the output is a terminal; else return None.");
7598
7599static PyObject *
7600device_encoding(PyObject *self, PyObject *args)
7601{
Victor Stinner8c62be82010-05-06 00:08:46 +00007602 int fd;
7603 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7604 return NULL;
7605 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7606 Py_INCREF(Py_None);
7607 return Py_None;
7608 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007609#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 if (fd == 0) {
7611 char buf[100];
7612 sprintf(buf, "cp%d", GetConsoleCP());
7613 return PyUnicode_FromString(buf);
7614 }
7615 if (fd == 1 || fd == 2) {
7616 char buf[100];
7617 sprintf(buf, "cp%d", GetConsoleOutputCP());
7618 return PyUnicode_FromString(buf);
7619 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007620#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007621 {
7622 char *codeset = nl_langinfo(CODESET);
7623 if (codeset != NULL && codeset[0] != 0)
7624 return PyUnicode_FromString(codeset);
7625 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007626#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 Py_INCREF(Py_None);
7628 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007629}
7630
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007631#ifdef __VMS
7632/* Use openssl random routine */
7633#include <openssl/rand.h>
7634PyDoc_STRVAR(vms_urandom__doc__,
7635"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007636Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007637
7638static PyObject*
7639vms_urandom(PyObject *self, PyObject *args)
7640{
Victor Stinner8c62be82010-05-06 00:08:46 +00007641 int howMany;
7642 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007643
Victor Stinner8c62be82010-05-06 00:08:46 +00007644 /* Read arguments */
7645 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7646 return NULL;
7647 if (howMany < 0)
7648 return PyErr_Format(PyExc_ValueError,
7649 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007650
Victor Stinner8c62be82010-05-06 00:08:46 +00007651 /* Allocate bytes */
7652 result = PyBytes_FromStringAndSize(NULL, howMany);
7653 if (result != NULL) {
7654 /* Get random data */
7655 if (RAND_pseudo_bytes((unsigned char*)
7656 PyBytes_AS_STRING(result),
7657 howMany) < 0) {
7658 Py_DECREF(result);
7659 return PyErr_Format(PyExc_ValueError,
7660 "RAND_pseudo_bytes");
7661 }
7662 }
7663 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007664}
7665#endif
7666
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007667#ifdef HAVE_SETRESUID
7668PyDoc_STRVAR(posix_setresuid__doc__,
7669"setresuid(ruid, euid, suid)\n\n\
7670Set the current process's real, effective, and saved user ids.");
7671
7672static PyObject*
7673posix_setresuid (PyObject *self, PyObject *args)
7674{
Victor Stinner8c62be82010-05-06 00:08:46 +00007675 /* We assume uid_t is no larger than a long. */
7676 long ruid, euid, suid;
7677 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7678 return NULL;
7679 if (setresuid(ruid, euid, suid) < 0)
7680 return posix_error();
7681 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007682}
7683#endif
7684
7685#ifdef HAVE_SETRESGID
7686PyDoc_STRVAR(posix_setresgid__doc__,
7687"setresgid(rgid, egid, sgid)\n\n\
7688Set the current process's real, effective, and saved group ids.");
7689
7690static PyObject*
7691posix_setresgid (PyObject *self, PyObject *args)
7692{
Victor Stinner8c62be82010-05-06 00:08:46 +00007693 /* We assume uid_t is no larger than a long. */
7694 long rgid, egid, sgid;
7695 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7696 return NULL;
7697 if (setresgid(rgid, egid, sgid) < 0)
7698 return posix_error();
7699 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007700}
7701#endif
7702
7703#ifdef HAVE_GETRESUID
7704PyDoc_STRVAR(posix_getresuid__doc__,
7705"getresuid() -> (ruid, euid, suid)\n\n\
7706Get tuple of the current process's real, effective, and saved user ids.");
7707
7708static PyObject*
7709posix_getresuid (PyObject *self, PyObject *noargs)
7710{
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 uid_t ruid, euid, suid;
7712 long l_ruid, l_euid, l_suid;
7713 if (getresuid(&ruid, &euid, &suid) < 0)
7714 return posix_error();
7715 /* Force the values into long's as we don't know the size of uid_t. */
7716 l_ruid = ruid;
7717 l_euid = euid;
7718 l_suid = suid;
7719 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007720}
7721#endif
7722
7723#ifdef HAVE_GETRESGID
7724PyDoc_STRVAR(posix_getresgid__doc__,
7725"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007726Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007727
7728static PyObject*
7729posix_getresgid (PyObject *self, PyObject *noargs)
7730{
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 uid_t rgid, egid, sgid;
7732 long l_rgid, l_egid, l_sgid;
7733 if (getresgid(&rgid, &egid, &sgid) < 0)
7734 return posix_error();
7735 /* Force the values into long's as we don't know the size of uid_t. */
7736 l_rgid = rgid;
7737 l_egid = egid;
7738 l_sgid = sgid;
7739 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007740}
7741#endif
7742
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007743static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007744 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007745#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007747#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007748 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007749#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007750 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007751#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007752 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007753#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007754 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007755#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007756#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007758#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007759#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007761#endif /* HAVE_LCHMOD */
7762#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007764#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007765#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007767#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007768#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007770#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007771#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007773#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007774#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007775 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007776#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007777#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7779 METH_NOARGS, posix_getcwd__doc__},
7780 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7781 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007782#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007783#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007785#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007786 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7787 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7788 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007789#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007791#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007792#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007794#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007795#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007796 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007797#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007798 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7799 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7800 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007802#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007803 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007804#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007805#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
7806 {"_symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
7807 win_symlink__doc__},
7808#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007809#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007810 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007811#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007813#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007815#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007816 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7817 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7818 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007819#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007821#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007823#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007824 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7825 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007826#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007827#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007828 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7829 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007830#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7832 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007833#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007834#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007835#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007837#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007838#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007840#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007841#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007843#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007844#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007846#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007847#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007849#endif /* HAVE_GETEGID */
7850#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007852#endif /* HAVE_GETEUID */
7853#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007855#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007856#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007858#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007860#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007862#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007863#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007865#endif /* HAVE_GETPPID */
7866#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007868#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007869#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007871#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007872#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007874#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007875#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007877#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007878#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007880#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007881#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007882 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7883 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007884 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007885#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007886#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007887 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007888#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007889#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007890 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007891#endif /* HAVE_SETEUID */
7892#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007893 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007894#endif /* HAVE_SETEGID */
7895#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007896 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007897#endif /* HAVE_SETREUID */
7898#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007900#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007901#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007903#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007904#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007905 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007906#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007907#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007908 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007909#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007910#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007911 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007912#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007913#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007914 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007915#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007916#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007917 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007918#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007919#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007920 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007921#endif /* HAVE_WAIT3 */
7922#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007923 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007924#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007925#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007926 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007927#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007928#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007929 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007930#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007931#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007932 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007933#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007934#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007935 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007936#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007937#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007938 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007939#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007940#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007942#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7944 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7945 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7946 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7947 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7948 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7949 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7950 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7951 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7952 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7953 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007954#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007955 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007956#endif
7957#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007959#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007960#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007962#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007963#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007964 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7965 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7966 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007967#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007968#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007970#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007971#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007973#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007974#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007976#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007978#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007980#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007981#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007983#endif
7984#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007985 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007986#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007987#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007988#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007989 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007990#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007991#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007992 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007993#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007994#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007996#endif /* WIFSTOPPED */
7997#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007999#endif /* WIFSIGNALED */
8000#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008002#endif /* WIFEXITED */
8003#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008005#endif /* WEXITSTATUS */
8006#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008008#endif /* WTERMSIG */
8009#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008011#endif /* WSTOPSIG */
8012#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008013#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008015#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008016#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008018#endif
Fred Drakec9680921999-12-13 16:37:25 +00008019#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008021#endif
8022#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008023 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008024#endif
8025#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008027#endif
8028#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008030#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008031 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008032#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008034 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008035 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008036#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008037#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008038 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008039#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008040 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008041 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008042 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008043 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008044 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008045 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008046#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008047 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008048#endif
8049#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008051#endif
8052#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008054#endif
8055#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008057#endif
8058
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008060};
8061
8062
Barry Warsaw4a342091996-12-19 23:50:02 +00008063static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008064ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008065{
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008067}
8068
Guido van Rossumd48f2521997-12-05 22:19:34 +00008069#if defined(PYOS_OS2)
8070/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008071static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008072{
8073 APIRET rc;
8074 ULONG values[QSV_MAX+1];
8075 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008076 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008077
8078 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008079 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008080 Py_END_ALLOW_THREADS
8081
8082 if (rc != NO_ERROR) {
8083 os2_error(rc);
8084 return -1;
8085 }
8086
Fred Drake4d1e64b2002-04-15 19:40:07 +00008087 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8088 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8089 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8090 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8091 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8092 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8093 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008094
8095 switch (values[QSV_VERSION_MINOR]) {
8096 case 0: ver = "2.00"; break;
8097 case 10: ver = "2.10"; break;
8098 case 11: ver = "2.11"; break;
8099 case 30: ver = "3.00"; break;
8100 case 40: ver = "4.00"; break;
8101 case 50: ver = "5.00"; break;
8102 default:
Tim Peters885d4572001-11-28 20:27:42 +00008103 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008104 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008105 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008106 ver = &tmp[0];
8107 }
8108
8109 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008110 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008111 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008112
8113 /* Add Indicator of Which Drive was Used to Boot the System */
8114 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8115 tmp[1] = ':';
8116 tmp[2] = '\0';
8117
Fred Drake4d1e64b2002-04-15 19:40:07 +00008118 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008119}
8120#endif
8121
Brian Curtin52173d42010-12-02 18:29:18 +00008122#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
8123void
8124enable_symlink()
8125{
8126 HANDLE tok;
8127 TOKEN_PRIVILEGES tok_priv;
8128 LUID luid;
8129 int meth_idx = 0;
8130
8131 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
8132 return;
8133
8134 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
8135 return;
8136
8137 tok_priv.PrivilegeCount = 1;
8138 tok_priv.Privileges[0].Luid = luid;
8139 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8140
8141 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8142 sizeof(TOKEN_PRIVILEGES),
8143 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
8144 return;
8145
8146 if(GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
8147 /* We couldn't acquire the necessary privilege, so leave the
8148 method hidden for this user. */
8149 return;
8150 } else {
8151 /* We've successfully acquired the symlink privilege so rename
8152 the method to it's proper "os.symlink" name. */
8153 while(posix_methods[meth_idx].ml_meth != (PyCFunction)win_symlink)
8154 meth_idx++;
8155 posix_methods[meth_idx].ml_name = "symlink";
8156
8157 return;
8158 }
8159}
8160#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8161
Barry Warsaw4a342091996-12-19 23:50:02 +00008162static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008163all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008164{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008165#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008166 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008167#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008168#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008169 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008170#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008171#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008172 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008173#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008174#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008175 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008176#endif
Fred Drakec9680921999-12-13 16:37:25 +00008177#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008178 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008179#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008180#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008181 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008182#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008183#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008184 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008185#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008186#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008187 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008188#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008189#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008190 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008191#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008192#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008193 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008194#endif
8195#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008196 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008197#endif
8198#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008199 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008200#endif
8201#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008202 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008203#endif
8204#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008205 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008206#endif
8207#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008208 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008209#endif
8210#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008211 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008212#endif
8213#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008215#endif
8216#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008218#endif
8219#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008221#endif
8222#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008224#endif
8225#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008226 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008227#endif
8228#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008230#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008231#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008233#endif
8234#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008236#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008237#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008239#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008240#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008242#endif
8243#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008245#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008246
Tim Peters5aa91602002-01-30 05:46:57 +00008247/* MS Windows */
8248#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 /* Don't inherit in child processes. */
8250 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008251#endif
8252#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008253 /* Optimize for short life (keep in memory). */
8254 /* MS forgot to define this one with a non-underscore form too. */
8255 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008256#endif
8257#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008258 /* Automatically delete when last handle is closed. */
8259 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008260#endif
8261#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008262 /* Optimize for random access. */
8263 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008264#endif
8265#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008266 /* Optimize for sequential access. */
8267 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008268#endif
8269
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008270/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008271#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 /* Send a SIGIO signal whenever input or output
8273 becomes available on file descriptor */
8274 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008275#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008276#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 /* Direct disk access. */
8278 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008279#endif
8280#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008281 /* Must be a directory. */
8282 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008283#endif
8284#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008285 /* Do not follow links. */
8286 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008287#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008288#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008289 /* Do not update the access time. */
8290 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008291#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008292
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008294#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008295 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008296#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008297#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008298 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008299#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008300#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008301 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008302#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008303#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008305#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008306#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008308#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008309#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008311#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008312#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008314#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008315#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008316 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008317#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008318#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008319 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008320#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008321#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008322 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008323#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008324#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008325 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008326#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008327#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008329#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008330#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008332#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008333#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008335#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008336#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008338#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008339#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008341#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008342#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008343 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008344#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008345
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008346 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008347#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008348 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008349#endif /* ST_RDONLY */
8350#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008351 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008352#endif /* ST_NOSUID */
8353
Guido van Rossum246bc171999-02-01 23:54:31 +00008354#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008355#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008356 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8357 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8358 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8359 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8360 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8361 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8362 if (ins(d, "P_PM", (long)P_PM)) return -1;
8363 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8364 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8365 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8366 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8367 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8368 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8369 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8370 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8371 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8372 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8373 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8374 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8375 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008376#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8378 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8379 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8380 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8381 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008382#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008383#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008384
Guido van Rossumd48f2521997-12-05 22:19:34 +00008385#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008386 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008387#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008389}
8390
8391
Tim Peters5aa91602002-01-30 05:46:57 +00008392#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008393#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008394#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008395
8396#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008397#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008398#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008399
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008400#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008401#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008402#define MODNAME "posix"
8403#endif
8404
Martin v. Löwis1a214512008-06-11 05:26:20 +00008405static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008406 PyModuleDef_HEAD_INIT,
8407 MODNAME,
8408 posix__doc__,
8409 -1,
8410 posix_methods,
8411 NULL,
8412 NULL,
8413 NULL,
8414 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008415};
8416
8417
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008418PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008419INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008420{
Victor Stinner8c62be82010-05-06 00:08:46 +00008421 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008422
Brian Curtin52173d42010-12-02 18:29:18 +00008423#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
8424 enable_symlink();
8425#endif
8426
Victor Stinner8c62be82010-05-06 00:08:46 +00008427 m = PyModule_Create(&posixmodule);
8428 if (m == NULL)
8429 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008430
Victor Stinner8c62be82010-05-06 00:08:46 +00008431 /* Initialize environ dictionary */
8432 v = convertenviron();
8433 Py_XINCREF(v);
8434 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8435 return NULL;
8436 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008437
Victor Stinner8c62be82010-05-06 00:08:46 +00008438 if (all_ins(m))
8439 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008440
Victor Stinner8c62be82010-05-06 00:08:46 +00008441 if (setup_confname_tables(m))
8442 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008443
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 Py_INCREF(PyExc_OSError);
8445 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008446
Guido van Rossumb3d39562000-01-31 18:41:26 +00008447#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008448 if (posix_putenv_garbage == NULL)
8449 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008450#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008451
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 if (!initialized) {
8453 stat_result_desc.name = MODNAME ".stat_result";
8454 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8455 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8456 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8457 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8458 structseq_new = StatResultType.tp_new;
8459 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008460
Victor Stinner8c62be82010-05-06 00:08:46 +00008461 statvfs_result_desc.name = MODNAME ".statvfs_result";
8462 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008463#ifdef NEED_TICKS_PER_SECOND
8464# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008466# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008468# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008469 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008470# endif
8471#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008472 }
8473 Py_INCREF((PyObject*) &StatResultType);
8474 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8475 Py_INCREF((PyObject*) &StatVFSResultType);
8476 PyModule_AddObject(m, "statvfs_result",
8477 (PyObject*) &StatVFSResultType);
8478 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008479
8480#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 /*
8482 * Step 2 of weak-linking support on Mac OS X.
8483 *
8484 * The code below removes functions that are not available on the
8485 * currently active platform.
8486 *
8487 * This block allow one to use a python binary that was build on
8488 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8489 * OSX 10.4.
8490 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008491#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 if (fstatvfs == NULL) {
8493 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8494 return NULL;
8495 }
8496 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008497#endif /* HAVE_FSTATVFS */
8498
8499#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 if (statvfs == NULL) {
8501 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8502 return NULL;
8503 }
8504 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008505#endif /* HAVE_STATVFS */
8506
8507# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008508 if (lchown == NULL) {
8509 if (PyObject_DelAttrString(m, "lchown") == -1) {
8510 return NULL;
8511 }
8512 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008513#endif /* HAVE_LCHOWN */
8514
8515
8516#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008517 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008518
Guido van Rossumb6775db1994-08-01 11:34:53 +00008519}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008520
8521#ifdef __cplusplus
8522}
8523#endif