blob: 077343e995e867c45531c949b6c930cefb5efd73 [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
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000098#ifdef HAVE_SYS_SENDFILE_H
99#include <sys/sendfile.h>
100#endif
101
102#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
103#ifdef HAVE_SYS_SOCKET_H
104#include <sys/socket.h>
105#endif
106
107#ifdef HAVE_SYS_UIO_H
108#include <sys/uio.h>
109#endif
110#endif
111
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000113/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000114#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000115#include <process.h>
116#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000117#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000118#define HAVE_GETCWD 1
119#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000120#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000121#if defined(__OS2__)
122#define HAVE_EXECV 1
123#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000124#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#include <process.h>
126#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000127#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128#define HAVE_EXECV 1
129#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#define HAVE_OPENDIR 1
131#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000132#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000133#define HAVE_WAIT 1
134#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000136#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000137#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000138#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140#define HAVE_EXECV 1
141#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000142#define HAVE_SYSTEM 1
143#define HAVE_CWAIT 1
144#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000145#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000146#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000147#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
148/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000149#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150/* Unix functions that the configure script doesn't check for */
151#define HAVE_EXECV 1
152#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000154#define HAVE_FORK1 1
155#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_GETCWD 1
157#define HAVE_GETEGID 1
158#define HAVE_GETEUID 1
159#define HAVE_GETGID 1
160#define HAVE_GETPPID 1
161#define HAVE_GETUID 1
162#define HAVE_KILL 1
163#define HAVE_OPENDIR 1
164#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000165#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000168#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000169#endif /* _MSC_VER */
170#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000172#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000173
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000175
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000176#if defined(__sgi)&&_COMPILER_VERSION>=700
177/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
178 (default) */
179extern char *ctermid_r(char *);
180#endif
181
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000182#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000184extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000186#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000188#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000191#endif
192#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000193extern int chdir(char *);
194extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000195#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int chdir(const char *);
197extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000198#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000199#ifdef __BORLANDC__
200extern int chmod(const char *, int);
201#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000202extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000203#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000204/*#ifdef HAVE_FCHMOD
205extern int fchmod(int, mode_t);
206#endif*/
207/*#ifdef HAVE_LCHMOD
208extern int lchmod(const char *, mode_t);
209#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000210extern int chown(const char *, uid_t, gid_t);
211extern char *getcwd(char *, int);
212extern char *strerror(int);
213extern int link(const char *, const char *);
214extern int rename(const char *, const char *);
215extern int stat(const char *, struct stat *);
216extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000219#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000221extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000222#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000224
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#ifdef HAVE_UTIME_H
228#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000231#ifdef HAVE_SYS_UTIME_H
232#include <sys/utime.h>
233#define HAVE_UTIME_H /* pretend we do for the rest of this file */
234#endif /* HAVE_SYS_UTIME_H */
235
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236#ifdef HAVE_SYS_TIMES_H
237#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000238#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239
240#ifdef HAVE_SYS_PARAM_H
241#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000242#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243
244#ifdef HAVE_SYS_UTSNAME_H
245#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000246#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#define NAMLEN(dirent) strlen((dirent)->d_name)
251#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000252#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000253#include <direct.h>
254#define NAMLEN(dirent) strlen((dirent)->d_name)
255#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000257#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000258#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000259#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000261#endif
262#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000264#endif
265#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000267#endif
268#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000270#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273#endif
274#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000276#endif
277#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000280#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000281#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000282#endif
283#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000284#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000285#endif
286#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000287#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000288#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000289#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000290#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000292#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000293#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000294#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
295#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000296static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000297#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000298#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299
Guido van Rossumd48f2521997-12-05 22:19:34 +0000300#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000302#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303
Tim Petersbc2e10e2002-03-03 23:17:02 +0000304#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000305#if defined(PATH_MAX) && PATH_MAX > 1024
306#define MAXPATHLEN PATH_MAX
307#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000308#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000309#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000310#endif /* MAXPATHLEN */
311
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000312#ifdef UNION_WAIT
313/* Emulate some macros on systems that have a union instead of macros */
314
315#ifndef WIFEXITED
316#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
317#endif
318
319#ifndef WEXITSTATUS
320#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
321#endif
322
323#ifndef WTERMSIG
324#define WTERMSIG(u_wait) ((u_wait).w_termsig)
325#endif
326
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327#define WAIT_TYPE union wait
328#define WAIT_STATUS_INT(s) (s.w_status)
329
330#else /* !UNION_WAIT */
331#define WAIT_TYPE int
332#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000333#endif /* UNION_WAIT */
334
Greg Wardb48bc172000-03-01 21:51:56 +0000335/* Don't use the "_r" form if we don't need it (also, won't have a
336 prototype for it, at least on Solaris -- maybe others as well?). */
337#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
338#define USE_CTERMID_R
339#endif
340
Fred Drake699f3522000-06-29 21:12:41 +0000341/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000342#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000343#undef FSTAT
344#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000345#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000346# define STAT win32_stat
347# define FSTAT win32_fstat
348# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000349#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000350# define STAT stat
351# define FSTAT fstat
352# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000353#endif
354
Tim Peters11b23062003-04-23 02:39:17 +0000355#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000356#include <sys/mkdev.h>
357#else
358#if defined(MAJOR_IN_SYSMACROS)
359#include <sys/sysmacros.h>
360#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000361#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
362#include <sys/mkdev.h>
363#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000364#endif
Fred Drake699f3522000-06-29 21:12:41 +0000365
Georg Brandla391b112011-02-25 15:23:18 +0000366static int
367_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000368{
369#if !defined(HAVE_LARGEFILE_SUPPORT)
370 *((off_t*)addr) = PyLong_AsLong(arg);
371#else
372 *((off_t*)addr) = PyLong_Check(arg) ? PyLong_AsLongLong(arg)
373 : PyLong_AsLong(arg);
374#endif
375 if (PyErr_Occurred())
376 return 0;
377 return 1;
378}
379
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380#if defined _MSC_VER && _MSC_VER >= 1400
381/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
382 * valid and throw an assertion if it isn't.
383 * Normally, an invalid fd is likely to be a C program error and therefore
384 * an assertion can be useful, but it does contradict the POSIX standard
385 * which for write(2) states:
386 * "Otherwise, -1 shall be returned and errno set to indicate the error."
387 * "[EBADF] The fildes argument is not a valid file descriptor open for
388 * writing."
389 * Furthermore, python allows the user to enter any old integer
390 * as a fd and should merely raise a python exception on error.
391 * The Microsoft CRT doesn't provide an official way to check for the
392 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000393 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000394 * internal structures involved.
395 * The structures below must be updated for each version of visual studio
396 * according to the file internal.h in the CRT source, until MS comes
397 * up with a less hacky way to do this.
398 * (all of this is to avoid globally modifying the CRT behaviour using
399 * _set_invalid_parameter_handler() and _CrtSetReportMode())
400 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000401/* The actual size of the structure is determined at runtime.
402 * Only the first items must be present.
403 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000404typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000405 intptr_t osfhnd;
406 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000407} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000409extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000410#define IOINFO_L2E 5
411#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
412#define IOINFO_ARRAYS 64
413#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
414#define FOPEN 0x01
415#define _NO_CONSOLE_FILENO (intptr_t)-2
416
417/* This function emulates what the windows CRT does to validate file handles */
418int
419_PyVerify_fd(int fd)
420{
Victor Stinner8c62be82010-05-06 00:08:46 +0000421 const int i1 = fd >> IOINFO_L2E;
422 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000423
Antoine Pitrou22e41552010-08-15 18:07:50 +0000424 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000425
Victor Stinner8c62be82010-05-06 00:08:46 +0000426 /* Determine the actual size of the ioinfo structure,
427 * as used by the CRT loaded in memory
428 */
429 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
430 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
431 }
432 if (sizeof_ioinfo == 0) {
433 /* This should not happen... */
434 goto fail;
435 }
436
437 /* See that it isn't a special CLEAR fileno */
438 if (fd != _NO_CONSOLE_FILENO) {
439 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
440 * we check pointer validity and other info
441 */
442 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
443 /* finally, check that the file is open */
444 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
445 if (info->osfile & FOPEN) {
446 return 1;
447 }
448 }
449 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000450 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000451 errno = EBADF;
452 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000453}
454
455/* the special case of checking dup2. The target fd must be in a sensible range */
456static int
457_PyVerify_fd_dup2(int fd1, int fd2)
458{
Victor Stinner8c62be82010-05-06 00:08:46 +0000459 if (!_PyVerify_fd(fd1))
460 return 0;
461 if (fd2 == _NO_CONSOLE_FILENO)
462 return 0;
463 if ((unsigned)fd2 < _NHANDLE_)
464 return 1;
465 else
466 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000467}
468#else
469/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
470#define _PyVerify_fd_dup2(A, B) (1)
471#endif
472
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000473#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000474/* The following structure was copied from
475 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
476 include doesn't seem to be present in the Windows SDK (at least as included
477 with Visual Studio Express). */
478typedef struct _REPARSE_DATA_BUFFER {
479 ULONG ReparseTag;
480 USHORT ReparseDataLength;
481 USHORT Reserved;
482 union {
483 struct {
484 USHORT SubstituteNameOffset;
485 USHORT SubstituteNameLength;
486 USHORT PrintNameOffset;
487 USHORT PrintNameLength;
488 ULONG Flags;
489 WCHAR PathBuffer[1];
490 } SymbolicLinkReparseBuffer;
491
492 struct {
493 USHORT SubstituteNameOffset;
494 USHORT SubstituteNameLength;
495 USHORT PrintNameOffset;
496 USHORT PrintNameLength;
497 WCHAR PathBuffer[1];
498 } MountPointReparseBuffer;
499
500 struct {
501 UCHAR DataBuffer[1];
502 } GenericReparseBuffer;
503 };
504} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
505
506#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
507 GenericReparseBuffer)
508#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
509
510static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000511win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000512{
513 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
514 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
515 DWORD n_bytes_returned;
516 const wchar_t *ptr;
517 wchar_t *buf;
518 size_t len;
519
520 if (0 == DeviceIoControl(
521 reparse_point_handle,
522 FSCTL_GET_REPARSE_POINT,
523 NULL, 0, /* in buffer */
524 target_buffer, sizeof(target_buffer),
525 &n_bytes_returned,
526 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000527 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000528
529 if (reparse_tag)
530 *reparse_tag = rdb->ReparseTag;
531
532 if (target_path) {
533 switch (rdb->ReparseTag) {
534 case IO_REPARSE_TAG_SYMLINK:
535 /* XXX: Maybe should use SubstituteName? */
536 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
537 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
538 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
539 break;
540 case IO_REPARSE_TAG_MOUNT_POINT:
541 ptr = rdb->MountPointReparseBuffer.PathBuffer +
542 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
543 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
544 break;
545 default:
546 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000547 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000548 }
549 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
550 if (!buf) {
551 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000552 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000553 }
554 wcsncpy(buf, ptr, len);
555 buf[len] = L'\0';
556 if (wcsncmp(buf, L"\\??\\", 4) == 0)
557 buf[1] = L'\\';
558 *target_path = buf;
559 }
560
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000561 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000562}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000563#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000564
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000566#ifdef WITH_NEXT_FRAMEWORK
567/* On Darwin/MacOSX a shared library or framework has no access to
568** environ directly, we must obtain it with _NSGetEnviron().
569*/
570#include <crt_externs.h>
571static char **environ;
572#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000574#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575
Barry Warsaw53699e91996-12-10 23:23:01 +0000576static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000577convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578{
Victor Stinner8c62be82010-05-06 00:08:46 +0000579 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000580#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000581 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000582#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000583 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000584#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000585#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000586 APIRET rc;
587 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
588#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000589
Victor Stinner8c62be82010-05-06 00:08:46 +0000590 d = PyDict_New();
591 if (d == NULL)
592 return NULL;
593#ifdef WITH_NEXT_FRAMEWORK
594 if (environ == NULL)
595 environ = *_NSGetEnviron();
596#endif
597#ifdef MS_WINDOWS
598 /* _wenviron must be initialized in this way if the program is started
599 through main() instead of wmain(). */
600 _wgetenv(L"");
601 if (_wenviron == NULL)
602 return d;
603 /* This part ignores errors */
604 for (e = _wenviron; *e != NULL; e++) {
605 PyObject *k;
606 PyObject *v;
607 wchar_t *p = wcschr(*e, L'=');
608 if (p == NULL)
609 continue;
610 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
611 if (k == NULL) {
612 PyErr_Clear();
613 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000614 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000615 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
616 if (v == NULL) {
617 PyErr_Clear();
618 Py_DECREF(k);
619 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000620 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000621 if (PyDict_GetItem(d, k) == NULL) {
622 if (PyDict_SetItem(d, k, v) != 0)
623 PyErr_Clear();
624 }
625 Py_DECREF(k);
626 Py_DECREF(v);
627 }
628#else
629 if (environ == NULL)
630 return d;
631 /* This part ignores errors */
632 for (e = environ; *e != NULL; e++) {
633 PyObject *k;
634 PyObject *v;
635 char *p = strchr(*e, '=');
636 if (p == NULL)
637 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000638 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000639 if (k == NULL) {
640 PyErr_Clear();
641 continue;
642 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000643 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000644 if (v == NULL) {
645 PyErr_Clear();
646 Py_DECREF(k);
647 continue;
648 }
649 if (PyDict_GetItem(d, k) == NULL) {
650 if (PyDict_SetItem(d, k, v) != 0)
651 PyErr_Clear();
652 }
653 Py_DECREF(k);
654 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000655 }
656#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000657#if defined(PYOS_OS2)
658 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
659 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
660 PyObject *v = PyBytes_FromString(buffer);
661 PyDict_SetItemString(d, "BEGINLIBPATH", v);
662 Py_DECREF(v);
663 }
664 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
665 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
666 PyObject *v = PyBytes_FromString(buffer);
667 PyDict_SetItemString(d, "ENDLIBPATH", v);
668 Py_DECREF(v);
669 }
670#endif
671 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672}
673
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674/* Set a POSIX-specific error from errno, and return NULL */
675
Barry Warsawd58d7641998-07-23 16:14:40 +0000676static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000677posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000678{
Victor Stinner8c62be82010-05-06 00:08:46 +0000679 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000680}
Barry Warsawd58d7641998-07-23 16:14:40 +0000681static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000682posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000683{
Victor Stinner8c62be82010-05-06 00:08:46 +0000684 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000685}
686
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000687
Mark Hammondef8b6542001-05-13 08:04:26 +0000688static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000689posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000690{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000691 PyObject *name_str, *rc;
692 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
693 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000694 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000695 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
696 name_str);
697 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000698 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000699}
700
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000701#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000702static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000703win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000704{
Victor Stinner8c62be82010-05-06 00:08:46 +0000705 /* XXX We should pass the function name along in the future.
706 (winreg.c also wants to pass the function name.)
707 This would however require an additional param to the
708 Windows error object, which is non-trivial.
709 */
710 errno = GetLastError();
711 if (filename)
712 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
713 else
714 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000715}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000717static PyObject *
718win32_error_unicode(char* function, Py_UNICODE* filename)
719{
Victor Stinner8c62be82010-05-06 00:08:46 +0000720 /* XXX - see win32_error for comments on 'function' */
721 errno = GetLastError();
722 if (filename)
723 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
724 else
725 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000726}
727
Thomas Wouters477c8d52006-05-27 19:21:47 +0000728static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000729convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000730{
Victor Stinner8c62be82010-05-06 00:08:46 +0000731 if (PyUnicode_CheckExact(*param))
732 Py_INCREF(*param);
733 else if (PyUnicode_Check(*param))
734 /* For a Unicode subtype that's not a Unicode object,
735 return a true Unicode object with the same data. */
736 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
737 PyUnicode_GET_SIZE(*param));
738 else
739 *param = PyUnicode_FromEncodedObject(*param,
740 Py_FileSystemDefaultEncoding,
741 "strict");
742 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000743}
744
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000745#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000746
Guido van Rossumd48f2521997-12-05 22:19:34 +0000747#if defined(PYOS_OS2)
748/**********************************************************************
749 * Helper Function to Trim and Format OS/2 Messages
750 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000751static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000752os2_formatmsg(char *msgbuf, int msglen, char *reason)
753{
754 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
755
756 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
757 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
758
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000759 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000760 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
761 }
762
763 /* Add Optional Reason Text */
764 if (reason) {
765 strcat(msgbuf, " : ");
766 strcat(msgbuf, reason);
767 }
768}
769
770/**********************************************************************
771 * Decode an OS/2 Operating System Error Code
772 *
773 * A convenience function to lookup an OS/2 error code and return a
774 * text message we can use to raise a Python exception.
775 *
776 * Notes:
777 * The messages for errors returned from the OS/2 kernel reside in
778 * the file OSO001.MSG in the \OS2 directory hierarchy.
779 *
780 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000781static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000782os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
783{
784 APIRET rc;
785 ULONG msglen;
786
787 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
788 Py_BEGIN_ALLOW_THREADS
789 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
790 errorcode, "oso001.msg", &msglen);
791 Py_END_ALLOW_THREADS
792
793 if (rc == NO_ERROR)
794 os2_formatmsg(msgbuf, msglen, reason);
795 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000796 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000797 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000798
799 return msgbuf;
800}
801
802/* Set an OS/2-specific error and return NULL. OS/2 kernel
803 errors are not in a global variable e.g. 'errno' nor are
804 they congruent with posix error numbers. */
805
Victor Stinner8c62be82010-05-06 00:08:46 +0000806static PyObject *
807os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000808{
809 char text[1024];
810 PyObject *v;
811
812 os2_strerror(text, sizeof(text), code, "");
813
814 v = Py_BuildValue("(is)", code, text);
815 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000816 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000817 Py_DECREF(v);
818 }
819 return NULL; /* Signal to Python that an Exception is Pending */
820}
821
822#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000823
824/* POSIX generic methods */
825
Barry Warsaw53699e91996-12-10 23:23:01 +0000826static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000827posix_fildes(PyObject *fdobj, int (*func)(int))
828{
Victor Stinner8c62be82010-05-06 00:08:46 +0000829 int fd;
830 int res;
831 fd = PyObject_AsFileDescriptor(fdobj);
832 if (fd < 0)
833 return NULL;
834 if (!_PyVerify_fd(fd))
835 return posix_error();
836 Py_BEGIN_ALLOW_THREADS
837 res = (*func)(fd);
838 Py_END_ALLOW_THREADS
839 if (res < 0)
840 return posix_error();
841 Py_INCREF(Py_None);
842 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000843}
Guido van Rossum21142a01999-01-08 21:05:37 +0000844
845static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000846posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847{
Victor Stinner8c62be82010-05-06 00:08:46 +0000848 PyObject *opath1 = NULL;
849 char *path1;
850 int res;
851 if (!PyArg_ParseTuple(args, format,
852 PyUnicode_FSConverter, &opath1))
853 return NULL;
854 path1 = PyBytes_AsString(opath1);
855 Py_BEGIN_ALLOW_THREADS
856 res = (*func)(path1);
857 Py_END_ALLOW_THREADS
858 if (res < 0)
859 return posix_error_with_allocated_filename(opath1);
860 Py_DECREF(opath1);
861 Py_INCREF(Py_None);
862 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000863}
864
Barry Warsaw53699e91996-12-10 23:23:01 +0000865static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000866posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000867 char *format,
868 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869{
Victor Stinner8c62be82010-05-06 00:08:46 +0000870 PyObject *opath1 = NULL, *opath2 = NULL;
871 char *path1, *path2;
872 int res;
873 if (!PyArg_ParseTuple(args, format,
874 PyUnicode_FSConverter, &opath1,
875 PyUnicode_FSConverter, &opath2)) {
876 return NULL;
877 }
878 path1 = PyBytes_AsString(opath1);
879 path2 = PyBytes_AsString(opath2);
880 Py_BEGIN_ALLOW_THREADS
881 res = (*func)(path1, path2);
882 Py_END_ALLOW_THREADS
883 Py_DECREF(opath1);
884 Py_DECREF(opath2);
885 if (res != 0)
886 /* XXX how to report both path1 and path2??? */
887 return posix_error();
888 Py_INCREF(Py_None);
889 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000890}
891
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000892#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000893static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000894win32_1str(PyObject* args, char* func,
895 char* format, BOOL (__stdcall *funcA)(LPCSTR),
896 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000897{
Victor Stinner8c62be82010-05-06 00:08:46 +0000898 PyObject *uni;
899 char *ansi;
900 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000901
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 if (!PyArg_ParseTuple(args, wformat, &uni))
903 PyErr_Clear();
904 else {
905 Py_BEGIN_ALLOW_THREADS
906 result = funcW(PyUnicode_AsUnicode(uni));
907 Py_END_ALLOW_THREADS
908 if (!result)
909 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
910 Py_INCREF(Py_None);
911 return Py_None;
912 }
913 if (!PyArg_ParseTuple(args, format, &ansi))
914 return NULL;
915 Py_BEGIN_ALLOW_THREADS
916 result = funcA(ansi);
917 Py_END_ALLOW_THREADS
918 if (!result)
919 return win32_error(func, ansi);
920 Py_INCREF(Py_None);
921 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922
923}
924
925/* This is a reimplementation of the C library's chdir function,
926 but one that produces Win32 errors instead of DOS error codes.
927 chdir is essentially a wrapper around SetCurrentDirectory; however,
928 it also needs to set "magic" environment variables indicating
929 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000930static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000931win32_chdir(LPCSTR path)
932{
Victor Stinner8c62be82010-05-06 00:08:46 +0000933 char new_path[MAX_PATH+1];
934 int result;
935 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000936
Victor Stinner8c62be82010-05-06 00:08:46 +0000937 if(!SetCurrentDirectoryA(path))
938 return FALSE;
939 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
940 if (!result)
941 return FALSE;
942 /* In the ANSI API, there should not be any paths longer
943 than MAX_PATH. */
944 assert(result <= MAX_PATH+1);
945 if (strncmp(new_path, "\\\\", 2) == 0 ||
946 strncmp(new_path, "//", 2) == 0)
947 /* UNC path, nothing to do. */
948 return TRUE;
949 env[1] = new_path[0];
950 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951}
952
953/* The Unicode version differs from the ANSI version
954 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000955static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956win32_wchdir(LPCWSTR path)
957{
Victor Stinner8c62be82010-05-06 00:08:46 +0000958 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
959 int result;
960 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961
Victor Stinner8c62be82010-05-06 00:08:46 +0000962 if(!SetCurrentDirectoryW(path))
963 return FALSE;
964 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
965 if (!result)
966 return FALSE;
967 if (result > MAX_PATH+1) {
968 new_path = malloc(result * sizeof(wchar_t));
969 if (!new_path) {
970 SetLastError(ERROR_OUTOFMEMORY);
971 return FALSE;
972 }
973 result = GetCurrentDirectoryW(result, new_path);
974 if (!result) {
975 free(new_path);
976 return FALSE;
977 }
978 }
979 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
980 wcsncmp(new_path, L"//", 2) == 0)
981 /* UNC path, nothing to do. */
982 return TRUE;
983 env[1] = new_path[0];
984 result = SetEnvironmentVariableW(env, new_path);
985 if (new_path != _new_path)
986 free(new_path);
987 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988}
989#endif
990
Martin v. Löwis14694662006-02-03 12:54:16 +0000991#ifdef MS_WINDOWS
992/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
993 - time stamps are restricted to second resolution
994 - file modification times suffer from forth-and-back conversions between
995 UTC and local time
996 Therefore, we implement our own stat, based on the Win32 API directly.
997*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000998#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000999
1000struct win32_stat{
1001 int st_dev;
1002 __int64 st_ino;
1003 unsigned short st_mode;
1004 int st_nlink;
1005 int st_uid;
1006 int st_gid;
1007 int st_rdev;
1008 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001009 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001010 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001011 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001012 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001013 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001014 int st_ctime_nsec;
1015};
1016
1017static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1018
1019static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001020FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001021{
Victor Stinner8c62be82010-05-06 00:08:46 +00001022 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1023 /* Cannot simply cast and dereference in_ptr,
1024 since it might not be aligned properly */
1025 __int64 in;
1026 memcpy(&in, in_ptr, sizeof(in));
1027 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001028 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001029}
1030
Thomas Wouters477c8d52006-05-27 19:21:47 +00001031static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001032time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001033{
Victor Stinner8c62be82010-05-06 00:08:46 +00001034 /* XXX endianness */
1035 __int64 out;
1036 out = time_in + secs_between_epochs;
1037 out = out * 10000000 + nsec_in / 100;
1038 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039}
1040
Martin v. Löwis14694662006-02-03 12:54:16 +00001041/* Below, we *know* that ugo+r is 0444 */
1042#if _S_IREAD != 0400
1043#error Unsupported C library
1044#endif
1045static int
1046attributes_to_mode(DWORD attr)
1047{
Victor Stinner8c62be82010-05-06 00:08:46 +00001048 int m = 0;
1049 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1050 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1051 else
1052 m |= _S_IFREG;
1053 if (attr & FILE_ATTRIBUTE_READONLY)
1054 m |= 0444;
1055 else
1056 m |= 0666;
1057 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001058}
1059
1060static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001061attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001062{
Victor Stinner8c62be82010-05-06 00:08:46 +00001063 memset(result, 0, sizeof(*result));
1064 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1065 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1066 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1067 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1068 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001069 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001070 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001071 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1072 /* first clear the S_IFMT bits */
1073 result->st_mode ^= (result->st_mode & 0170000);
1074 /* now set the bits that make this a symlink */
1075 result->st_mode |= 0120000;
1076 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001077
Victor Stinner8c62be82010-05-06 00:08:46 +00001078 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001079}
1080
Guido van Rossumd8faa362007-04-27 19:54:29 +00001081static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001082attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001083{
Victor Stinner8c62be82010-05-06 00:08:46 +00001084 HANDLE hFindFile;
1085 WIN32_FIND_DATAA FileData;
1086 hFindFile = FindFirstFileA(pszFile, &FileData);
1087 if (hFindFile == INVALID_HANDLE_VALUE)
1088 return FALSE;
1089 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001090 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001091 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001092 info->dwFileAttributes = FileData.dwFileAttributes;
1093 info->ftCreationTime = FileData.ftCreationTime;
1094 info->ftLastAccessTime = FileData.ftLastAccessTime;
1095 info->ftLastWriteTime = FileData.ftLastWriteTime;
1096 info->nFileSizeHigh = FileData.nFileSizeHigh;
1097 info->nFileSizeLow = FileData.nFileSizeLow;
1098/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001099 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1100 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001101 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001102}
1103
1104static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001105attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001106{
Victor Stinner8c62be82010-05-06 00:08:46 +00001107 HANDLE hFindFile;
1108 WIN32_FIND_DATAW FileData;
1109 hFindFile = FindFirstFileW(pszFile, &FileData);
1110 if (hFindFile == INVALID_HANDLE_VALUE)
1111 return FALSE;
1112 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001113 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001114 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001115 info->dwFileAttributes = FileData.dwFileAttributes;
1116 info->ftCreationTime = FileData.ftCreationTime;
1117 info->ftLastAccessTime = FileData.ftLastAccessTime;
1118 info->ftLastWriteTime = FileData.ftLastWriteTime;
1119 info->nFileSizeHigh = FileData.nFileSizeHigh;
1120 info->nFileSizeLow = FileData.nFileSizeLow;
1121/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001122 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1123 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001124 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001125}
1126
Brian Curtinf5e76d02010-11-24 13:14:05 +00001127#ifndef SYMLOOP_MAX
1128#define SYMLOOP_MAX ( 88 )
1129#endif
1130
1131static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001132win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001133
1134static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001135win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001136{
1137 int code;
1138 HANDLE hFile;
1139 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001140 ULONG reparse_tag = 0;
Hirokazu Yamamoto74673512010-12-05 02:48:08 +00001141 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001142 const char *dot;
1143
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001144 if (depth > SYMLOOP_MAX) {
1145 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1146 return -1;
1147 }
1148
Brian Curtinf5e76d02010-11-24 13:14:05 +00001149 hFile = CreateFileA(
1150 path,
1151 0, /* desired access */
1152 0, /* share mode */
1153 NULL, /* security attributes */
1154 OPEN_EXISTING,
1155 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1156 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1157 NULL);
1158
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001159 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001160 /* Either the target doesn't exist, or we don't have access to
1161 get a handle to it. If the former, we need to return an error.
1162 If the latter, we can use attributes_from_dir. */
1163 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001164 return -1;
1165 /* Could not get attributes on open file. Fall back to
1166 reading the directory. */
1167 if (!attributes_from_dir(path, &info, &reparse_tag))
1168 /* Very strange. This should not fail now */
1169 return -1;
1170 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1171 if (traverse) {
1172 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001173 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001174 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001175 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001176 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001177 } else {
1178 if (!GetFileInformationByHandle(hFile, &info)) {
1179 CloseHandle(hFile);
1180 return -1;;
1181 }
1182 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1183 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1184 CloseHandle(hFile);
1185 if (code < 0)
1186 return code;
1187 if (traverse) {
1188 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1189 free(target_path);
1190 return code;
1191 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001192 } else
1193 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001194 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001195 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001196
1197 /* Set S_IEXEC if it is an .exe, .bat, ... */
1198 dot = strrchr(path, '.');
1199 if (dot) {
1200 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1201 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1202 result->st_mode |= 0111;
1203 }
1204 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001205}
1206
1207static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001208win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001209{
1210 int code;
1211 HANDLE hFile;
1212 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001213 ULONG reparse_tag = 0;
1214 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001215 const wchar_t *dot;
1216
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001217 if (depth > SYMLOOP_MAX) {
1218 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1219 return -1;
1220 }
1221
Brian Curtinf5e76d02010-11-24 13:14:05 +00001222 hFile = CreateFileW(
1223 path,
1224 0, /* desired access */
1225 0, /* share mode */
1226 NULL, /* security attributes */
1227 OPEN_EXISTING,
1228 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1229 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1230 NULL);
1231
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001232 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001233 /* Either the target doesn't exist, or we don't have access to
1234 get a handle to it. If the former, we need to return an error.
1235 If the latter, we can use attributes_from_dir. */
1236 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001237 return -1;
1238 /* Could not get attributes on open file. Fall back to
1239 reading the directory. */
1240 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1241 /* Very strange. This should not fail now */
1242 return -1;
1243 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1244 if (traverse) {
1245 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001246 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001247 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001249 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001250 } else {
1251 if (!GetFileInformationByHandle(hFile, &info)) {
1252 CloseHandle(hFile);
1253 return -1;;
1254 }
1255 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1256 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1257 CloseHandle(hFile);
1258 if (code < 0)
1259 return code;
1260 if (traverse) {
1261 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1262 free(target_path);
1263 return code;
1264 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001265 } else
1266 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001267 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001268 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001269
1270 /* Set S_IEXEC if it is an .exe, .bat, ... */
1271 dot = wcsrchr(path, '.');
1272 if (dot) {
1273 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1274 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1275 result->st_mode |= 0111;
1276 }
1277 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001278}
1279
1280static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001281win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001282{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001283 /* Protocol violation: we explicitly clear errno, instead of
1284 setting it to a POSIX error. Callers should use GetLastError. */
1285 int code = win32_xstat_impl(path, result, traverse, 0);
1286 errno = 0;
1287 return code;
1288}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001289
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001290static int
1291win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1292{
1293 /* Protocol violation: we explicitly clear errno, instead of
1294 setting it to a POSIX error. Callers should use GetLastError. */
1295 int code = win32_xstat_impl_w(path, result, traverse, 0);
1296 errno = 0;
1297 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001298}
1299
Brian Curtind40e6f72010-07-08 21:39:08 +00001300/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1301 win32_stat_w
1302
1303 In Posix, stat automatically traverses symlinks and returns the stat
1304 structure for the target. In Windows, the equivalent GetFileAttributes by
1305 default does not traverse symlinks and instead returns attributes for
1306 the symlink.
1307
1308 Therefore, win32_lstat will get the attributes traditionally, and
1309 win32_stat will first explicitly resolve the symlink target and then will
1310 call win32_lstat on that result.
1311
1312 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1313
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001314static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001315win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001316{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001317 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001318}
1319
Victor Stinner8c62be82010-05-06 00:08:46 +00001320static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001321win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001322{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001323 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001324}
1325
1326static int
1327win32_stat(const char* path, struct win32_stat *result)
1328{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001329 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001330}
1331
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001332static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001333win32_stat_w(const wchar_t* path, struct win32_stat *result)
1334{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001335 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001336}
1337
1338static int
1339win32_fstat(int file_number, struct win32_stat *result)
1340{
Victor Stinner8c62be82010-05-06 00:08:46 +00001341 BY_HANDLE_FILE_INFORMATION info;
1342 HANDLE h;
1343 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001344
Victor Stinner8c62be82010-05-06 00:08:46 +00001345 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001346
Victor Stinner8c62be82010-05-06 00:08:46 +00001347 /* Protocol violation: we explicitly clear errno, instead of
1348 setting it to a POSIX error. Callers should use GetLastError. */
1349 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001350
Victor Stinner8c62be82010-05-06 00:08:46 +00001351 if (h == INVALID_HANDLE_VALUE) {
1352 /* This is really a C library error (invalid file handle).
1353 We set the Win32 error to the closes one matching. */
1354 SetLastError(ERROR_INVALID_HANDLE);
1355 return -1;
1356 }
1357 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001358
Victor Stinner8c62be82010-05-06 00:08:46 +00001359 type = GetFileType(h);
1360 if (type == FILE_TYPE_UNKNOWN) {
1361 DWORD error = GetLastError();
1362 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001363 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001364 }
1365 /* else: valid but unknown file */
1366 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001367
Victor Stinner8c62be82010-05-06 00:08:46 +00001368 if (type != FILE_TYPE_DISK) {
1369 if (type == FILE_TYPE_CHAR)
1370 result->st_mode = _S_IFCHR;
1371 else if (type == FILE_TYPE_PIPE)
1372 result->st_mode = _S_IFIFO;
1373 return 0;
1374 }
1375
1376 if (!GetFileInformationByHandle(h, &info)) {
1377 return -1;
1378 }
1379
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001380 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001381 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001382 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1383 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001384}
1385
1386#endif /* MS_WINDOWS */
1387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001388PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001389"stat_result: Result from stat or lstat.\n\n\
1390This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001391 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001392or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1393\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001394Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1395or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001396\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001397See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001398
1399static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001400 {"st_mode", "protection bits"},
1401 {"st_ino", "inode"},
1402 {"st_dev", "device"},
1403 {"st_nlink", "number of hard links"},
1404 {"st_uid", "user ID of owner"},
1405 {"st_gid", "group ID of owner"},
1406 {"st_size", "total size, in bytes"},
1407 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1408 {NULL, "integer time of last access"},
1409 {NULL, "integer time of last modification"},
1410 {NULL, "integer time of last change"},
1411 {"st_atime", "time of last access"},
1412 {"st_mtime", "time of last modification"},
1413 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001414#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001415 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001416#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001417#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001419#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001420#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001421 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001422#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001423#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001424 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001425#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001426#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001427 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001428#endif
1429#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001430 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001431#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001432 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001433};
1434
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001435#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001436#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001437#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001438#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001439#endif
1440
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001441#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001442#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1443#else
1444#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1445#endif
1446
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001447#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001448#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1449#else
1450#define ST_RDEV_IDX ST_BLOCKS_IDX
1451#endif
1452
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001453#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1454#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1455#else
1456#define ST_FLAGS_IDX ST_RDEV_IDX
1457#endif
1458
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001459#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001460#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001461#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001462#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001463#endif
1464
1465#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1466#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1467#else
1468#define ST_BIRTHTIME_IDX ST_GEN_IDX
1469#endif
1470
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001471static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001472 "stat_result", /* name */
1473 stat_result__doc__, /* doc */
1474 stat_result_fields,
1475 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001476};
1477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001478PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001479"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1480This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001481 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001482or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001483\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001484See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001485
1486static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001487 {"f_bsize", },
1488 {"f_frsize", },
1489 {"f_blocks", },
1490 {"f_bfree", },
1491 {"f_bavail", },
1492 {"f_files", },
1493 {"f_ffree", },
1494 {"f_favail", },
1495 {"f_flag", },
1496 {"f_namemax",},
1497 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001498};
1499
1500static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001501 "statvfs_result", /* name */
1502 statvfs_result__doc__, /* doc */
1503 statvfs_result_fields,
1504 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001505};
1506
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001507static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001508static PyTypeObject StatResultType;
1509static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001510static newfunc structseq_new;
1511
1512static PyObject *
1513statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1514{
Victor Stinner8c62be82010-05-06 00:08:46 +00001515 PyStructSequence *result;
1516 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001517
Victor Stinner8c62be82010-05-06 00:08:46 +00001518 result = (PyStructSequence*)structseq_new(type, args, kwds);
1519 if (!result)
1520 return NULL;
1521 /* If we have been initialized from a tuple,
1522 st_?time might be set to None. Initialize it
1523 from the int slots. */
1524 for (i = 7; i <= 9; i++) {
1525 if (result->ob_item[i+3] == Py_None) {
1526 Py_DECREF(Py_None);
1527 Py_INCREF(result->ob_item[i]);
1528 result->ob_item[i+3] = result->ob_item[i];
1529 }
1530 }
1531 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001532}
1533
1534
1535
1536/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001537static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001538
1539PyDoc_STRVAR(stat_float_times__doc__,
1540"stat_float_times([newval]) -> oldval\n\n\
1541Determine whether os.[lf]stat represents time stamps as float objects.\n\
1542If newval is True, future calls to stat() return floats, if it is False,\n\
1543future calls return ints. \n\
1544If newval is omitted, return the current setting.\n");
1545
1546static PyObject*
1547stat_float_times(PyObject* self, PyObject *args)
1548{
Victor Stinner8c62be82010-05-06 00:08:46 +00001549 int newval = -1;
1550 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1551 return NULL;
1552 if (newval == -1)
1553 /* Return old value */
1554 return PyBool_FromLong(_stat_float_times);
1555 _stat_float_times = newval;
1556 Py_INCREF(Py_None);
1557 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001558}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001559
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001560static void
1561fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1562{
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001564#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001565 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001566#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001567 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001568#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001569 if (!ival)
1570 return;
1571 if (_stat_float_times) {
1572 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1573 } else {
1574 fval = ival;
1575 Py_INCREF(fval);
1576 }
1577 PyStructSequence_SET_ITEM(v, index, ival);
1578 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001579}
1580
Tim Peters5aa91602002-01-30 05:46:57 +00001581/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001582 (used by posix_stat() and posix_fstat()) */
1583static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001584_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001585{
Victor Stinner8c62be82010-05-06 00:08:46 +00001586 unsigned long ansec, mnsec, cnsec;
1587 PyObject *v = PyStructSequence_New(&StatResultType);
1588 if (v == NULL)
1589 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001590
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001592#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 PyStructSequence_SET_ITEM(v, 1,
1594 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001595#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001596 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001597#endif
1598#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 PyStructSequence_SET_ITEM(v, 2,
1600 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001601#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001602 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001603#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1605 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1606 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001607#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 PyStructSequence_SET_ITEM(v, 6,
1609 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001610#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001611 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001612#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001613
Martin v. Löwis14694662006-02-03 12:54:16 +00001614#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001615 ansec = st->st_atim.tv_nsec;
1616 mnsec = st->st_mtim.tv_nsec;
1617 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001618#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001619 ansec = st->st_atimespec.tv_nsec;
1620 mnsec = st->st_mtimespec.tv_nsec;
1621 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001622#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001623 ansec = st->st_atime_nsec;
1624 mnsec = st->st_mtime_nsec;
1625 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001626#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001628#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001629 fill_time(v, 7, st->st_atime, ansec);
1630 fill_time(v, 8, st->st_mtime, mnsec);
1631 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001632
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001633#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001634 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1635 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001636#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001637#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001638 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1639 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001640#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001641#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001642 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1643 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001644#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001645#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001646 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1647 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001648#endif
1649#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001650 {
1651 PyObject *val;
1652 unsigned long bsec,bnsec;
1653 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001654#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001656#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001657 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001658#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001659 if (_stat_float_times) {
1660 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1661 } else {
1662 val = PyLong_FromLong((long)bsec);
1663 }
1664 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1665 val);
1666 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001667#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001668#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001669 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1670 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001671#endif
Fred Drake699f3522000-06-29 21:12:41 +00001672
Victor Stinner8c62be82010-05-06 00:08:46 +00001673 if (PyErr_Occurred()) {
1674 Py_DECREF(v);
1675 return NULL;
1676 }
Fred Drake699f3522000-06-29 21:12:41 +00001677
Victor Stinner8c62be82010-05-06 00:08:46 +00001678 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001679}
1680
Barry Warsaw53699e91996-12-10 23:23:01 +00001681static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001682posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001684#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001685 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001686#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001687 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001688#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 char *wformat,
1690 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001691{
Victor Stinner8c62be82010-05-06 00:08:46 +00001692 STRUCT_STAT st;
1693 PyObject *opath;
1694 char *path;
1695 int res;
1696 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001697
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001698#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 PyUnicodeObject *po;
1700 if (PyArg_ParseTuple(args, wformat, &po)) {
1701 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001702
Victor Stinner8c62be82010-05-06 00:08:46 +00001703 Py_BEGIN_ALLOW_THREADS
1704 /* PyUnicode_AS_UNICODE result OK without
1705 thread lock as it is a simple dereference. */
1706 res = wstatfunc(wpath, &st);
1707 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001708
Victor Stinner8c62be82010-05-06 00:08:46 +00001709 if (res != 0)
1710 return win32_error_unicode("stat", wpath);
1711 return _pystat_fromstructstat(&st);
1712 }
1713 /* Drop the argument parsing error as narrow strings
1714 are also valid. */
1715 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001716#endif
1717
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 if (!PyArg_ParseTuple(args, format,
1719 PyUnicode_FSConverter, &opath))
1720 return NULL;
1721 path = PyBytes_AsString(opath);
1722 Py_BEGIN_ALLOW_THREADS
1723 res = (*statfunc)(path, &st);
1724 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001725
Victor Stinner8c62be82010-05-06 00:08:46 +00001726 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001727#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001729#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001731#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 }
1733 else
1734 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001735
Victor Stinner8c62be82010-05-06 00:08:46 +00001736 Py_DECREF(opath);
1737 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001738}
1739
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001740/* POSIX methods */
1741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001742PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001743"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001744Use the real uid/gid to test for access to a path. Note that most\n\
1745operations will use the effective uid/gid, therefore this routine can\n\
1746be used in a suid/sgid environment to test if the invoking user has the\n\
1747specified access to the path. The mode argument can be F_OK to test\n\
1748existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001749
1750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001751posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001752{
Victor Stinner8c62be82010-05-06 00:08:46 +00001753 PyObject *opath;
1754 char *path;
1755 int mode;
1756
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001757#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 DWORD attr;
1759 PyUnicodeObject *po;
1760 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1761 Py_BEGIN_ALLOW_THREADS
1762 /* PyUnicode_AS_UNICODE OK without thread lock as
1763 it is a simple dereference. */
1764 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1765 Py_END_ALLOW_THREADS
1766 goto finish;
1767 }
1768 /* Drop the argument parsing error as narrow strings
1769 are also valid. */
1770 PyErr_Clear();
1771 if (!PyArg_ParseTuple(args, "O&i:access",
1772 PyUnicode_FSConverter, &opath, &mode))
1773 return NULL;
1774 path = PyBytes_AsString(opath);
1775 Py_BEGIN_ALLOW_THREADS
1776 attr = GetFileAttributesA(path);
1777 Py_END_ALLOW_THREADS
1778 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001779finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 if (attr == 0xFFFFFFFF)
1781 /* File does not exist, or cannot read attributes */
1782 return PyBool_FromLong(0);
1783 /* Access is possible if either write access wasn't requested, or
1784 the file isn't read-only, or if it's a directory, as there are
1785 no read-only directories on Windows. */
1786 return PyBool_FromLong(!(mode & 2)
1787 || !(attr & FILE_ATTRIBUTE_READONLY)
1788 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001789#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 int res;
1791 if (!PyArg_ParseTuple(args, "O&i:access",
1792 PyUnicode_FSConverter, &opath, &mode))
1793 return NULL;
1794 path = PyBytes_AsString(opath);
1795 Py_BEGIN_ALLOW_THREADS
1796 res = access(path, mode);
1797 Py_END_ALLOW_THREADS
1798 Py_DECREF(opath);
1799 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001800#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001801}
1802
Guido van Rossumd371ff11999-01-25 16:12:23 +00001803#ifndef F_OK
1804#define F_OK 0
1805#endif
1806#ifndef R_OK
1807#define R_OK 4
1808#endif
1809#ifndef W_OK
1810#define W_OK 2
1811#endif
1812#ifndef X_OK
1813#define X_OK 1
1814#endif
1815
1816#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001817PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001818"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001820
1821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001822posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001823{
Victor Stinner8c62be82010-05-06 00:08:46 +00001824 int id;
1825 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001826
Victor Stinner8c62be82010-05-06 00:08:46 +00001827 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1828 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001829
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001830#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 /* file descriptor 0 only, the default input device (stdin) */
1832 if (id == 0) {
1833 ret = ttyname();
1834 }
1835 else {
1836 ret = NULL;
1837 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001838#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001840#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001841 if (ret == NULL)
1842 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001843 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001844}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001845#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001846
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001847#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001848PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001849"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001850Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001851
1852static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001853posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001854{
Victor Stinner8c62be82010-05-06 00:08:46 +00001855 char *ret;
1856 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001857
Greg Wardb48bc172000-03-01 21:51:56 +00001858#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001860#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001861 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001862#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001863 if (ret == NULL)
1864 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001865 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001866}
1867#endif
1868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001870"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001872
Barry Warsaw53699e91996-12-10 23:23:01 +00001873static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001874posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001875{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001876#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001877 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001878#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001879 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001880#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001881 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001882#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001884#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885}
1886
Fred Drake4d1e64b2002-04-15 19:40:07 +00001887#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001888PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001889"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001890Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001891opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001892
1893static PyObject *
1894posix_fchdir(PyObject *self, PyObject *fdobj)
1895{
Victor Stinner8c62be82010-05-06 00:08:46 +00001896 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001897}
1898#endif /* HAVE_FCHDIR */
1899
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001901PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001902"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001903Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001904
Barry Warsaw53699e91996-12-10 23:23:01 +00001905static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001906posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001907{
Victor Stinner8c62be82010-05-06 00:08:46 +00001908 PyObject *opath = NULL;
1909 char *path = NULL;
1910 int i;
1911 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001912#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001913 DWORD attr;
1914 PyUnicodeObject *po;
1915 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1916 Py_BEGIN_ALLOW_THREADS
1917 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1918 if (attr != 0xFFFFFFFF) {
1919 if (i & _S_IWRITE)
1920 attr &= ~FILE_ATTRIBUTE_READONLY;
1921 else
1922 attr |= FILE_ATTRIBUTE_READONLY;
1923 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1924 }
1925 else
1926 res = 0;
1927 Py_END_ALLOW_THREADS
1928 if (!res)
1929 return win32_error_unicode("chmod",
1930 PyUnicode_AS_UNICODE(po));
1931 Py_INCREF(Py_None);
1932 return Py_None;
1933 }
1934 /* Drop the argument parsing error as narrow strings
1935 are also valid. */
1936 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001937
Victor Stinner8c62be82010-05-06 00:08:46 +00001938 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1939 &opath, &i))
1940 return NULL;
1941 path = PyBytes_AsString(opath);
1942 Py_BEGIN_ALLOW_THREADS
1943 attr = GetFileAttributesA(path);
1944 if (attr != 0xFFFFFFFF) {
1945 if (i & _S_IWRITE)
1946 attr &= ~FILE_ATTRIBUTE_READONLY;
1947 else
1948 attr |= FILE_ATTRIBUTE_READONLY;
1949 res = SetFileAttributesA(path, attr);
1950 }
1951 else
1952 res = 0;
1953 Py_END_ALLOW_THREADS
1954 if (!res) {
1955 win32_error("chmod", path);
1956 Py_DECREF(opath);
1957 return NULL;
1958 }
1959 Py_DECREF(opath);
1960 Py_INCREF(Py_None);
1961 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001962#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001963 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1964 &opath, &i))
1965 return NULL;
1966 path = PyBytes_AsString(opath);
1967 Py_BEGIN_ALLOW_THREADS
1968 res = chmod(path, i);
1969 Py_END_ALLOW_THREADS
1970 if (res < 0)
1971 return posix_error_with_allocated_filename(opath);
1972 Py_DECREF(opath);
1973 Py_INCREF(Py_None);
1974 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001975#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001976}
1977
Christian Heimes4e30a842007-11-30 22:12:06 +00001978#ifdef HAVE_FCHMOD
1979PyDoc_STRVAR(posix_fchmod__doc__,
1980"fchmod(fd, mode)\n\n\
1981Change the access permissions of the file given by file\n\
1982descriptor fd.");
1983
1984static PyObject *
1985posix_fchmod(PyObject *self, PyObject *args)
1986{
Victor Stinner8c62be82010-05-06 00:08:46 +00001987 int fd, mode, res;
1988 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1989 return NULL;
1990 Py_BEGIN_ALLOW_THREADS
1991 res = fchmod(fd, mode);
1992 Py_END_ALLOW_THREADS
1993 if (res < 0)
1994 return posix_error();
1995 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001996}
1997#endif /* HAVE_FCHMOD */
1998
1999#ifdef HAVE_LCHMOD
2000PyDoc_STRVAR(posix_lchmod__doc__,
2001"lchmod(path, mode)\n\n\
2002Change the access permissions of a file. If path is a symlink, this\n\
2003affects the link itself rather than the target.");
2004
2005static PyObject *
2006posix_lchmod(PyObject *self, PyObject *args)
2007{
Victor Stinner8c62be82010-05-06 00:08:46 +00002008 PyObject *opath;
2009 char *path;
2010 int i;
2011 int res;
2012 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2013 &opath, &i))
2014 return NULL;
2015 path = PyBytes_AsString(opath);
2016 Py_BEGIN_ALLOW_THREADS
2017 res = lchmod(path, i);
2018 Py_END_ALLOW_THREADS
2019 if (res < 0)
2020 return posix_error_with_allocated_filename(opath);
2021 Py_DECREF(opath);
2022 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002023}
2024#endif /* HAVE_LCHMOD */
2025
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002026
Thomas Wouterscf297e42007-02-23 15:07:44 +00002027#ifdef HAVE_CHFLAGS
2028PyDoc_STRVAR(posix_chflags__doc__,
2029"chflags(path, flags)\n\n\
2030Set file flags.");
2031
2032static PyObject *
2033posix_chflags(PyObject *self, PyObject *args)
2034{
Victor Stinner8c62be82010-05-06 00:08:46 +00002035 PyObject *opath;
2036 char *path;
2037 unsigned long flags;
2038 int res;
2039 if (!PyArg_ParseTuple(args, "O&k:chflags",
2040 PyUnicode_FSConverter, &opath, &flags))
2041 return NULL;
2042 path = PyBytes_AsString(opath);
2043 Py_BEGIN_ALLOW_THREADS
2044 res = chflags(path, flags);
2045 Py_END_ALLOW_THREADS
2046 if (res < 0)
2047 return posix_error_with_allocated_filename(opath);
2048 Py_DECREF(opath);
2049 Py_INCREF(Py_None);
2050 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002051}
2052#endif /* HAVE_CHFLAGS */
2053
2054#ifdef HAVE_LCHFLAGS
2055PyDoc_STRVAR(posix_lchflags__doc__,
2056"lchflags(path, flags)\n\n\
2057Set file flags.\n\
2058This function will not follow symbolic links.");
2059
2060static PyObject *
2061posix_lchflags(PyObject *self, PyObject *args)
2062{
Victor Stinner8c62be82010-05-06 00:08:46 +00002063 PyObject *opath;
2064 char *path;
2065 unsigned long flags;
2066 int res;
2067 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2068 PyUnicode_FSConverter, &opath, &flags))
2069 return NULL;
2070 path = PyBytes_AsString(opath);
2071 Py_BEGIN_ALLOW_THREADS
2072 res = lchflags(path, flags);
2073 Py_END_ALLOW_THREADS
2074 if (res < 0)
2075 return posix_error_with_allocated_filename(opath);
2076 Py_DECREF(opath);
2077 Py_INCREF(Py_None);
2078 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002079}
2080#endif /* HAVE_LCHFLAGS */
2081
Martin v. Löwis244edc82001-10-04 22:44:26 +00002082#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002083PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002084"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002086
2087static PyObject *
2088posix_chroot(PyObject *self, PyObject *args)
2089{
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002091}
2092#endif
2093
Guido van Rossum21142a01999-01-08 21:05:37 +00002094#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002095PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002096"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002097force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002098
2099static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002100posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002101{
Stefan Krah0e803b32010-11-26 16:16:47 +00002102 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002103}
2104#endif /* HAVE_FSYNC */
2105
2106#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002107
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002108#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002109extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2110#endif
2111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002112PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002113"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002114force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002115 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002116
2117static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002118posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002119{
Stefan Krah0e803b32010-11-26 16:16:47 +00002120 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002121}
2122#endif /* HAVE_FDATASYNC */
2123
2124
Fredrik Lundh10723342000-07-10 16:38:09 +00002125#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002126PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002127"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002128Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002129
Barry Warsaw53699e91996-12-10 23:23:01 +00002130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002131posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002132{
Victor Stinner8c62be82010-05-06 00:08:46 +00002133 PyObject *opath;
2134 char *path;
2135 long uid, gid;
2136 int res;
2137 if (!PyArg_ParseTuple(args, "O&ll:chown",
2138 PyUnicode_FSConverter, &opath,
2139 &uid, &gid))
2140 return NULL;
2141 path = PyBytes_AsString(opath);
2142 Py_BEGIN_ALLOW_THREADS
2143 res = chown(path, (uid_t) uid, (gid_t) gid);
2144 Py_END_ALLOW_THREADS
2145 if (res < 0)
2146 return posix_error_with_allocated_filename(opath);
2147 Py_DECREF(opath);
2148 Py_INCREF(Py_None);
2149 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002150}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002151#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002152
Christian Heimes4e30a842007-11-30 22:12:06 +00002153#ifdef HAVE_FCHOWN
2154PyDoc_STRVAR(posix_fchown__doc__,
2155"fchown(fd, uid, gid)\n\n\
2156Change the owner and group id of the file given by file descriptor\n\
2157fd to the numeric uid and gid.");
2158
2159static PyObject *
2160posix_fchown(PyObject *self, PyObject *args)
2161{
Victor Stinner8c62be82010-05-06 00:08:46 +00002162 int fd;
2163 long uid, gid;
2164 int res;
2165 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2166 return NULL;
2167 Py_BEGIN_ALLOW_THREADS
2168 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2169 Py_END_ALLOW_THREADS
2170 if (res < 0)
2171 return posix_error();
2172 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002173}
2174#endif /* HAVE_FCHOWN */
2175
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002176#ifdef HAVE_LCHOWN
2177PyDoc_STRVAR(posix_lchown__doc__,
2178"lchown(path, uid, gid)\n\n\
2179Change the owner and group id of path to the numeric uid and gid.\n\
2180This function will not follow symbolic links.");
2181
2182static PyObject *
2183posix_lchown(PyObject *self, PyObject *args)
2184{
Victor Stinner8c62be82010-05-06 00:08:46 +00002185 PyObject *opath;
2186 char *path;
2187 long uid, gid;
2188 int res;
2189 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2190 PyUnicode_FSConverter, &opath,
2191 &uid, &gid))
2192 return NULL;
2193 path = PyBytes_AsString(opath);
2194 Py_BEGIN_ALLOW_THREADS
2195 res = lchown(path, (uid_t) uid, (gid_t) gid);
2196 Py_END_ALLOW_THREADS
2197 if (res < 0)
2198 return posix_error_with_allocated_filename(opath);
2199 Py_DECREF(opath);
2200 Py_INCREF(Py_None);
2201 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002202}
2203#endif /* HAVE_LCHOWN */
2204
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002205
Guido van Rossum36bc6801995-06-14 22:54:23 +00002206#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002207static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002208posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002209{
Victor Stinner8c62be82010-05-06 00:08:46 +00002210 char buf[1026];
2211 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002212
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002213#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002214 if (!use_bytes) {
2215 wchar_t wbuf[1026];
2216 wchar_t *wbuf2 = wbuf;
2217 PyObject *resobj;
2218 DWORD len;
2219 Py_BEGIN_ALLOW_THREADS
2220 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2221 /* If the buffer is large enough, len does not include the
2222 terminating \0. If the buffer is too small, len includes
2223 the space needed for the terminator. */
2224 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2225 wbuf2 = malloc(len * sizeof(wchar_t));
2226 if (wbuf2)
2227 len = GetCurrentDirectoryW(len, wbuf2);
2228 }
2229 Py_END_ALLOW_THREADS
2230 if (!wbuf2) {
2231 PyErr_NoMemory();
2232 return NULL;
2233 }
2234 if (!len) {
2235 if (wbuf2 != wbuf) free(wbuf2);
2236 return win32_error("getcwdu", NULL);
2237 }
2238 resobj = PyUnicode_FromWideChar(wbuf2, len);
2239 if (wbuf2 != wbuf) free(wbuf2);
2240 return resobj;
2241 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002242#endif
2243
Victor Stinner8c62be82010-05-06 00:08:46 +00002244 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002245#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002246 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002247#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002248 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002249#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002250 Py_END_ALLOW_THREADS
2251 if (res == NULL)
2252 return posix_error();
2253 if (use_bytes)
2254 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002255 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002256}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002257
2258PyDoc_STRVAR(posix_getcwd__doc__,
2259"getcwd() -> path\n\n\
2260Return a unicode string representing the current working directory.");
2261
2262static PyObject *
2263posix_getcwd_unicode(PyObject *self)
2264{
2265 return posix_getcwd(0);
2266}
2267
2268PyDoc_STRVAR(posix_getcwdb__doc__,
2269"getcwdb() -> path\n\n\
2270Return a bytes string representing the current working directory.");
2271
2272static PyObject *
2273posix_getcwd_bytes(PyObject *self)
2274{
2275 return posix_getcwd(1);
2276}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002277#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002279
Guido van Rossumb6775db1994-08-01 11:34:53 +00002280#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002281PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002282"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002283Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002284
Barry Warsaw53699e91996-12-10 23:23:01 +00002285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002286posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002287{
Victor Stinner8c62be82010-05-06 00:08:46 +00002288 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002289}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002290#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002291
Brian Curtin1b9df392010-11-24 20:24:31 +00002292#ifdef MS_WINDOWS
2293PyDoc_STRVAR(win32_link__doc__,
2294"link(src, dst)\n\n\
2295Create a hard link to a file.");
2296
2297static PyObject *
2298win32_link(PyObject *self, PyObject *args)
2299{
2300 PyObject *osrc, *odst;
2301 char *src, *dst;
2302 BOOL rslt;
2303
Brian Curtinfc889c42010-11-28 23:59:46 +00002304 PyUnicodeObject *usrc, *udst;
2305 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2306 Py_BEGIN_ALLOW_THREADS
2307 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2308 PyUnicode_AS_UNICODE(usrc), NULL);
2309 Py_END_ALLOW_THREADS
2310
2311 if (rslt == 0)
2312 return win32_error("link", NULL);
2313
2314 Py_RETURN_NONE;
2315 }
2316
2317 /* Narrow strings also valid. */
2318 PyErr_Clear();
2319
Brian Curtin1b9df392010-11-24 20:24:31 +00002320 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2321 PyUnicode_FSConverter, &odst))
2322 return NULL;
2323
2324 src = PyBytes_AsString(osrc);
2325 dst = PyBytes_AsString(odst);
2326
2327 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002328 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002329 Py_END_ALLOW_THREADS
2330
Stefan Krah30b341f2010-11-27 11:44:18 +00002331 Py_DECREF(osrc);
2332 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002333 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002334 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002335
2336 Py_RETURN_NONE;
2337}
2338#endif /* MS_WINDOWS */
2339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002340
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002341PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002342"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002343Return a list containing the names of the entries in the directory.\n\
2344\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002345 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002346\n\
2347The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002348entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002349
Barry Warsaw53699e91996-12-10 23:23:01 +00002350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002351posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002352{
Victor Stinner8c62be82010-05-06 00:08:46 +00002353 /* XXX Should redo this putting the (now four) versions of opendir
2354 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002355#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002356
Victor Stinner8c62be82010-05-06 00:08:46 +00002357 PyObject *d, *v;
2358 HANDLE hFindFile;
2359 BOOL result;
2360 WIN32_FIND_DATA FileData;
2361 PyObject *opath;
2362 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2363 char *bufptr = namebuf;
2364 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002365
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002366 PyObject *po = NULL;
2367 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002369 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002370
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002371 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002372 po_wchars = L".";
2373 len = 1;
2374 } else {
2375 po_wchars = PyUnicode_AS_UNICODE(po);
2376 len = PyUnicode_GET_SIZE(po);
2377 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002378 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002379 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2380 if (!wnamebuf) {
2381 PyErr_NoMemory();
2382 return NULL;
2383 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002384 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002385 if (len > 0) {
2386 Py_UNICODE wch = wnamebuf[len-1];
2387 if (wch != L'/' && wch != L'\\' && wch != L':')
2388 wnamebuf[len++] = L'\\';
2389 wcscpy(wnamebuf + len, L"*.*");
2390 }
2391 if ((d = PyList_New(0)) == NULL) {
2392 free(wnamebuf);
2393 return NULL;
2394 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002395 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002396 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002397 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002398 if (hFindFile == INVALID_HANDLE_VALUE) {
2399 int error = GetLastError();
2400 if (error == ERROR_FILE_NOT_FOUND) {
2401 free(wnamebuf);
2402 return d;
2403 }
2404 Py_DECREF(d);
2405 win32_error_unicode("FindFirstFileW", wnamebuf);
2406 free(wnamebuf);
2407 return NULL;
2408 }
2409 do {
2410 /* Skip over . and .. */
2411 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2412 wcscmp(wFileData.cFileName, L"..") != 0) {
2413 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2414 if (v == NULL) {
2415 Py_DECREF(d);
2416 d = NULL;
2417 break;
2418 }
2419 if (PyList_Append(d, v) != 0) {
2420 Py_DECREF(v);
2421 Py_DECREF(d);
2422 d = NULL;
2423 break;
2424 }
2425 Py_DECREF(v);
2426 }
2427 Py_BEGIN_ALLOW_THREADS
2428 result = FindNextFileW(hFindFile, &wFileData);
2429 Py_END_ALLOW_THREADS
2430 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2431 it got to the end of the directory. */
2432 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2433 Py_DECREF(d);
2434 win32_error_unicode("FindNextFileW", wnamebuf);
2435 FindClose(hFindFile);
2436 free(wnamebuf);
2437 return NULL;
2438 }
2439 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002440
Victor Stinner8c62be82010-05-06 00:08:46 +00002441 if (FindClose(hFindFile) == FALSE) {
2442 Py_DECREF(d);
2443 win32_error_unicode("FindClose", wnamebuf);
2444 free(wnamebuf);
2445 return NULL;
2446 }
2447 free(wnamebuf);
2448 return d;
2449 }
2450 /* Drop the argument parsing error as narrow strings
2451 are also valid. */
2452 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002453
Victor Stinner8c62be82010-05-06 00:08:46 +00002454 if (!PyArg_ParseTuple(args, "O&:listdir",
2455 PyUnicode_FSConverter, &opath))
2456 return NULL;
2457 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2458 PyErr_SetString(PyExc_ValueError, "path too long");
2459 Py_DECREF(opath);
2460 return NULL;
2461 }
2462 strcpy(namebuf, PyBytes_AsString(opath));
2463 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002464 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002465 if (len > 0) {
2466 char ch = namebuf[len-1];
2467 if (ch != SEP && ch != ALTSEP && ch != ':')
2468 namebuf[len++] = '/';
2469 strcpy(namebuf + len, "*.*");
2470 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002471
Victor Stinner8c62be82010-05-06 00:08:46 +00002472 if ((d = PyList_New(0)) == NULL)
2473 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002474
Antoine Pitroub73caab2010-08-09 23:39:31 +00002475 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002476 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002477 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002478 if (hFindFile == INVALID_HANDLE_VALUE) {
2479 int error = GetLastError();
2480 if (error == ERROR_FILE_NOT_FOUND)
2481 return d;
2482 Py_DECREF(d);
2483 return win32_error("FindFirstFile", namebuf);
2484 }
2485 do {
2486 /* Skip over . and .. */
2487 if (strcmp(FileData.cFileName, ".") != 0 &&
2488 strcmp(FileData.cFileName, "..") != 0) {
2489 v = PyBytes_FromString(FileData.cFileName);
2490 if (v == NULL) {
2491 Py_DECREF(d);
2492 d = NULL;
2493 break;
2494 }
2495 if (PyList_Append(d, v) != 0) {
2496 Py_DECREF(v);
2497 Py_DECREF(d);
2498 d = NULL;
2499 break;
2500 }
2501 Py_DECREF(v);
2502 }
2503 Py_BEGIN_ALLOW_THREADS
2504 result = FindNextFile(hFindFile, &FileData);
2505 Py_END_ALLOW_THREADS
2506 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2507 it got to the end of the directory. */
2508 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2509 Py_DECREF(d);
2510 win32_error("FindNextFile", namebuf);
2511 FindClose(hFindFile);
2512 return NULL;
2513 }
2514 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002515
Victor Stinner8c62be82010-05-06 00:08:46 +00002516 if (FindClose(hFindFile) == FALSE) {
2517 Py_DECREF(d);
2518 return win32_error("FindClose", namebuf);
2519 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002520
Victor Stinner8c62be82010-05-06 00:08:46 +00002521 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002522
Tim Peters0bb44a42000-09-15 07:44:49 +00002523#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002524
2525#ifndef MAX_PATH
2526#define MAX_PATH CCHMAXPATH
2527#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002528 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002529 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002530 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002531 PyObject *d, *v;
2532 char namebuf[MAX_PATH+5];
2533 HDIR hdir = 1;
2534 ULONG srchcnt = 1;
2535 FILEFINDBUF3 ep;
2536 APIRET rc;
2537
Victor Stinner8c62be82010-05-06 00:08:46 +00002538 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002539 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002540 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002541 name = PyBytes_AsString(oname);
2542 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002543 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002544 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002545 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002546 return NULL;
2547 }
2548 strcpy(namebuf, name);
2549 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002550 if (*pt == ALTSEP)
2551 *pt = SEP;
2552 if (namebuf[len-1] != SEP)
2553 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002554 strcpy(namebuf + len, "*.*");
2555
Neal Norwitz6c913782007-10-14 03:23:09 +00002556 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002557 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002558 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002559 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002560
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002561 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2562 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002563 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002564 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2565 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2566 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002567
2568 if (rc != NO_ERROR) {
2569 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002570 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002571 }
2572
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002573 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002574 do {
2575 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002576 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002577 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002578
2579 strcpy(namebuf, ep.achName);
2580
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002581 /* Leave Case of Name Alone -- In Native Form */
2582 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002583
Christian Heimes72b710a2008-05-26 13:28:38 +00002584 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002585 if (v == NULL) {
2586 Py_DECREF(d);
2587 d = NULL;
2588 break;
2589 }
2590 if (PyList_Append(d, v) != 0) {
2591 Py_DECREF(v);
2592 Py_DECREF(d);
2593 d = NULL;
2594 break;
2595 }
2596 Py_DECREF(v);
2597 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2598 }
2599
Victor Stinnerdcb24032010-04-22 12:08:36 +00002600 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002601 return d;
2602#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002603 PyObject *oname;
2604 char *name;
2605 PyObject *d, *v;
2606 DIR *dirp;
2607 struct dirent *ep;
2608 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002609
Victor Stinner8c62be82010-05-06 00:08:46 +00002610 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002611 /* v is never read, so it does not need to be initialized yet. */
2612 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002613 arg_is_unicode = 0;
2614 PyErr_Clear();
2615 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002616 oname = NULL;
2617 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002618 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002619 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002620 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002621 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002622 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002623 Py_BEGIN_ALLOW_THREADS
2624 dirp = opendir(name);
2625 Py_END_ALLOW_THREADS
2626 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002627 return posix_error_with_allocated_filename(oname);
2628 }
2629 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002630 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002631 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002632 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002633 Py_DECREF(oname);
2634 return NULL;
2635 }
2636 for (;;) {
2637 errno = 0;
2638 Py_BEGIN_ALLOW_THREADS
2639 ep = readdir(dirp);
2640 Py_END_ALLOW_THREADS
2641 if (ep == NULL) {
2642 if (errno == 0) {
2643 break;
2644 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002645 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002646 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002647 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002648 Py_DECREF(d);
2649 return posix_error_with_allocated_filename(oname);
2650 }
2651 }
2652 if (ep->d_name[0] == '.' &&
2653 (NAMLEN(ep) == 1 ||
2654 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2655 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002656 if (arg_is_unicode)
2657 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2658 else
2659 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002660 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002661 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002662 break;
2663 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002664 if (PyList_Append(d, v) != 0) {
2665 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002666 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002667 break;
2668 }
2669 Py_DECREF(v);
2670 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002671 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002672 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002673 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002674 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002675
Victor Stinner8c62be82010-05-06 00:08:46 +00002676 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002677
Tim Peters0bb44a42000-09-15 07:44:49 +00002678#endif /* which OS */
2679} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002680
Antoine Pitrou8250e232011-02-25 23:41:16 +00002681#ifdef HAVE_FDOPENDIR
2682PyDoc_STRVAR(posix_fdlistdir__doc__,
2683"fdlistdir(fd) -> list_of_strings\n\n\
2684Like listdir(), but uses a file descriptor instead.\n\
2685After succesful execution of this function, fd will be closed.");
2686
2687static PyObject *
2688posix_fdlistdir(PyObject *self, PyObject *args)
2689{
2690 PyObject *d, *v;
2691 DIR *dirp;
2692 struct dirent *ep;
2693 int fd;
2694
2695 errno = 0;
2696 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2697 return NULL;
2698 Py_BEGIN_ALLOW_THREADS
2699 dirp = fdopendir(fd);
2700 Py_END_ALLOW_THREADS
2701 if (dirp == NULL) {
2702 close(fd);
2703 return posix_error();
2704 }
2705 if ((d = PyList_New(0)) == NULL) {
2706 Py_BEGIN_ALLOW_THREADS
2707 closedir(dirp);
2708 Py_END_ALLOW_THREADS
2709 return NULL;
2710 }
2711 for (;;) {
2712 errno = 0;
2713 Py_BEGIN_ALLOW_THREADS
2714 ep = readdir(dirp);
2715 Py_END_ALLOW_THREADS
2716 if (ep == NULL) {
2717 if (errno == 0) {
2718 break;
2719 } else {
2720 Py_BEGIN_ALLOW_THREADS
2721 closedir(dirp);
2722 Py_END_ALLOW_THREADS
2723 Py_DECREF(d);
2724 return posix_error();
2725 }
2726 }
2727 if (ep->d_name[0] == '.' &&
2728 (NAMLEN(ep) == 1 ||
2729 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2730 continue;
2731 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2732 if (v == NULL) {
2733 Py_CLEAR(d);
2734 break;
2735 }
2736 if (PyList_Append(d, v) != 0) {
2737 Py_DECREF(v);
2738 Py_CLEAR(d);
2739 break;
2740 }
2741 Py_DECREF(v);
2742 }
2743 Py_BEGIN_ALLOW_THREADS
2744 closedir(dirp);
2745 Py_END_ALLOW_THREADS
2746
2747 return d;
2748}
2749#endif
2750
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002751#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002752/* A helper function for abspath on win32 */
2753static PyObject *
2754posix__getfullpathname(PyObject *self, PyObject *args)
2755{
Victor Stinner8c62be82010-05-06 00:08:46 +00002756 PyObject *opath;
2757 char *path;
2758 char outbuf[MAX_PATH*2];
2759 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002760#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002761 PyUnicodeObject *po;
2762 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2763 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2764 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2765 Py_UNICODE *wtemp;
2766 DWORD result;
2767 PyObject *v;
2768 result = GetFullPathNameW(wpath,
2769 sizeof(woutbuf)/sizeof(woutbuf[0]),
2770 woutbuf, &wtemp);
2771 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2772 woutbufp = malloc(result * sizeof(Py_UNICODE));
2773 if (!woutbufp)
2774 return PyErr_NoMemory();
2775 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2776 }
2777 if (result)
2778 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2779 else
2780 v = win32_error_unicode("GetFullPathNameW", wpath);
2781 if (woutbufp != woutbuf)
2782 free(woutbufp);
2783 return v;
2784 }
2785 /* Drop the argument parsing error as narrow strings
2786 are also valid. */
2787 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002788
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002789#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002790 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2791 PyUnicode_FSConverter, &opath))
2792 return NULL;
2793 path = PyBytes_AsString(opath);
2794 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2795 outbuf, &temp)) {
2796 win32_error("GetFullPathName", path);
2797 Py_DECREF(opath);
2798 return NULL;
2799 }
2800 Py_DECREF(opath);
2801 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2802 return PyUnicode_Decode(outbuf, strlen(outbuf),
2803 Py_FileSystemDefaultEncoding, NULL);
2804 }
2805 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002806} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002807
Brian Curtinf5e76d02010-11-24 13:14:05 +00002808/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2809static int has_GetFinalPathNameByHandle = 0;
2810static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2811 DWORD);
2812static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2813 DWORD);
2814static int
2815check_GetFinalPathNameByHandle()
2816{
2817 HINSTANCE hKernel32;
2818 /* only recheck */
2819 if (!has_GetFinalPathNameByHandle)
2820 {
2821 hKernel32 = GetModuleHandle("KERNEL32");
2822 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2823 "GetFinalPathNameByHandleA");
2824 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2825 "GetFinalPathNameByHandleW");
2826 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2827 Py_GetFinalPathNameByHandleW;
2828 }
2829 return has_GetFinalPathNameByHandle;
2830}
2831
Brian Curtind40e6f72010-07-08 21:39:08 +00002832/* A helper function for samepath on windows */
2833static PyObject *
2834posix__getfinalpathname(PyObject *self, PyObject *args)
2835{
2836 HANDLE hFile;
2837 int buf_size;
2838 wchar_t *target_path;
2839 int result_length;
2840 PyObject *result;
2841 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002842
Brian Curtin94622b02010-09-24 00:03:39 +00002843 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002844 return NULL;
2845 }
2846
2847 if(!check_GetFinalPathNameByHandle()) {
2848 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2849 NotImplementedError. */
2850 return PyErr_Format(PyExc_NotImplementedError,
2851 "GetFinalPathNameByHandle not available on this platform");
2852 }
2853
2854 hFile = CreateFileW(
2855 path,
2856 0, /* desired access */
2857 0, /* share mode */
2858 NULL, /* security attributes */
2859 OPEN_EXISTING,
2860 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2861 FILE_FLAG_BACKUP_SEMANTICS,
2862 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002863
Brian Curtind40e6f72010-07-08 21:39:08 +00002864 if(hFile == INVALID_HANDLE_VALUE) {
2865 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002866 return PyErr_Format(PyExc_RuntimeError,
2867 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002868 }
2869
2870 /* We have a good handle to the target, use it to determine the
2871 target path name. */
2872 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2873
2874 if(!buf_size)
2875 return win32_error_unicode("GetFinalPathNameByHandle", path);
2876
2877 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2878 if(!target_path)
2879 return PyErr_NoMemory();
2880
2881 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2882 buf_size, VOLUME_NAME_DOS);
2883 if(!result_length)
2884 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2885
2886 if(!CloseHandle(hFile))
2887 return win32_error_unicode("GetFinalPathNameByHandle", path);
2888
2889 target_path[result_length] = 0;
2890 result = PyUnicode_FromUnicode(target_path, result_length);
2891 free(target_path);
2892 return result;
2893
2894} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002895
2896static PyObject *
2897posix__getfileinformation(PyObject *self, PyObject *args)
2898{
2899 HANDLE hFile;
2900 BY_HANDLE_FILE_INFORMATION info;
2901 int fd;
2902
2903 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2904 return NULL;
2905
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002906 if (!_PyVerify_fd(fd))
2907 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002908
2909 hFile = (HANDLE)_get_osfhandle(fd);
2910 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002911 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002912
2913 if (!GetFileInformationByHandle(hFile, &info))
2914 return win32_error("_getfileinformation", NULL);
2915
2916 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2917 info.nFileIndexHigh,
2918 info.nFileIndexLow);
2919}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002920#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002921
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002922PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002923"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002924Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002925
Barry Warsaw53699e91996-12-10 23:23:01 +00002926static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002927posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002928{
Victor Stinner8c62be82010-05-06 00:08:46 +00002929 int res;
2930 PyObject *opath;
2931 char *path;
2932 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002933
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002934#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002935 PyUnicodeObject *po;
2936 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2937 Py_BEGIN_ALLOW_THREADS
2938 /* PyUnicode_AS_UNICODE OK without thread lock as
2939 it is a simple dereference. */
2940 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2941 Py_END_ALLOW_THREADS
2942 if (!res)
2943 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2944 Py_INCREF(Py_None);
2945 return Py_None;
2946 }
2947 /* Drop the argument parsing error as narrow strings
2948 are also valid. */
2949 PyErr_Clear();
2950 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2951 PyUnicode_FSConverter, &opath, &mode))
2952 return NULL;
2953 path = PyBytes_AsString(opath);
2954 Py_BEGIN_ALLOW_THREADS
2955 /* PyUnicode_AS_UNICODE OK without thread lock as
2956 it is a simple dereference. */
2957 res = CreateDirectoryA(path, NULL);
2958 Py_END_ALLOW_THREADS
2959 if (!res) {
2960 win32_error("mkdir", path);
2961 Py_DECREF(opath);
2962 return NULL;
2963 }
2964 Py_DECREF(opath);
2965 Py_INCREF(Py_None);
2966 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002967#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002968
Victor Stinner8c62be82010-05-06 00:08:46 +00002969 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2970 PyUnicode_FSConverter, &opath, &mode))
2971 return NULL;
2972 path = PyBytes_AsString(opath);
2973 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002974#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002975 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002976#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002977 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002978#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002979 Py_END_ALLOW_THREADS
2980 if (res < 0)
2981 return posix_error_with_allocated_filename(opath);
2982 Py_DECREF(opath);
2983 Py_INCREF(Py_None);
2984 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002985#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002986}
2987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002988
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002989/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2990#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002991#include <sys/resource.h>
2992#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002993
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002994
2995#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002996PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002997"nice(inc) -> new_priority\n\n\
2998Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002999
Barry Warsaw53699e91996-12-10 23:23:01 +00003000static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003001posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003002{
Victor Stinner8c62be82010-05-06 00:08:46 +00003003 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003004
Victor Stinner8c62be82010-05-06 00:08:46 +00003005 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3006 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003007
Victor Stinner8c62be82010-05-06 00:08:46 +00003008 /* There are two flavours of 'nice': one that returns the new
3009 priority (as required by almost all standards out there) and the
3010 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3011 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003012
Victor Stinner8c62be82010-05-06 00:08:46 +00003013 If we are of the nice family that returns the new priority, we
3014 need to clear errno before the call, and check if errno is filled
3015 before calling posix_error() on a returnvalue of -1, because the
3016 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003017
Victor Stinner8c62be82010-05-06 00:08:46 +00003018 errno = 0;
3019 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003020#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003021 if (value == 0)
3022 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003023#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003024 if (value == -1 && errno != 0)
3025 /* either nice() or getpriority() returned an error */
3026 return posix_error();
3027 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003028}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003029#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003030
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003031
3032#ifdef HAVE_GETPRIORITY
3033PyDoc_STRVAR(posix_getpriority__doc__,
3034"getpriority(which, who) -> current_priority\n\n\
3035Get program scheduling priority.");
3036
3037static PyObject *
3038posix_getpriority(PyObject *self, PyObject *args)
3039{
3040 int which, who, retval;
3041
3042 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3043 return NULL;
3044 errno = 0;
3045 Py_BEGIN_ALLOW_THREADS
3046 retval = getpriority(which, who);
3047 Py_END_ALLOW_THREADS
3048 if (errno != 0)
3049 return posix_error();
3050 return PyLong_FromLong((long)retval);
3051}
3052#endif /* HAVE_GETPRIORITY */
3053
3054
3055#ifdef HAVE_SETPRIORITY
3056PyDoc_STRVAR(posix_setpriority__doc__,
3057"setpriority(which, who, prio) -> None\n\n\
3058Set program scheduling priority.");
3059
3060static PyObject *
3061posix_setpriority(PyObject *self, PyObject *args)
3062{
3063 int which, who, prio, retval;
3064
3065 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3066 return NULL;
3067 Py_BEGIN_ALLOW_THREADS
3068 retval = setpriority(which, who, prio);
3069 Py_END_ALLOW_THREADS
3070 if (retval == -1)
3071 return posix_error();
3072 Py_RETURN_NONE;
3073}
3074#endif /* HAVE_SETPRIORITY */
3075
3076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003077PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003078"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003079Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003080
Barry Warsaw53699e91996-12-10 23:23:01 +00003081static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003082posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003083{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003084#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003085 PyObject *o1, *o2;
3086 char *p1, *p2;
3087 BOOL result;
3088 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3089 goto error;
3090 if (!convert_to_unicode(&o1))
3091 goto error;
3092 if (!convert_to_unicode(&o2)) {
3093 Py_DECREF(o1);
3094 goto error;
3095 }
3096 Py_BEGIN_ALLOW_THREADS
3097 result = MoveFileW(PyUnicode_AsUnicode(o1),
3098 PyUnicode_AsUnicode(o2));
3099 Py_END_ALLOW_THREADS
3100 Py_DECREF(o1);
3101 Py_DECREF(o2);
3102 if (!result)
3103 return win32_error("rename", NULL);
3104 Py_INCREF(Py_None);
3105 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003106error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003107 PyErr_Clear();
3108 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3109 return NULL;
3110 Py_BEGIN_ALLOW_THREADS
3111 result = MoveFileA(p1, p2);
3112 Py_END_ALLOW_THREADS
3113 if (!result)
3114 return win32_error("rename", NULL);
3115 Py_INCREF(Py_None);
3116 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003117#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003118 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003119#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003120}
3121
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003123PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003124"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003125Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003126
Barry Warsaw53699e91996-12-10 23:23:01 +00003127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003128posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003129{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003130#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003131 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003132#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003133 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003134#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003135}
3136
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003137
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003138PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003139"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003140Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003141
Barry Warsaw53699e91996-12-10 23:23:01 +00003142static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003143posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003144{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003145#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003146 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003147#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003149#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003150}
3151
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003152
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003153#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003154PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003155"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003157
Barry Warsaw53699e91996-12-10 23:23:01 +00003158static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003159posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003160{
Victor Stinner8c62be82010-05-06 00:08:46 +00003161 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003162#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 wchar_t *command;
3164 if (!PyArg_ParseTuple(args, "u:system", &command))
3165 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003166
Victor Stinner8c62be82010-05-06 00:08:46 +00003167 Py_BEGIN_ALLOW_THREADS
3168 sts = _wsystem(command);
3169 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003170#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003171 PyObject *command_obj;
3172 char *command;
3173 if (!PyArg_ParseTuple(args, "O&:system",
3174 PyUnicode_FSConverter, &command_obj))
3175 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003176
Victor Stinner8c62be82010-05-06 00:08:46 +00003177 command = PyBytes_AsString(command_obj);
3178 Py_BEGIN_ALLOW_THREADS
3179 sts = system(command);
3180 Py_END_ALLOW_THREADS
3181 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003182#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003183 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003184}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003185#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003186
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003188PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003189"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003190Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003191
Barry Warsaw53699e91996-12-10 23:23:01 +00003192static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003193posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003194{
Victor Stinner8c62be82010-05-06 00:08:46 +00003195 int i;
3196 if (!PyArg_ParseTuple(args, "i:umask", &i))
3197 return NULL;
3198 i = (int)umask(i);
3199 if (i < 0)
3200 return posix_error();
3201 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003202}
3203
Brian Curtind40e6f72010-07-08 21:39:08 +00003204#ifdef MS_WINDOWS
3205
3206/* override the default DeleteFileW behavior so that directory
3207symlinks can be removed with this function, the same as with
3208Unix symlinks */
3209BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3210{
3211 WIN32_FILE_ATTRIBUTE_DATA info;
3212 WIN32_FIND_DATAW find_data;
3213 HANDLE find_data_handle;
3214 int is_directory = 0;
3215 int is_link = 0;
3216
3217 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3218 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003219
Brian Curtind40e6f72010-07-08 21:39:08 +00003220 /* Get WIN32_FIND_DATA structure for the path to determine if
3221 it is a symlink */
3222 if(is_directory &&
3223 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3224 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3225
3226 if(find_data_handle != INVALID_HANDLE_VALUE) {
3227 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3228 FindClose(find_data_handle);
3229 }
3230 }
3231 }
3232
3233 if (is_directory && is_link)
3234 return RemoveDirectoryW(lpFileName);
3235
3236 return DeleteFileW(lpFileName);
3237}
3238#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003240PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003241"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003242Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003243
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003244PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003245"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003246Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003247
Barry Warsaw53699e91996-12-10 23:23:01 +00003248static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003249posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003250{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003251#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003252 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3253 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003254#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003255 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003256#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003257}
3258
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003259
Guido van Rossumb6775db1994-08-01 11:34:53 +00003260#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003261PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003262"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003263Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003264
Barry Warsaw53699e91996-12-10 23:23:01 +00003265static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003266posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003267{
Victor Stinner8c62be82010-05-06 00:08:46 +00003268 struct utsname u;
3269 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003270
Victor Stinner8c62be82010-05-06 00:08:46 +00003271 Py_BEGIN_ALLOW_THREADS
3272 res = uname(&u);
3273 Py_END_ALLOW_THREADS
3274 if (res < 0)
3275 return posix_error();
3276 return Py_BuildValue("(sssss)",
3277 u.sysname,
3278 u.nodename,
3279 u.release,
3280 u.version,
3281 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003282}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003283#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003284
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003285static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003286extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003287{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003288 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003289 if (PyFloat_Check(t)) {
3290 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003291 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 if (!intobj)
3293 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003294#if SIZEOF_TIME_T > SIZEOF_LONG
3295 intval = PyLong_AsUnsignedLongLongMask(intobj);
3296#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003297 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003298#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003299 Py_DECREF(intobj);
3300 if (intval == -1 && PyErr_Occurred())
3301 return -1;
3302 *sec = intval;
3303 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3304 if (*usec < 0)
3305 /* If rounding gave us a negative number,
3306 truncate. */
3307 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003308 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003309 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003310#if SIZEOF_TIME_T > SIZEOF_LONG
3311 intval = PyLong_AsUnsignedLongLongMask(t);
3312#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003313 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003314#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003315 if (intval == -1 && PyErr_Occurred())
3316 return -1;
3317 *sec = intval;
3318 *usec = 0;
3319 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003320}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003322PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003323"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003324utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003325Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003326second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003327
Barry Warsaw53699e91996-12-10 23:23:01 +00003328static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003329posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003330{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003331#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003332 PyObject *arg;
3333 PyUnicodeObject *obwpath;
3334 wchar_t *wpath = NULL;
3335 PyObject *oapath;
3336 char *apath;
3337 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003338 time_t atimesec, mtimesec;
3339 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003340 FILETIME atime, mtime;
3341 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003342
Victor Stinner8c62be82010-05-06 00:08:46 +00003343 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3344 wpath = PyUnicode_AS_UNICODE(obwpath);
3345 Py_BEGIN_ALLOW_THREADS
3346 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3347 NULL, OPEN_EXISTING,
3348 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3349 Py_END_ALLOW_THREADS
3350 if (hFile == INVALID_HANDLE_VALUE)
3351 return win32_error_unicode("utime", wpath);
3352 } else
3353 /* Drop the argument parsing error as narrow strings
3354 are also valid. */
3355 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003356
Victor Stinner8c62be82010-05-06 00:08:46 +00003357 if (!wpath) {
3358 if (!PyArg_ParseTuple(args, "O&O:utime",
3359 PyUnicode_FSConverter, &oapath, &arg))
3360 return NULL;
3361 apath = PyBytes_AsString(oapath);
3362 Py_BEGIN_ALLOW_THREADS
3363 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3364 NULL, OPEN_EXISTING,
3365 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3366 Py_END_ALLOW_THREADS
3367 if (hFile == INVALID_HANDLE_VALUE) {
3368 win32_error("utime", apath);
3369 Py_DECREF(oapath);
3370 return NULL;
3371 }
3372 Py_DECREF(oapath);
3373 }
3374
3375 if (arg == Py_None) {
3376 SYSTEMTIME now;
3377 GetSystemTime(&now);
3378 if (!SystemTimeToFileTime(&now, &mtime) ||
3379 !SystemTimeToFileTime(&now, &atime)) {
3380 win32_error("utime", NULL);
3381 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003382 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003383 }
3384 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3385 PyErr_SetString(PyExc_TypeError,
3386 "utime() arg 2 must be a tuple (atime, mtime)");
3387 goto done;
3388 }
3389 else {
3390 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3391 &atimesec, &ausec) == -1)
3392 goto done;
3393 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3394 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3395 &mtimesec, &musec) == -1)
3396 goto done;
3397 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3398 }
3399 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3400 /* Avoid putting the file name into the error here,
3401 as that may confuse the user into believing that
3402 something is wrong with the file, when it also
3403 could be the time stamp that gives a problem. */
3404 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003405 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003406 }
3407 Py_INCREF(Py_None);
3408 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003409done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003410 CloseHandle(hFile);
3411 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003412#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003413
Victor Stinner8c62be82010-05-06 00:08:46 +00003414 PyObject *opath;
3415 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003416 time_t atime, mtime;
3417 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003418 int res;
3419 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003420
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003421#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003422 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003423#define ATIME buf[0].tv_sec
3424#define MTIME buf[1].tv_sec
3425#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003426/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003427 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003428#define ATIME buf.actime
3429#define MTIME buf.modtime
3430#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003431#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003432 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003433#define ATIME buf[0]
3434#define MTIME buf[1]
3435#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003436#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003437
Mark Hammond817c9292003-12-03 01:22:38 +00003438
Victor Stinner8c62be82010-05-06 00:08:46 +00003439 if (!PyArg_ParseTuple(args, "O&O:utime",
3440 PyUnicode_FSConverter, &opath, &arg))
3441 return NULL;
3442 path = PyBytes_AsString(opath);
3443 if (arg == Py_None) {
3444 /* optional time values not given */
3445 Py_BEGIN_ALLOW_THREADS
3446 res = utime(path, NULL);
3447 Py_END_ALLOW_THREADS
3448 }
3449 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3450 PyErr_SetString(PyExc_TypeError,
3451 "utime() arg 2 must be a tuple (atime, mtime)");
3452 Py_DECREF(opath);
3453 return NULL;
3454 }
3455 else {
3456 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3457 &atime, &ausec) == -1) {
3458 Py_DECREF(opath);
3459 return NULL;
3460 }
3461 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3462 &mtime, &musec) == -1) {
3463 Py_DECREF(opath);
3464 return NULL;
3465 }
3466 ATIME = atime;
3467 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003468#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003469 buf[0].tv_usec = ausec;
3470 buf[1].tv_usec = musec;
3471 Py_BEGIN_ALLOW_THREADS
3472 res = utimes(path, buf);
3473 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003474#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003475 Py_BEGIN_ALLOW_THREADS
3476 res = utime(path, UTIME_ARG);
3477 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003478#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003479 }
3480 if (res < 0) {
3481 return posix_error_with_allocated_filename(opath);
3482 }
3483 Py_DECREF(opath);
3484 Py_INCREF(Py_None);
3485 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003486#undef UTIME_ARG
3487#undef ATIME
3488#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003489#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003490}
3491
Guido van Rossum85e3b011991-06-03 12:42:10 +00003492
Guido van Rossum3b066191991-06-04 19:40:25 +00003493/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003495PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003496"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003497Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003498
Barry Warsaw53699e91996-12-10 23:23:01 +00003499static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003500posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003501{
Victor Stinner8c62be82010-05-06 00:08:46 +00003502 int sts;
3503 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3504 return NULL;
3505 _exit(sts);
3506 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003507}
3508
Martin v. Löwis114619e2002-10-07 06:44:21 +00003509#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3510static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003511free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003512{
Victor Stinner8c62be82010-05-06 00:08:46 +00003513 Py_ssize_t i;
3514 for (i = 0; i < count; i++)
3515 PyMem_Free(array[i]);
3516 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003517}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003518
Antoine Pitrou69f71142009-05-24 21:25:49 +00003519static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003520int fsconvert_strdup(PyObject *o, char**out)
3521{
Victor Stinner8c62be82010-05-06 00:08:46 +00003522 PyObject *bytes;
3523 Py_ssize_t size;
3524 if (!PyUnicode_FSConverter(o, &bytes))
3525 return 0;
3526 size = PyBytes_GET_SIZE(bytes);
3527 *out = PyMem_Malloc(size+1);
3528 if (!*out)
3529 return 0;
3530 memcpy(*out, PyBytes_AsString(bytes), size+1);
3531 Py_DECREF(bytes);
3532 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003533}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003534#endif
3535
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003536
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003537#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003538PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003539"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003540Execute an executable path with arguments, replacing current process.\n\
3541\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003542 path: path of executable file\n\
3543 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003544
Barry Warsaw53699e91996-12-10 23:23:01 +00003545static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003546posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003547{
Victor Stinner8c62be82010-05-06 00:08:46 +00003548 PyObject *opath;
3549 char *path;
3550 PyObject *argv;
3551 char **argvlist;
3552 Py_ssize_t i, argc;
3553 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003554
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 /* execv has two arguments: (path, argv), where
3556 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003557
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 if (!PyArg_ParseTuple(args, "O&O:execv",
3559 PyUnicode_FSConverter,
3560 &opath, &argv))
3561 return NULL;
3562 path = PyBytes_AsString(opath);
3563 if (PyList_Check(argv)) {
3564 argc = PyList_Size(argv);
3565 getitem = PyList_GetItem;
3566 }
3567 else if (PyTuple_Check(argv)) {
3568 argc = PyTuple_Size(argv);
3569 getitem = PyTuple_GetItem;
3570 }
3571 else {
3572 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3573 Py_DECREF(opath);
3574 return NULL;
3575 }
3576 if (argc < 1) {
3577 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3578 Py_DECREF(opath);
3579 return NULL;
3580 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003581
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 argvlist = PyMem_NEW(char *, argc+1);
3583 if (argvlist == NULL) {
3584 Py_DECREF(opath);
3585 return PyErr_NoMemory();
3586 }
3587 for (i = 0; i < argc; i++) {
3588 if (!fsconvert_strdup((*getitem)(argv, i),
3589 &argvlist[i])) {
3590 free_string_array(argvlist, i);
3591 PyErr_SetString(PyExc_TypeError,
3592 "execv() arg 2 must contain only strings");
3593 Py_DECREF(opath);
3594 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003595
Victor Stinner8c62be82010-05-06 00:08:46 +00003596 }
3597 }
3598 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003599
Victor Stinner8c62be82010-05-06 00:08:46 +00003600 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003601
Victor Stinner8c62be82010-05-06 00:08:46 +00003602 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003603
Victor Stinner8c62be82010-05-06 00:08:46 +00003604 free_string_array(argvlist, argc);
3605 Py_DECREF(opath);
3606 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003607}
3608
Victor Stinner13bb71c2010-04-23 21:41:56 +00003609static char**
3610parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3611{
Victor Stinner8c62be82010-05-06 00:08:46 +00003612 char **envlist;
3613 Py_ssize_t i, pos, envc;
3614 PyObject *keys=NULL, *vals=NULL;
3615 PyObject *key, *val, *key2, *val2;
3616 char *p, *k, *v;
3617 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003618
Victor Stinner8c62be82010-05-06 00:08:46 +00003619 i = PyMapping_Size(env);
3620 if (i < 0)
3621 return NULL;
3622 envlist = PyMem_NEW(char *, i + 1);
3623 if (envlist == NULL) {
3624 PyErr_NoMemory();
3625 return NULL;
3626 }
3627 envc = 0;
3628 keys = PyMapping_Keys(env);
3629 vals = PyMapping_Values(env);
3630 if (!keys || !vals)
3631 goto error;
3632 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3633 PyErr_Format(PyExc_TypeError,
3634 "env.keys() or env.values() is not a list");
3635 goto error;
3636 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003637
Victor Stinner8c62be82010-05-06 00:08:46 +00003638 for (pos = 0; pos < i; pos++) {
3639 key = PyList_GetItem(keys, pos);
3640 val = PyList_GetItem(vals, pos);
3641 if (!key || !val)
3642 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003643
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 if (PyUnicode_FSConverter(key, &key2) == 0)
3645 goto error;
3646 if (PyUnicode_FSConverter(val, &val2) == 0) {
3647 Py_DECREF(key2);
3648 goto error;
3649 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003650
3651#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003652 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3653 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003654#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003655 k = PyBytes_AsString(key2);
3656 v = PyBytes_AsString(val2);
3657 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003658
Victor Stinner8c62be82010-05-06 00:08:46 +00003659 p = PyMem_NEW(char, len);
3660 if (p == NULL) {
3661 PyErr_NoMemory();
3662 Py_DECREF(key2);
3663 Py_DECREF(val2);
3664 goto error;
3665 }
3666 PyOS_snprintf(p, len, "%s=%s", k, v);
3667 envlist[envc++] = p;
3668 Py_DECREF(key2);
3669 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003670#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003671 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003672#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003673 }
3674 Py_DECREF(vals);
3675 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003676
Victor Stinner8c62be82010-05-06 00:08:46 +00003677 envlist[envc] = 0;
3678 *envc_ptr = envc;
3679 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003680
3681error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003682 Py_XDECREF(keys);
3683 Py_XDECREF(vals);
3684 while (--envc >= 0)
3685 PyMem_DEL(envlist[envc]);
3686 PyMem_DEL(envlist);
3687 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003688}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003690PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003691"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003692Execute a path with arguments and environment, replacing current process.\n\
3693\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003694 path: path of executable file\n\
3695 args: tuple or list of arguments\n\
3696 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003697
Barry Warsaw53699e91996-12-10 23:23:01 +00003698static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003699posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003700{
Victor Stinner8c62be82010-05-06 00:08:46 +00003701 PyObject *opath;
3702 char *path;
3703 PyObject *argv, *env;
3704 char **argvlist;
3705 char **envlist;
3706 Py_ssize_t i, argc, envc;
3707 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3708 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003709
Victor Stinner8c62be82010-05-06 00:08:46 +00003710 /* execve has three arguments: (path, argv, env), where
3711 argv is a list or tuple of strings and env is a dictionary
3712 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003713
Victor Stinner8c62be82010-05-06 00:08:46 +00003714 if (!PyArg_ParseTuple(args, "O&OO:execve",
3715 PyUnicode_FSConverter,
3716 &opath, &argv, &env))
3717 return NULL;
3718 path = PyBytes_AsString(opath);
3719 if (PyList_Check(argv)) {
3720 argc = PyList_Size(argv);
3721 getitem = PyList_GetItem;
3722 }
3723 else if (PyTuple_Check(argv)) {
3724 argc = PyTuple_Size(argv);
3725 getitem = PyTuple_GetItem;
3726 }
3727 else {
3728 PyErr_SetString(PyExc_TypeError,
3729 "execve() arg 2 must be a tuple or list");
3730 goto fail_0;
3731 }
3732 if (!PyMapping_Check(env)) {
3733 PyErr_SetString(PyExc_TypeError,
3734 "execve() arg 3 must be a mapping object");
3735 goto fail_0;
3736 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003737
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 argvlist = PyMem_NEW(char *, argc+1);
3739 if (argvlist == NULL) {
3740 PyErr_NoMemory();
3741 goto fail_0;
3742 }
3743 for (i = 0; i < argc; i++) {
3744 if (!fsconvert_strdup((*getitem)(argv, i),
3745 &argvlist[i]))
3746 {
3747 lastarg = i;
3748 goto fail_1;
3749 }
3750 }
3751 lastarg = argc;
3752 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003753
Victor Stinner8c62be82010-05-06 00:08:46 +00003754 envlist = parse_envlist(env, &envc);
3755 if (envlist == NULL)
3756 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003757
Victor Stinner8c62be82010-05-06 00:08:46 +00003758 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003759
Victor Stinner8c62be82010-05-06 00:08:46 +00003760 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003761
Victor Stinner8c62be82010-05-06 00:08:46 +00003762 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003763
Victor Stinner8c62be82010-05-06 00:08:46 +00003764 while (--envc >= 0)
3765 PyMem_DEL(envlist[envc]);
3766 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003767 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003768 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003769 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003770 Py_DECREF(opath);
3771 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003772}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003773#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003774
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003775
Guido van Rossuma1065681999-01-25 23:20:23 +00003776#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003777PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003778"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003779Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003780\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003781 mode: mode of process creation\n\
3782 path: path of executable file\n\
3783 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003784
3785static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003786posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003787{
Victor Stinner8c62be82010-05-06 00:08:46 +00003788 PyObject *opath;
3789 char *path;
3790 PyObject *argv;
3791 char **argvlist;
3792 int mode, i;
3793 Py_ssize_t argc;
3794 Py_intptr_t spawnval;
3795 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003796
Victor Stinner8c62be82010-05-06 00:08:46 +00003797 /* spawnv has three arguments: (mode, path, argv), where
3798 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003799
Victor Stinner8c62be82010-05-06 00:08:46 +00003800 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3801 PyUnicode_FSConverter,
3802 &opath, &argv))
3803 return NULL;
3804 path = PyBytes_AsString(opath);
3805 if (PyList_Check(argv)) {
3806 argc = PyList_Size(argv);
3807 getitem = PyList_GetItem;
3808 }
3809 else if (PyTuple_Check(argv)) {
3810 argc = PyTuple_Size(argv);
3811 getitem = PyTuple_GetItem;
3812 }
3813 else {
3814 PyErr_SetString(PyExc_TypeError,
3815 "spawnv() arg 2 must be a tuple or list");
3816 Py_DECREF(opath);
3817 return NULL;
3818 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003819
Victor Stinner8c62be82010-05-06 00:08:46 +00003820 argvlist = PyMem_NEW(char *, argc+1);
3821 if (argvlist == NULL) {
3822 Py_DECREF(opath);
3823 return PyErr_NoMemory();
3824 }
3825 for (i = 0; i < argc; i++) {
3826 if (!fsconvert_strdup((*getitem)(argv, i),
3827 &argvlist[i])) {
3828 free_string_array(argvlist, i);
3829 PyErr_SetString(
3830 PyExc_TypeError,
3831 "spawnv() arg 2 must contain only strings");
3832 Py_DECREF(opath);
3833 return NULL;
3834 }
3835 }
3836 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003837
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003838#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 Py_BEGIN_ALLOW_THREADS
3840 spawnval = spawnv(mode, path, argvlist);
3841 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003842#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003843 if (mode == _OLD_P_OVERLAY)
3844 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003845
Victor Stinner8c62be82010-05-06 00:08:46 +00003846 Py_BEGIN_ALLOW_THREADS
3847 spawnval = _spawnv(mode, path, argvlist);
3848 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003849#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003850
Victor Stinner8c62be82010-05-06 00:08:46 +00003851 free_string_array(argvlist, argc);
3852 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003853
Victor Stinner8c62be82010-05-06 00:08:46 +00003854 if (spawnval == -1)
3855 return posix_error();
3856 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003857#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003858 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003859#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003860 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003861#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003862}
3863
3864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003865PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003866"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003867Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003868\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003869 mode: mode of process creation\n\
3870 path: path of executable file\n\
3871 args: tuple or list of arguments\n\
3872 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003873
3874static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003875posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003876{
Victor Stinner8c62be82010-05-06 00:08:46 +00003877 PyObject *opath;
3878 char *path;
3879 PyObject *argv, *env;
3880 char **argvlist;
3881 char **envlist;
3882 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003883 int mode;
3884 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003885 Py_intptr_t spawnval;
3886 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3887 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003888
Victor Stinner8c62be82010-05-06 00:08:46 +00003889 /* spawnve has four arguments: (mode, path, argv, env), where
3890 argv is a list or tuple of strings and env is a dictionary
3891 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003892
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3894 PyUnicode_FSConverter,
3895 &opath, &argv, &env))
3896 return NULL;
3897 path = PyBytes_AsString(opath);
3898 if (PyList_Check(argv)) {
3899 argc = PyList_Size(argv);
3900 getitem = PyList_GetItem;
3901 }
3902 else if (PyTuple_Check(argv)) {
3903 argc = PyTuple_Size(argv);
3904 getitem = PyTuple_GetItem;
3905 }
3906 else {
3907 PyErr_SetString(PyExc_TypeError,
3908 "spawnve() arg 2 must be a tuple or list");
3909 goto fail_0;
3910 }
3911 if (!PyMapping_Check(env)) {
3912 PyErr_SetString(PyExc_TypeError,
3913 "spawnve() arg 3 must be a mapping object");
3914 goto fail_0;
3915 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003916
Victor Stinner8c62be82010-05-06 00:08:46 +00003917 argvlist = PyMem_NEW(char *, argc+1);
3918 if (argvlist == NULL) {
3919 PyErr_NoMemory();
3920 goto fail_0;
3921 }
3922 for (i = 0; i < argc; i++) {
3923 if (!fsconvert_strdup((*getitem)(argv, i),
3924 &argvlist[i]))
3925 {
3926 lastarg = i;
3927 goto fail_1;
3928 }
3929 }
3930 lastarg = argc;
3931 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003932
Victor Stinner8c62be82010-05-06 00:08:46 +00003933 envlist = parse_envlist(env, &envc);
3934 if (envlist == NULL)
3935 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003936
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003937#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003938 Py_BEGIN_ALLOW_THREADS
3939 spawnval = spawnve(mode, path, argvlist, envlist);
3940 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003941#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003942 if (mode == _OLD_P_OVERLAY)
3943 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003944
Victor Stinner8c62be82010-05-06 00:08:46 +00003945 Py_BEGIN_ALLOW_THREADS
3946 spawnval = _spawnve(mode, path, argvlist, envlist);
3947 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003948#endif
Tim Peters25059d32001-12-07 20:35:43 +00003949
Victor Stinner8c62be82010-05-06 00:08:46 +00003950 if (spawnval == -1)
3951 (void) posix_error();
3952 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003953#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003954 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003955#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003956 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003957#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003958
Victor Stinner8c62be82010-05-06 00:08:46 +00003959 while (--envc >= 0)
3960 PyMem_DEL(envlist[envc]);
3961 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003962 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003963 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003964 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003965 Py_DECREF(opath);
3966 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003967}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003968
3969/* OS/2 supports spawnvp & spawnvpe natively */
3970#if defined(PYOS_OS2)
3971PyDoc_STRVAR(posix_spawnvp__doc__,
3972"spawnvp(mode, file, args)\n\n\
3973Execute the program 'file' in a new process, using the environment\n\
3974search path to find the file.\n\
3975\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003976 mode: mode of process creation\n\
3977 file: executable file name\n\
3978 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003979
3980static PyObject *
3981posix_spawnvp(PyObject *self, PyObject *args)
3982{
Victor Stinner8c62be82010-05-06 00:08:46 +00003983 PyObject *opath;
3984 char *path;
3985 PyObject *argv;
3986 char **argvlist;
3987 int mode, i, argc;
3988 Py_intptr_t spawnval;
3989 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003990
Victor Stinner8c62be82010-05-06 00:08:46 +00003991 /* spawnvp has three arguments: (mode, path, argv), where
3992 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003993
Victor Stinner8c62be82010-05-06 00:08:46 +00003994 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3995 PyUnicode_FSConverter,
3996 &opath, &argv))
3997 return NULL;
3998 path = PyBytes_AsString(opath);
3999 if (PyList_Check(argv)) {
4000 argc = PyList_Size(argv);
4001 getitem = PyList_GetItem;
4002 }
4003 else if (PyTuple_Check(argv)) {
4004 argc = PyTuple_Size(argv);
4005 getitem = PyTuple_GetItem;
4006 }
4007 else {
4008 PyErr_SetString(PyExc_TypeError,
4009 "spawnvp() arg 2 must be a tuple or list");
4010 Py_DECREF(opath);
4011 return NULL;
4012 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004013
Victor Stinner8c62be82010-05-06 00:08:46 +00004014 argvlist = PyMem_NEW(char *, argc+1);
4015 if (argvlist == NULL) {
4016 Py_DECREF(opath);
4017 return PyErr_NoMemory();
4018 }
4019 for (i = 0; i < argc; i++) {
4020 if (!fsconvert_strdup((*getitem)(argv, i),
4021 &argvlist[i])) {
4022 free_string_array(argvlist, i);
4023 PyErr_SetString(
4024 PyExc_TypeError,
4025 "spawnvp() arg 2 must contain only strings");
4026 Py_DECREF(opath);
4027 return NULL;
4028 }
4029 }
4030 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004031
Victor Stinner8c62be82010-05-06 00:08:46 +00004032 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004033#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004034 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004035#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004036 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004037#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004038 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004039
Victor Stinner8c62be82010-05-06 00:08:46 +00004040 free_string_array(argvlist, argc);
4041 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004042
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 if (spawnval == -1)
4044 return posix_error();
4045 else
4046 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004047}
4048
4049
4050PyDoc_STRVAR(posix_spawnvpe__doc__,
4051"spawnvpe(mode, file, args, env)\n\n\
4052Execute the program 'file' in a new process, using the environment\n\
4053search path to find the file.\n\
4054\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004055 mode: mode of process creation\n\
4056 file: executable file name\n\
4057 args: tuple or list of arguments\n\
4058 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004059
4060static PyObject *
4061posix_spawnvpe(PyObject *self, PyObject *args)
4062{
Victor Stinner8c62be82010-05-06 00:08:46 +00004063 PyObject *opath
4064 char *path;
4065 PyObject *argv, *env;
4066 char **argvlist;
4067 char **envlist;
4068 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004069 int mode;
4070 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004071 Py_intptr_t spawnval;
4072 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4073 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004074
Victor Stinner8c62be82010-05-06 00:08:46 +00004075 /* spawnvpe has four arguments: (mode, path, argv, env), where
4076 argv is a list or tuple of strings and env is a dictionary
4077 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004078
Victor Stinner8c62be82010-05-06 00:08:46 +00004079 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4080 PyUnicode_FSConverter,
4081 &opath, &argv, &env))
4082 return NULL;
4083 path = PyBytes_AsString(opath);
4084 if (PyList_Check(argv)) {
4085 argc = PyList_Size(argv);
4086 getitem = PyList_GetItem;
4087 }
4088 else if (PyTuple_Check(argv)) {
4089 argc = PyTuple_Size(argv);
4090 getitem = PyTuple_GetItem;
4091 }
4092 else {
4093 PyErr_SetString(PyExc_TypeError,
4094 "spawnvpe() arg 2 must be a tuple or list");
4095 goto fail_0;
4096 }
4097 if (!PyMapping_Check(env)) {
4098 PyErr_SetString(PyExc_TypeError,
4099 "spawnvpe() arg 3 must be a mapping object");
4100 goto fail_0;
4101 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004102
Victor Stinner8c62be82010-05-06 00:08:46 +00004103 argvlist = PyMem_NEW(char *, argc+1);
4104 if (argvlist == NULL) {
4105 PyErr_NoMemory();
4106 goto fail_0;
4107 }
4108 for (i = 0; i < argc; i++) {
4109 if (!fsconvert_strdup((*getitem)(argv, i),
4110 &argvlist[i]))
4111 {
4112 lastarg = i;
4113 goto fail_1;
4114 }
4115 }
4116 lastarg = argc;
4117 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004118
Victor Stinner8c62be82010-05-06 00:08:46 +00004119 envlist = parse_envlist(env, &envc);
4120 if (envlist == NULL)
4121 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004122
Victor Stinner8c62be82010-05-06 00:08:46 +00004123 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004124#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004125 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004126#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004127 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004128#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004129 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004130
Victor Stinner8c62be82010-05-06 00:08:46 +00004131 if (spawnval == -1)
4132 (void) posix_error();
4133 else
4134 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004135
Victor Stinner8c62be82010-05-06 00:08:46 +00004136 while (--envc >= 0)
4137 PyMem_DEL(envlist[envc]);
4138 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004139 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004140 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004141 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004142 Py_DECREF(opath);
4143 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004144}
4145#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004146#endif /* HAVE_SPAWNV */
4147
4148
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004149#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004150PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004151"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004152Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4153\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004154Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004155
4156static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004157posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004158{
Victor Stinner8c62be82010-05-06 00:08:46 +00004159 pid_t pid;
4160 int result = 0;
4161 _PyImport_AcquireLock();
4162 pid = fork1();
4163 if (pid == 0) {
4164 /* child: this clobbers and resets the import lock. */
4165 PyOS_AfterFork();
4166 } else {
4167 /* parent: release the import lock. */
4168 result = _PyImport_ReleaseLock();
4169 }
4170 if (pid == -1)
4171 return posix_error();
4172 if (result < 0) {
4173 /* Don't clobber the OSError if the fork failed. */
4174 PyErr_SetString(PyExc_RuntimeError,
4175 "not holding the import lock");
4176 return NULL;
4177 }
4178 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004179}
4180#endif
4181
4182
Guido van Rossumad0ee831995-03-01 10:34:45 +00004183#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004184PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004185"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004186Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004187Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004188
Barry Warsaw53699e91996-12-10 23:23:01 +00004189static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004190posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004191{
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 pid_t pid;
4193 int result = 0;
4194 _PyImport_AcquireLock();
4195 pid = fork();
4196 if (pid == 0) {
4197 /* child: this clobbers and resets the import lock. */
4198 PyOS_AfterFork();
4199 } else {
4200 /* parent: release the import lock. */
4201 result = _PyImport_ReleaseLock();
4202 }
4203 if (pid == -1)
4204 return posix_error();
4205 if (result < 0) {
4206 /* Don't clobber the OSError if the fork failed. */
4207 PyErr_SetString(PyExc_RuntimeError,
4208 "not holding the import lock");
4209 return NULL;
4210 }
4211 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004212}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004213#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004214
Neal Norwitzb59798b2003-03-21 01:43:31 +00004215/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004216/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4217#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004218#define DEV_PTY_FILE "/dev/ptc"
4219#define HAVE_DEV_PTMX
4220#else
4221#define DEV_PTY_FILE "/dev/ptmx"
4222#endif
4223
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004224#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004225#ifdef HAVE_PTY_H
4226#include <pty.h>
4227#else
4228#ifdef HAVE_LIBUTIL_H
4229#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004230#else
4231#ifdef HAVE_UTIL_H
4232#include <util.h>
4233#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004234#endif /* HAVE_LIBUTIL_H */
4235#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004236#ifdef HAVE_STROPTS_H
4237#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004238#endif
4239#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004240
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004241#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004242PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004243"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004244Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004245
4246static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004247posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004248{
Victor Stinner8c62be82010-05-06 00:08:46 +00004249 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004250#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004251 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004252#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004253#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004254 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004255#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004256 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004257#endif
4258#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004259
Thomas Wouters70c21a12000-07-14 14:28:33 +00004260#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004261 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4262 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004263#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004264 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4265 if (slave_name == NULL)
4266 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004267
Victor Stinner8c62be82010-05-06 00:08:46 +00004268 slave_fd = open(slave_name, O_RDWR);
4269 if (slave_fd < 0)
4270 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004271#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004272 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4273 if (master_fd < 0)
4274 return posix_error();
4275 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4276 /* change permission of slave */
4277 if (grantpt(master_fd) < 0) {
4278 PyOS_setsig(SIGCHLD, sig_saved);
4279 return posix_error();
4280 }
4281 /* unlock slave */
4282 if (unlockpt(master_fd) < 0) {
4283 PyOS_setsig(SIGCHLD, sig_saved);
4284 return posix_error();
4285 }
4286 PyOS_setsig(SIGCHLD, sig_saved);
4287 slave_name = ptsname(master_fd); /* get name of slave */
4288 if (slave_name == NULL)
4289 return posix_error();
4290 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4291 if (slave_fd < 0)
4292 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004293#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004294 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4295 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004296#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004297 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004298#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004299#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004300#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004301
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004303
Fred Drake8cef4cf2000-06-28 16:40:38 +00004304}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004305#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004306
4307#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004308PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004309"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004310Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4311Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004312To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004313
4314static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004315posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004316{
Victor Stinner8c62be82010-05-06 00:08:46 +00004317 int master_fd = -1, result = 0;
4318 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004319
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 _PyImport_AcquireLock();
4321 pid = forkpty(&master_fd, NULL, NULL, NULL);
4322 if (pid == 0) {
4323 /* child: this clobbers and resets the import lock. */
4324 PyOS_AfterFork();
4325 } else {
4326 /* parent: release the import lock. */
4327 result = _PyImport_ReleaseLock();
4328 }
4329 if (pid == -1)
4330 return posix_error();
4331 if (result < 0) {
4332 /* Don't clobber the OSError if the fork failed. */
4333 PyErr_SetString(PyExc_RuntimeError,
4334 "not holding the import lock");
4335 return NULL;
4336 }
4337 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004338}
4339#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004340
Guido van Rossumad0ee831995-03-01 10:34:45 +00004341#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004342PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004343"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004344Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004345
Barry Warsaw53699e91996-12-10 23:23:01 +00004346static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004347posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004348{
Victor Stinner8c62be82010-05-06 00:08:46 +00004349 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004350}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004351#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004352
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004353
Guido van Rossumad0ee831995-03-01 10:34:45 +00004354#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004355PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004356"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004357Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004358
Barry Warsaw53699e91996-12-10 23:23:01 +00004359static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004360posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004361{
Victor Stinner8c62be82010-05-06 00:08:46 +00004362 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004363}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004364#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004365
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004366
Guido van Rossumad0ee831995-03-01 10:34:45 +00004367#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004368PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004369"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004370Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004371
Barry Warsaw53699e91996-12-10 23:23:01 +00004372static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004373posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004374{
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004376}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004377#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004378
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004380PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004381"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004382Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004383
Barry Warsaw53699e91996-12-10 23:23:01 +00004384static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004385posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004386{
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004388}
4389
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004390
Fred Drakec9680921999-12-13 16:37:25 +00004391#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004392PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004393"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004394Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004395
4396static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004397posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004398{
4399 PyObject *result = NULL;
4400
Fred Drakec9680921999-12-13 16:37:25 +00004401#ifdef NGROUPS_MAX
4402#define MAX_GROUPS NGROUPS_MAX
4403#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004404 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004405#define MAX_GROUPS 64
4406#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004407 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004408
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004409 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004410 * This is a helper variable to store the intermediate result when
4411 * that happens.
4412 *
4413 * To keep the code readable the OSX behaviour is unconditional,
4414 * according to the POSIX spec this should be safe on all unix-y
4415 * systems.
4416 */
4417 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004418 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004419
Victor Stinner8c62be82010-05-06 00:08:46 +00004420 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004421 if (n < 0) {
4422 if (errno == EINVAL) {
4423 n = getgroups(0, NULL);
4424 if (n == -1) {
4425 return posix_error();
4426 }
4427 if (n == 0) {
4428 /* Avoid malloc(0) */
4429 alt_grouplist = grouplist;
4430 } else {
4431 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4432 if (alt_grouplist == NULL) {
4433 errno = EINVAL;
4434 return posix_error();
4435 }
4436 n = getgroups(n, alt_grouplist);
4437 if (n == -1) {
4438 PyMem_Free(alt_grouplist);
4439 return posix_error();
4440 }
4441 }
4442 } else {
4443 return posix_error();
4444 }
4445 }
4446 result = PyList_New(n);
4447 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004448 int i;
4449 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004450 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004451 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004452 Py_DECREF(result);
4453 result = NULL;
4454 break;
Fred Drakec9680921999-12-13 16:37:25 +00004455 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004456 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004457 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004458 }
4459
4460 if (alt_grouplist != grouplist) {
4461 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004462 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004463
Fred Drakec9680921999-12-13 16:37:25 +00004464 return result;
4465}
4466#endif
4467
Antoine Pitroub7572f02009-12-02 20:46:48 +00004468#ifdef HAVE_INITGROUPS
4469PyDoc_STRVAR(posix_initgroups__doc__,
4470"initgroups(username, gid) -> None\n\n\
4471Call the system initgroups() to initialize the group access list with all of\n\
4472the groups of which the specified username is a member, plus the specified\n\
4473group id.");
4474
4475static PyObject *
4476posix_initgroups(PyObject *self, PyObject *args)
4477{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004478 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004479 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004480 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004481 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004482
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004483 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4484 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004485 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004486 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004487
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004488 res = initgroups(username, (gid_t) gid);
4489 Py_DECREF(oname);
4490 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004491 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004492
Victor Stinner8c62be82010-05-06 00:08:46 +00004493 Py_INCREF(Py_None);
4494 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004495}
4496#endif
4497
Martin v. Löwis606edc12002-06-13 21:09:11 +00004498#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004499PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004500"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004501Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004502
4503static PyObject *
4504posix_getpgid(PyObject *self, PyObject *args)
4505{
Victor Stinner8c62be82010-05-06 00:08:46 +00004506 pid_t pid, pgid;
4507 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4508 return NULL;
4509 pgid = getpgid(pid);
4510 if (pgid < 0)
4511 return posix_error();
4512 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004513}
4514#endif /* HAVE_GETPGID */
4515
4516
Guido van Rossumb6775db1994-08-01 11:34:53 +00004517#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004518PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004519"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004520Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004521
Barry Warsaw53699e91996-12-10 23:23:01 +00004522static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004523posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004524{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004525#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004526 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004527#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004528 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004529#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004530}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004531#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004533
Guido van Rossumb6775db1994-08-01 11:34:53 +00004534#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004535PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004536"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004537Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004538
Barry Warsaw53699e91996-12-10 23:23:01 +00004539static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004540posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004541{
Guido van Rossum64933891994-10-20 21:56:42 +00004542#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004543 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004544#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004545 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004546#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004547 return posix_error();
4548 Py_INCREF(Py_None);
4549 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004550}
4551
Guido van Rossumb6775db1994-08-01 11:34:53 +00004552#endif /* HAVE_SETPGRP */
4553
Guido van Rossumad0ee831995-03-01 10:34:45 +00004554#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004555
4556#ifdef MS_WINDOWS
4557#include <tlhelp32.h>
4558
4559static PyObject*
4560win32_getppid()
4561{
4562 HANDLE snapshot;
4563 pid_t mypid;
4564 PyObject* result = NULL;
4565 BOOL have_record;
4566 PROCESSENTRY32 pe;
4567
4568 mypid = getpid(); /* This function never fails */
4569
4570 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4571 if (snapshot == INVALID_HANDLE_VALUE)
4572 return PyErr_SetFromWindowsErr(GetLastError());
4573
4574 pe.dwSize = sizeof(pe);
4575 have_record = Process32First(snapshot, &pe);
4576 while (have_record) {
4577 if (mypid == (pid_t)pe.th32ProcessID) {
4578 /* We could cache the ulong value in a static variable. */
4579 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4580 break;
4581 }
4582
4583 have_record = Process32Next(snapshot, &pe);
4584 }
4585
4586 /* If our loop exits and our pid was not found (result will be NULL)
4587 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4588 * error anyway, so let's raise it. */
4589 if (!result)
4590 result = PyErr_SetFromWindowsErr(GetLastError());
4591
4592 CloseHandle(snapshot);
4593
4594 return result;
4595}
4596#endif /*MS_WINDOWS*/
4597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004598PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004599"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004600Return the parent's process id. If the parent process has already exited,\n\
4601Windows machines will still return its id; others systems will return the id\n\
4602of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004603
Barry Warsaw53699e91996-12-10 23:23:01 +00004604static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004605posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004606{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004607#ifdef MS_WINDOWS
4608 return win32_getppid();
4609#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004610 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004611#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004612}
4613#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004614
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004615
Fred Drake12c6e2d1999-12-14 21:25:03 +00004616#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004617PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004618"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004619Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004620
4621static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004622posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004623{
Victor Stinner8c62be82010-05-06 00:08:46 +00004624 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004625#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004626 wchar_t user_name[UNLEN + 1];
4627 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4628
4629 if (GetUserNameW(user_name, &num_chars)) {
4630 /* num_chars is the number of unicode chars plus null terminator */
4631 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004632 }
4633 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004634 result = PyErr_SetFromWindowsErr(GetLastError());
4635#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004636 char *name;
4637 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004638
Victor Stinner8c62be82010-05-06 00:08:46 +00004639 errno = 0;
4640 name = getlogin();
4641 if (name == NULL) {
4642 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004643 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004644 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004645 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004646 }
4647 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004648 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004649 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004650#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004651 return result;
4652}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004653#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004654
Guido van Rossumad0ee831995-03-01 10:34:45 +00004655#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004656PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004657"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004658Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004659
Barry Warsaw53699e91996-12-10 23:23:01 +00004660static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004661posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004662{
Victor Stinner8c62be82010-05-06 00:08:46 +00004663 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004664}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004665#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004666
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004667
Guido van Rossumad0ee831995-03-01 10:34:45 +00004668#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004669PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004670"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004671Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004672
Barry Warsaw53699e91996-12-10 23:23:01 +00004673static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004674posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004675{
Victor Stinner8c62be82010-05-06 00:08:46 +00004676 pid_t pid;
4677 int sig;
4678 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4679 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004680#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004681 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4682 APIRET rc;
4683 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004684 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004685
4686 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4687 APIRET rc;
4688 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004689 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004690
4691 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004692 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004693#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004694 if (kill(pid, sig) == -1)
4695 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004696#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004697 Py_INCREF(Py_None);
4698 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004699}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004700#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004701
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004702#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004703PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004704"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004705Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004706
4707static PyObject *
4708posix_killpg(PyObject *self, PyObject *args)
4709{
Victor Stinner8c62be82010-05-06 00:08:46 +00004710 int sig;
4711 pid_t pgid;
4712 /* XXX some man pages make the `pgid` parameter an int, others
4713 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4714 take the same type. Moreover, pid_t is always at least as wide as
4715 int (else compilation of this module fails), which is safe. */
4716 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4717 return NULL;
4718 if (killpg(pgid, sig) == -1)
4719 return posix_error();
4720 Py_INCREF(Py_None);
4721 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004722}
4723#endif
4724
Brian Curtineb24d742010-04-12 17:16:38 +00004725#ifdef MS_WINDOWS
4726PyDoc_STRVAR(win32_kill__doc__,
4727"kill(pid, sig)\n\n\
4728Kill a process with a signal.");
4729
4730static PyObject *
4731win32_kill(PyObject *self, PyObject *args)
4732{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004733 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004734 DWORD pid, sig, err;
4735 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004736
Victor Stinner8c62be82010-05-06 00:08:46 +00004737 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4738 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004739
Victor Stinner8c62be82010-05-06 00:08:46 +00004740 /* Console processes which share a common console can be sent CTRL+C or
4741 CTRL+BREAK events, provided they handle said events. */
4742 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4743 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4744 err = GetLastError();
4745 PyErr_SetFromWindowsErr(err);
4746 }
4747 else
4748 Py_RETURN_NONE;
4749 }
Brian Curtineb24d742010-04-12 17:16:38 +00004750
Victor Stinner8c62be82010-05-06 00:08:46 +00004751 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4752 attempt to open and terminate the process. */
4753 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4754 if (handle == NULL) {
4755 err = GetLastError();
4756 return PyErr_SetFromWindowsErr(err);
4757 }
Brian Curtineb24d742010-04-12 17:16:38 +00004758
Victor Stinner8c62be82010-05-06 00:08:46 +00004759 if (TerminateProcess(handle, sig) == 0) {
4760 err = GetLastError();
4761 result = PyErr_SetFromWindowsErr(err);
4762 } else {
4763 Py_INCREF(Py_None);
4764 result = Py_None;
4765 }
Brian Curtineb24d742010-04-12 17:16:38 +00004766
Victor Stinner8c62be82010-05-06 00:08:46 +00004767 CloseHandle(handle);
4768 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004769}
4770#endif /* MS_WINDOWS */
4771
Guido van Rossumc0125471996-06-28 18:55:32 +00004772#ifdef HAVE_PLOCK
4773
4774#ifdef HAVE_SYS_LOCK_H
4775#include <sys/lock.h>
4776#endif
4777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004778PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004779"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004780Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004781
Barry Warsaw53699e91996-12-10 23:23:01 +00004782static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004783posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004784{
Victor Stinner8c62be82010-05-06 00:08:46 +00004785 int op;
4786 if (!PyArg_ParseTuple(args, "i:plock", &op))
4787 return NULL;
4788 if (plock(op) == -1)
4789 return posix_error();
4790 Py_INCREF(Py_None);
4791 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004792}
4793#endif
4794
Guido van Rossumb6775db1994-08-01 11:34:53 +00004795#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004796PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004797"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004798Set the current process's user id.");
4799
Barry Warsaw53699e91996-12-10 23:23:01 +00004800static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004801posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004802{
Victor Stinner8c62be82010-05-06 00:08:46 +00004803 long uid_arg;
4804 uid_t uid;
4805 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4806 return NULL;
4807 uid = uid_arg;
4808 if (uid != uid_arg) {
4809 PyErr_SetString(PyExc_OverflowError, "user id too big");
4810 return NULL;
4811 }
4812 if (setuid(uid) < 0)
4813 return posix_error();
4814 Py_INCREF(Py_None);
4815 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004816}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004817#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004818
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004819
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004820#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004821PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004822"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004823Set the current process's effective user id.");
4824
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004825static PyObject *
4826posix_seteuid (PyObject *self, PyObject *args)
4827{
Victor Stinner8c62be82010-05-06 00:08:46 +00004828 long euid_arg;
4829 uid_t euid;
4830 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4831 return NULL;
4832 euid = euid_arg;
4833 if (euid != euid_arg) {
4834 PyErr_SetString(PyExc_OverflowError, "user id too big");
4835 return NULL;
4836 }
4837 if (seteuid(euid) < 0) {
4838 return posix_error();
4839 } else {
4840 Py_INCREF(Py_None);
4841 return Py_None;
4842 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004843}
4844#endif /* HAVE_SETEUID */
4845
4846#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004847PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004848"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004849Set the current process's effective group id.");
4850
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004851static PyObject *
4852posix_setegid (PyObject *self, PyObject *args)
4853{
Victor Stinner8c62be82010-05-06 00:08:46 +00004854 long egid_arg;
4855 gid_t egid;
4856 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4857 return NULL;
4858 egid = egid_arg;
4859 if (egid != egid_arg) {
4860 PyErr_SetString(PyExc_OverflowError, "group id too big");
4861 return NULL;
4862 }
4863 if (setegid(egid) < 0) {
4864 return posix_error();
4865 } else {
4866 Py_INCREF(Py_None);
4867 return Py_None;
4868 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004869}
4870#endif /* HAVE_SETEGID */
4871
4872#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004873PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004874"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004875Set the current process's real and effective user ids.");
4876
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004877static PyObject *
4878posix_setreuid (PyObject *self, PyObject *args)
4879{
Victor Stinner8c62be82010-05-06 00:08:46 +00004880 long ruid_arg, euid_arg;
4881 uid_t ruid, euid;
4882 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4883 return NULL;
4884 if (ruid_arg == -1)
4885 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4886 else
4887 ruid = ruid_arg; /* otherwise, assign from our long */
4888 if (euid_arg == -1)
4889 euid = (uid_t)-1;
4890 else
4891 euid = euid_arg;
4892 if ((euid_arg != -1 && euid != euid_arg) ||
4893 (ruid_arg != -1 && ruid != ruid_arg)) {
4894 PyErr_SetString(PyExc_OverflowError, "user id too big");
4895 return NULL;
4896 }
4897 if (setreuid(ruid, euid) < 0) {
4898 return posix_error();
4899 } else {
4900 Py_INCREF(Py_None);
4901 return Py_None;
4902 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004903}
4904#endif /* HAVE_SETREUID */
4905
4906#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004907PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004908"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004909Set the current process's real and effective group ids.");
4910
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004911static PyObject *
4912posix_setregid (PyObject *self, PyObject *args)
4913{
Victor Stinner8c62be82010-05-06 00:08:46 +00004914 long rgid_arg, egid_arg;
4915 gid_t rgid, egid;
4916 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4917 return NULL;
4918 if (rgid_arg == -1)
4919 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4920 else
4921 rgid = rgid_arg; /* otherwise, assign from our long */
4922 if (egid_arg == -1)
4923 egid = (gid_t)-1;
4924 else
4925 egid = egid_arg;
4926 if ((egid_arg != -1 && egid != egid_arg) ||
4927 (rgid_arg != -1 && rgid != rgid_arg)) {
4928 PyErr_SetString(PyExc_OverflowError, "group id too big");
4929 return NULL;
4930 }
4931 if (setregid(rgid, egid) < 0) {
4932 return posix_error();
4933 } else {
4934 Py_INCREF(Py_None);
4935 return Py_None;
4936 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004937}
4938#endif /* HAVE_SETREGID */
4939
Guido van Rossumb6775db1994-08-01 11:34:53 +00004940#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004941PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004942"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004943Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004944
Barry Warsaw53699e91996-12-10 23:23:01 +00004945static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004946posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004947{
Victor Stinner8c62be82010-05-06 00:08:46 +00004948 long gid_arg;
4949 gid_t gid;
4950 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4951 return NULL;
4952 gid = gid_arg;
4953 if (gid != gid_arg) {
4954 PyErr_SetString(PyExc_OverflowError, "group id too big");
4955 return NULL;
4956 }
4957 if (setgid(gid) < 0)
4958 return posix_error();
4959 Py_INCREF(Py_None);
4960 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004961}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004962#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004963
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004964#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004965PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004966"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004967Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004968
4969static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004970posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004971{
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 int i, len;
4973 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004974
Victor Stinner8c62be82010-05-06 00:08:46 +00004975 if (!PySequence_Check(groups)) {
4976 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4977 return NULL;
4978 }
4979 len = PySequence_Size(groups);
4980 if (len > MAX_GROUPS) {
4981 PyErr_SetString(PyExc_ValueError, "too many groups");
4982 return NULL;
4983 }
4984 for(i = 0; i < len; i++) {
4985 PyObject *elem;
4986 elem = PySequence_GetItem(groups, i);
4987 if (!elem)
4988 return NULL;
4989 if (!PyLong_Check(elem)) {
4990 PyErr_SetString(PyExc_TypeError,
4991 "groups must be integers");
4992 Py_DECREF(elem);
4993 return NULL;
4994 } else {
4995 unsigned long x = PyLong_AsUnsignedLong(elem);
4996 if (PyErr_Occurred()) {
4997 PyErr_SetString(PyExc_TypeError,
4998 "group id too big");
4999 Py_DECREF(elem);
5000 return NULL;
5001 }
5002 grouplist[i] = x;
5003 /* read back the value to see if it fitted in gid_t */
5004 if (grouplist[i] != x) {
5005 PyErr_SetString(PyExc_TypeError,
5006 "group id too big");
5007 Py_DECREF(elem);
5008 return NULL;
5009 }
5010 }
5011 Py_DECREF(elem);
5012 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005013
Victor Stinner8c62be82010-05-06 00:08:46 +00005014 if (setgroups(len, grouplist) < 0)
5015 return posix_error();
5016 Py_INCREF(Py_None);
5017 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005018}
5019#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005020
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005021#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5022static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005023wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005024{
Victor Stinner8c62be82010-05-06 00:08:46 +00005025 PyObject *result;
5026 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005027
Victor Stinner8c62be82010-05-06 00:08:46 +00005028 if (pid == -1)
5029 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005030
Victor Stinner8c62be82010-05-06 00:08:46 +00005031 if (struct_rusage == NULL) {
5032 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5033 if (m == NULL)
5034 return NULL;
5035 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5036 Py_DECREF(m);
5037 if (struct_rusage == NULL)
5038 return NULL;
5039 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005040
Victor Stinner8c62be82010-05-06 00:08:46 +00005041 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5042 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5043 if (!result)
5044 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005045
5046#ifndef doubletime
5047#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5048#endif
5049
Victor Stinner8c62be82010-05-06 00:08:46 +00005050 PyStructSequence_SET_ITEM(result, 0,
5051 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5052 PyStructSequence_SET_ITEM(result, 1,
5053 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005054#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005055 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5056 SET_INT(result, 2, ru->ru_maxrss);
5057 SET_INT(result, 3, ru->ru_ixrss);
5058 SET_INT(result, 4, ru->ru_idrss);
5059 SET_INT(result, 5, ru->ru_isrss);
5060 SET_INT(result, 6, ru->ru_minflt);
5061 SET_INT(result, 7, ru->ru_majflt);
5062 SET_INT(result, 8, ru->ru_nswap);
5063 SET_INT(result, 9, ru->ru_inblock);
5064 SET_INT(result, 10, ru->ru_oublock);
5065 SET_INT(result, 11, ru->ru_msgsnd);
5066 SET_INT(result, 12, ru->ru_msgrcv);
5067 SET_INT(result, 13, ru->ru_nsignals);
5068 SET_INT(result, 14, ru->ru_nvcsw);
5069 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005070#undef SET_INT
5071
Victor Stinner8c62be82010-05-06 00:08:46 +00005072 if (PyErr_Occurred()) {
5073 Py_DECREF(result);
5074 return NULL;
5075 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005076
Victor Stinner8c62be82010-05-06 00:08:46 +00005077 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005078}
5079#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5080
5081#ifdef HAVE_WAIT3
5082PyDoc_STRVAR(posix_wait3__doc__,
5083"wait3(options) -> (pid, status, rusage)\n\n\
5084Wait for completion of a child process.");
5085
5086static PyObject *
5087posix_wait3(PyObject *self, PyObject *args)
5088{
Victor Stinner8c62be82010-05-06 00:08:46 +00005089 pid_t pid;
5090 int options;
5091 struct rusage ru;
5092 WAIT_TYPE status;
5093 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005094
Victor Stinner8c62be82010-05-06 00:08:46 +00005095 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5096 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005097
Victor Stinner8c62be82010-05-06 00:08:46 +00005098 Py_BEGIN_ALLOW_THREADS
5099 pid = wait3(&status, options, &ru);
5100 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005101
Victor Stinner8c62be82010-05-06 00:08:46 +00005102 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005103}
5104#endif /* HAVE_WAIT3 */
5105
5106#ifdef HAVE_WAIT4
5107PyDoc_STRVAR(posix_wait4__doc__,
5108"wait4(pid, options) -> (pid, status, rusage)\n\n\
5109Wait for completion of a given child process.");
5110
5111static PyObject *
5112posix_wait4(PyObject *self, PyObject *args)
5113{
Victor Stinner8c62be82010-05-06 00:08:46 +00005114 pid_t pid;
5115 int options;
5116 struct rusage ru;
5117 WAIT_TYPE status;
5118 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005119
Victor Stinner8c62be82010-05-06 00:08:46 +00005120 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5121 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005122
Victor Stinner8c62be82010-05-06 00:08:46 +00005123 Py_BEGIN_ALLOW_THREADS
5124 pid = wait4(pid, &status, options, &ru);
5125 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005126
Victor Stinner8c62be82010-05-06 00:08:46 +00005127 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005128}
5129#endif /* HAVE_WAIT4 */
5130
Guido van Rossumb6775db1994-08-01 11:34:53 +00005131#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005132PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005133"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005134Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005135
Barry Warsaw53699e91996-12-10 23:23:01 +00005136static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005137posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005138{
Victor Stinner8c62be82010-05-06 00:08:46 +00005139 pid_t pid;
5140 int options;
5141 WAIT_TYPE status;
5142 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005143
Victor Stinner8c62be82010-05-06 00:08:46 +00005144 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5145 return NULL;
5146 Py_BEGIN_ALLOW_THREADS
5147 pid = waitpid(pid, &status, options);
5148 Py_END_ALLOW_THREADS
5149 if (pid == -1)
5150 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005151
Victor Stinner8c62be82010-05-06 00:08:46 +00005152 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005153}
5154
Tim Petersab034fa2002-02-01 11:27:43 +00005155#elif defined(HAVE_CWAIT)
5156
5157/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005158PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005159"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005160"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005161
5162static PyObject *
5163posix_waitpid(PyObject *self, PyObject *args)
5164{
Victor Stinner8c62be82010-05-06 00:08:46 +00005165 Py_intptr_t pid;
5166 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005167
Victor Stinner8c62be82010-05-06 00:08:46 +00005168 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5169 return NULL;
5170 Py_BEGIN_ALLOW_THREADS
5171 pid = _cwait(&status, pid, options);
5172 Py_END_ALLOW_THREADS
5173 if (pid == -1)
5174 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005175
Victor Stinner8c62be82010-05-06 00:08:46 +00005176 /* shift the status left a byte so this is more like the POSIX waitpid */
5177 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005178}
5179#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005180
Guido van Rossumad0ee831995-03-01 10:34:45 +00005181#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005182PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005183"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005184Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005185
Barry Warsaw53699e91996-12-10 23:23:01 +00005186static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005187posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005188{
Victor Stinner8c62be82010-05-06 00:08:46 +00005189 pid_t pid;
5190 WAIT_TYPE status;
5191 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005192
Victor Stinner8c62be82010-05-06 00:08:46 +00005193 Py_BEGIN_ALLOW_THREADS
5194 pid = wait(&status);
5195 Py_END_ALLOW_THREADS
5196 if (pid == -1)
5197 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005198
Victor Stinner8c62be82010-05-06 00:08:46 +00005199 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005200}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005201#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005202
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005204PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005205"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005206Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005207
Barry Warsaw53699e91996-12-10 23:23:01 +00005208static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005209posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005210{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005211#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005212 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005213#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005214#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005215 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5216 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005217#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005218 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005219#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005220#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005221}
5222
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005223
Guido van Rossumb6775db1994-08-01 11:34:53 +00005224#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005225PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005226"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005227Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005228
Barry Warsaw53699e91996-12-10 23:23:01 +00005229static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005230posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005231{
Victor Stinner8c62be82010-05-06 00:08:46 +00005232 PyObject* v;
5233 char buf[MAXPATHLEN];
5234 PyObject *opath;
5235 char *path;
5236 int n;
5237 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005238
Victor Stinner8c62be82010-05-06 00:08:46 +00005239 if (!PyArg_ParseTuple(args, "O&:readlink",
5240 PyUnicode_FSConverter, &opath))
5241 return NULL;
5242 path = PyBytes_AsString(opath);
5243 v = PySequence_GetItem(args, 0);
5244 if (v == NULL) {
5245 Py_DECREF(opath);
5246 return NULL;
5247 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005248
Victor Stinner8c62be82010-05-06 00:08:46 +00005249 if (PyUnicode_Check(v)) {
5250 arg_is_unicode = 1;
5251 }
5252 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005253
Victor Stinner8c62be82010-05-06 00:08:46 +00005254 Py_BEGIN_ALLOW_THREADS
5255 n = readlink(path, buf, (int) sizeof buf);
5256 Py_END_ALLOW_THREADS
5257 if (n < 0)
5258 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005259
Victor Stinner8c62be82010-05-06 00:08:46 +00005260 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005261 if (arg_is_unicode)
5262 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5263 else
5264 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005265}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005266#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005268
Brian Curtin52173d42010-12-02 18:29:18 +00005269#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005270PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005271"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005272Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005273
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005274static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005275posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005276{
Victor Stinner8c62be82010-05-06 00:08:46 +00005277 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005278}
5279#endif /* HAVE_SYMLINK */
5280
Brian Curtind40e6f72010-07-08 21:39:08 +00005281#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5282
5283PyDoc_STRVAR(win_readlink__doc__,
5284"readlink(path) -> path\n\n\
5285Return a string representing the path to which the symbolic link points.");
5286
Brian Curtind40e6f72010-07-08 21:39:08 +00005287/* Windows readlink implementation */
5288static PyObject *
5289win_readlink(PyObject *self, PyObject *args)
5290{
5291 wchar_t *path;
5292 DWORD n_bytes_returned;
5293 DWORD io_result;
5294 PyObject *result;
5295 HANDLE reparse_point_handle;
5296
5297 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5298 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5299 wchar_t *print_name;
5300
5301 if (!PyArg_ParseTuple(args,
5302 "u:readlink",
5303 &path))
5304 return NULL;
5305
5306 /* First get a handle to the reparse point */
5307 Py_BEGIN_ALLOW_THREADS
5308 reparse_point_handle = CreateFileW(
5309 path,
5310 0,
5311 0,
5312 0,
5313 OPEN_EXISTING,
5314 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5315 0);
5316 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005317
Brian Curtind40e6f72010-07-08 21:39:08 +00005318 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5319 {
5320 return win32_error_unicode("readlink", path);
5321 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005322
Brian Curtind40e6f72010-07-08 21:39:08 +00005323 Py_BEGIN_ALLOW_THREADS
5324 /* New call DeviceIoControl to read the reparse point */
5325 io_result = DeviceIoControl(
5326 reparse_point_handle,
5327 FSCTL_GET_REPARSE_POINT,
5328 0, 0, /* in buffer */
5329 target_buffer, sizeof(target_buffer),
5330 &n_bytes_returned,
5331 0 /* we're not using OVERLAPPED_IO */
5332 );
5333 CloseHandle(reparse_point_handle);
5334 Py_END_ALLOW_THREADS
5335
5336 if (io_result==0)
5337 {
5338 return win32_error_unicode("readlink", path);
5339 }
5340
5341 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5342 {
5343 PyErr_SetString(PyExc_ValueError,
5344 "not a symbolic link");
5345 return NULL;
5346 }
Brian Curtin74e45612010-07-09 15:58:59 +00005347 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5348 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5349
5350 result = PyUnicode_FromWideChar(print_name,
5351 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005352 return result;
5353}
5354
5355#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5356
Brian Curtin52173d42010-12-02 18:29:18 +00005357#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005358
5359/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5360static int has_CreateSymbolicLinkW = 0;
5361static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5362static int
5363check_CreateSymbolicLinkW()
5364{
5365 HINSTANCE hKernel32;
5366 /* only recheck */
5367 if (has_CreateSymbolicLinkW)
5368 return has_CreateSymbolicLinkW;
5369 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005370 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5371 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005372 if (Py_CreateSymbolicLinkW)
5373 has_CreateSymbolicLinkW = 1;
5374 return has_CreateSymbolicLinkW;
5375}
5376
5377PyDoc_STRVAR(win_symlink__doc__,
5378"symlink(src, dst, target_is_directory=False)\n\n\
5379Create a symbolic link pointing to src named dst.\n\
5380target_is_directory is required if the target is to be interpreted as\n\
5381a directory.\n\
5382This function requires Windows 6.0 or greater, and raises a\n\
5383NotImplementedError otherwise.");
5384
5385static PyObject *
5386win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5387{
5388 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5389 PyObject *src, *dest;
5390 int target_is_directory = 0;
5391 DWORD res;
5392 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005393
Brian Curtind40e6f72010-07-08 21:39:08 +00005394 if (!check_CreateSymbolicLinkW())
5395 {
5396 /* raise NotImplementedError */
5397 return PyErr_Format(PyExc_NotImplementedError,
5398 "CreateSymbolicLinkW not found");
5399 }
5400 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5401 kwlist, &src, &dest, &target_is_directory))
5402 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005403
5404 if (win32_can_symlink == 0)
5405 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5406
Brian Curtind40e6f72010-07-08 21:39:08 +00005407 if (!convert_to_unicode(&src)) { return NULL; }
5408 if (!convert_to_unicode(&dest)) {
5409 Py_DECREF(src);
5410 return NULL;
5411 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005412
Brian Curtind40e6f72010-07-08 21:39:08 +00005413 /* if src is a directory, ensure target_is_directory==1 */
5414 if(
5415 GetFileAttributesExW(
5416 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5417 ))
5418 {
5419 target_is_directory = target_is_directory ||
5420 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5421 }
5422
5423 Py_BEGIN_ALLOW_THREADS
5424 res = Py_CreateSymbolicLinkW(
5425 PyUnicode_AsUnicode(dest),
5426 PyUnicode_AsUnicode(src),
5427 target_is_directory);
5428 Py_END_ALLOW_THREADS
5429 Py_DECREF(src);
5430 Py_DECREF(dest);
5431 if (!res)
5432 {
5433 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5434 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005435
Brian Curtind40e6f72010-07-08 21:39:08 +00005436 Py_INCREF(Py_None);
5437 return Py_None;
5438}
Brian Curtin52173d42010-12-02 18:29:18 +00005439#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005440
5441#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005442#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5443static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005444system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005445{
5446 ULONG value = 0;
5447
5448 Py_BEGIN_ALLOW_THREADS
5449 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5450 Py_END_ALLOW_THREADS
5451
5452 return value;
5453}
5454
5455static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005456posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005457{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005458 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005459 return Py_BuildValue("ddddd",
5460 (double)0 /* t.tms_utime / HZ */,
5461 (double)0 /* t.tms_stime / HZ */,
5462 (double)0 /* t.tms_cutime / HZ */,
5463 (double)0 /* t.tms_cstime / HZ */,
5464 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005465}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005466#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005467#define NEED_TICKS_PER_SECOND
5468static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005469static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005470posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005471{
Victor Stinner8c62be82010-05-06 00:08:46 +00005472 struct tms t;
5473 clock_t c;
5474 errno = 0;
5475 c = times(&t);
5476 if (c == (clock_t) -1)
5477 return posix_error();
5478 return Py_BuildValue("ddddd",
5479 (double)t.tms_utime / ticks_per_second,
5480 (double)t.tms_stime / ticks_per_second,
5481 (double)t.tms_cutime / ticks_per_second,
5482 (double)t.tms_cstime / ticks_per_second,
5483 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005484}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005485#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005486#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005487
5488
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005489#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005490#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005491static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005492posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005493{
Victor Stinner8c62be82010-05-06 00:08:46 +00005494 FILETIME create, exit, kernel, user;
5495 HANDLE hProc;
5496 hProc = GetCurrentProcess();
5497 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5498 /* The fields of a FILETIME structure are the hi and lo part
5499 of a 64-bit value expressed in 100 nanosecond units.
5500 1e7 is one second in such units; 1e-7 the inverse.
5501 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5502 */
5503 return Py_BuildValue(
5504 "ddddd",
5505 (double)(user.dwHighDateTime*429.4967296 +
5506 user.dwLowDateTime*1e-7),
5507 (double)(kernel.dwHighDateTime*429.4967296 +
5508 kernel.dwLowDateTime*1e-7),
5509 (double)0,
5510 (double)0,
5511 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005512}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005513#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005514
5515#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005516PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005517"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005518Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005519#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005520
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005521
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005522#ifdef HAVE_GETSID
5523PyDoc_STRVAR(posix_getsid__doc__,
5524"getsid(pid) -> sid\n\n\
5525Call the system call getsid().");
5526
5527static PyObject *
5528posix_getsid(PyObject *self, PyObject *args)
5529{
Victor Stinner8c62be82010-05-06 00:08:46 +00005530 pid_t pid;
5531 int sid;
5532 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5533 return NULL;
5534 sid = getsid(pid);
5535 if (sid < 0)
5536 return posix_error();
5537 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005538}
5539#endif /* HAVE_GETSID */
5540
5541
Guido van Rossumb6775db1994-08-01 11:34:53 +00005542#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005543PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005544"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005545Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005546
Barry Warsaw53699e91996-12-10 23:23:01 +00005547static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005548posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005549{
Victor Stinner8c62be82010-05-06 00:08:46 +00005550 if (setsid() < 0)
5551 return posix_error();
5552 Py_INCREF(Py_None);
5553 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005554}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005555#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005556
Guido van Rossumb6775db1994-08-01 11:34:53 +00005557#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005559"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005561
Barry Warsaw53699e91996-12-10 23:23:01 +00005562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005563posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005564{
Victor Stinner8c62be82010-05-06 00:08:46 +00005565 pid_t pid;
5566 int pgrp;
5567 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5568 return NULL;
5569 if (setpgid(pid, pgrp) < 0)
5570 return posix_error();
5571 Py_INCREF(Py_None);
5572 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005573}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005574#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005575
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005576
Guido van Rossumb6775db1994-08-01 11:34:53 +00005577#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005579"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005581
Barry Warsaw53699e91996-12-10 23:23:01 +00005582static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005583posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005584{
Victor Stinner8c62be82010-05-06 00:08:46 +00005585 int fd;
5586 pid_t pgid;
5587 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5588 return NULL;
5589 pgid = tcgetpgrp(fd);
5590 if (pgid < 0)
5591 return posix_error();
5592 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005593}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005594#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005595
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005596
Guido van Rossumb6775db1994-08-01 11:34:53 +00005597#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005599"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005600Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005601
Barry Warsaw53699e91996-12-10 23:23:01 +00005602static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005603posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005604{
Victor Stinner8c62be82010-05-06 00:08:46 +00005605 int fd;
5606 pid_t pgid;
5607 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5608 return NULL;
5609 if (tcsetpgrp(fd, pgid) < 0)
5610 return posix_error();
5611 Py_INCREF(Py_None);
5612 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005613}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005614#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005615
Guido van Rossum687dd131993-05-17 08:34:16 +00005616/* Functions acting on file descriptors */
5617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005618PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005619"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005620Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005621
Barry Warsaw53699e91996-12-10 23:23:01 +00005622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005623posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005624{
Victor Stinner8c62be82010-05-06 00:08:46 +00005625 PyObject *ofile;
5626 char *file;
5627 int flag;
5628 int mode = 0777;
5629 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005630
5631#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 PyUnicodeObject *po;
5633 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5634 Py_BEGIN_ALLOW_THREADS
5635 /* PyUnicode_AS_UNICODE OK without thread
5636 lock as it is a simple dereference. */
5637 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5638 Py_END_ALLOW_THREADS
5639 if (fd < 0)
5640 return posix_error();
5641 return PyLong_FromLong((long)fd);
5642 }
5643 /* Drop the argument parsing error as narrow strings
5644 are also valid. */
5645 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005646#endif
5647
Victor Stinner8c62be82010-05-06 00:08:46 +00005648 if (!PyArg_ParseTuple(args, "O&i|i",
5649 PyUnicode_FSConverter, &ofile,
5650 &flag, &mode))
5651 return NULL;
5652 file = PyBytes_AsString(ofile);
5653 Py_BEGIN_ALLOW_THREADS
5654 fd = open(file, flag, mode);
5655 Py_END_ALLOW_THREADS
5656 if (fd < 0)
5657 return posix_error_with_allocated_filename(ofile);
5658 Py_DECREF(ofile);
5659 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005660}
5661
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005663PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005664"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005665Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005666
Barry Warsaw53699e91996-12-10 23:23:01 +00005667static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005668posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005669{
Victor Stinner8c62be82010-05-06 00:08:46 +00005670 int fd, res;
5671 if (!PyArg_ParseTuple(args, "i:close", &fd))
5672 return NULL;
5673 if (!_PyVerify_fd(fd))
5674 return posix_error();
5675 Py_BEGIN_ALLOW_THREADS
5676 res = close(fd);
5677 Py_END_ALLOW_THREADS
5678 if (res < 0)
5679 return posix_error();
5680 Py_INCREF(Py_None);
5681 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005682}
5683
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005684
Victor Stinner8c62be82010-05-06 00:08:46 +00005685PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005686"closerange(fd_low, fd_high)\n\n\
5687Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5688
5689static PyObject *
5690posix_closerange(PyObject *self, PyObject *args)
5691{
Victor Stinner8c62be82010-05-06 00:08:46 +00005692 int fd_from, fd_to, i;
5693 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5694 return NULL;
5695 Py_BEGIN_ALLOW_THREADS
5696 for (i = fd_from; i < fd_to; i++)
5697 if (_PyVerify_fd(i))
5698 close(i);
5699 Py_END_ALLOW_THREADS
5700 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005701}
5702
5703
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005704PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005705"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005706Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005707
Barry Warsaw53699e91996-12-10 23:23:01 +00005708static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005709posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005710{
Victor Stinner8c62be82010-05-06 00:08:46 +00005711 int fd;
5712 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5713 return NULL;
5714 if (!_PyVerify_fd(fd))
5715 return posix_error();
5716 Py_BEGIN_ALLOW_THREADS
5717 fd = dup(fd);
5718 Py_END_ALLOW_THREADS
5719 if (fd < 0)
5720 return posix_error();
5721 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005722}
5723
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005725PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005726"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005727Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005728
Barry Warsaw53699e91996-12-10 23:23:01 +00005729static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005730posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005731{
Victor Stinner8c62be82010-05-06 00:08:46 +00005732 int fd, fd2, res;
5733 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5734 return NULL;
5735 if (!_PyVerify_fd_dup2(fd, fd2))
5736 return posix_error();
5737 Py_BEGIN_ALLOW_THREADS
5738 res = dup2(fd, fd2);
5739 Py_END_ALLOW_THREADS
5740 if (res < 0)
5741 return posix_error();
5742 Py_INCREF(Py_None);
5743 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005744}
5745
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005747PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005748"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005749Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005750
Barry Warsaw53699e91996-12-10 23:23:01 +00005751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005752posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005753{
Victor Stinner8c62be82010-05-06 00:08:46 +00005754 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005755#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005756 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005757#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005758 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005759#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005760 PyObject *posobj;
5761 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5762 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005763#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005764 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5765 switch (how) {
5766 case 0: how = SEEK_SET; break;
5767 case 1: how = SEEK_CUR; break;
5768 case 2: how = SEEK_END; break;
5769 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005770#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005771
5772#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005773 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005774#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005775 pos = PyLong_Check(posobj) ?
5776 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005777#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005778 if (PyErr_Occurred())
5779 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005780
Victor Stinner8c62be82010-05-06 00:08:46 +00005781 if (!_PyVerify_fd(fd))
5782 return posix_error();
5783 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005784#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005786#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005788#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005789 Py_END_ALLOW_THREADS
5790 if (res < 0)
5791 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005792
5793#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005794 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005795#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005796 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005797#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005798}
5799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005802"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005803Read a file descriptor.");
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_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005807{
Victor Stinner8c62be82010-05-06 00:08:46 +00005808 int fd, size;
5809 Py_ssize_t n;
5810 PyObject *buffer;
5811 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5812 return NULL;
5813 if (size < 0) {
5814 errno = EINVAL;
5815 return posix_error();
5816 }
5817 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5818 if (buffer == NULL)
5819 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005820 if (!_PyVerify_fd(fd)) {
5821 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005822 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005823 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005824 Py_BEGIN_ALLOW_THREADS
5825 n = read(fd, PyBytes_AS_STRING(buffer), size);
5826 Py_END_ALLOW_THREADS
5827 if (n < 0) {
5828 Py_DECREF(buffer);
5829 return posix_error();
5830 }
5831 if (n != size)
5832 _PyBytes_Resize(&buffer, n);
5833 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005834}
5835
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005836
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005837PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005838"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005839Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005840
Barry Warsaw53699e91996-12-10 23:23:01 +00005841static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005842posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005843{
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 Py_buffer pbuf;
5845 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005846 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005847
Victor Stinner8c62be82010-05-06 00:08:46 +00005848 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5849 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005850 if (!_PyVerify_fd(fd)) {
5851 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005852 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005853 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005854 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005855 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005856#if defined(MS_WIN64) || defined(MS_WINDOWS)
5857 if (len > INT_MAX)
5858 len = INT_MAX;
5859 size = write(fd, pbuf.buf, (int)len);
5860#else
Victor Stinner72344792011-01-11 00:04:12 +00005861 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005862#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005863 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005864 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 if (size < 0)
5866 return posix_error();
5867 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005868}
5869
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005870#ifdef HAVE_SENDFILE
5871#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
5872static int
5873iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
5874{
5875 int i, j;
5876 *iov = PyMem_New(struct iovec, cnt);
5877 if (*iov == NULL) {
5878 PyErr_NoMemory();
5879 return 0;
5880 }
5881 *buf = PyMem_New(Py_buffer, cnt);
5882 if (*buf == NULL) {
5883 PyMem_Del(*iov);
5884 PyErr_NoMemory();
5885 return 0;
5886 }
5887
5888 for (i = 0; i < cnt; i++) {
5889 if (PyObject_GetBuffer(PySequence_GetItem(seq, i), &(*buf)[i],
5890 type) == -1) {
5891 PyMem_Del(*iov);
5892 for (j = 0; j < i; j++) {
5893 PyBuffer_Release(&(*buf)[j]);
5894 }
5895 PyMem_Del(*buf);
5896 return 0;
5897 }
5898 (*iov)[i].iov_base = (*buf)[i].buf;
5899 (*iov)[i].iov_len = (*buf)[i].len;
5900 }
5901 return 1;
5902}
5903
5904static void
5905iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
5906{
5907 int i;
5908 PyMem_Del(iov);
5909 for (i = 0; i < cnt; i++) {
5910 PyBuffer_Release(&buf[i]);
5911 }
5912 PyMem_Del(buf);
5913}
5914#endif
5915
5916PyDoc_STRVAR(posix_sendfile__doc__,
5917"sendfile(out, in, offset, nbytes) -> byteswritten\n\
5918sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
5919 -> byteswritten\n\
5920Copy nbytes bytes from file descriptor in to file descriptor out.");
5921
5922static PyObject *
5923posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
5924{
5925 int in, out;
5926 Py_ssize_t ret;
5927 off_t offset;
5928
5929#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
5930#ifndef __APPLE__
5931 Py_ssize_t len;
5932#endif
5933 PyObject *headers = NULL, *trailers = NULL;
5934 Py_buffer *hbuf, *tbuf;
5935 off_t sbytes;
5936 struct sf_hdtr sf;
5937 int flags = 0;
5938 sf.headers = NULL;
5939 sf.trailers = NULL;
5940 static char *keywords[] = {"out", "in",
5941 "offset", "count",
5942 "headers", "trailers", "flags", NULL};
5943
5944#ifdef __APPLE__
5945 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00005946 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005947#else
5948 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00005949 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005950#endif
5951 &headers, &trailers, &flags))
5952 return NULL;
5953 if (headers != NULL) {
5954 if (!PySequence_Check(headers)) {
5955 PyErr_SetString(PyExc_TypeError,
5956 "sendfile() headers must be a sequence or None");
5957 return NULL;
5958 } else {
5959 sf.hdr_cnt = PySequence_Size(headers);
5960 if (sf.hdr_cnt > 0 && !iov_setup(&(sf.headers), &hbuf,
5961 headers, sf.hdr_cnt, PyBUF_SIMPLE))
5962 return NULL;
5963 }
5964 }
5965 if (trailers != NULL) {
5966 if (!PySequence_Check(trailers)) {
5967 PyErr_SetString(PyExc_TypeError,
5968 "sendfile() trailers must be a sequence or None");
5969 return NULL;
5970 } else {
5971 sf.trl_cnt = PySequence_Size(trailers);
5972 if (sf.trl_cnt > 0 && !iov_setup(&(sf.trailers), &tbuf,
5973 trailers, sf.trl_cnt, PyBUF_SIMPLE))
5974 return NULL;
5975 }
5976 }
5977
5978 Py_BEGIN_ALLOW_THREADS
5979#ifdef __APPLE__
5980 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
5981#else
5982 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
5983#endif
5984 Py_END_ALLOW_THREADS
5985
5986 if (sf.headers != NULL)
5987 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
5988 if (sf.trailers != NULL)
5989 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
5990
5991 if (ret < 0) {
5992 if ((errno == EAGAIN) || (errno == EBUSY)) {
5993 if (sbytes != 0) {
5994 // some data has been sent
5995 goto done;
5996 }
5997 else {
5998 // no data has been sent; upper application is supposed
5999 // to retry on EAGAIN or EBUSY
6000 return posix_error();
6001 }
6002 }
6003 return posix_error();
6004 }
6005 goto done;
6006
6007done:
6008 #if !defined(HAVE_LARGEFILE_SUPPORT)
6009 return Py_BuildValue("l", sbytes);
6010 #else
6011 return Py_BuildValue("L", sbytes);
6012 #endif
6013
6014#else
6015 Py_ssize_t count;
6016 PyObject *offobj;
6017 static char *keywords[] = {"out", "in",
6018 "offset", "count", NULL};
6019 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
6020 keywords, &out, &in, &offobj, &count))
6021 return NULL;
6022#ifdef linux
6023 if (offobj == Py_None) {
6024 Py_BEGIN_ALLOW_THREADS
6025 ret = sendfile(out, in, NULL, count);
6026 Py_END_ALLOW_THREADS
6027 if (ret < 0)
6028 return posix_error();
6029 Py_INCREF(Py_None);
6030 return Py_BuildValue("nO", ret, Py_None);
6031 }
6032#endif
Georg Brandla391b112011-02-25 15:23:18 +00006033 _parse_off_t(offobj, &offset);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006034 Py_BEGIN_ALLOW_THREADS
6035 ret = sendfile(out, in, &offset, count);
6036 Py_END_ALLOW_THREADS
6037 if (ret < 0)
6038 return posix_error();
6039 return Py_BuildValue("n", ret);
6040#endif
6041}
6042#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006044PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006045"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006046Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006047
Barry Warsaw53699e91996-12-10 23:23:01 +00006048static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006049posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006050{
Victor Stinner8c62be82010-05-06 00:08:46 +00006051 int fd;
6052 STRUCT_STAT st;
6053 int res;
6054 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6055 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006056#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 /* on OpenVMS we must ensure that all bytes are written to the file */
6058 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006059#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006060 if (!_PyVerify_fd(fd))
6061 return posix_error();
6062 Py_BEGIN_ALLOW_THREADS
6063 res = FSTAT(fd, &st);
6064 Py_END_ALLOW_THREADS
6065 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006066#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006067 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006068#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006069 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006070#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006071 }
Tim Peters5aa91602002-01-30 05:46:57 +00006072
Victor Stinner8c62be82010-05-06 00:08:46 +00006073 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006074}
6075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006076PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006077"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006078Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006079connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006080
6081static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006082posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006083{
Victor Stinner8c62be82010-05-06 00:08:46 +00006084 int fd;
6085 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6086 return NULL;
6087 if (!_PyVerify_fd(fd))
6088 return PyBool_FromLong(0);
6089 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006090}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006091
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006092#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006093PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006094"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006095Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006096
Barry Warsaw53699e91996-12-10 23:23:01 +00006097static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006098posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006099{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006100#if defined(PYOS_OS2)
6101 HFILE read, write;
6102 APIRET rc;
6103
Victor Stinner8c62be82010-05-06 00:08:46 +00006104 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006105 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00006106 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006107 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006108 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006109
6110 return Py_BuildValue("(ii)", read, write);
6111#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006112#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006113 int fds[2];
6114 int res;
6115 Py_BEGIN_ALLOW_THREADS
6116 res = pipe(fds);
6117 Py_END_ALLOW_THREADS
6118 if (res != 0)
6119 return posix_error();
6120 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006121#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00006122 HANDLE read, write;
6123 int read_fd, write_fd;
6124 BOOL ok;
6125 Py_BEGIN_ALLOW_THREADS
6126 ok = CreatePipe(&read, &write, NULL, 0);
6127 Py_END_ALLOW_THREADS
6128 if (!ok)
6129 return win32_error("CreatePipe", NULL);
6130 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6131 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6132 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006133#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006134#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006135}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006136#endif /* HAVE_PIPE */
6137
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006138
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006139#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006140PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006141"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006142Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006143
Barry Warsaw53699e91996-12-10 23:23:01 +00006144static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006145posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006146{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006147 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 char *filename;
6149 int mode = 0666;
6150 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006151 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
6152 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00006153 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006154 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006155 Py_BEGIN_ALLOW_THREADS
6156 res = mkfifo(filename, mode);
6157 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006158 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006159 if (res < 0)
6160 return posix_error();
6161 Py_INCREF(Py_None);
6162 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006163}
6164#endif
6165
6166
Neal Norwitz11690112002-07-30 01:08:28 +00006167#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006168PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006169"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006170Create a filesystem node (file, device special file or named pipe)\n\
6171named filename. mode specifies both the permissions to use and the\n\
6172type of node to be created, being combined (bitwise OR) with one of\n\
6173S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006174device defines the newly created device special file (probably using\n\
6175os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006176
6177
6178static PyObject *
6179posix_mknod(PyObject *self, PyObject *args)
6180{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006181 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 char *filename;
6183 int mode = 0600;
6184 int device = 0;
6185 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006186 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
6187 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00006188 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006189 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006190 Py_BEGIN_ALLOW_THREADS
6191 res = mknod(filename, mode, device);
6192 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006193 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006194 if (res < 0)
6195 return posix_error();
6196 Py_INCREF(Py_None);
6197 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006198}
6199#endif
6200
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006201#ifdef HAVE_DEVICE_MACROS
6202PyDoc_STRVAR(posix_major__doc__,
6203"major(device) -> major number\n\
6204Extracts a device major number from a raw device number.");
6205
6206static PyObject *
6207posix_major(PyObject *self, PyObject *args)
6208{
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 int device;
6210 if (!PyArg_ParseTuple(args, "i:major", &device))
6211 return NULL;
6212 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006213}
6214
6215PyDoc_STRVAR(posix_minor__doc__,
6216"minor(device) -> minor number\n\
6217Extracts a device minor number from a raw device number.");
6218
6219static PyObject *
6220posix_minor(PyObject *self, PyObject *args)
6221{
Victor Stinner8c62be82010-05-06 00:08:46 +00006222 int device;
6223 if (!PyArg_ParseTuple(args, "i:minor", &device))
6224 return NULL;
6225 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006226}
6227
6228PyDoc_STRVAR(posix_makedev__doc__,
6229"makedev(major, minor) -> device number\n\
6230Composes a raw device number from the major and minor device numbers.");
6231
6232static PyObject *
6233posix_makedev(PyObject *self, PyObject *args)
6234{
Victor Stinner8c62be82010-05-06 00:08:46 +00006235 int major, minor;
6236 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6237 return NULL;
6238 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006239}
6240#endif /* device macros */
6241
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006242
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006243#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006244PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006245"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006246Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006247
Barry Warsaw53699e91996-12-10 23:23:01 +00006248static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006249posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006250{
Victor Stinner8c62be82010-05-06 00:08:46 +00006251 int fd;
6252 off_t length;
6253 int res;
6254 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006255
Victor Stinner8c62be82010-05-06 00:08:46 +00006256 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6257 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006258
6259#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006260 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006261#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006262 length = PyLong_Check(lenobj) ?
6263 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006264#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006265 if (PyErr_Occurred())
6266 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006267
Victor Stinner8c62be82010-05-06 00:08:46 +00006268 Py_BEGIN_ALLOW_THREADS
6269 res = ftruncate(fd, length);
6270 Py_END_ALLOW_THREADS
6271 if (res < 0)
6272 return posix_error();
6273 Py_INCREF(Py_None);
6274 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006275}
6276#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006277
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006278#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006279PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006280"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006281Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006282
Fred Drake762e2061999-08-26 17:23:54 +00006283/* Save putenv() parameters as values here, so we can collect them when they
6284 * get re-set with another call for the same key. */
6285static PyObject *posix_putenv_garbage;
6286
Tim Peters5aa91602002-01-30 05:46:57 +00006287static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006288posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006289{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006290#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006291 wchar_t *s1, *s2;
6292 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006293#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006294 PyObject *os1, *os2;
6295 char *s1, *s2;
6296 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006297#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006298 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006300
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006301#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006302 if (!PyArg_ParseTuple(args,
6303 "uu:putenv",
6304 &s1, &s2))
6305 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006306#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006307 if (!PyArg_ParseTuple(args,
6308 "O&O&:putenv",
6309 PyUnicode_FSConverter, &os1,
6310 PyUnicode_FSConverter, &os2))
6311 return NULL;
6312 s1 = PyBytes_AsString(os1);
6313 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006314#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006315
6316#if defined(PYOS_OS2)
6317 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6318 APIRET rc;
6319
Guido van Rossumd48f2521997-12-05 22:19:34 +00006320 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006321 if (rc != NO_ERROR) {
6322 os2_error(rc);
6323 goto error;
6324 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006325
6326 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6327 APIRET rc;
6328
Guido van Rossumd48f2521997-12-05 22:19:34 +00006329 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006330 if (rc != NO_ERROR) {
6331 os2_error(rc);
6332 goto error;
6333 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006334 } else {
6335#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006336 /* XXX This can leak memory -- not easy to fix :-( */
6337 /* len includes space for a trailing \0; the size arg to
6338 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006339#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006340 len = wcslen(s1) + wcslen(s2) + 2;
6341 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006342#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006343 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006344 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006345#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006346 if (newstr == NULL) {
6347 PyErr_NoMemory();
6348 goto error;
6349 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006350#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006351 newenv = PyUnicode_AsUnicode(newstr);
6352 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6353 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006354 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006355 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006356 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006357#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 newenv = PyBytes_AS_STRING(newstr);
6359 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6360 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006362 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006364#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006365
Victor Stinner8c62be82010-05-06 00:08:46 +00006366 /* Install the first arg and newstr in posix_putenv_garbage;
6367 * this will cause previous value to be collected. This has to
6368 * happen after the real putenv() call because the old value
6369 * was still accessible until then. */
6370 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006371#ifdef MS_WINDOWS
6372 PyTuple_GET_ITEM(args, 0),
6373#else
6374 os1,
6375#endif
6376 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006377 /* really not much we can do; just leak */
6378 PyErr_Clear();
6379 }
6380 else {
6381 Py_DECREF(newstr);
6382 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006383
6384#if defined(PYOS_OS2)
6385 }
6386#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006387
Martin v. Löwis011e8422009-05-05 04:43:17 +00006388#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 Py_DECREF(os1);
6390 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006391#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006392 Py_RETURN_NONE;
6393
6394error:
6395#ifndef MS_WINDOWS
6396 Py_DECREF(os1);
6397 Py_DECREF(os2);
6398#endif
6399 Py_XDECREF(newstr);
6400 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006401}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006402#endif /* putenv */
6403
Guido van Rossumc524d952001-10-19 01:31:59 +00006404#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006406"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006407Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006408
6409static PyObject *
6410posix_unsetenv(PyObject *self, PyObject *args)
6411{
Victor Stinner84ae1182010-05-06 22:05:07 +00006412#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006413 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006414
Victor Stinner8c62be82010-05-06 00:08:46 +00006415 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6416 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006417#else
6418 PyObject *os1;
6419 char *s1;
6420
6421 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6422 PyUnicode_FSConverter, &os1))
6423 return NULL;
6424 s1 = PyBytes_AsString(os1);
6425#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006426
Victor Stinner8c62be82010-05-06 00:08:46 +00006427 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006428
Victor Stinner8c62be82010-05-06 00:08:46 +00006429 /* Remove the key from posix_putenv_garbage;
6430 * this will cause it to be collected. This has to
6431 * happen after the real unsetenv() call because the
6432 * old value was still accessible until then.
6433 */
6434 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006435#ifdef MS_WINDOWS
6436 PyTuple_GET_ITEM(args, 0)
6437#else
6438 os1
6439#endif
6440 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006441 /* really not much we can do; just leak */
6442 PyErr_Clear();
6443 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006444
Victor Stinner84ae1182010-05-06 22:05:07 +00006445#ifndef MS_WINDOWS
6446 Py_DECREF(os1);
6447#endif
6448 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006449}
6450#endif /* unsetenv */
6451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006452PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006453"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006454Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006455
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006456static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006457posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006458{
Victor Stinner8c62be82010-05-06 00:08:46 +00006459 int code;
6460 char *message;
6461 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6462 return NULL;
6463 message = strerror(code);
6464 if (message == NULL) {
6465 PyErr_SetString(PyExc_ValueError,
6466 "strerror() argument out of range");
6467 return NULL;
6468 }
6469 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006470}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006471
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006472
Guido van Rossumc9641791998-08-04 15:26:23 +00006473#ifdef HAVE_SYS_WAIT_H
6474
Fred Drake106c1a02002-04-23 15:58:02 +00006475#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006476PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006477"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006478Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006479
6480static PyObject *
6481posix_WCOREDUMP(PyObject *self, PyObject *args)
6482{
Victor Stinner8c62be82010-05-06 00:08:46 +00006483 WAIT_TYPE status;
6484 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006485
Victor Stinner8c62be82010-05-06 00:08:46 +00006486 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6487 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006488
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006490}
6491#endif /* WCOREDUMP */
6492
6493#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006494PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006495"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006496Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006497job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006498
6499static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006500posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006501{
Victor Stinner8c62be82010-05-06 00:08:46 +00006502 WAIT_TYPE status;
6503 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006504
Victor Stinner8c62be82010-05-06 00:08:46 +00006505 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6506 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006507
Victor Stinner8c62be82010-05-06 00:08:46 +00006508 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006509}
6510#endif /* WIFCONTINUED */
6511
Guido van Rossumc9641791998-08-04 15:26:23 +00006512#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006513PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006514"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006515Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006516
6517static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006518posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006519{
Victor Stinner8c62be82010-05-06 00:08:46 +00006520 WAIT_TYPE status;
6521 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006522
Victor Stinner8c62be82010-05-06 00:08:46 +00006523 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6524 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006525
Victor Stinner8c62be82010-05-06 00:08:46 +00006526 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006527}
6528#endif /* WIFSTOPPED */
6529
6530#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006531PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006532"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006533Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006534
6535static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006536posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006537{
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 WAIT_TYPE status;
6539 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006540
Victor Stinner8c62be82010-05-06 00:08:46 +00006541 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6542 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006543
Victor Stinner8c62be82010-05-06 00:08:46 +00006544 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006545}
6546#endif /* WIFSIGNALED */
6547
6548#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006549PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006550"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006551Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006552system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006553
6554static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006555posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006556{
Victor Stinner8c62be82010-05-06 00:08:46 +00006557 WAIT_TYPE status;
6558 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006559
Victor Stinner8c62be82010-05-06 00:08:46 +00006560 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6561 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006562
Victor Stinner8c62be82010-05-06 00:08:46 +00006563 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006564}
6565#endif /* WIFEXITED */
6566
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006567#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006568PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006569"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006570Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006571
6572static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006573posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006574{
Victor Stinner8c62be82010-05-06 00:08:46 +00006575 WAIT_TYPE status;
6576 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006577
Victor Stinner8c62be82010-05-06 00:08:46 +00006578 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6579 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006580
Victor Stinner8c62be82010-05-06 00:08:46 +00006581 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006582}
6583#endif /* WEXITSTATUS */
6584
6585#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006586PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006587"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006588Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006589value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006590
6591static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006592posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006593{
Victor Stinner8c62be82010-05-06 00:08:46 +00006594 WAIT_TYPE status;
6595 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006596
Victor Stinner8c62be82010-05-06 00:08:46 +00006597 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6598 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006599
Victor Stinner8c62be82010-05-06 00:08:46 +00006600 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006601}
6602#endif /* WTERMSIG */
6603
6604#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006605PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006606"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006607Return the signal that stopped the process that provided\n\
6608the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006609
6610static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006611posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006612{
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 WAIT_TYPE status;
6614 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006615
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6617 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006618
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006620}
6621#endif /* WSTOPSIG */
6622
6623#endif /* HAVE_SYS_WAIT_H */
6624
6625
Thomas Wouters477c8d52006-05-27 19:21:47 +00006626#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006627#ifdef _SCO_DS
6628/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6629 needed definitions in sys/statvfs.h */
6630#define _SVID3
6631#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006632#include <sys/statvfs.h>
6633
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006634static PyObject*
6635_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6637 if (v == NULL)
6638 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006639
6640#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006641 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6642 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6643 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6644 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6645 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6646 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6647 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6648 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6649 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6650 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006651#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6653 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6654 PyStructSequence_SET_ITEM(v, 2,
6655 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6656 PyStructSequence_SET_ITEM(v, 3,
6657 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6658 PyStructSequence_SET_ITEM(v, 4,
6659 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6660 PyStructSequence_SET_ITEM(v, 5,
6661 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6662 PyStructSequence_SET_ITEM(v, 6,
6663 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6664 PyStructSequence_SET_ITEM(v, 7,
6665 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6666 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6667 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006668#endif
6669
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006671}
6672
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006673PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006674"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006675Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006676
6677static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006678posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006679{
Victor Stinner8c62be82010-05-06 00:08:46 +00006680 int fd, res;
6681 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006682
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6684 return NULL;
6685 Py_BEGIN_ALLOW_THREADS
6686 res = fstatvfs(fd, &st);
6687 Py_END_ALLOW_THREADS
6688 if (res != 0)
6689 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006690
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006692}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006693#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006694
6695
Thomas Wouters477c8d52006-05-27 19:21:47 +00006696#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006697#include <sys/statvfs.h>
6698
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006699PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006700"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006701Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006702
6703static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006704posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006705{
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 char *path;
6707 int res;
6708 struct statvfs st;
6709 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6710 return NULL;
6711 Py_BEGIN_ALLOW_THREADS
6712 res = statvfs(path, &st);
6713 Py_END_ALLOW_THREADS
6714 if (res != 0)
6715 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006716
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006718}
6719#endif /* HAVE_STATVFS */
6720
Fred Drakec9680921999-12-13 16:37:25 +00006721/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6722 * It maps strings representing configuration variable names to
6723 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006724 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006725 * rarely-used constants. There are three separate tables that use
6726 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006727 *
6728 * This code is always included, even if none of the interfaces that
6729 * need it are included. The #if hackery needed to avoid it would be
6730 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006731 */
6732struct constdef {
6733 char *name;
6734 long value;
6735};
6736
Fred Drake12c6e2d1999-12-14 21:25:03 +00006737static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006738conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006739 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006740{
Christian Heimes217cfd12007-12-02 14:31:20 +00006741 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006742 *valuep = PyLong_AS_LONG(arg);
6743 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006744 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006745 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006746 /* look up the value in the table using a binary search */
6747 size_t lo = 0;
6748 size_t mid;
6749 size_t hi = tablesize;
6750 int cmp;
6751 const char *confname;
6752 if (!PyUnicode_Check(arg)) {
6753 PyErr_SetString(PyExc_TypeError,
6754 "configuration names must be strings or integers");
6755 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006756 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006757 confname = _PyUnicode_AsString(arg);
6758 if (confname == NULL)
6759 return 0;
6760 while (lo < hi) {
6761 mid = (lo + hi) / 2;
6762 cmp = strcmp(confname, table[mid].name);
6763 if (cmp < 0)
6764 hi = mid;
6765 else if (cmp > 0)
6766 lo = mid + 1;
6767 else {
6768 *valuep = table[mid].value;
6769 return 1;
6770 }
6771 }
6772 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6773 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006774 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006775}
6776
6777
6778#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6779static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006780#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006781 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006782#endif
6783#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006784 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006785#endif
Fred Drakec9680921999-12-13 16:37:25 +00006786#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006788#endif
6789#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006791#endif
6792#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006793 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006794#endif
6795#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006797#endif
6798#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006799 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006800#endif
6801#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006803#endif
6804#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006806#endif
6807#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006808 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006809#endif
6810#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006812#endif
6813#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006815#endif
6816#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006817 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006818#endif
6819#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006821#endif
6822#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006824#endif
6825#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006827#endif
6828#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006830#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006831#ifdef _PC_ACL_ENABLED
6832 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6833#endif
6834#ifdef _PC_MIN_HOLE_SIZE
6835 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6836#endif
6837#ifdef _PC_ALLOC_SIZE_MIN
6838 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6839#endif
6840#ifdef _PC_REC_INCR_XFER_SIZE
6841 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6842#endif
6843#ifdef _PC_REC_MAX_XFER_SIZE
6844 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6845#endif
6846#ifdef _PC_REC_MIN_XFER_SIZE
6847 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6848#endif
6849#ifdef _PC_REC_XFER_ALIGN
6850 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6851#endif
6852#ifdef _PC_SYMLINK_MAX
6853 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6854#endif
6855#ifdef _PC_XATTR_ENABLED
6856 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6857#endif
6858#ifdef _PC_XATTR_EXISTS
6859 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6860#endif
6861#ifdef _PC_TIMESTAMP_RESOLUTION
6862 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6863#endif
Fred Drakec9680921999-12-13 16:37:25 +00006864};
6865
Fred Drakec9680921999-12-13 16:37:25 +00006866static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006867conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006868{
6869 return conv_confname(arg, valuep, posix_constants_pathconf,
6870 sizeof(posix_constants_pathconf)
6871 / sizeof(struct constdef));
6872}
6873#endif
6874
6875#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006876PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006877"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006878Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006879If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006880
6881static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006882posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006883{
6884 PyObject *result = NULL;
6885 int name, fd;
6886
Fred Drake12c6e2d1999-12-14 21:25:03 +00006887 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6888 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006889 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006890
Stefan Krah0e803b32010-11-26 16:16:47 +00006891 errno = 0;
6892 limit = fpathconf(fd, name);
6893 if (limit == -1 && errno != 0)
6894 posix_error();
6895 else
6896 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006897 }
6898 return result;
6899}
6900#endif
6901
6902
6903#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006904PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006905"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006906Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006907If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006908
6909static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006910posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006911{
6912 PyObject *result = NULL;
6913 int name;
6914 char *path;
6915
6916 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6917 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006919
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 errno = 0;
6921 limit = pathconf(path, name);
6922 if (limit == -1 && errno != 0) {
6923 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006924 /* could be a path or name problem */
6925 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006926 else
Stefan Krah99439262010-11-26 12:58:05 +00006927 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 }
6929 else
6930 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006931 }
6932 return result;
6933}
6934#endif
6935
6936#ifdef HAVE_CONFSTR
6937static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006938#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006939 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006940#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006941#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006942 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006943#endif
6944#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006945 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006946#endif
Fred Draked86ed291999-12-15 15:34:33 +00006947#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006948 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006949#endif
6950#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006951 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006952#endif
6953#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006954 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006955#endif
6956#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006958#endif
Fred Drakec9680921999-12-13 16:37:25 +00006959#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006961#endif
6962#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006963 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006964#endif
6965#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006967#endif
6968#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006969 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006970#endif
6971#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006972 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006973#endif
6974#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006975 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006976#endif
6977#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006978 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006979#endif
6980#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006981 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006982#endif
Fred Draked86ed291999-12-15 15:34:33 +00006983#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006985#endif
Fred Drakec9680921999-12-13 16:37:25 +00006986#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006988#endif
Fred Draked86ed291999-12-15 15:34:33 +00006989#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006991#endif
6992#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006993 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006994#endif
6995#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006996 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006997#endif
6998#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006999 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007000#endif
Fred Drakec9680921999-12-13 16:37:25 +00007001#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007002 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007003#endif
7004#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007006#endif
7007#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007009#endif
7010#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007011 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007012#endif
7013#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007014 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007015#endif
7016#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007017 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007018#endif
7019#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007020 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007021#endif
7022#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007024#endif
7025#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007026 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007027#endif
7028#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007030#endif
7031#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007033#endif
7034#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007035 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007036#endif
7037#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007038 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007039#endif
7040#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007042#endif
7043#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007044 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007045#endif
7046#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007047 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007048#endif
Fred Draked86ed291999-12-15 15:34:33 +00007049#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007050 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007051#endif
7052#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007053 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007054#endif
7055#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007057#endif
7058#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007060#endif
7061#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007063#endif
7064#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00007065 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007066#endif
7067#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007069#endif
7070#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00007071 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007072#endif
7073#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007075#endif
7076#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007078#endif
7079#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007081#endif
7082#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007084#endif
7085#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007087#endif
Fred Drakec9680921999-12-13 16:37:25 +00007088};
7089
7090static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007091conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007092{
7093 return conv_confname(arg, valuep, posix_constants_confstr,
7094 sizeof(posix_constants_confstr)
7095 / sizeof(struct constdef));
7096}
7097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007098PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007099"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007100Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007101
7102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007103posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007104{
7105 PyObject *result = NULL;
7106 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00007107 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00007108 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007109
Victor Stinnercb043522010-09-10 23:49:04 +00007110 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
7111 return NULL;
7112
7113 errno = 0;
7114 len = confstr(name, buffer, sizeof(buffer));
7115 if (len == 0) {
7116 if (errno) {
7117 posix_error();
7118 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00007119 }
7120 else {
Victor Stinnercb043522010-09-10 23:49:04 +00007121 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00007122 }
7123 }
Victor Stinnercb043522010-09-10 23:49:04 +00007124
7125 if ((unsigned int)len >= sizeof(buffer)) {
7126 char *buf = PyMem_Malloc(len);
7127 if (buf == NULL)
7128 return PyErr_NoMemory();
7129 confstr(name, buf, len);
7130 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
7131 PyMem_Free(buf);
7132 }
7133 else
7134 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007135 return result;
7136}
7137#endif
7138
7139
7140#ifdef HAVE_SYSCONF
7141static struct constdef posix_constants_sysconf[] = {
7142#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007143 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007144#endif
7145#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007147#endif
7148#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007149 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007150#endif
7151#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007153#endif
7154#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007155 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007156#endif
7157#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00007158 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007159#endif
7160#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00007161 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007162#endif
7163#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007164 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007165#endif
7166#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007168#endif
7169#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007171#endif
Fred Draked86ed291999-12-15 15:34:33 +00007172#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007173 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007174#endif
7175#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00007176 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007177#endif
Fred Drakec9680921999-12-13 16:37:25 +00007178#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007180#endif
Fred Drakec9680921999-12-13 16:37:25 +00007181#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007182 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007183#endif
7184#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007186#endif
7187#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007188 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007189#endif
7190#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007191 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007192#endif
7193#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007194 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007195#endif
Fred Draked86ed291999-12-15 15:34:33 +00007196#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007197 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007198#endif
Fred Drakec9680921999-12-13 16:37:25 +00007199#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007201#endif
7202#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007204#endif
7205#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007206 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007207#endif
7208#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007209 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007210#endif
7211#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007213#endif
Fred Draked86ed291999-12-15 15:34:33 +00007214#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00007215 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007216#endif
Fred Drakec9680921999-12-13 16:37:25 +00007217#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007218 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007219#endif
7220#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007221 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007222#endif
7223#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007225#endif
7226#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007227 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007228#endif
7229#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007230 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007231#endif
7232#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007233 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007234#endif
7235#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007236 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007237#endif
7238#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007240#endif
7241#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007242 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007243#endif
7244#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007246#endif
7247#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007249#endif
7250#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007252#endif
7253#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007255#endif
7256#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007258#endif
7259#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007261#endif
7262#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007263 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007264#endif
7265#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007267#endif
7268#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007269 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007270#endif
7271#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007272 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007273#endif
7274#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007276#endif
7277#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007278 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007279#endif
7280#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007281 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007282#endif
7283#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007284 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007285#endif
Fred Draked86ed291999-12-15 15:34:33 +00007286#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007287 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007288#endif
Fred Drakec9680921999-12-13 16:37:25 +00007289#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007290 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007291#endif
7292#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007293 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007294#endif
7295#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007296 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007297#endif
Fred Draked86ed291999-12-15 15:34:33 +00007298#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007299 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007300#endif
Fred Drakec9680921999-12-13 16:37:25 +00007301#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007302 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007303#endif
Fred Draked86ed291999-12-15 15:34:33 +00007304#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007305 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007306#endif
7307#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007308 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007309#endif
Fred Drakec9680921999-12-13 16:37:25 +00007310#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007311 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007312#endif
7313#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007314 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007315#endif
7316#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007318#endif
7319#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007321#endif
Fred Draked86ed291999-12-15 15:34:33 +00007322#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007324#endif
Fred Drakec9680921999-12-13 16:37:25 +00007325#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007326 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007327#endif
7328#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007329 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007330#endif
7331#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007332 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007333#endif
7334#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007335 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007336#endif
7337#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007338 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007339#endif
7340#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007341 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007342#endif
7343#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007345#endif
Fred Draked86ed291999-12-15 15:34:33 +00007346#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007348#endif
Fred Drakec9680921999-12-13 16:37:25 +00007349#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007350 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007351#endif
7352#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007353 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007354#endif
Fred Draked86ed291999-12-15 15:34:33 +00007355#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007357#endif
Fred Drakec9680921999-12-13 16:37:25 +00007358#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007359 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007360#endif
7361#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007362 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007363#endif
7364#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007365 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007366#endif
7367#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007369#endif
7370#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007372#endif
7373#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007375#endif
7376#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007377 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007378#endif
7379#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007381#endif
7382#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007384#endif
Fred Draked86ed291999-12-15 15:34:33 +00007385#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007386 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007387#endif
7388#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007389 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007390#endif
Fred Drakec9680921999-12-13 16:37:25 +00007391#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007392 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007393#endif
7394#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007395 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007396#endif
7397#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007398 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007399#endif
7400#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007401 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007402#endif
7403#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007404 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007405#endif
7406#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007407 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007408#endif
7409#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007410 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007411#endif
7412#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007413 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007414#endif
7415#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007416 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007417#endif
7418#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007419 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007420#endif
7421#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007422 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007423#endif
7424#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007425 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007426#endif
7427#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007428 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007429#endif
7430#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007431 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007432#endif
7433#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007434 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007435#endif
7436#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007437 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007438#endif
7439#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007440 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007441#endif
7442#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007443 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007444#endif
7445#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007446 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007447#endif
7448#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007449 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007450#endif
7451#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007453#endif
7454#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007455 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007456#endif
7457#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007458 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007459#endif
7460#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007461 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007462#endif
7463#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007464 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007465#endif
7466#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007467 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007468#endif
7469#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007470 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007471#endif
7472#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007473 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007474#endif
7475#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007476 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007477#endif
7478#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007480#endif
7481#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007482 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007483#endif
7484#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007485 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007486#endif
7487#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007488 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007489#endif
7490#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007491 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007492#endif
7493#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007494 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007495#endif
Fred Draked86ed291999-12-15 15:34:33 +00007496#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007497 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007498#endif
Fred Drakec9680921999-12-13 16:37:25 +00007499#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007500 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007501#endif
7502#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007503 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007504#endif
7505#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007506 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007507#endif
7508#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007509 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007510#endif
7511#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007512 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007513#endif
7514#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007515 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007516#endif
7517#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007518 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007519#endif
7520#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007521 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007522#endif
7523#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007524 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007525#endif
7526#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007527 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007528#endif
7529#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007530 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007531#endif
7532#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007533 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007534#endif
7535#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007536 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007537#endif
7538#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007539 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007540#endif
7541#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007542 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007543#endif
7544#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007545 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007546#endif
7547#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007548 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007549#endif
7550#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007551 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007552#endif
7553#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007554 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007555#endif
7556#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007557 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007558#endif
7559#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007560 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007561#endif
7562#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007564#endif
7565#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007566 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007567#endif
7568#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007569 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007570#endif
7571#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007572 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007573#endif
7574#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007575 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007576#endif
7577#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007578 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007579#endif
7580#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007581 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007582#endif
7583#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007584 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007585#endif
7586#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007587 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007588#endif
7589#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007590 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007591#endif
7592#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007594#endif
7595#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007596 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007597#endif
7598#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007599 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007600#endif
7601#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007602 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007603#endif
7604#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007606#endif
7607#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007608 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007609#endif
7610#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007611 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007612#endif
7613#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007614 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007615#endif
7616#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007617 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007618#endif
7619#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007620 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007621#endif
7622#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007623 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007624#endif
7625#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007626 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007627#endif
7628#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007629 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007630#endif
7631#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007632 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007633#endif
7634};
7635
7636static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007637conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007638{
7639 return conv_confname(arg, valuep, posix_constants_sysconf,
7640 sizeof(posix_constants_sysconf)
7641 / sizeof(struct constdef));
7642}
7643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007644PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007645"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007646Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007647
7648static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007649posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007650{
7651 PyObject *result = NULL;
7652 int name;
7653
7654 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7655 int value;
7656
7657 errno = 0;
7658 value = sysconf(name);
7659 if (value == -1 && errno != 0)
7660 posix_error();
7661 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007662 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007663 }
7664 return result;
7665}
7666#endif
7667
7668
Fred Drakebec628d1999-12-15 18:31:10 +00007669/* This code is used to ensure that the tables of configuration value names
7670 * are in sorted order as required by conv_confname(), and also to build the
7671 * the exported dictionaries that are used to publish information about the
7672 * names available on the host platform.
7673 *
7674 * Sorting the table at runtime ensures that the table is properly ordered
7675 * when used, even for platforms we're not able to test on. It also makes
7676 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007677 */
Fred Drakebec628d1999-12-15 18:31:10 +00007678
7679static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007680cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007681{
7682 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007683 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007684 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007685 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007686
7687 return strcmp(c1->name, c2->name);
7688}
7689
7690static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007691setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007692 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007693{
Fred Drakebec628d1999-12-15 18:31:10 +00007694 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007695 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007696
7697 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7698 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007699 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007700 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007701
Barry Warsaw3155db32000-04-13 15:20:40 +00007702 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007703 PyObject *o = PyLong_FromLong(table[i].value);
7704 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7705 Py_XDECREF(o);
7706 Py_DECREF(d);
7707 return -1;
7708 }
7709 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007710 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007711 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007712}
7713
Fred Drakebec628d1999-12-15 18:31:10 +00007714/* Return -1 on failure, 0 on success. */
7715static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007716setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007717{
7718#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007719 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007720 sizeof(posix_constants_pathconf)
7721 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007722 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007723 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007724#endif
7725#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007726 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007727 sizeof(posix_constants_confstr)
7728 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007729 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007730 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007731#endif
7732#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007733 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007734 sizeof(posix_constants_sysconf)
7735 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007736 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007737 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007738#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007739 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007740}
Fred Draked86ed291999-12-15 15:34:33 +00007741
7742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007743PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007744"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007745Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007746in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007747
7748static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007749posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007750{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007751 abort();
7752 /*NOTREACHED*/
7753 Py_FatalError("abort() called from Python code didn't abort!");
7754 return NULL;
7755}
Fred Drakebec628d1999-12-15 18:31:10 +00007756
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007757#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007758PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007759"startfile(filepath [, operation]) - Start a file with its associated\n\
7760application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007761\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007762When \"operation\" is not specified or \"open\", this acts like\n\
7763double-clicking the file in Explorer, or giving the file name as an\n\
7764argument to the DOS \"start\" command: the file is opened with whatever\n\
7765application (if any) its extension is associated.\n\
7766When another \"operation\" is given, it specifies what should be done with\n\
7767the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007768\n\
7769startfile returns as soon as the associated application is launched.\n\
7770There is no option to wait for the application to close, and no way\n\
7771to retrieve the application's exit status.\n\
7772\n\
7773The filepath is relative to the current directory. If you want to use\n\
7774an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007775the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007776
7777static PyObject *
7778win32_startfile(PyObject *self, PyObject *args)
7779{
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 PyObject *ofilepath;
7781 char *filepath;
7782 char *operation = NULL;
7783 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007784
Victor Stinner8c62be82010-05-06 00:08:46 +00007785 PyObject *unipath, *woperation = NULL;
7786 if (!PyArg_ParseTuple(args, "U|s:startfile",
7787 &unipath, &operation)) {
7788 PyErr_Clear();
7789 goto normal;
7790 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007791
Victor Stinner8c62be82010-05-06 00:08:46 +00007792 if (operation) {
7793 woperation = PyUnicode_DecodeASCII(operation,
7794 strlen(operation), NULL);
7795 if (!woperation) {
7796 PyErr_Clear();
7797 operation = NULL;
7798 goto normal;
7799 }
7800 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007801
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 Py_BEGIN_ALLOW_THREADS
7803 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7804 PyUnicode_AS_UNICODE(unipath),
7805 NULL, NULL, SW_SHOWNORMAL);
7806 Py_END_ALLOW_THREADS
7807
7808 Py_XDECREF(woperation);
7809 if (rc <= (HINSTANCE)32) {
7810 PyObject *errval = win32_error_unicode("startfile",
7811 PyUnicode_AS_UNICODE(unipath));
7812 return errval;
7813 }
7814 Py_INCREF(Py_None);
7815 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007816
7817normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007818 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7819 PyUnicode_FSConverter, &ofilepath,
7820 &operation))
7821 return NULL;
7822 filepath = PyBytes_AsString(ofilepath);
7823 Py_BEGIN_ALLOW_THREADS
7824 rc = ShellExecute((HWND)0, operation, filepath,
7825 NULL, NULL, SW_SHOWNORMAL);
7826 Py_END_ALLOW_THREADS
7827 if (rc <= (HINSTANCE)32) {
7828 PyObject *errval = win32_error("startfile", filepath);
7829 Py_DECREF(ofilepath);
7830 return errval;
7831 }
7832 Py_DECREF(ofilepath);
7833 Py_INCREF(Py_None);
7834 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007835}
7836#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007837
Martin v. Löwis438b5342002-12-27 10:16:42 +00007838#ifdef HAVE_GETLOADAVG
7839PyDoc_STRVAR(posix_getloadavg__doc__,
7840"getloadavg() -> (float, float, float)\n\n\
7841Return the number of processes in the system run queue averaged over\n\
7842the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7843was unobtainable");
7844
7845static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007846posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007847{
7848 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007849 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007850 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7851 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007852 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007853 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007854}
7855#endif
7856
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007857#ifdef MS_WINDOWS
7858
7859PyDoc_STRVAR(win32_urandom__doc__,
7860"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007861Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007862
7863typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7864 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7865 DWORD dwFlags );
7866typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7867 BYTE *pbBuffer );
7868
7869static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007870/* This handle is never explicitly released. Instead, the operating
7871 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007872static HCRYPTPROV hCryptProv = 0;
7873
Tim Peters4ad82172004-08-30 17:02:04 +00007874static PyObject*
7875win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007876{
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 int howMany;
7878 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007879
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 /* Read arguments */
7881 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7882 return NULL;
7883 if (howMany < 0)
7884 return PyErr_Format(PyExc_ValueError,
7885 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007886
Victor Stinner8c62be82010-05-06 00:08:46 +00007887 if (hCryptProv == 0) {
7888 HINSTANCE hAdvAPI32 = NULL;
7889 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007890
Victor Stinner8c62be82010-05-06 00:08:46 +00007891 /* Obtain handle to the DLL containing CryptoAPI
7892 This should not fail */
7893 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7894 if(hAdvAPI32 == NULL)
7895 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007896
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 /* Obtain pointers to the CryptoAPI functions
7898 This will fail on some early versions of Win95 */
7899 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7900 hAdvAPI32,
7901 "CryptAcquireContextA");
7902 if (pCryptAcquireContext == NULL)
7903 return PyErr_Format(PyExc_NotImplementedError,
7904 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007905
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7907 hAdvAPI32, "CryptGenRandom");
7908 if (pCryptGenRandom == NULL)
7909 return PyErr_Format(PyExc_NotImplementedError,
7910 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007911
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 /* Acquire context */
7913 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7914 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7915 return win32_error("CryptAcquireContext", NULL);
7916 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007917
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 /* Allocate bytes */
7919 result = PyBytes_FromStringAndSize(NULL, howMany);
7920 if (result != NULL) {
7921 /* Get random data */
7922 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7923 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7924 PyBytes_AS_STRING(result))) {
7925 Py_DECREF(result);
7926 return win32_error("CryptGenRandom", NULL);
7927 }
7928 }
7929 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007930}
7931#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007932
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007933PyDoc_STRVAR(device_encoding__doc__,
7934"device_encoding(fd) -> str\n\n\
7935Return a string describing the encoding of the device\n\
7936if the output is a terminal; else return None.");
7937
7938static PyObject *
7939device_encoding(PyObject *self, PyObject *args)
7940{
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 int fd;
7942 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7943 return NULL;
7944 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7945 Py_INCREF(Py_None);
7946 return Py_None;
7947 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007948#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007949 if (fd == 0) {
7950 char buf[100];
7951 sprintf(buf, "cp%d", GetConsoleCP());
7952 return PyUnicode_FromString(buf);
7953 }
7954 if (fd == 1 || fd == 2) {
7955 char buf[100];
7956 sprintf(buf, "cp%d", GetConsoleOutputCP());
7957 return PyUnicode_FromString(buf);
7958 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007959#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {
7961 char *codeset = nl_langinfo(CODESET);
7962 if (codeset != NULL && codeset[0] != 0)
7963 return PyUnicode_FromString(codeset);
7964 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007965#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007966 Py_INCREF(Py_None);
7967 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007968}
7969
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007970#ifdef __VMS
7971/* Use openssl random routine */
7972#include <openssl/rand.h>
7973PyDoc_STRVAR(vms_urandom__doc__,
7974"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007975Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007976
7977static PyObject*
7978vms_urandom(PyObject *self, PyObject *args)
7979{
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 int howMany;
7981 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007982
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 /* Read arguments */
7984 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7985 return NULL;
7986 if (howMany < 0)
7987 return PyErr_Format(PyExc_ValueError,
7988 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007989
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 /* Allocate bytes */
7991 result = PyBytes_FromStringAndSize(NULL, howMany);
7992 if (result != NULL) {
7993 /* Get random data */
7994 if (RAND_pseudo_bytes((unsigned char*)
7995 PyBytes_AS_STRING(result),
7996 howMany) < 0) {
7997 Py_DECREF(result);
7998 return PyErr_Format(PyExc_ValueError,
7999 "RAND_pseudo_bytes");
8000 }
8001 }
8002 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008003}
8004#endif
8005
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008006#ifdef HAVE_SETRESUID
8007PyDoc_STRVAR(posix_setresuid__doc__,
8008"setresuid(ruid, euid, suid)\n\n\
8009Set the current process's real, effective, and saved user ids.");
8010
8011static PyObject*
8012posix_setresuid (PyObject *self, PyObject *args)
8013{
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 /* We assume uid_t is no larger than a long. */
8015 long ruid, euid, suid;
8016 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
8017 return NULL;
8018 if (setresuid(ruid, euid, suid) < 0)
8019 return posix_error();
8020 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008021}
8022#endif
8023
8024#ifdef HAVE_SETRESGID
8025PyDoc_STRVAR(posix_setresgid__doc__,
8026"setresgid(rgid, egid, sgid)\n\n\
8027Set the current process's real, effective, and saved group ids.");
8028
8029static PyObject*
8030posix_setresgid (PyObject *self, PyObject *args)
8031{
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 /* We assume uid_t is no larger than a long. */
8033 long rgid, egid, sgid;
8034 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
8035 return NULL;
8036 if (setresgid(rgid, egid, sgid) < 0)
8037 return posix_error();
8038 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008039}
8040#endif
8041
8042#ifdef HAVE_GETRESUID
8043PyDoc_STRVAR(posix_getresuid__doc__,
8044"getresuid() -> (ruid, euid, suid)\n\n\
8045Get tuple of the current process's real, effective, and saved user ids.");
8046
8047static PyObject*
8048posix_getresuid (PyObject *self, PyObject *noargs)
8049{
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 uid_t ruid, euid, suid;
8051 long l_ruid, l_euid, l_suid;
8052 if (getresuid(&ruid, &euid, &suid) < 0)
8053 return posix_error();
8054 /* Force the values into long's as we don't know the size of uid_t. */
8055 l_ruid = ruid;
8056 l_euid = euid;
8057 l_suid = suid;
8058 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008059}
8060#endif
8061
8062#ifdef HAVE_GETRESGID
8063PyDoc_STRVAR(posix_getresgid__doc__,
8064"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00008065Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008066
8067static PyObject*
8068posix_getresgid (PyObject *self, PyObject *noargs)
8069{
Victor Stinner8c62be82010-05-06 00:08:46 +00008070 uid_t rgid, egid, sgid;
8071 long l_rgid, l_egid, l_sgid;
8072 if (getresgid(&rgid, &egid, &sgid) < 0)
8073 return posix_error();
8074 /* Force the values into long's as we don't know the size of uid_t. */
8075 l_rgid = rgid;
8076 l_egid = egid;
8077 l_sgid = sgid;
8078 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008079}
8080#endif
8081
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008082/* Posix *at family of functions:
8083 faccessat, fchmodat, fchownat, fstatat, futimesat,
8084 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
8085 unlinkat, utimensat, mkfifoat */
8086
8087#ifdef HAVE_FACCESSAT
8088PyDoc_STRVAR(posix_faccessat__doc__,
8089"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
8090Like access() but if path is relative, it is taken as relative to dirfd.\n\
8091flags is optional and can be constructed by ORing together zero or more\n\
8092of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
8093If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8094is interpreted relative to the current working directory.");
8095
8096static PyObject *
8097posix_faccessat(PyObject *self, PyObject *args)
8098{
8099 PyObject *opath;
8100 char *path;
8101 int mode;
8102 int res;
8103 int dirfd, flags = 0;
8104 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
8105 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8106 return NULL;
8107 path = PyBytes_AsString(opath);
8108 Py_BEGIN_ALLOW_THREADS
8109 res = faccessat(dirfd, path, mode, flags);
8110 Py_END_ALLOW_THREADS
8111 Py_DECREF(opath);
8112 return PyBool_FromLong(res == 0);
8113}
8114#endif
8115
8116#ifdef HAVE_FCHMODAT
8117PyDoc_STRVAR(posix_fchmodat__doc__,
8118"fchmodat(dirfd, path, mode, flags=0)\n\n\
8119Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
8120flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8121If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8122is interpreted relative to the current working directory.");
8123
8124static PyObject *
8125posix_fchmodat(PyObject *self, PyObject *args)
8126{
8127 int dirfd, mode, res;
8128 int flags = 0;
8129 PyObject *opath;
8130 char *path;
8131
8132 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
8133 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8134 return NULL;
8135
8136 path = PyBytes_AsString(opath);
8137
8138 Py_BEGIN_ALLOW_THREADS
8139 res = fchmodat(dirfd, path, mode, flags);
8140 Py_END_ALLOW_THREADS
8141 Py_DECREF(opath);
8142 if (res < 0)
8143 return posix_error();
8144 Py_RETURN_NONE;
8145}
8146#endif /* HAVE_FCHMODAT */
8147
8148#ifdef HAVE_FCHOWNAT
8149PyDoc_STRVAR(posix_fchownat__doc__,
8150"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
8151Like chown() but if path is relative, it is taken as relative to dirfd.\n\
8152flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8153If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8154is interpreted relative to the current working directory.");
8155
8156static PyObject *
8157posix_fchownat(PyObject *self, PyObject *args)
8158{
8159 PyObject *opath;
8160 int dirfd, res;
8161 long uid, gid;
8162 int flags = 0;
8163 char *path;
8164
8165 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
8166 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
8167 return NULL;
8168
8169 path = PyBytes_AsString(opath);
8170
8171 Py_BEGIN_ALLOW_THREADS
8172 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
8173 Py_END_ALLOW_THREADS
8174 Py_DECREF(opath);
8175 if (res < 0)
8176 return posix_error();
8177 Py_RETURN_NONE;
8178}
8179#endif /* HAVE_FCHOWNAT */
8180
8181#ifdef HAVE_FSTATAT
8182PyDoc_STRVAR(posix_fstatat__doc__,
8183"fstatat(dirfd, path, flags=0) -> stat result\n\n\
8184Like stat() but if path is relative, it is taken as relative to dirfd.\n\
8185flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8186If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8187is interpreted relative to the current working directory.");
8188
8189static PyObject *
8190posix_fstatat(PyObject *self, PyObject *args)
8191{
8192 PyObject *opath;
8193 char *path;
8194 STRUCT_STAT st;
8195 int dirfd, res, flags = 0;
8196
8197 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
8198 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8199 return NULL;
8200 path = PyBytes_AsString(opath);
8201
8202 Py_BEGIN_ALLOW_THREADS
8203 res = fstatat(dirfd, path, &st, flags);
8204 Py_END_ALLOW_THREADS
8205 Py_DECREF(opath);
8206 if (res != 0)
8207 return posix_error();
8208
8209 return _pystat_fromstructstat(&st);
8210}
8211#endif
8212
8213#ifdef HAVE_FUTIMESAT
8214PyDoc_STRVAR(posix_futimesat__doc__,
8215"futimesat(dirfd, path, (atime, mtime))\n\
8216futimesat(dirfd, path, None)\n\n\
8217Like utime() but if path is relative, it is taken as relative to dirfd.\n\
8218If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8219is interpreted relative to the current working directory.");
8220
8221static PyObject *
8222posix_futimesat(PyObject *self, PyObject *args)
8223{
8224 PyObject *opath;
8225 char *path;
8226 int res, dirfd;
8227 PyObject* arg;
8228
8229 struct timeval buf[2];
8230
8231 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
8232 &dirfd, PyUnicode_FSConverter, &opath, &arg))
8233 return NULL;
8234 path = PyBytes_AsString(opath);
8235 if (arg == Py_None) {
8236 /* optional time values not given */
8237 Py_BEGIN_ALLOW_THREADS
8238 res = futimesat(dirfd, path, NULL);
8239 Py_END_ALLOW_THREADS
8240 }
8241 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
8242 PyErr_SetString(PyExc_TypeError,
8243 "futimesat() arg 3 must be a tuple (atime, mtime)");
8244 Py_DECREF(opath);
8245 return NULL;
8246 }
8247 else {
8248 if (extract_time(PyTuple_GET_ITEM(arg, 0),
8249 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
8250 Py_DECREF(opath);
8251 return NULL;
8252 }
8253 if (extract_time(PyTuple_GET_ITEM(arg, 1),
8254 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
8255 Py_DECREF(opath);
8256 return NULL;
8257 }
8258 Py_BEGIN_ALLOW_THREADS
8259 res = futimesat(dirfd, path, buf);
8260 Py_END_ALLOW_THREADS
8261 }
8262 Py_DECREF(opath);
8263 if (res < 0) {
8264 return posix_error();
8265 }
8266 Py_RETURN_NONE;
8267}
8268#endif
8269
8270#ifdef HAVE_LINKAT
8271PyDoc_STRVAR(posix_linkat__doc__,
8272"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
8273Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
8274and if dstpath is relative, it is taken as relative to dstfd.\n\
8275flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
8276If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
8277srcpath is interpreted relative to the current working directory. This\n\
8278also applies for dstpath.");
8279
8280static PyObject *
8281posix_linkat(PyObject *self, PyObject *args)
8282{
8283 PyObject *osrc, *odst;
8284 char *src, *dst;
8285 int res, srcfd, dstfd;
8286 int flags = 0;
8287
8288 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
8289 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
8290 return NULL;
8291 src = PyBytes_AsString(osrc);
8292 dst = PyBytes_AsString(odst);
8293 Py_BEGIN_ALLOW_THREADS
8294 res = linkat(srcfd, src, dstfd, dst, flags);
8295 Py_END_ALLOW_THREADS
8296 Py_DECREF(osrc);
8297 Py_DECREF(odst);
8298 if (res < 0)
8299 return posix_error();
8300 Py_RETURN_NONE;
8301}
8302#endif /* HAVE_LINKAT */
8303
8304#ifdef HAVE_MKDIRAT
8305PyDoc_STRVAR(posix_mkdirat__doc__,
8306"mkdirat(dirfd, path, mode=0o777)\n\n\
8307Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
8308If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8309is interpreted relative to the current working directory.");
8310
8311static PyObject *
8312posix_mkdirat(PyObject *self, PyObject *args)
8313{
8314 int res, dirfd;
8315 PyObject *opath;
8316 char *path;
8317 int mode = 0777;
8318
8319 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
8320 &dirfd, PyUnicode_FSConverter, &opath, &mode))
8321 return NULL;
8322 path = PyBytes_AsString(opath);
8323 Py_BEGIN_ALLOW_THREADS
8324 res = mkdirat(dirfd, path, mode);
8325 Py_END_ALLOW_THREADS
8326 Py_DECREF(opath);
8327 if (res < 0)
8328 return posix_error();
8329 Py_RETURN_NONE;
8330}
8331#endif
8332
8333#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
8334PyDoc_STRVAR(posix_mknodat__doc__,
8335"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
8336Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
8337If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8338is interpreted relative to the current working directory.");
8339
8340static PyObject *
8341posix_mknodat(PyObject *self, PyObject *args)
8342{
8343 PyObject *opath;
8344 char *filename;
8345 int mode = 0600;
8346 int device = 0;
8347 int res, dirfd;
8348 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
8349 PyUnicode_FSConverter, &opath, &mode, &device))
8350 return NULL;
8351 filename = PyBytes_AS_STRING(opath);
8352 Py_BEGIN_ALLOW_THREADS
8353 res = mknodat(dirfd, filename, mode, device);
8354 Py_END_ALLOW_THREADS
8355 Py_DECREF(opath);
8356 if (res < 0)
8357 return posix_error();
8358 Py_RETURN_NONE;
8359}
8360#endif
8361
8362#ifdef HAVE_OPENAT
8363PyDoc_STRVAR(posix_openat__doc__,
8364"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
8365Like open() but if path is relative, it is taken as relative to dirfd.\n\
8366If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8367is interpreted relative to the current working directory.");
8368
8369static PyObject *
8370posix_openat(PyObject *self, PyObject *args)
8371{
8372 PyObject *ofile;
8373 char *file;
8374 int flag, dirfd, fd;
8375 int mode = 0777;
8376
8377 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
8378 &dirfd, PyUnicode_FSConverter, &ofile,
8379 &flag, &mode))
8380 return NULL;
8381 file = PyBytes_AsString(ofile);
8382 Py_BEGIN_ALLOW_THREADS
8383 fd = openat(dirfd, file, flag, mode);
8384 Py_END_ALLOW_THREADS
8385 Py_DECREF(ofile);
8386 if (fd < 0)
8387 return posix_error();
8388 return PyLong_FromLong((long)fd);
8389}
8390#endif
8391
8392#ifdef HAVE_READLINKAT
8393PyDoc_STRVAR(posix_readlinkat__doc__,
8394"readlinkat(dirfd, path) -> path\n\n\
8395Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
8396If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8397is interpreted relative to the current working directory.");
8398
8399static PyObject *
8400posix_readlinkat(PyObject *self, PyObject *args)
8401{
8402 PyObject *v, *opath;
8403 char buf[MAXPATHLEN];
8404 char *path;
8405 int n, dirfd;
8406 int arg_is_unicode = 0;
8407
8408 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
8409 &dirfd, PyUnicode_FSConverter, &opath))
8410 return NULL;
8411 path = PyBytes_AsString(opath);
8412 v = PySequence_GetItem(args, 1);
8413 if (v == NULL) {
8414 Py_DECREF(opath);
8415 return NULL;
8416 }
8417
8418 if (PyUnicode_Check(v)) {
8419 arg_is_unicode = 1;
8420 }
8421 Py_DECREF(v);
8422
8423 Py_BEGIN_ALLOW_THREADS
8424 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
8425 Py_END_ALLOW_THREADS
8426 Py_DECREF(opath);
8427 if (n < 0)
8428 return posix_error();
8429
8430 if (arg_is_unicode)
8431 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
8432 else
8433 return PyBytes_FromStringAndSize(buf, n);
8434}
8435#endif /* HAVE_READLINKAT */
8436
8437#ifdef HAVE_RENAMEAT
8438PyDoc_STRVAR(posix_renameat__doc__,
8439"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
8440Like rename() but if oldpath is relative, it is taken as relative to\n\
8441olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
8442If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
8443oldpath is interpreted relative to the current working directory. This\n\
8444also applies for newpath.");
8445
8446static PyObject *
8447posix_renameat(PyObject *self, PyObject *args)
8448{
8449 int res;
8450 PyObject *opathold, *opathnew;
8451 char *opath, *npath;
8452 int oldfd, newfd;
8453
8454 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
8455 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
8456 return NULL;
8457 opath = PyBytes_AsString(opathold);
8458 npath = PyBytes_AsString(opathnew);
8459 Py_BEGIN_ALLOW_THREADS
8460 res = renameat(oldfd, opath, newfd, npath);
8461 Py_END_ALLOW_THREADS
8462 Py_DECREF(opathold);
8463 Py_DECREF(opathnew);
8464 if (res < 0)
8465 return posix_error();
8466 Py_RETURN_NONE;
8467}
8468#endif
8469
8470#if HAVE_SYMLINKAT
8471PyDoc_STRVAR(posix_symlinkat__doc__,
8472"symlinkat(src, dstfd, dst)\n\n\
8473Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
8474If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
8475is interpreted relative to the current working directory.");
8476
8477static PyObject *
8478posix_symlinkat(PyObject *self, PyObject *args)
8479{
8480 int res, dstfd;
8481 PyObject *osrc, *odst;
8482 char *src, *dst;
8483
8484 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
8485 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
8486 return NULL;
8487 src = PyBytes_AsString(osrc);
8488 dst = PyBytes_AsString(odst);
8489 Py_BEGIN_ALLOW_THREADS
8490 res = symlinkat(src, dstfd, dst);
8491 Py_END_ALLOW_THREADS
8492 Py_DECREF(osrc);
8493 Py_DECREF(odst);
8494 if (res < 0)
8495 return posix_error();
8496 Py_RETURN_NONE;
8497}
8498#endif /* HAVE_SYMLINKAT */
8499
8500#ifdef HAVE_UNLINKAT
8501PyDoc_STRVAR(posix_unlinkat__doc__,
8502"unlinkat(dirfd, path, flags=0)\n\n\
8503Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
8504flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
8505specified, unlinkat() behaves like rmdir().\n\
8506If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8507is interpreted relative to the current working directory.");
8508
8509static PyObject *
8510posix_unlinkat(PyObject *self, PyObject *args)
8511{
8512 int dirfd, res, flags = 0;
8513 PyObject *opath;
8514 char *path;
8515
8516 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
8517 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8518 return NULL;
8519 path = PyBytes_AsString(opath);
8520 Py_BEGIN_ALLOW_THREADS
8521 res = unlinkat(dirfd, path, flags);
8522 Py_END_ALLOW_THREADS
8523 Py_DECREF(opath);
8524 if (res < 0)
8525 return posix_error();
8526 Py_RETURN_NONE;
8527}
8528#endif
8529
8530#ifdef HAVE_UTIMENSAT
8531PyDoc_STRVAR(posix_utimensat__doc__,
8532"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
8533 (mtime_sec, mtime_nsec), flags)\n\
8534utimensat(dirfd, path, None, None, flags)\n\n\
8535Updates the timestamps of a file with nanosecond precision. If path is\n\
8536relative, it is taken as relative to dirfd.\n\
8537The second form sets atime and mtime to the current time.\n\
8538flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8539If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8540is interpreted relative to the current working directory.\n\
8541If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
8542current time.\n\
8543If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
8544
8545static PyObject *
8546posix_utimensat(PyObject *self, PyObject *args)
8547{
8548 PyObject *opath;
8549 char *path;
8550 int res, dirfd, flags = 0;
8551 PyObject *atime, *mtime;
8552
8553 struct timespec buf[2];
8554
8555 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
8556 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
8557 return NULL;
8558 path = PyBytes_AsString(opath);
8559 if (atime == Py_None && mtime == Py_None) {
8560 /* optional time values not given */
8561 Py_BEGIN_ALLOW_THREADS
8562 res = utimensat(dirfd, path, NULL, flags);
8563 Py_END_ALLOW_THREADS
8564 }
8565 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
8566 PyErr_SetString(PyExc_TypeError,
8567 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
8568 Py_DECREF(opath);
8569 return NULL;
8570 }
8571 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
8572 PyErr_SetString(PyExc_TypeError,
8573 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
8574 Py_DECREF(opath);
8575 return NULL;
8576 }
8577 else {
8578 if (!PyArg_ParseTuple(atime, "ll:utimensat",
8579 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
8580 Py_DECREF(opath);
8581 return NULL;
8582 }
8583 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
8584 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
8585 Py_DECREF(opath);
8586 return NULL;
8587 }
8588 Py_BEGIN_ALLOW_THREADS
8589 res = utimensat(dirfd, path, buf, flags);
8590 Py_END_ALLOW_THREADS
8591 }
8592 Py_DECREF(opath);
8593 if (res < 0) {
8594 return posix_error();
8595 }
8596 Py_RETURN_NONE;
8597}
8598#endif
8599
8600#ifdef HAVE_MKFIFOAT
8601PyDoc_STRVAR(posix_mkfifoat__doc__,
8602"mkfifoat(dirfd, path, mode=0o666)\n\n\
8603Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
8604If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8605is interpreted relative to the current working directory.");
8606
8607static PyObject *
8608posix_mkfifoat(PyObject *self, PyObject *args)
8609{
8610 PyObject *opath;
8611 char *filename;
8612 int mode = 0666;
8613 int res, dirfd;
8614 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
8615 &dirfd, PyUnicode_FSConverter, &opath, &mode))
8616 return NULL;
8617 filename = PyBytes_AS_STRING(opath);
8618 Py_BEGIN_ALLOW_THREADS
8619 res = mkfifoat(dirfd, filename, mode);
8620 Py_END_ALLOW_THREADS
8621 Py_DECREF(opath);
8622 if (res < 0)
8623 return posix_error();
8624 Py_RETURN_NONE;
8625}
8626#endif
8627
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008628static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008630#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008631 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008632#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008633 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00008634#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00008636#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00008637 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00008638#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00008640#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008641#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008643#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00008644#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00008645 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00008646#endif /* HAVE_LCHMOD */
8647#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008648 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00008649#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00008650#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008651 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00008652#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008653#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008654 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008655#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008656#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00008657 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008658#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008659#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00008660 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008661#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008662#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00008663 {"getcwd", (PyCFunction)posix_getcwd_unicode,
8664 METH_NOARGS, posix_getcwd__doc__},
8665 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
8666 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008667#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008668#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00008669 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008670#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00008672#ifdef HAVE_FDOPENDIR
8673 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
8674#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008675 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8676 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008677#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00008678 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008679#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00008680#ifdef HAVE_GETPRIORITY
8681 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
8682#endif /* HAVE_GETPRIORITY */
8683#ifdef HAVE_SETPRIORITY
8684 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
8685#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008686#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00008687 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008688#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00008689#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00008690 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00008691#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00008692 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8693 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8694 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00008695 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00008696#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00008697 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008698#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00008699#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008700 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00008701 win_symlink__doc__},
8702#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008703#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008705#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008706 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008707#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008708 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008709#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00008710 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8711 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8712 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008713#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00008714 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008715#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00008716 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008717#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00008718 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8719 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008720#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008721#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8723 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008724#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008725 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8726 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008727#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008728#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008729#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00008730 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008731#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008732#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00008733 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008734#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008735#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00008736 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008737#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008738#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008739 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008740#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008741#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008742 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008743#endif /* HAVE_GETEGID */
8744#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008745 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008746#endif /* HAVE_GETEUID */
8747#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008748 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008749#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008750#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008751 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008752#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008753 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008754#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008756#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008757#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008759#endif /* HAVE_GETPPID */
8760#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008761 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008762#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008763#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008764 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008765#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008766#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00008767 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008768#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008769#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00008770 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008771#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008772#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008773 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008774#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00008775#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8777 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00008778 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00008779#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008780#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008781 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008782#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008783#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008784 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008785#endif /* HAVE_SETEUID */
8786#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008787 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008788#endif /* HAVE_SETEGID */
8789#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008790 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008791#endif /* HAVE_SETREUID */
8792#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008793 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008794#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008795#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008796 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008797#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008798#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008799 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008800#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00008801#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008802 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00008803#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008804#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008805 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008806#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008807#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008808 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008809#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008810#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008811 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008812#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008813#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00008814 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008815#endif /* HAVE_WAIT3 */
8816#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00008817 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008818#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008819#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008820 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008821#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008822#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00008823 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008824#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008825#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00008826 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008827#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008828#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008829 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008830#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008831#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008832 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008833#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008834#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008835 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008836#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8838 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8839 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8840 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
8841 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8842 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8843 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8844 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8845 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008846#ifdef HAVE_SENDFILE
8847 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
8848 posix_sendfile__doc__},
8849#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008850 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8851 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008852#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008853 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008854#endif
8855#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00008856 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008857#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008858#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00008859 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008860#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008861#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00008862 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8863 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8864 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008865#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008866#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00008867 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008868#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008869#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008870 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008871#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008872#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008874#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008875 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008876#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00008877 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008878#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008879#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008880 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008881#endif
8882#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008883 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008884#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008885#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008886#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008888#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008889#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008890 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008891#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008892#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008893 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008894#endif /* WIFSTOPPED */
8895#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008896 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008897#endif /* WIFSIGNALED */
8898#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008899 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008900#endif /* WIFEXITED */
8901#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008902 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008903#endif /* WEXITSTATUS */
8904#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008905 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008906#endif /* WTERMSIG */
8907#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008908 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008909#endif /* WSTOPSIG */
8910#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008911#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008912 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008913#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008914#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008915 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008916#endif
Fred Drakec9680921999-12-13 16:37:25 +00008917#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008918 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008919#endif
8920#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008921 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008922#endif
8923#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008924 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008925#endif
8926#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008928#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008929 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008930#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008931 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008932 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008933 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008934#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008935#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008936 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008937#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008938 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008939 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008940 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008941 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008942 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008943 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008944#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008945 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008946#endif
8947#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008948 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008949#endif
8950#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008951 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008952#endif
8953#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008954 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008955#endif
8956
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008957/* posix *at family of functions */
8958#ifdef HAVE_FACCESSAT
8959 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
8960#endif
8961#ifdef HAVE_FCHMODAT
8962 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
8963#endif /* HAVE_FCHMODAT */
8964#ifdef HAVE_FCHOWNAT
8965 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
8966#endif /* HAVE_FCHOWNAT */
8967#ifdef HAVE_FSTATAT
8968 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
8969#endif
8970#ifdef HAVE_FUTIMESAT
8971 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
8972#endif
8973#ifdef HAVE_LINKAT
8974 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
8975#endif /* HAVE_LINKAT */
8976#ifdef HAVE_MKDIRAT
8977 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
8978#endif
8979#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
8980 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
8981#endif
8982#ifdef HAVE_OPENAT
8983 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
8984#endif
8985#ifdef HAVE_READLINKAT
8986 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
8987#endif /* HAVE_READLINKAT */
8988#ifdef HAVE_RENAMEAT
8989 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
8990#endif
8991#if HAVE_SYMLINKAT
8992 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
8993#endif /* HAVE_SYMLINKAT */
8994#ifdef HAVE_UNLINKAT
8995 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
8996#endif
8997#ifdef HAVE_UTIMENSAT
8998 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
8999#endif
9000#ifdef HAVE_MKFIFOAT
9001 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
9002#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009003 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009004};
9005
9006
Barry Warsaw4a342091996-12-19 23:50:02 +00009007static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009008ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009009{
Victor Stinner8c62be82010-05-06 00:08:46 +00009010 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009011}
9012
Guido van Rossumd48f2521997-12-05 22:19:34 +00009013#if defined(PYOS_OS2)
9014/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009015static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009016{
9017 APIRET rc;
9018 ULONG values[QSV_MAX+1];
9019 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009020 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009021
9022 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009023 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009024 Py_END_ALLOW_THREADS
9025
9026 if (rc != NO_ERROR) {
9027 os2_error(rc);
9028 return -1;
9029 }
9030
Fred Drake4d1e64b2002-04-15 19:40:07 +00009031 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9032 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9033 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9034 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9035 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9036 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9037 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009038
9039 switch (values[QSV_VERSION_MINOR]) {
9040 case 0: ver = "2.00"; break;
9041 case 10: ver = "2.10"; break;
9042 case 11: ver = "2.11"; break;
9043 case 30: ver = "3.00"; break;
9044 case 40: ver = "4.00"; break;
9045 case 50: ver = "5.00"; break;
9046 default:
Tim Peters885d4572001-11-28 20:27:42 +00009047 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00009048 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009049 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009050 ver = &tmp[0];
9051 }
9052
9053 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009054 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009055 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009056
9057 /* Add Indicator of Which Drive was Used to Boot the System */
9058 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9059 tmp[1] = ':';
9060 tmp[2] = '\0';
9061
Fred Drake4d1e64b2002-04-15 19:40:07 +00009062 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009063}
9064#endif
9065
Brian Curtin52173d42010-12-02 18:29:18 +00009066#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009067static int
Brian Curtin52173d42010-12-02 18:29:18 +00009068enable_symlink()
9069{
9070 HANDLE tok;
9071 TOKEN_PRIVILEGES tok_priv;
9072 LUID luid;
9073 int meth_idx = 0;
9074
9075 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009076 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009077
9078 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009079 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009080
9081 tok_priv.PrivilegeCount = 1;
9082 tok_priv.Privileges[0].Luid = luid;
9083 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
9084
9085 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
9086 sizeof(TOKEN_PRIVILEGES),
9087 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009088 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009089
Brian Curtin3b4499c2010-12-28 14:31:47 +00009090 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
9091 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00009092}
9093#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
9094
Barry Warsaw4a342091996-12-19 23:50:02 +00009095static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009096all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009097{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009098#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009099 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009100#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009101#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009102 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009103#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009104#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009105 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009106#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009107#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009108 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009109#endif
Fred Drakec9680921999-12-13 16:37:25 +00009110#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009111 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009112#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009113#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009114 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009115#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009116#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009117 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009118#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009119#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00009120 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009121#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009122#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00009123 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009124#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009125#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009126 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009127#endif
9128#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009129 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009130#endif
9131#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00009132 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009133#endif
9134#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00009135 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009136#endif
9137#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009138 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009139#endif
9140#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00009141 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009142#endif
9143#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009144 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009145#endif
9146#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009147 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009148#endif
9149#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009150 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009151#endif
9152#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009153 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009154#endif
9155#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009156 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009157#endif
9158#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00009159 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009160#endif
9161#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009162 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009163#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009164#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009165 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009166#endif
9167#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00009168 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009169#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009170#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009171 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009172#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009173#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009174 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009175#endif
9176#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009177 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009178#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009179#ifdef PRIO_PROCESS
9180 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
9181#endif
9182#ifdef PRIO_PGRP
9183 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
9184#endif
9185#ifdef PRIO_USER
9186 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
9187#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009188/* posix - constants for *at functions */
9189#ifdef AT_SYMLINK_NOFOLLOW
9190 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
9191#endif
9192#ifdef AT_EACCESS
9193 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
9194#endif
9195#ifdef AT_FDCWD
9196 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
9197#endif
9198#ifdef AT_REMOVEDIR
9199 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
9200#endif
9201#ifdef AT_SYMLINK_FOLLOW
9202 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
9203#endif
9204#ifdef UTIME_NOW
9205 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
9206#endif
9207#ifdef UTIME_OMIT
9208 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
9209#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009210
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009211
Tim Peters5aa91602002-01-30 05:46:57 +00009212/* MS Windows */
9213#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009214 /* Don't inherit in child processes. */
9215 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009216#endif
9217#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00009218 /* Optimize for short life (keep in memory). */
9219 /* MS forgot to define this one with a non-underscore form too. */
9220 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009221#endif
9222#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009223 /* Automatically delete when last handle is closed. */
9224 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009225#endif
9226#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00009227 /* Optimize for random access. */
9228 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009229#endif
9230#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009231 /* Optimize for sequential access. */
9232 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009233#endif
9234
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009235/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009236#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009237 /* Send a SIGIO signal whenever input or output
9238 becomes available on file descriptor */
9239 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009240#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009241#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009242 /* Direct disk access. */
9243 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009244#endif
9245#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00009246 /* Must be a directory. */
9247 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009248#endif
9249#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00009250 /* Do not follow links. */
9251 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009252#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009253#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009254 /* Do not update the access time. */
9255 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009256#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009257
Victor Stinner8c62be82010-05-06 00:08:46 +00009258 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009259#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009260 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009261#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009262#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009263 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009264#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009265#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009266 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009267#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009268#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009269 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009270#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009271#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00009272 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009273#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009274#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00009275 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009276#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009277#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009278 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009279#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009280#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00009281 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009282#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009283#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009284 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009285#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009286#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009287 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009288#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009289#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009290 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009291#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009292#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009293 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009294#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009295#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00009296 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009297#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009298#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00009299 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009300#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009301#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009302 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009303#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009304#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009306#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009307#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00009308 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009309#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009310
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009311 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009312#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009313 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009314#endif /* ST_RDONLY */
9315#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009316 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009317#endif /* ST_NOSUID */
9318
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009319 /* FreeBSD sendfile() constants */
9320#ifdef SF_NODISKIO
9321 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
9322#endif
9323#ifdef SF_MNOWAIT
9324 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
9325#endif
9326#ifdef SF_SYNC
9327 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
9328#endif
9329
Guido van Rossum246bc171999-02-01 23:54:31 +00009330#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009331#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00009332 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
9333 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
9334 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
9335 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
9336 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
9337 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
9338 if (ins(d, "P_PM", (long)P_PM)) return -1;
9339 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
9340 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
9341 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
9342 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
9343 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
9344 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
9345 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
9346 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
9347 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
9348 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
9349 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
9350 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
9351 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009352#else
Victor Stinner8c62be82010-05-06 00:08:46 +00009353 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
9354 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
9355 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
9356 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
9357 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00009358#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009359#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00009360
Guido van Rossumd48f2521997-12-05 22:19:34 +00009361#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009363#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009364 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00009365}
9366
9367
Tim Peters5aa91602002-01-30 05:46:57 +00009368#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00009369#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009370#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009371
9372#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00009373#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009374#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009375
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009376#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00009377#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009378#define MODNAME "posix"
9379#endif
9380
Martin v. Löwis1a214512008-06-11 05:26:20 +00009381static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009382 PyModuleDef_HEAD_INIT,
9383 MODNAME,
9384 posix__doc__,
9385 -1,
9386 posix_methods,
9387 NULL,
9388 NULL,
9389 NULL,
9390 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00009391};
9392
9393
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009394PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009395INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009396{
Victor Stinner8c62be82010-05-06 00:08:46 +00009397 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009398
Brian Curtin52173d42010-12-02 18:29:18 +00009399#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009400 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00009401#endif
9402
Victor Stinner8c62be82010-05-06 00:08:46 +00009403 m = PyModule_Create(&posixmodule);
9404 if (m == NULL)
9405 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00009406
Victor Stinner8c62be82010-05-06 00:08:46 +00009407 /* Initialize environ dictionary */
9408 v = convertenviron();
9409 Py_XINCREF(v);
9410 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9411 return NULL;
9412 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009413
Victor Stinner8c62be82010-05-06 00:08:46 +00009414 if (all_ins(m))
9415 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00009416
Victor Stinner8c62be82010-05-06 00:08:46 +00009417 if (setup_confname_tables(m))
9418 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00009419
Victor Stinner8c62be82010-05-06 00:08:46 +00009420 Py_INCREF(PyExc_OSError);
9421 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009422
Guido van Rossumb3d39562000-01-31 18:41:26 +00009423#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009424 if (posix_putenv_garbage == NULL)
9425 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009426#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009427
Victor Stinner8c62be82010-05-06 00:08:46 +00009428 if (!initialized) {
9429 stat_result_desc.name = MODNAME ".stat_result";
9430 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9431 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9432 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9433 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9434 structseq_new = StatResultType.tp_new;
9435 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009436
Victor Stinner8c62be82010-05-06 00:08:46 +00009437 statvfs_result_desc.name = MODNAME ".statvfs_result";
9438 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00009439#ifdef NEED_TICKS_PER_SECOND
9440# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00009441 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00009442# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00009443 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00009444# else
Victor Stinner8c62be82010-05-06 00:08:46 +00009445 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00009446# endif
9447#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009448 }
9449 Py_INCREF((PyObject*) &StatResultType);
9450 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9451 Py_INCREF((PyObject*) &StatVFSResultType);
9452 PyModule_AddObject(m, "statvfs_result",
9453 (PyObject*) &StatVFSResultType);
9454 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00009455
9456#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00009457 /*
9458 * Step 2 of weak-linking support on Mac OS X.
9459 *
9460 * The code below removes functions that are not available on the
9461 * currently active platform.
9462 *
9463 * This block allow one to use a python binary that was build on
9464 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9465 * OSX 10.4.
9466 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00009467#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00009468 if (fstatvfs == NULL) {
9469 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9470 return NULL;
9471 }
9472 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00009473#endif /* HAVE_FSTATVFS */
9474
9475#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00009476 if (statvfs == NULL) {
9477 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9478 return NULL;
9479 }
9480 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00009481#endif /* HAVE_STATVFS */
9482
9483# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009484 if (lchown == NULL) {
9485 if (PyObject_DelAttrString(m, "lchown") == -1) {
9486 return NULL;
9487 }
9488 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00009489#endif /* HAVE_LCHOWN */
9490
9491
9492#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00009493 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00009494
Guido van Rossumb6775db1994-08-01 11:34:53 +00009495}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009496
9497#ifdef __cplusplus
9498}
9499#endif