blob: aace2fed0bd69e59b645fe3ef16bbec436e37cec [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
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000372 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000373#endif
374 if (PyErr_Occurred())
375 return 0;
376 return 1;
377}
378
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000379#if defined _MSC_VER && _MSC_VER >= 1400
380/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
381 * valid and throw an assertion if it isn't.
382 * Normally, an invalid fd is likely to be a C program error and therefore
383 * an assertion can be useful, but it does contradict the POSIX standard
384 * which for write(2) states:
385 * "Otherwise, -1 shall be returned and errno set to indicate the error."
386 * "[EBADF] The fildes argument is not a valid file descriptor open for
387 * writing."
388 * Furthermore, python allows the user to enter any old integer
389 * as a fd and should merely raise a python exception on error.
390 * The Microsoft CRT doesn't provide an official way to check for the
391 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000392 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000393 * internal structures involved.
394 * The structures below must be updated for each version of visual studio
395 * according to the file internal.h in the CRT source, until MS comes
396 * up with a less hacky way to do this.
397 * (all of this is to avoid globally modifying the CRT behaviour using
398 * _set_invalid_parameter_handler() and _CrtSetReportMode())
399 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000400/* The actual size of the structure is determined at runtime.
401 * Only the first items must be present.
402 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000403typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000404 intptr_t osfhnd;
405 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000406} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000407
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000408extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000409#define IOINFO_L2E 5
410#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
411#define IOINFO_ARRAYS 64
412#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
413#define FOPEN 0x01
414#define _NO_CONSOLE_FILENO (intptr_t)-2
415
416/* This function emulates what the windows CRT does to validate file handles */
417int
418_PyVerify_fd(int fd)
419{
Victor Stinner8c62be82010-05-06 00:08:46 +0000420 const int i1 = fd >> IOINFO_L2E;
421 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000422
Antoine Pitrou22e41552010-08-15 18:07:50 +0000423 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000424
Victor Stinner8c62be82010-05-06 00:08:46 +0000425 /* Determine the actual size of the ioinfo structure,
426 * as used by the CRT loaded in memory
427 */
428 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
429 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
430 }
431 if (sizeof_ioinfo == 0) {
432 /* This should not happen... */
433 goto fail;
434 }
435
436 /* See that it isn't a special CLEAR fileno */
437 if (fd != _NO_CONSOLE_FILENO) {
438 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
439 * we check pointer validity and other info
440 */
441 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
442 /* finally, check that the file is open */
443 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
444 if (info->osfile & FOPEN) {
445 return 1;
446 }
447 }
448 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000449 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000450 errno = EBADF;
451 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000452}
453
454/* the special case of checking dup2. The target fd must be in a sensible range */
455static int
456_PyVerify_fd_dup2(int fd1, int fd2)
457{
Victor Stinner8c62be82010-05-06 00:08:46 +0000458 if (!_PyVerify_fd(fd1))
459 return 0;
460 if (fd2 == _NO_CONSOLE_FILENO)
461 return 0;
462 if ((unsigned)fd2 < _NHANDLE_)
463 return 1;
464 else
465 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000466}
467#else
468/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
469#define _PyVerify_fd_dup2(A, B) (1)
470#endif
471
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000472#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000473/* The following structure was copied from
474 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
475 include doesn't seem to be present in the Windows SDK (at least as included
476 with Visual Studio Express). */
477typedef struct _REPARSE_DATA_BUFFER {
478 ULONG ReparseTag;
479 USHORT ReparseDataLength;
480 USHORT Reserved;
481 union {
482 struct {
483 USHORT SubstituteNameOffset;
484 USHORT SubstituteNameLength;
485 USHORT PrintNameOffset;
486 USHORT PrintNameLength;
487 ULONG Flags;
488 WCHAR PathBuffer[1];
489 } SymbolicLinkReparseBuffer;
490
491 struct {
492 USHORT SubstituteNameOffset;
493 USHORT SubstituteNameLength;
494 USHORT PrintNameOffset;
495 USHORT PrintNameLength;
496 WCHAR PathBuffer[1];
497 } MountPointReparseBuffer;
498
499 struct {
500 UCHAR DataBuffer[1];
501 } GenericReparseBuffer;
502 };
503} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
504
505#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
506 GenericReparseBuffer)
507#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
508
509static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000510win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000511{
512 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
513 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
514 DWORD n_bytes_returned;
515 const wchar_t *ptr;
516 wchar_t *buf;
517 size_t len;
518
519 if (0 == DeviceIoControl(
520 reparse_point_handle,
521 FSCTL_GET_REPARSE_POINT,
522 NULL, 0, /* in buffer */
523 target_buffer, sizeof(target_buffer),
524 &n_bytes_returned,
525 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000526 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000527
528 if (reparse_tag)
529 *reparse_tag = rdb->ReparseTag;
530
531 if (target_path) {
532 switch (rdb->ReparseTag) {
533 case IO_REPARSE_TAG_SYMLINK:
534 /* XXX: Maybe should use SubstituteName? */
535 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
536 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
537 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
538 break;
539 case IO_REPARSE_TAG_MOUNT_POINT:
540 ptr = rdb->MountPointReparseBuffer.PathBuffer +
541 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
542 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
543 break;
544 default:
545 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000546 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000547 }
548 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
549 if (!buf) {
550 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000551 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000552 }
553 wcsncpy(buf, ptr, len);
554 buf[len] = L'\0';
555 if (wcsncmp(buf, L"\\??\\", 4) == 0)
556 buf[1] = L'\\';
557 *target_path = buf;
558 }
559
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000560 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000561}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000562#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000563
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000565#ifdef WITH_NEXT_FRAMEWORK
566/* On Darwin/MacOSX a shared library or framework has no access to
567** environ directly, we must obtain it with _NSGetEnviron().
568*/
569#include <crt_externs.h>
570static char **environ;
571#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000573#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574
Barry Warsaw53699e91996-12-10 23:23:01 +0000575static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000576convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577{
Victor Stinner8c62be82010-05-06 00:08:46 +0000578 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000579#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000580 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000581#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000582 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000583#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000584#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000585 APIRET rc;
586 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
587#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000588
Victor Stinner8c62be82010-05-06 00:08:46 +0000589 d = PyDict_New();
590 if (d == NULL)
591 return NULL;
592#ifdef WITH_NEXT_FRAMEWORK
593 if (environ == NULL)
594 environ = *_NSGetEnviron();
595#endif
596#ifdef MS_WINDOWS
597 /* _wenviron must be initialized in this way if the program is started
598 through main() instead of wmain(). */
599 _wgetenv(L"");
600 if (_wenviron == NULL)
601 return d;
602 /* This part ignores errors */
603 for (e = _wenviron; *e != NULL; e++) {
604 PyObject *k;
605 PyObject *v;
606 wchar_t *p = wcschr(*e, L'=');
607 if (p == NULL)
608 continue;
609 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
610 if (k == NULL) {
611 PyErr_Clear();
612 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000613 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000614 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
615 if (v == NULL) {
616 PyErr_Clear();
617 Py_DECREF(k);
618 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000619 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000620 if (PyDict_GetItem(d, k) == NULL) {
621 if (PyDict_SetItem(d, k, v) != 0)
622 PyErr_Clear();
623 }
624 Py_DECREF(k);
625 Py_DECREF(v);
626 }
627#else
628 if (environ == NULL)
629 return d;
630 /* This part ignores errors */
631 for (e = environ; *e != NULL; e++) {
632 PyObject *k;
633 PyObject *v;
634 char *p = strchr(*e, '=');
635 if (p == NULL)
636 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000637 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000638 if (k == NULL) {
639 PyErr_Clear();
640 continue;
641 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000642 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000643 if (v == NULL) {
644 PyErr_Clear();
645 Py_DECREF(k);
646 continue;
647 }
648 if (PyDict_GetItem(d, k) == NULL) {
649 if (PyDict_SetItem(d, k, v) != 0)
650 PyErr_Clear();
651 }
652 Py_DECREF(k);
653 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000654 }
655#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000656#if defined(PYOS_OS2)
657 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
658 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
659 PyObject *v = PyBytes_FromString(buffer);
660 PyDict_SetItemString(d, "BEGINLIBPATH", v);
661 Py_DECREF(v);
662 }
663 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
664 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
665 PyObject *v = PyBytes_FromString(buffer);
666 PyDict_SetItemString(d, "ENDLIBPATH", v);
667 Py_DECREF(v);
668 }
669#endif
670 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671}
672
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673/* Set a POSIX-specific error from errno, and return NULL */
674
Barry Warsawd58d7641998-07-23 16:14:40 +0000675static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000676posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000677{
Victor Stinner8c62be82010-05-06 00:08:46 +0000678 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679}
Barry Warsawd58d7641998-07-23 16:14:40 +0000680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000681posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000682{
Victor Stinner8c62be82010-05-06 00:08:46 +0000683 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000684}
685
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686
Mark Hammondef8b6542001-05-13 08:04:26 +0000687static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000688posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000689{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000690 PyObject *name_str, *rc;
691 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
692 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000693 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000694 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
695 name_str);
696 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000697 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000698}
699
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000700#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000701static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000702win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000703{
Victor Stinner8c62be82010-05-06 00:08:46 +0000704 /* XXX We should pass the function name along in the future.
705 (winreg.c also wants to pass the function name.)
706 This would however require an additional param to the
707 Windows error object, which is non-trivial.
708 */
709 errno = GetLastError();
710 if (filename)
711 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
712 else
713 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000714}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000715
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716static PyObject *
717win32_error_unicode(char* function, Py_UNICODE* filename)
718{
Victor Stinner8c62be82010-05-06 00:08:46 +0000719 /* XXX - see win32_error for comments on 'function' */
720 errno = GetLastError();
721 if (filename)
722 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
723 else
724 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000725}
726
Thomas Wouters477c8d52006-05-27 19:21:47 +0000727static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000728convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729{
Victor Stinner8c62be82010-05-06 00:08:46 +0000730 if (PyUnicode_CheckExact(*param))
731 Py_INCREF(*param);
732 else if (PyUnicode_Check(*param))
733 /* For a Unicode subtype that's not a Unicode object,
734 return a true Unicode object with the same data. */
735 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
736 PyUnicode_GET_SIZE(*param));
737 else
738 *param = PyUnicode_FromEncodedObject(*param,
739 Py_FileSystemDefaultEncoding,
740 "strict");
741 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000742}
743
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000744#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745
Guido van Rossumd48f2521997-12-05 22:19:34 +0000746#if defined(PYOS_OS2)
747/**********************************************************************
748 * Helper Function to Trim and Format OS/2 Messages
749 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000750static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000751os2_formatmsg(char *msgbuf, int msglen, char *reason)
752{
753 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
754
755 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
756 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
757
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000758 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000759 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
760 }
761
762 /* Add Optional Reason Text */
763 if (reason) {
764 strcat(msgbuf, " : ");
765 strcat(msgbuf, reason);
766 }
767}
768
769/**********************************************************************
770 * Decode an OS/2 Operating System Error Code
771 *
772 * A convenience function to lookup an OS/2 error code and return a
773 * text message we can use to raise a Python exception.
774 *
775 * Notes:
776 * The messages for errors returned from the OS/2 kernel reside in
777 * the file OSO001.MSG in the \OS2 directory hierarchy.
778 *
779 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000780static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000781os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
782{
783 APIRET rc;
784 ULONG msglen;
785
786 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
787 Py_BEGIN_ALLOW_THREADS
788 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
789 errorcode, "oso001.msg", &msglen);
790 Py_END_ALLOW_THREADS
791
792 if (rc == NO_ERROR)
793 os2_formatmsg(msgbuf, msglen, reason);
794 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000795 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000796 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000797
798 return msgbuf;
799}
800
801/* Set an OS/2-specific error and return NULL. OS/2 kernel
802 errors are not in a global variable e.g. 'errno' nor are
803 they congruent with posix error numbers. */
804
Victor Stinner8c62be82010-05-06 00:08:46 +0000805static PyObject *
806os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000807{
808 char text[1024];
809 PyObject *v;
810
811 os2_strerror(text, sizeof(text), code, "");
812
813 v = Py_BuildValue("(is)", code, text);
814 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000815 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000816 Py_DECREF(v);
817 }
818 return NULL; /* Signal to Python that an Exception is Pending */
819}
820
821#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822
823/* POSIX generic methods */
824
Barry Warsaw53699e91996-12-10 23:23:01 +0000825static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000826posix_fildes(PyObject *fdobj, int (*func)(int))
827{
Victor Stinner8c62be82010-05-06 00:08:46 +0000828 int fd;
829 int res;
830 fd = PyObject_AsFileDescriptor(fdobj);
831 if (fd < 0)
832 return NULL;
833 if (!_PyVerify_fd(fd))
834 return posix_error();
835 Py_BEGIN_ALLOW_THREADS
836 res = (*func)(fd);
837 Py_END_ALLOW_THREADS
838 if (res < 0)
839 return posix_error();
840 Py_INCREF(Py_None);
841 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000842}
Guido van Rossum21142a01999-01-08 21:05:37 +0000843
844static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000845posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000846{
Victor Stinner8c62be82010-05-06 00:08:46 +0000847 PyObject *opath1 = NULL;
848 char *path1;
849 int res;
850 if (!PyArg_ParseTuple(args, format,
851 PyUnicode_FSConverter, &opath1))
852 return NULL;
853 path1 = PyBytes_AsString(opath1);
854 Py_BEGIN_ALLOW_THREADS
855 res = (*func)(path1);
856 Py_END_ALLOW_THREADS
857 if (res < 0)
858 return posix_error_with_allocated_filename(opath1);
859 Py_DECREF(opath1);
860 Py_INCREF(Py_None);
861 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862}
863
Barry Warsaw53699e91996-12-10 23:23:01 +0000864static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000865posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000866 char *format,
867 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868{
Victor Stinner8c62be82010-05-06 00:08:46 +0000869 PyObject *opath1 = NULL, *opath2 = NULL;
870 char *path1, *path2;
871 int res;
872 if (!PyArg_ParseTuple(args, format,
873 PyUnicode_FSConverter, &opath1,
874 PyUnicode_FSConverter, &opath2)) {
875 return NULL;
876 }
877 path1 = PyBytes_AsString(opath1);
878 path2 = PyBytes_AsString(opath2);
879 Py_BEGIN_ALLOW_THREADS
880 res = (*func)(path1, path2);
881 Py_END_ALLOW_THREADS
882 Py_DECREF(opath1);
883 Py_DECREF(opath2);
884 if (res != 0)
885 /* XXX how to report both path1 and path2??? */
886 return posix_error();
887 Py_INCREF(Py_None);
888 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889}
890
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000891#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000893win32_1str(PyObject* args, char* func,
894 char* format, BOOL (__stdcall *funcA)(LPCSTR),
895 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896{
Victor Stinner8c62be82010-05-06 00:08:46 +0000897 PyObject *uni;
898 char *ansi;
899 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000900
Victor Stinner8c62be82010-05-06 00:08:46 +0000901 if (!PyArg_ParseTuple(args, wformat, &uni))
902 PyErr_Clear();
903 else {
904 Py_BEGIN_ALLOW_THREADS
905 result = funcW(PyUnicode_AsUnicode(uni));
906 Py_END_ALLOW_THREADS
907 if (!result)
908 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
909 Py_INCREF(Py_None);
910 return Py_None;
911 }
912 if (!PyArg_ParseTuple(args, format, &ansi))
913 return NULL;
914 Py_BEGIN_ALLOW_THREADS
915 result = funcA(ansi);
916 Py_END_ALLOW_THREADS
917 if (!result)
918 return win32_error(func, ansi);
919 Py_INCREF(Py_None);
920 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921
922}
923
924/* This is a reimplementation of the C library's chdir function,
925 but one that produces Win32 errors instead of DOS error codes.
926 chdir is essentially a wrapper around SetCurrentDirectory; however,
927 it also needs to set "magic" environment variables indicating
928 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000929static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930win32_chdir(LPCSTR path)
931{
Victor Stinner8c62be82010-05-06 00:08:46 +0000932 char new_path[MAX_PATH+1];
933 int result;
934 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935
Victor Stinner8c62be82010-05-06 00:08:46 +0000936 if(!SetCurrentDirectoryA(path))
937 return FALSE;
938 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
939 if (!result)
940 return FALSE;
941 /* In the ANSI API, there should not be any paths longer
942 than MAX_PATH. */
943 assert(result <= MAX_PATH+1);
944 if (strncmp(new_path, "\\\\", 2) == 0 ||
945 strncmp(new_path, "//", 2) == 0)
946 /* UNC path, nothing to do. */
947 return TRUE;
948 env[1] = new_path[0];
949 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000950}
951
952/* The Unicode version differs from the ANSI version
953 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000954static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955win32_wchdir(LPCWSTR path)
956{
Victor Stinner8c62be82010-05-06 00:08:46 +0000957 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
958 int result;
959 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960
Victor Stinner8c62be82010-05-06 00:08:46 +0000961 if(!SetCurrentDirectoryW(path))
962 return FALSE;
963 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
964 if (!result)
965 return FALSE;
966 if (result > MAX_PATH+1) {
967 new_path = malloc(result * sizeof(wchar_t));
968 if (!new_path) {
969 SetLastError(ERROR_OUTOFMEMORY);
970 return FALSE;
971 }
972 result = GetCurrentDirectoryW(result, new_path);
973 if (!result) {
974 free(new_path);
975 return FALSE;
976 }
977 }
978 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
979 wcsncmp(new_path, L"//", 2) == 0)
980 /* UNC path, nothing to do. */
981 return TRUE;
982 env[1] = new_path[0];
983 result = SetEnvironmentVariableW(env, new_path);
984 if (new_path != _new_path)
985 free(new_path);
986 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000987}
988#endif
989
Martin v. Löwis14694662006-02-03 12:54:16 +0000990#ifdef MS_WINDOWS
991/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
992 - time stamps are restricted to second resolution
993 - file modification times suffer from forth-and-back conversions between
994 UTC and local time
995 Therefore, we implement our own stat, based on the Win32 API directly.
996*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000997#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000998
999struct win32_stat{
1000 int st_dev;
1001 __int64 st_ino;
1002 unsigned short st_mode;
1003 int st_nlink;
1004 int st_uid;
1005 int st_gid;
1006 int st_rdev;
1007 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001008 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001009 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001010 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001011 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001012 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001013 int st_ctime_nsec;
1014};
1015
1016static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1017
1018static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001019FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001020{
Victor Stinner8c62be82010-05-06 00:08:46 +00001021 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1022 /* Cannot simply cast and dereference in_ptr,
1023 since it might not be aligned properly */
1024 __int64 in;
1025 memcpy(&in, in_ptr, sizeof(in));
1026 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001027 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001028}
1029
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001031time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032{
Victor Stinner8c62be82010-05-06 00:08:46 +00001033 /* XXX endianness */
1034 __int64 out;
1035 out = time_in + secs_between_epochs;
1036 out = out * 10000000 + nsec_in / 100;
1037 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038}
1039
Martin v. Löwis14694662006-02-03 12:54:16 +00001040/* Below, we *know* that ugo+r is 0444 */
1041#if _S_IREAD != 0400
1042#error Unsupported C library
1043#endif
1044static int
1045attributes_to_mode(DWORD attr)
1046{
Victor Stinner8c62be82010-05-06 00:08:46 +00001047 int m = 0;
1048 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1049 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1050 else
1051 m |= _S_IFREG;
1052 if (attr & FILE_ATTRIBUTE_READONLY)
1053 m |= 0444;
1054 else
1055 m |= 0666;
1056 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001057}
1058
1059static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001060attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001061{
Victor Stinner8c62be82010-05-06 00:08:46 +00001062 memset(result, 0, sizeof(*result));
1063 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1064 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1065 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1066 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1067 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001068 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001069 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001070 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1071 /* first clear the S_IFMT bits */
1072 result->st_mode ^= (result->st_mode & 0170000);
1073 /* now set the bits that make this a symlink */
1074 result->st_mode |= 0120000;
1075 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001076
Victor Stinner8c62be82010-05-06 00:08:46 +00001077 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001078}
1079
Guido van Rossumd8faa362007-04-27 19:54:29 +00001080static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001081attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001082{
Victor Stinner8c62be82010-05-06 00:08:46 +00001083 HANDLE hFindFile;
1084 WIN32_FIND_DATAA FileData;
1085 hFindFile = FindFirstFileA(pszFile, &FileData);
1086 if (hFindFile == INVALID_HANDLE_VALUE)
1087 return FALSE;
1088 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001089 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001090 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001091 info->dwFileAttributes = FileData.dwFileAttributes;
1092 info->ftCreationTime = FileData.ftCreationTime;
1093 info->ftLastAccessTime = FileData.ftLastAccessTime;
1094 info->ftLastWriteTime = FileData.ftLastWriteTime;
1095 info->nFileSizeHigh = FileData.nFileSizeHigh;
1096 info->nFileSizeLow = FileData.nFileSizeLow;
1097/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001098 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1099 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001100 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001101}
1102
1103static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001104attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001105{
Victor Stinner8c62be82010-05-06 00:08:46 +00001106 HANDLE hFindFile;
1107 WIN32_FIND_DATAW FileData;
1108 hFindFile = FindFirstFileW(pszFile, &FileData);
1109 if (hFindFile == INVALID_HANDLE_VALUE)
1110 return FALSE;
1111 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001112 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001113 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001114 info->dwFileAttributes = FileData.dwFileAttributes;
1115 info->ftCreationTime = FileData.ftCreationTime;
1116 info->ftLastAccessTime = FileData.ftLastAccessTime;
1117 info->ftLastWriteTime = FileData.ftLastWriteTime;
1118 info->nFileSizeHigh = FileData.nFileSizeHigh;
1119 info->nFileSizeLow = FileData.nFileSizeLow;
1120/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001121 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1122 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001123 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001124}
1125
Brian Curtinf5e76d02010-11-24 13:14:05 +00001126#ifndef SYMLOOP_MAX
1127#define SYMLOOP_MAX ( 88 )
1128#endif
1129
1130static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001131win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001132
1133static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001134win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001135{
1136 int code;
1137 HANDLE hFile;
1138 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001139 ULONG reparse_tag = 0;
Hirokazu Yamamoto74673512010-12-05 02:48:08 +00001140 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001141 const char *dot;
1142
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001143 if (depth > SYMLOOP_MAX) {
1144 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1145 return -1;
1146 }
1147
Brian Curtinf5e76d02010-11-24 13:14:05 +00001148 hFile = CreateFileA(
1149 path,
1150 0, /* desired access */
1151 0, /* share mode */
1152 NULL, /* security attributes */
1153 OPEN_EXISTING,
1154 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1155 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1156 NULL);
1157
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001158 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001159 /* Either the target doesn't exist, or we don't have access to
1160 get a handle to it. If the former, we need to return an error.
1161 If the latter, we can use attributes_from_dir. */
1162 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001163 return -1;
1164 /* Could not get attributes on open file. Fall back to
1165 reading the directory. */
1166 if (!attributes_from_dir(path, &info, &reparse_tag))
1167 /* Very strange. This should not fail now */
1168 return -1;
1169 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1170 if (traverse) {
1171 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001172 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001173 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001174 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001175 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001176 } else {
1177 if (!GetFileInformationByHandle(hFile, &info)) {
1178 CloseHandle(hFile);
1179 return -1;;
1180 }
1181 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1182 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1183 CloseHandle(hFile);
1184 if (code < 0)
1185 return code;
1186 if (traverse) {
1187 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1188 free(target_path);
1189 return code;
1190 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001191 } else
1192 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001193 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001194 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001195
1196 /* Set S_IEXEC if it is an .exe, .bat, ... */
1197 dot = strrchr(path, '.');
1198 if (dot) {
1199 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1200 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1201 result->st_mode |= 0111;
1202 }
1203 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001204}
1205
1206static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001207win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001208{
1209 int code;
1210 HANDLE hFile;
1211 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001212 ULONG reparse_tag = 0;
1213 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001214 const wchar_t *dot;
1215
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001216 if (depth > SYMLOOP_MAX) {
1217 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1218 return -1;
1219 }
1220
Brian Curtinf5e76d02010-11-24 13:14:05 +00001221 hFile = CreateFileW(
1222 path,
1223 0, /* desired access */
1224 0, /* share mode */
1225 NULL, /* security attributes */
1226 OPEN_EXISTING,
1227 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1228 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1229 NULL);
1230
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001231 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001232 /* Either the target doesn't exist, or we don't have access to
1233 get a handle to it. If the former, we need to return an error.
1234 If the latter, we can use attributes_from_dir. */
1235 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001236 return -1;
1237 /* Could not get attributes on open file. Fall back to
1238 reading the directory. */
1239 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1240 /* Very strange. This should not fail now */
1241 return -1;
1242 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1243 if (traverse) {
1244 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001245 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001246 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001247 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001249 } else {
1250 if (!GetFileInformationByHandle(hFile, &info)) {
1251 CloseHandle(hFile);
1252 return -1;;
1253 }
1254 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1255 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1256 CloseHandle(hFile);
1257 if (code < 0)
1258 return code;
1259 if (traverse) {
1260 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1261 free(target_path);
1262 return code;
1263 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001264 } else
1265 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001266 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001267 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268
1269 /* Set S_IEXEC if it is an .exe, .bat, ... */
1270 dot = wcsrchr(path, '.');
1271 if (dot) {
1272 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1273 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1274 result->st_mode |= 0111;
1275 }
1276 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001277}
1278
1279static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001280win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001281{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001282 /* Protocol violation: we explicitly clear errno, instead of
1283 setting it to a POSIX error. Callers should use GetLastError. */
1284 int code = win32_xstat_impl(path, result, traverse, 0);
1285 errno = 0;
1286 return code;
1287}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001288
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001289static int
1290win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1291{
1292 /* Protocol violation: we explicitly clear errno, instead of
1293 setting it to a POSIX error. Callers should use GetLastError. */
1294 int code = win32_xstat_impl_w(path, result, traverse, 0);
1295 errno = 0;
1296 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001297}
1298
Brian Curtind40e6f72010-07-08 21:39:08 +00001299/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1300 win32_stat_w
1301
1302 In Posix, stat automatically traverses symlinks and returns the stat
1303 structure for the target. In Windows, the equivalent GetFileAttributes by
1304 default does not traverse symlinks and instead returns attributes for
1305 the symlink.
1306
1307 Therefore, win32_lstat will get the attributes traditionally, and
1308 win32_stat will first explicitly resolve the symlink target and then will
1309 call win32_lstat on that result.
1310
1311 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1312
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001313static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001314win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001315{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001316 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001317}
1318
Victor Stinner8c62be82010-05-06 00:08:46 +00001319static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001320win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001321{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001322 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001323}
1324
1325static int
1326win32_stat(const char* path, struct win32_stat *result)
1327{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001328 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001329}
1330
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001331static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001332win32_stat_w(const wchar_t* path, struct win32_stat *result)
1333{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001334 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001335}
1336
1337static int
1338win32_fstat(int file_number, struct win32_stat *result)
1339{
Victor Stinner8c62be82010-05-06 00:08:46 +00001340 BY_HANDLE_FILE_INFORMATION info;
1341 HANDLE h;
1342 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001343
Victor Stinner8c62be82010-05-06 00:08:46 +00001344 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001345
Victor Stinner8c62be82010-05-06 00:08:46 +00001346 /* Protocol violation: we explicitly clear errno, instead of
1347 setting it to a POSIX error. Callers should use GetLastError. */
1348 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001349
Victor Stinner8c62be82010-05-06 00:08:46 +00001350 if (h == INVALID_HANDLE_VALUE) {
1351 /* This is really a C library error (invalid file handle).
1352 We set the Win32 error to the closes one matching. */
1353 SetLastError(ERROR_INVALID_HANDLE);
1354 return -1;
1355 }
1356 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001357
Victor Stinner8c62be82010-05-06 00:08:46 +00001358 type = GetFileType(h);
1359 if (type == FILE_TYPE_UNKNOWN) {
1360 DWORD error = GetLastError();
1361 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001362 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001363 }
1364 /* else: valid but unknown file */
1365 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001366
Victor Stinner8c62be82010-05-06 00:08:46 +00001367 if (type != FILE_TYPE_DISK) {
1368 if (type == FILE_TYPE_CHAR)
1369 result->st_mode = _S_IFCHR;
1370 else if (type == FILE_TYPE_PIPE)
1371 result->st_mode = _S_IFIFO;
1372 return 0;
1373 }
1374
1375 if (!GetFileInformationByHandle(h, &info)) {
1376 return -1;
1377 }
1378
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001379 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001380 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001381 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1382 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001383}
1384
1385#endif /* MS_WINDOWS */
1386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001387PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001388"stat_result: Result from stat or lstat.\n\n\
1389This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001390 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001391or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1392\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001393Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1394or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001395\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001396See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001397
1398static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001399 {"st_mode", "protection bits"},
1400 {"st_ino", "inode"},
1401 {"st_dev", "device"},
1402 {"st_nlink", "number of hard links"},
1403 {"st_uid", "user ID of owner"},
1404 {"st_gid", "group ID of owner"},
1405 {"st_size", "total size, in bytes"},
1406 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1407 {NULL, "integer time of last access"},
1408 {NULL, "integer time of last modification"},
1409 {NULL, "integer time of last change"},
1410 {"st_atime", "time of last access"},
1411 {"st_mtime", "time of last modification"},
1412 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001413#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001414 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001415#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001416#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001417 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001418#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001419#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001421#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001422#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001423 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001424#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001425#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001426 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001427#endif
1428#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001429 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001430#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001431 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001432};
1433
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001434#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001435#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001436#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001437#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001438#endif
1439
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001440#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001441#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1442#else
1443#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1444#endif
1445
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001446#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001447#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1448#else
1449#define ST_RDEV_IDX ST_BLOCKS_IDX
1450#endif
1451
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001452#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1453#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1454#else
1455#define ST_FLAGS_IDX ST_RDEV_IDX
1456#endif
1457
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001458#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001459#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001460#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001461#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001462#endif
1463
1464#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1465#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1466#else
1467#define ST_BIRTHTIME_IDX ST_GEN_IDX
1468#endif
1469
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001470static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 "stat_result", /* name */
1472 stat_result__doc__, /* doc */
1473 stat_result_fields,
1474 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001475};
1476
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001477PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001478"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1479This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001480 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001481or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001482\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001483See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001484
1485static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001486 {"f_bsize", },
1487 {"f_frsize", },
1488 {"f_blocks", },
1489 {"f_bfree", },
1490 {"f_bavail", },
1491 {"f_files", },
1492 {"f_ffree", },
1493 {"f_favail", },
1494 {"f_flag", },
1495 {"f_namemax",},
1496 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001497};
1498
1499static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001500 "statvfs_result", /* name */
1501 statvfs_result__doc__, /* doc */
1502 statvfs_result_fields,
1503 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001504};
1505
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001506static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001507static PyTypeObject StatResultType;
1508static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001509static newfunc structseq_new;
1510
1511static PyObject *
1512statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1513{
Victor Stinner8c62be82010-05-06 00:08:46 +00001514 PyStructSequence *result;
1515 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001516
Victor Stinner8c62be82010-05-06 00:08:46 +00001517 result = (PyStructSequence*)structseq_new(type, args, kwds);
1518 if (!result)
1519 return NULL;
1520 /* If we have been initialized from a tuple,
1521 st_?time might be set to None. Initialize it
1522 from the int slots. */
1523 for (i = 7; i <= 9; i++) {
1524 if (result->ob_item[i+3] == Py_None) {
1525 Py_DECREF(Py_None);
1526 Py_INCREF(result->ob_item[i]);
1527 result->ob_item[i+3] = result->ob_item[i];
1528 }
1529 }
1530 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001531}
1532
1533
1534
1535/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001536static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001537
1538PyDoc_STRVAR(stat_float_times__doc__,
1539"stat_float_times([newval]) -> oldval\n\n\
1540Determine whether os.[lf]stat represents time stamps as float objects.\n\
1541If newval is True, future calls to stat() return floats, if it is False,\n\
1542future calls return ints. \n\
1543If newval is omitted, return the current setting.\n");
1544
1545static PyObject*
1546stat_float_times(PyObject* self, PyObject *args)
1547{
Victor Stinner8c62be82010-05-06 00:08:46 +00001548 int newval = -1;
1549 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1550 return NULL;
1551 if (newval == -1)
1552 /* Return old value */
1553 return PyBool_FromLong(_stat_float_times);
1554 _stat_float_times = newval;
1555 Py_INCREF(Py_None);
1556 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001557}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001558
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001559static void
1560fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1561{
Victor Stinner8c62be82010-05-06 00:08:46 +00001562 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001563#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001564 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001565#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001566 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001567#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001568 if (!ival)
1569 return;
1570 if (_stat_float_times) {
1571 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1572 } else {
1573 fval = ival;
1574 Py_INCREF(fval);
1575 }
1576 PyStructSequence_SET_ITEM(v, index, ival);
1577 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001578}
1579
Tim Peters5aa91602002-01-30 05:46:57 +00001580/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001581 (used by posix_stat() and posix_fstat()) */
1582static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001583_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001584{
Victor Stinner8c62be82010-05-06 00:08:46 +00001585 unsigned long ansec, mnsec, cnsec;
1586 PyObject *v = PyStructSequence_New(&StatResultType);
1587 if (v == NULL)
1588 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001589
Victor Stinner8c62be82010-05-06 00:08:46 +00001590 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001591#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001592 PyStructSequence_SET_ITEM(v, 1,
1593 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001594#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001596#endif
1597#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001598 PyStructSequence_SET_ITEM(v, 2,
1599 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001600#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001601 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001602#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001603 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1604 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1605 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001606#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001607 PyStructSequence_SET_ITEM(v, 6,
1608 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001609#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001610 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001611#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001612
Martin v. Löwis14694662006-02-03 12:54:16 +00001613#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001614 ansec = st->st_atim.tv_nsec;
1615 mnsec = st->st_mtim.tv_nsec;
1616 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001617#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 ansec = st->st_atimespec.tv_nsec;
1619 mnsec = st->st_mtimespec.tv_nsec;
1620 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001621#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 ansec = st->st_atime_nsec;
1623 mnsec = st->st_mtime_nsec;
1624 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001625#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001626 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001627#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001628 fill_time(v, 7, st->st_atime, ansec);
1629 fill_time(v, 8, st->st_mtime, mnsec);
1630 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001631
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001632#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001633 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1634 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001635#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001636#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001637 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1638 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001639#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001640#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001641 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1642 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001643#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001644#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001645 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1646 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001647#endif
1648#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001649 {
1650 PyObject *val;
1651 unsigned long bsec,bnsec;
1652 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001653#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001654 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001655#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001656 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001657#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001658 if (_stat_float_times) {
1659 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1660 } else {
1661 val = PyLong_FromLong((long)bsec);
1662 }
1663 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1664 val);
1665 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001666#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001667#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1669 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001670#endif
Fred Drake699f3522000-06-29 21:12:41 +00001671
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 if (PyErr_Occurred()) {
1673 Py_DECREF(v);
1674 return NULL;
1675 }
Fred Drake699f3522000-06-29 21:12:41 +00001676
Victor Stinner8c62be82010-05-06 00:08:46 +00001677 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001678}
1679
Barry Warsaw53699e91996-12-10 23:23:01 +00001680static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001681posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001682 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001683#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001684 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001685#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001686 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001687#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001688 char *wformat,
1689 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001690{
Victor Stinner8c62be82010-05-06 00:08:46 +00001691 STRUCT_STAT st;
1692 PyObject *opath;
1693 char *path;
1694 int res;
1695 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001696
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001697#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 PyUnicodeObject *po;
1699 if (PyArg_ParseTuple(args, wformat, &po)) {
1700 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001701
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 Py_BEGIN_ALLOW_THREADS
1703 /* PyUnicode_AS_UNICODE result OK without
1704 thread lock as it is a simple dereference. */
1705 res = wstatfunc(wpath, &st);
1706 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001707
Victor Stinner8c62be82010-05-06 00:08:46 +00001708 if (res != 0)
1709 return win32_error_unicode("stat", wpath);
1710 return _pystat_fromstructstat(&st);
1711 }
1712 /* Drop the argument parsing error as narrow strings
1713 are also valid. */
1714 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001715#endif
1716
Victor Stinner8c62be82010-05-06 00:08:46 +00001717 if (!PyArg_ParseTuple(args, format,
1718 PyUnicode_FSConverter, &opath))
1719 return NULL;
1720 path = PyBytes_AsString(opath);
1721 Py_BEGIN_ALLOW_THREADS
1722 res = (*statfunc)(path, &st);
1723 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001724
Victor Stinner8c62be82010-05-06 00:08:46 +00001725 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001726#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001727 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001728#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001729 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001730#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001731 }
1732 else
1733 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001734
Victor Stinner8c62be82010-05-06 00:08:46 +00001735 Py_DECREF(opath);
1736 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001737}
1738
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001739/* POSIX methods */
1740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001741PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001742"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001743Use the real uid/gid to test for access to a path. Note that most\n\
1744operations will use the effective uid/gid, therefore this routine can\n\
1745be used in a suid/sgid environment to test if the invoking user has the\n\
1746specified access to the path. The mode argument can be F_OK to test\n\
1747existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001748
1749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001750posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001751{
Victor Stinner8c62be82010-05-06 00:08:46 +00001752 PyObject *opath;
1753 char *path;
1754 int mode;
1755
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001756#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001757 DWORD attr;
1758 PyUnicodeObject *po;
1759 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1760 Py_BEGIN_ALLOW_THREADS
1761 /* PyUnicode_AS_UNICODE OK without thread lock as
1762 it is a simple dereference. */
1763 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1764 Py_END_ALLOW_THREADS
1765 goto finish;
1766 }
1767 /* Drop the argument parsing error as narrow strings
1768 are also valid. */
1769 PyErr_Clear();
1770 if (!PyArg_ParseTuple(args, "O&i:access",
1771 PyUnicode_FSConverter, &opath, &mode))
1772 return NULL;
1773 path = PyBytes_AsString(opath);
1774 Py_BEGIN_ALLOW_THREADS
1775 attr = GetFileAttributesA(path);
1776 Py_END_ALLOW_THREADS
1777 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001778finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001779 if (attr == 0xFFFFFFFF)
1780 /* File does not exist, or cannot read attributes */
1781 return PyBool_FromLong(0);
1782 /* Access is possible if either write access wasn't requested, or
1783 the file isn't read-only, or if it's a directory, as there are
1784 no read-only directories on Windows. */
1785 return PyBool_FromLong(!(mode & 2)
1786 || !(attr & FILE_ATTRIBUTE_READONLY)
1787 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001788#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001789 int res;
1790 if (!PyArg_ParseTuple(args, "O&i:access",
1791 PyUnicode_FSConverter, &opath, &mode))
1792 return NULL;
1793 path = PyBytes_AsString(opath);
1794 Py_BEGIN_ALLOW_THREADS
1795 res = access(path, mode);
1796 Py_END_ALLOW_THREADS
1797 Py_DECREF(opath);
1798 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001799#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001800}
1801
Guido van Rossumd371ff11999-01-25 16:12:23 +00001802#ifndef F_OK
1803#define F_OK 0
1804#endif
1805#ifndef R_OK
1806#define R_OK 4
1807#endif
1808#ifndef W_OK
1809#define W_OK 2
1810#endif
1811#ifndef X_OK
1812#define X_OK 1
1813#endif
1814
1815#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001816PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001817"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001819
1820static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001821posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001822{
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 int id;
1824 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001825
Victor Stinner8c62be82010-05-06 00:08:46 +00001826 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1827 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001828
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001829#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001830 /* file descriptor 0 only, the default input device (stdin) */
1831 if (id == 0) {
1832 ret = ttyname();
1833 }
1834 else {
1835 ret = NULL;
1836 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001837#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001838 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001839#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001840 if (ret == NULL)
1841 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001842 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001843}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001844#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001845
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001846#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001848"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001850
1851static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001852posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001853{
Victor Stinner8c62be82010-05-06 00:08:46 +00001854 char *ret;
1855 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001856
Greg Wardb48bc172000-03-01 21:51:56 +00001857#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001858 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001859#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001860 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001861#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001862 if (ret == NULL)
1863 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001864 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001865}
1866#endif
1867
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001868PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001869"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001870Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001871
Barry Warsaw53699e91996-12-10 23:23:01 +00001872static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001873posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001874{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001875#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001876 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001877#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001878 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001879#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001880 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001881#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001882 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001883#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001884}
1885
Fred Drake4d1e64b2002-04-15 19:40:07 +00001886#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001888"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001889Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001891
1892static PyObject *
1893posix_fchdir(PyObject *self, PyObject *fdobj)
1894{
Victor Stinner8c62be82010-05-06 00:08:46 +00001895 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001896}
1897#endif /* HAVE_FCHDIR */
1898
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001899
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001900PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001901"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001903
Barry Warsaw53699e91996-12-10 23:23:01 +00001904static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001905posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001906{
Victor Stinner8c62be82010-05-06 00:08:46 +00001907 PyObject *opath = NULL;
1908 char *path = NULL;
1909 int i;
1910 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001911#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001912 DWORD attr;
1913 PyUnicodeObject *po;
1914 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1915 Py_BEGIN_ALLOW_THREADS
1916 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1917 if (attr != 0xFFFFFFFF) {
1918 if (i & _S_IWRITE)
1919 attr &= ~FILE_ATTRIBUTE_READONLY;
1920 else
1921 attr |= FILE_ATTRIBUTE_READONLY;
1922 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1923 }
1924 else
1925 res = 0;
1926 Py_END_ALLOW_THREADS
1927 if (!res)
1928 return win32_error_unicode("chmod",
1929 PyUnicode_AS_UNICODE(po));
1930 Py_INCREF(Py_None);
1931 return Py_None;
1932 }
1933 /* Drop the argument parsing error as narrow strings
1934 are also valid. */
1935 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001936
Victor Stinner8c62be82010-05-06 00:08:46 +00001937 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1938 &opath, &i))
1939 return NULL;
1940 path = PyBytes_AsString(opath);
1941 Py_BEGIN_ALLOW_THREADS
1942 attr = GetFileAttributesA(path);
1943 if (attr != 0xFFFFFFFF) {
1944 if (i & _S_IWRITE)
1945 attr &= ~FILE_ATTRIBUTE_READONLY;
1946 else
1947 attr |= FILE_ATTRIBUTE_READONLY;
1948 res = SetFileAttributesA(path, attr);
1949 }
1950 else
1951 res = 0;
1952 Py_END_ALLOW_THREADS
1953 if (!res) {
1954 win32_error("chmod", path);
1955 Py_DECREF(opath);
1956 return NULL;
1957 }
1958 Py_DECREF(opath);
1959 Py_INCREF(Py_None);
1960 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001961#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001962 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1963 &opath, &i))
1964 return NULL;
1965 path = PyBytes_AsString(opath);
1966 Py_BEGIN_ALLOW_THREADS
1967 res = chmod(path, i);
1968 Py_END_ALLOW_THREADS
1969 if (res < 0)
1970 return posix_error_with_allocated_filename(opath);
1971 Py_DECREF(opath);
1972 Py_INCREF(Py_None);
1973 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001974#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001975}
1976
Christian Heimes4e30a842007-11-30 22:12:06 +00001977#ifdef HAVE_FCHMOD
1978PyDoc_STRVAR(posix_fchmod__doc__,
1979"fchmod(fd, mode)\n\n\
1980Change the access permissions of the file given by file\n\
1981descriptor fd.");
1982
1983static PyObject *
1984posix_fchmod(PyObject *self, PyObject *args)
1985{
Victor Stinner8c62be82010-05-06 00:08:46 +00001986 int fd, mode, res;
1987 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1988 return NULL;
1989 Py_BEGIN_ALLOW_THREADS
1990 res = fchmod(fd, mode);
1991 Py_END_ALLOW_THREADS
1992 if (res < 0)
1993 return posix_error();
1994 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001995}
1996#endif /* HAVE_FCHMOD */
1997
1998#ifdef HAVE_LCHMOD
1999PyDoc_STRVAR(posix_lchmod__doc__,
2000"lchmod(path, mode)\n\n\
2001Change the access permissions of a file. If path is a symlink, this\n\
2002affects the link itself rather than the target.");
2003
2004static PyObject *
2005posix_lchmod(PyObject *self, PyObject *args)
2006{
Victor Stinner8c62be82010-05-06 00:08:46 +00002007 PyObject *opath;
2008 char *path;
2009 int i;
2010 int res;
2011 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2012 &opath, &i))
2013 return NULL;
2014 path = PyBytes_AsString(opath);
2015 Py_BEGIN_ALLOW_THREADS
2016 res = lchmod(path, i);
2017 Py_END_ALLOW_THREADS
2018 if (res < 0)
2019 return posix_error_with_allocated_filename(opath);
2020 Py_DECREF(opath);
2021 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002022}
2023#endif /* HAVE_LCHMOD */
2024
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002025
Thomas Wouterscf297e42007-02-23 15:07:44 +00002026#ifdef HAVE_CHFLAGS
2027PyDoc_STRVAR(posix_chflags__doc__,
2028"chflags(path, flags)\n\n\
2029Set file flags.");
2030
2031static PyObject *
2032posix_chflags(PyObject *self, PyObject *args)
2033{
Victor Stinner8c62be82010-05-06 00:08:46 +00002034 PyObject *opath;
2035 char *path;
2036 unsigned long flags;
2037 int res;
2038 if (!PyArg_ParseTuple(args, "O&k:chflags",
2039 PyUnicode_FSConverter, &opath, &flags))
2040 return NULL;
2041 path = PyBytes_AsString(opath);
2042 Py_BEGIN_ALLOW_THREADS
2043 res = chflags(path, flags);
2044 Py_END_ALLOW_THREADS
2045 if (res < 0)
2046 return posix_error_with_allocated_filename(opath);
2047 Py_DECREF(opath);
2048 Py_INCREF(Py_None);
2049 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002050}
2051#endif /* HAVE_CHFLAGS */
2052
2053#ifdef HAVE_LCHFLAGS
2054PyDoc_STRVAR(posix_lchflags__doc__,
2055"lchflags(path, flags)\n\n\
2056Set file flags.\n\
2057This function will not follow symbolic links.");
2058
2059static PyObject *
2060posix_lchflags(PyObject *self, PyObject *args)
2061{
Victor Stinner8c62be82010-05-06 00:08:46 +00002062 PyObject *opath;
2063 char *path;
2064 unsigned long flags;
2065 int res;
2066 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2067 PyUnicode_FSConverter, &opath, &flags))
2068 return NULL;
2069 path = PyBytes_AsString(opath);
2070 Py_BEGIN_ALLOW_THREADS
2071 res = lchflags(path, flags);
2072 Py_END_ALLOW_THREADS
2073 if (res < 0)
2074 return posix_error_with_allocated_filename(opath);
2075 Py_DECREF(opath);
2076 Py_INCREF(Py_None);
2077 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002078}
2079#endif /* HAVE_LCHFLAGS */
2080
Martin v. Löwis244edc82001-10-04 22:44:26 +00002081#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002083"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002085
2086static PyObject *
2087posix_chroot(PyObject *self, PyObject *args)
2088{
Victor Stinner8c62be82010-05-06 00:08:46 +00002089 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002090}
2091#endif
2092
Guido van Rossum21142a01999-01-08 21:05:37 +00002093#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002094PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002095"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002096force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002097
2098static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002099posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002100{
Stefan Krah0e803b32010-11-26 16:16:47 +00002101 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002102}
2103#endif /* HAVE_FSYNC */
2104
2105#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002106
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002107#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002108extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2109#endif
2110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002111PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002112"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002113force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002114 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002115
2116static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002117posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002118{
Stefan Krah0e803b32010-11-26 16:16:47 +00002119 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002120}
2121#endif /* HAVE_FDATASYNC */
2122
2123
Fredrik Lundh10723342000-07-10 16:38:09 +00002124#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002125PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002126"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002127Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002128
Barry Warsaw53699e91996-12-10 23:23:01 +00002129static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002130posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002131{
Victor Stinner8c62be82010-05-06 00:08:46 +00002132 PyObject *opath;
2133 char *path;
2134 long uid, gid;
2135 int res;
2136 if (!PyArg_ParseTuple(args, "O&ll:chown",
2137 PyUnicode_FSConverter, &opath,
2138 &uid, &gid))
2139 return NULL;
2140 path = PyBytes_AsString(opath);
2141 Py_BEGIN_ALLOW_THREADS
2142 res = chown(path, (uid_t) uid, (gid_t) gid);
2143 Py_END_ALLOW_THREADS
2144 if (res < 0)
2145 return posix_error_with_allocated_filename(opath);
2146 Py_DECREF(opath);
2147 Py_INCREF(Py_None);
2148 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002149}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002150#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002151
Christian Heimes4e30a842007-11-30 22:12:06 +00002152#ifdef HAVE_FCHOWN
2153PyDoc_STRVAR(posix_fchown__doc__,
2154"fchown(fd, uid, gid)\n\n\
2155Change the owner and group id of the file given by file descriptor\n\
2156fd to the numeric uid and gid.");
2157
2158static PyObject *
2159posix_fchown(PyObject *self, PyObject *args)
2160{
Victor Stinner8c62be82010-05-06 00:08:46 +00002161 int fd;
2162 long uid, gid;
2163 int res;
2164 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2165 return NULL;
2166 Py_BEGIN_ALLOW_THREADS
2167 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2168 Py_END_ALLOW_THREADS
2169 if (res < 0)
2170 return posix_error();
2171 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002172}
2173#endif /* HAVE_FCHOWN */
2174
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002175#ifdef HAVE_LCHOWN
2176PyDoc_STRVAR(posix_lchown__doc__,
2177"lchown(path, uid, gid)\n\n\
2178Change the owner and group id of path to the numeric uid and gid.\n\
2179This function will not follow symbolic links.");
2180
2181static PyObject *
2182posix_lchown(PyObject *self, PyObject *args)
2183{
Victor Stinner8c62be82010-05-06 00:08:46 +00002184 PyObject *opath;
2185 char *path;
2186 long uid, gid;
2187 int res;
2188 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2189 PyUnicode_FSConverter, &opath,
2190 &uid, &gid))
2191 return NULL;
2192 path = PyBytes_AsString(opath);
2193 Py_BEGIN_ALLOW_THREADS
2194 res = lchown(path, (uid_t) uid, (gid_t) gid);
2195 Py_END_ALLOW_THREADS
2196 if (res < 0)
2197 return posix_error_with_allocated_filename(opath);
2198 Py_DECREF(opath);
2199 Py_INCREF(Py_None);
2200 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002201}
2202#endif /* HAVE_LCHOWN */
2203
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002204
Guido van Rossum36bc6801995-06-14 22:54:23 +00002205#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002206static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002207posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002208{
Victor Stinner8c62be82010-05-06 00:08:46 +00002209 char buf[1026];
2210 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002211
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002212#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002213 if (!use_bytes) {
2214 wchar_t wbuf[1026];
2215 wchar_t *wbuf2 = wbuf;
2216 PyObject *resobj;
2217 DWORD len;
2218 Py_BEGIN_ALLOW_THREADS
2219 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2220 /* If the buffer is large enough, len does not include the
2221 terminating \0. If the buffer is too small, len includes
2222 the space needed for the terminator. */
2223 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2224 wbuf2 = malloc(len * sizeof(wchar_t));
2225 if (wbuf2)
2226 len = GetCurrentDirectoryW(len, wbuf2);
2227 }
2228 Py_END_ALLOW_THREADS
2229 if (!wbuf2) {
2230 PyErr_NoMemory();
2231 return NULL;
2232 }
2233 if (!len) {
2234 if (wbuf2 != wbuf) free(wbuf2);
2235 return win32_error("getcwdu", NULL);
2236 }
2237 resobj = PyUnicode_FromWideChar(wbuf2, len);
2238 if (wbuf2 != wbuf) free(wbuf2);
2239 return resobj;
2240 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002241#endif
2242
Victor Stinner8c62be82010-05-06 00:08:46 +00002243 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002244#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002245 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002246#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002247 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002248#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002249 Py_END_ALLOW_THREADS
2250 if (res == NULL)
2251 return posix_error();
2252 if (use_bytes)
2253 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002254 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002255}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002256
2257PyDoc_STRVAR(posix_getcwd__doc__,
2258"getcwd() -> path\n\n\
2259Return a unicode string representing the current working directory.");
2260
2261static PyObject *
2262posix_getcwd_unicode(PyObject *self)
2263{
2264 return posix_getcwd(0);
2265}
2266
2267PyDoc_STRVAR(posix_getcwdb__doc__,
2268"getcwdb() -> path\n\n\
2269Return a bytes string representing the current working directory.");
2270
2271static PyObject *
2272posix_getcwd_bytes(PyObject *self)
2273{
2274 return posix_getcwd(1);
2275}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002276#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002277
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002278
Guido van Rossumb6775db1994-08-01 11:34:53 +00002279#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002280PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002281"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002282Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002283
Barry Warsaw53699e91996-12-10 23:23:01 +00002284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002285posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002286{
Victor Stinner8c62be82010-05-06 00:08:46 +00002287 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002288}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002289#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002290
Brian Curtin1b9df392010-11-24 20:24:31 +00002291#ifdef MS_WINDOWS
2292PyDoc_STRVAR(win32_link__doc__,
2293"link(src, dst)\n\n\
2294Create a hard link to a file.");
2295
2296static PyObject *
2297win32_link(PyObject *self, PyObject *args)
2298{
2299 PyObject *osrc, *odst;
2300 char *src, *dst;
2301 BOOL rslt;
2302
Brian Curtinfc889c42010-11-28 23:59:46 +00002303 PyUnicodeObject *usrc, *udst;
2304 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2305 Py_BEGIN_ALLOW_THREADS
2306 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2307 PyUnicode_AS_UNICODE(usrc), NULL);
2308 Py_END_ALLOW_THREADS
2309
2310 if (rslt == 0)
2311 return win32_error("link", NULL);
2312
2313 Py_RETURN_NONE;
2314 }
2315
2316 /* Narrow strings also valid. */
2317 PyErr_Clear();
2318
Brian Curtin1b9df392010-11-24 20:24:31 +00002319 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2320 PyUnicode_FSConverter, &odst))
2321 return NULL;
2322
2323 src = PyBytes_AsString(osrc);
2324 dst = PyBytes_AsString(odst);
2325
2326 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002327 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002328 Py_END_ALLOW_THREADS
2329
Stefan Krah30b341f2010-11-27 11:44:18 +00002330 Py_DECREF(osrc);
2331 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002332 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002333 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002334
2335 Py_RETURN_NONE;
2336}
2337#endif /* MS_WINDOWS */
2338
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002339
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002340PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002341"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002342Return a list containing the names of the entries in the directory.\n\
2343\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002344 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002345\n\
2346The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002347entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002348
Barry Warsaw53699e91996-12-10 23:23:01 +00002349static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002350posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002351{
Victor Stinner8c62be82010-05-06 00:08:46 +00002352 /* XXX Should redo this putting the (now four) versions of opendir
2353 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002354#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002355
Victor Stinner8c62be82010-05-06 00:08:46 +00002356 PyObject *d, *v;
2357 HANDLE hFindFile;
2358 BOOL result;
2359 WIN32_FIND_DATA FileData;
2360 PyObject *opath;
2361 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2362 char *bufptr = namebuf;
2363 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002364
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002365 PyObject *po = NULL;
2366 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002367 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002368 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002369
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002370 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002371 po_wchars = L".";
2372 len = 1;
2373 } else {
2374 po_wchars = PyUnicode_AS_UNICODE(po);
2375 len = PyUnicode_GET_SIZE(po);
2376 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002377 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002378 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2379 if (!wnamebuf) {
2380 PyErr_NoMemory();
2381 return NULL;
2382 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002383 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002384 if (len > 0) {
2385 Py_UNICODE wch = wnamebuf[len-1];
2386 if (wch != L'/' && wch != L'\\' && wch != L':')
2387 wnamebuf[len++] = L'\\';
2388 wcscpy(wnamebuf + len, L"*.*");
2389 }
2390 if ((d = PyList_New(0)) == NULL) {
2391 free(wnamebuf);
2392 return NULL;
2393 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002394 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002395 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002396 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002397 if (hFindFile == INVALID_HANDLE_VALUE) {
2398 int error = GetLastError();
2399 if (error == ERROR_FILE_NOT_FOUND) {
2400 free(wnamebuf);
2401 return d;
2402 }
2403 Py_DECREF(d);
2404 win32_error_unicode("FindFirstFileW", wnamebuf);
2405 free(wnamebuf);
2406 return NULL;
2407 }
2408 do {
2409 /* Skip over . and .. */
2410 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2411 wcscmp(wFileData.cFileName, L"..") != 0) {
2412 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2413 if (v == NULL) {
2414 Py_DECREF(d);
2415 d = NULL;
2416 break;
2417 }
2418 if (PyList_Append(d, v) != 0) {
2419 Py_DECREF(v);
2420 Py_DECREF(d);
2421 d = NULL;
2422 break;
2423 }
2424 Py_DECREF(v);
2425 }
2426 Py_BEGIN_ALLOW_THREADS
2427 result = FindNextFileW(hFindFile, &wFileData);
2428 Py_END_ALLOW_THREADS
2429 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2430 it got to the end of the directory. */
2431 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2432 Py_DECREF(d);
2433 win32_error_unicode("FindNextFileW", wnamebuf);
2434 FindClose(hFindFile);
2435 free(wnamebuf);
2436 return NULL;
2437 }
2438 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002439
Victor Stinner8c62be82010-05-06 00:08:46 +00002440 if (FindClose(hFindFile) == FALSE) {
2441 Py_DECREF(d);
2442 win32_error_unicode("FindClose", wnamebuf);
2443 free(wnamebuf);
2444 return NULL;
2445 }
2446 free(wnamebuf);
2447 return d;
2448 }
2449 /* Drop the argument parsing error as narrow strings
2450 are also valid. */
2451 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002452
Victor Stinner8c62be82010-05-06 00:08:46 +00002453 if (!PyArg_ParseTuple(args, "O&:listdir",
2454 PyUnicode_FSConverter, &opath))
2455 return NULL;
2456 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2457 PyErr_SetString(PyExc_ValueError, "path too long");
2458 Py_DECREF(opath);
2459 return NULL;
2460 }
2461 strcpy(namebuf, PyBytes_AsString(opath));
2462 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002463 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002464 if (len > 0) {
2465 char ch = namebuf[len-1];
2466 if (ch != SEP && ch != ALTSEP && ch != ':')
2467 namebuf[len++] = '/';
2468 strcpy(namebuf + len, "*.*");
2469 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002470
Victor Stinner8c62be82010-05-06 00:08:46 +00002471 if ((d = PyList_New(0)) == NULL)
2472 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002473
Antoine Pitroub73caab2010-08-09 23:39:31 +00002474 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002475 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002476 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002477 if (hFindFile == INVALID_HANDLE_VALUE) {
2478 int error = GetLastError();
2479 if (error == ERROR_FILE_NOT_FOUND)
2480 return d;
2481 Py_DECREF(d);
2482 return win32_error("FindFirstFile", namebuf);
2483 }
2484 do {
2485 /* Skip over . and .. */
2486 if (strcmp(FileData.cFileName, ".") != 0 &&
2487 strcmp(FileData.cFileName, "..") != 0) {
2488 v = PyBytes_FromString(FileData.cFileName);
2489 if (v == NULL) {
2490 Py_DECREF(d);
2491 d = NULL;
2492 break;
2493 }
2494 if (PyList_Append(d, v) != 0) {
2495 Py_DECREF(v);
2496 Py_DECREF(d);
2497 d = NULL;
2498 break;
2499 }
2500 Py_DECREF(v);
2501 }
2502 Py_BEGIN_ALLOW_THREADS
2503 result = FindNextFile(hFindFile, &FileData);
2504 Py_END_ALLOW_THREADS
2505 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2506 it got to the end of the directory. */
2507 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2508 Py_DECREF(d);
2509 win32_error("FindNextFile", namebuf);
2510 FindClose(hFindFile);
2511 return NULL;
2512 }
2513 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002514
Victor Stinner8c62be82010-05-06 00:08:46 +00002515 if (FindClose(hFindFile) == FALSE) {
2516 Py_DECREF(d);
2517 return win32_error("FindClose", namebuf);
2518 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002519
Victor Stinner8c62be82010-05-06 00:08:46 +00002520 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002521
Tim Peters0bb44a42000-09-15 07:44:49 +00002522#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002523
2524#ifndef MAX_PATH
2525#define MAX_PATH CCHMAXPATH
2526#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002527 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002528 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002529 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002530 PyObject *d, *v;
2531 char namebuf[MAX_PATH+5];
2532 HDIR hdir = 1;
2533 ULONG srchcnt = 1;
2534 FILEFINDBUF3 ep;
2535 APIRET rc;
2536
Victor Stinner8c62be82010-05-06 00:08:46 +00002537 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002538 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002539 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002540 name = PyBytes_AsString(oname);
2541 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002542 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002543 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002544 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002545 return NULL;
2546 }
2547 strcpy(namebuf, name);
2548 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002549 if (*pt == ALTSEP)
2550 *pt = SEP;
2551 if (namebuf[len-1] != SEP)
2552 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002553 strcpy(namebuf + len, "*.*");
2554
Neal Norwitz6c913782007-10-14 03:23:09 +00002555 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002556 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002557 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002558 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002559
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002560 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2561 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002562 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002563 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2564 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2565 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002566
2567 if (rc != NO_ERROR) {
2568 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002569 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002570 }
2571
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002572 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002573 do {
2574 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002575 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002576 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002577
2578 strcpy(namebuf, ep.achName);
2579
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002580 /* Leave Case of Name Alone -- In Native Form */
2581 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002582
Christian Heimes72b710a2008-05-26 13:28:38 +00002583 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002584 if (v == NULL) {
2585 Py_DECREF(d);
2586 d = NULL;
2587 break;
2588 }
2589 if (PyList_Append(d, v) != 0) {
2590 Py_DECREF(v);
2591 Py_DECREF(d);
2592 d = NULL;
2593 break;
2594 }
2595 Py_DECREF(v);
2596 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2597 }
2598
Victor Stinnerdcb24032010-04-22 12:08:36 +00002599 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002600 return d;
2601#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002602 PyObject *oname;
2603 char *name;
2604 PyObject *d, *v;
2605 DIR *dirp;
2606 struct dirent *ep;
2607 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002608
Victor Stinner8c62be82010-05-06 00:08:46 +00002609 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002610 /* v is never read, so it does not need to be initialized yet. */
2611 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002612 arg_is_unicode = 0;
2613 PyErr_Clear();
2614 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002615 oname = NULL;
2616 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002617 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002618 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002619 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002620 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002621 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002622 Py_BEGIN_ALLOW_THREADS
2623 dirp = opendir(name);
2624 Py_END_ALLOW_THREADS
2625 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002626 return posix_error_with_allocated_filename(oname);
2627 }
2628 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002629 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002630 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002631 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 Py_DECREF(oname);
2633 return NULL;
2634 }
2635 for (;;) {
2636 errno = 0;
2637 Py_BEGIN_ALLOW_THREADS
2638 ep = readdir(dirp);
2639 Py_END_ALLOW_THREADS
2640 if (ep == NULL) {
2641 if (errno == 0) {
2642 break;
2643 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002644 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002645 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002646 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002647 Py_DECREF(d);
2648 return posix_error_with_allocated_filename(oname);
2649 }
2650 }
2651 if (ep->d_name[0] == '.' &&
2652 (NAMLEN(ep) == 1 ||
2653 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2654 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002655 if (arg_is_unicode)
2656 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2657 else
2658 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002659 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002660 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002661 break;
2662 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 if (PyList_Append(d, v) != 0) {
2664 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002665 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002666 break;
2667 }
2668 Py_DECREF(v);
2669 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002670 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002671 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002672 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002673 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002674
Victor Stinner8c62be82010-05-06 00:08:46 +00002675 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002676
Tim Peters0bb44a42000-09-15 07:44:49 +00002677#endif /* which OS */
2678} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002679
Antoine Pitrou8250e232011-02-25 23:41:16 +00002680#ifdef HAVE_FDOPENDIR
2681PyDoc_STRVAR(posix_fdlistdir__doc__,
2682"fdlistdir(fd) -> list_of_strings\n\n\
2683Like listdir(), but uses a file descriptor instead.\n\
2684After succesful execution of this function, fd will be closed.");
2685
2686static PyObject *
2687posix_fdlistdir(PyObject *self, PyObject *args)
2688{
2689 PyObject *d, *v;
2690 DIR *dirp;
2691 struct dirent *ep;
2692 int fd;
2693
2694 errno = 0;
2695 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2696 return NULL;
2697 Py_BEGIN_ALLOW_THREADS
2698 dirp = fdopendir(fd);
2699 Py_END_ALLOW_THREADS
2700 if (dirp == NULL) {
2701 close(fd);
2702 return posix_error();
2703 }
2704 if ((d = PyList_New(0)) == NULL) {
2705 Py_BEGIN_ALLOW_THREADS
2706 closedir(dirp);
2707 Py_END_ALLOW_THREADS
2708 return NULL;
2709 }
2710 for (;;) {
2711 errno = 0;
2712 Py_BEGIN_ALLOW_THREADS
2713 ep = readdir(dirp);
2714 Py_END_ALLOW_THREADS
2715 if (ep == NULL) {
2716 if (errno == 0) {
2717 break;
2718 } else {
2719 Py_BEGIN_ALLOW_THREADS
2720 closedir(dirp);
2721 Py_END_ALLOW_THREADS
2722 Py_DECREF(d);
2723 return posix_error();
2724 }
2725 }
2726 if (ep->d_name[0] == '.' &&
2727 (NAMLEN(ep) == 1 ||
2728 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2729 continue;
2730 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2731 if (v == NULL) {
2732 Py_CLEAR(d);
2733 break;
2734 }
2735 if (PyList_Append(d, v) != 0) {
2736 Py_DECREF(v);
2737 Py_CLEAR(d);
2738 break;
2739 }
2740 Py_DECREF(v);
2741 }
2742 Py_BEGIN_ALLOW_THREADS
2743 closedir(dirp);
2744 Py_END_ALLOW_THREADS
2745
2746 return d;
2747}
2748#endif
2749
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002750#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002751/* A helper function for abspath on win32 */
2752static PyObject *
2753posix__getfullpathname(PyObject *self, PyObject *args)
2754{
Victor Stinner8c62be82010-05-06 00:08:46 +00002755 PyObject *opath;
2756 char *path;
2757 char outbuf[MAX_PATH*2];
2758 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002759#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002760 PyUnicodeObject *po;
2761 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2762 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2763 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2764 Py_UNICODE *wtemp;
2765 DWORD result;
2766 PyObject *v;
2767 result = GetFullPathNameW(wpath,
2768 sizeof(woutbuf)/sizeof(woutbuf[0]),
2769 woutbuf, &wtemp);
2770 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2771 woutbufp = malloc(result * sizeof(Py_UNICODE));
2772 if (!woutbufp)
2773 return PyErr_NoMemory();
2774 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2775 }
2776 if (result)
2777 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2778 else
2779 v = win32_error_unicode("GetFullPathNameW", wpath);
2780 if (woutbufp != woutbuf)
2781 free(woutbufp);
2782 return v;
2783 }
2784 /* Drop the argument parsing error as narrow strings
2785 are also valid. */
2786 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002787
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002788#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002789 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2790 PyUnicode_FSConverter, &opath))
2791 return NULL;
2792 path = PyBytes_AsString(opath);
2793 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2794 outbuf, &temp)) {
2795 win32_error("GetFullPathName", path);
2796 Py_DECREF(opath);
2797 return NULL;
2798 }
2799 Py_DECREF(opath);
2800 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2801 return PyUnicode_Decode(outbuf, strlen(outbuf),
2802 Py_FileSystemDefaultEncoding, NULL);
2803 }
2804 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002805} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002806
Brian Curtinf5e76d02010-11-24 13:14:05 +00002807/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2808static int has_GetFinalPathNameByHandle = 0;
2809static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2810 DWORD);
2811static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2812 DWORD);
2813static int
2814check_GetFinalPathNameByHandle()
2815{
2816 HINSTANCE hKernel32;
2817 /* only recheck */
2818 if (!has_GetFinalPathNameByHandle)
2819 {
2820 hKernel32 = GetModuleHandle("KERNEL32");
2821 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2822 "GetFinalPathNameByHandleA");
2823 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2824 "GetFinalPathNameByHandleW");
2825 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2826 Py_GetFinalPathNameByHandleW;
2827 }
2828 return has_GetFinalPathNameByHandle;
2829}
2830
Brian Curtind40e6f72010-07-08 21:39:08 +00002831/* A helper function for samepath on windows */
2832static PyObject *
2833posix__getfinalpathname(PyObject *self, PyObject *args)
2834{
2835 HANDLE hFile;
2836 int buf_size;
2837 wchar_t *target_path;
2838 int result_length;
2839 PyObject *result;
2840 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002841
Brian Curtin94622b02010-09-24 00:03:39 +00002842 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002843 return NULL;
2844 }
2845
2846 if(!check_GetFinalPathNameByHandle()) {
2847 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2848 NotImplementedError. */
2849 return PyErr_Format(PyExc_NotImplementedError,
2850 "GetFinalPathNameByHandle not available on this platform");
2851 }
2852
2853 hFile = CreateFileW(
2854 path,
2855 0, /* desired access */
2856 0, /* share mode */
2857 NULL, /* security attributes */
2858 OPEN_EXISTING,
2859 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2860 FILE_FLAG_BACKUP_SEMANTICS,
2861 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002862
Brian Curtind40e6f72010-07-08 21:39:08 +00002863 if(hFile == INVALID_HANDLE_VALUE) {
2864 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002865 return PyErr_Format(PyExc_RuntimeError,
2866 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002867 }
2868
2869 /* We have a good handle to the target, use it to determine the
2870 target path name. */
2871 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2872
2873 if(!buf_size)
2874 return win32_error_unicode("GetFinalPathNameByHandle", path);
2875
2876 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2877 if(!target_path)
2878 return PyErr_NoMemory();
2879
2880 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2881 buf_size, VOLUME_NAME_DOS);
2882 if(!result_length)
2883 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2884
2885 if(!CloseHandle(hFile))
2886 return win32_error_unicode("GetFinalPathNameByHandle", path);
2887
2888 target_path[result_length] = 0;
2889 result = PyUnicode_FromUnicode(target_path, result_length);
2890 free(target_path);
2891 return result;
2892
2893} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002894
2895static PyObject *
2896posix__getfileinformation(PyObject *self, PyObject *args)
2897{
2898 HANDLE hFile;
2899 BY_HANDLE_FILE_INFORMATION info;
2900 int fd;
2901
2902 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2903 return NULL;
2904
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002905 if (!_PyVerify_fd(fd))
2906 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002907
2908 hFile = (HANDLE)_get_osfhandle(fd);
2909 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002910 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002911
2912 if (!GetFileInformationByHandle(hFile, &info))
2913 return win32_error("_getfileinformation", NULL);
2914
2915 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2916 info.nFileIndexHigh,
2917 info.nFileIndexLow);
2918}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002919#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002920
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002921PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002922"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002923Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002924
Barry Warsaw53699e91996-12-10 23:23:01 +00002925static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002926posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002927{
Victor Stinner8c62be82010-05-06 00:08:46 +00002928 int res;
2929 PyObject *opath;
2930 char *path;
2931 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002932
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002933#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002934 PyUnicodeObject *po;
2935 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2936 Py_BEGIN_ALLOW_THREADS
2937 /* PyUnicode_AS_UNICODE OK without thread lock as
2938 it is a simple dereference. */
2939 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2940 Py_END_ALLOW_THREADS
2941 if (!res)
2942 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2943 Py_INCREF(Py_None);
2944 return Py_None;
2945 }
2946 /* Drop the argument parsing error as narrow strings
2947 are also valid. */
2948 PyErr_Clear();
2949 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2950 PyUnicode_FSConverter, &opath, &mode))
2951 return NULL;
2952 path = PyBytes_AsString(opath);
2953 Py_BEGIN_ALLOW_THREADS
2954 /* PyUnicode_AS_UNICODE OK without thread lock as
2955 it is a simple dereference. */
2956 res = CreateDirectoryA(path, NULL);
2957 Py_END_ALLOW_THREADS
2958 if (!res) {
2959 win32_error("mkdir", path);
2960 Py_DECREF(opath);
2961 return NULL;
2962 }
2963 Py_DECREF(opath);
2964 Py_INCREF(Py_None);
2965 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002966#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002967
Victor Stinner8c62be82010-05-06 00:08:46 +00002968 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2969 PyUnicode_FSConverter, &opath, &mode))
2970 return NULL;
2971 path = PyBytes_AsString(opath);
2972 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002973#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002974 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002975#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002976 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002977#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002978 Py_END_ALLOW_THREADS
2979 if (res < 0)
2980 return posix_error_with_allocated_filename(opath);
2981 Py_DECREF(opath);
2982 Py_INCREF(Py_None);
2983 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002984#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002985}
2986
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002987
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002988/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2989#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002990#include <sys/resource.h>
2991#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002992
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002993
2994#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002995PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002996"nice(inc) -> new_priority\n\n\
2997Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002998
Barry Warsaw53699e91996-12-10 23:23:01 +00002999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003000posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003001{
Victor Stinner8c62be82010-05-06 00:08:46 +00003002 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003003
Victor Stinner8c62be82010-05-06 00:08:46 +00003004 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3005 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003006
Victor Stinner8c62be82010-05-06 00:08:46 +00003007 /* There are two flavours of 'nice': one that returns the new
3008 priority (as required by almost all standards out there) and the
3009 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3010 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003011
Victor Stinner8c62be82010-05-06 00:08:46 +00003012 If we are of the nice family that returns the new priority, we
3013 need to clear errno before the call, and check if errno is filled
3014 before calling posix_error() on a returnvalue of -1, because the
3015 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003016
Victor Stinner8c62be82010-05-06 00:08:46 +00003017 errno = 0;
3018 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003019#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003020 if (value == 0)
3021 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003022#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003023 if (value == -1 && errno != 0)
3024 /* either nice() or getpriority() returned an error */
3025 return posix_error();
3026 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003027}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003028#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003029
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003030
3031#ifdef HAVE_GETPRIORITY
3032PyDoc_STRVAR(posix_getpriority__doc__,
3033"getpriority(which, who) -> current_priority\n\n\
3034Get program scheduling priority.");
3035
3036static PyObject *
3037posix_getpriority(PyObject *self, PyObject *args)
3038{
3039 int which, who, retval;
3040
3041 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3042 return NULL;
3043 errno = 0;
3044 Py_BEGIN_ALLOW_THREADS
3045 retval = getpriority(which, who);
3046 Py_END_ALLOW_THREADS
3047 if (errno != 0)
3048 return posix_error();
3049 return PyLong_FromLong((long)retval);
3050}
3051#endif /* HAVE_GETPRIORITY */
3052
3053
3054#ifdef HAVE_SETPRIORITY
3055PyDoc_STRVAR(posix_setpriority__doc__,
3056"setpriority(which, who, prio) -> None\n\n\
3057Set program scheduling priority.");
3058
3059static PyObject *
3060posix_setpriority(PyObject *self, PyObject *args)
3061{
3062 int which, who, prio, retval;
3063
3064 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3065 return NULL;
3066 Py_BEGIN_ALLOW_THREADS
3067 retval = setpriority(which, who, prio);
3068 Py_END_ALLOW_THREADS
3069 if (retval == -1)
3070 return posix_error();
3071 Py_RETURN_NONE;
3072}
3073#endif /* HAVE_SETPRIORITY */
3074
3075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003076PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003077"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003078Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003079
Barry Warsaw53699e91996-12-10 23:23:01 +00003080static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003081posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003082{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003083#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003084 PyObject *o1, *o2;
3085 char *p1, *p2;
3086 BOOL result;
3087 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3088 goto error;
3089 if (!convert_to_unicode(&o1))
3090 goto error;
3091 if (!convert_to_unicode(&o2)) {
3092 Py_DECREF(o1);
3093 goto error;
3094 }
3095 Py_BEGIN_ALLOW_THREADS
3096 result = MoveFileW(PyUnicode_AsUnicode(o1),
3097 PyUnicode_AsUnicode(o2));
3098 Py_END_ALLOW_THREADS
3099 Py_DECREF(o1);
3100 Py_DECREF(o2);
3101 if (!result)
3102 return win32_error("rename", NULL);
3103 Py_INCREF(Py_None);
3104 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003105error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003106 PyErr_Clear();
3107 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3108 return NULL;
3109 Py_BEGIN_ALLOW_THREADS
3110 result = MoveFileA(p1, p2);
3111 Py_END_ALLOW_THREADS
3112 if (!result)
3113 return win32_error("rename", NULL);
3114 Py_INCREF(Py_None);
3115 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003116#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003117 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003118#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003119}
3120
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003122PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003123"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003124Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003125
Barry Warsaw53699e91996-12-10 23:23:01 +00003126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003127posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003128{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003129#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003130 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003131#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003132 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003133#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003134}
3135
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003137PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003138"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003139Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003140
Barry Warsaw53699e91996-12-10 23:23:01 +00003141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003142posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003143{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003144#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003145 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003146#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003147 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003148#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003149}
3150
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003151
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003152#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003153PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003154"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003155Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003156
Barry Warsaw53699e91996-12-10 23:23:01 +00003157static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003158posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003159{
Victor Stinner8c62be82010-05-06 00:08:46 +00003160 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003161#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003162 wchar_t *command;
3163 if (!PyArg_ParseTuple(args, "u:system", &command))
3164 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003165
Victor Stinner8c62be82010-05-06 00:08:46 +00003166 Py_BEGIN_ALLOW_THREADS
3167 sts = _wsystem(command);
3168 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003169#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003170 PyObject *command_obj;
3171 char *command;
3172 if (!PyArg_ParseTuple(args, "O&:system",
3173 PyUnicode_FSConverter, &command_obj))
3174 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003175
Victor Stinner8c62be82010-05-06 00:08:46 +00003176 command = PyBytes_AsString(command_obj);
3177 Py_BEGIN_ALLOW_THREADS
3178 sts = system(command);
3179 Py_END_ALLOW_THREADS
3180 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003181#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003182 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003183}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003184#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003185
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003186
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003187PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003188"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003189Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003190
Barry Warsaw53699e91996-12-10 23:23:01 +00003191static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003192posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003193{
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 int i;
3195 if (!PyArg_ParseTuple(args, "i:umask", &i))
3196 return NULL;
3197 i = (int)umask(i);
3198 if (i < 0)
3199 return posix_error();
3200 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003201}
3202
Brian Curtind40e6f72010-07-08 21:39:08 +00003203#ifdef MS_WINDOWS
3204
3205/* override the default DeleteFileW behavior so that directory
3206symlinks can be removed with this function, the same as with
3207Unix symlinks */
3208BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3209{
3210 WIN32_FILE_ATTRIBUTE_DATA info;
3211 WIN32_FIND_DATAW find_data;
3212 HANDLE find_data_handle;
3213 int is_directory = 0;
3214 int is_link = 0;
3215
3216 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3217 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003218
Brian Curtind40e6f72010-07-08 21:39:08 +00003219 /* Get WIN32_FIND_DATA structure for the path to determine if
3220 it is a symlink */
3221 if(is_directory &&
3222 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3223 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3224
3225 if(find_data_handle != INVALID_HANDLE_VALUE) {
3226 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3227 FindClose(find_data_handle);
3228 }
3229 }
3230 }
3231
3232 if (is_directory && is_link)
3233 return RemoveDirectoryW(lpFileName);
3234
3235 return DeleteFileW(lpFileName);
3236}
3237#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003239PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003240"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003241Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003243PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003244"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003245Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003246
Barry Warsaw53699e91996-12-10 23:23:01 +00003247static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003248posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003249{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003250#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003251 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3252 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003253#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003254 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003255#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003256}
3257
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003258
Guido van Rossumb6775db1994-08-01 11:34:53 +00003259#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003260PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003261"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003262Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003263
Barry Warsaw53699e91996-12-10 23:23:01 +00003264static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003265posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003266{
Victor Stinner8c62be82010-05-06 00:08:46 +00003267 struct utsname u;
3268 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003269
Victor Stinner8c62be82010-05-06 00:08:46 +00003270 Py_BEGIN_ALLOW_THREADS
3271 res = uname(&u);
3272 Py_END_ALLOW_THREADS
3273 if (res < 0)
3274 return posix_error();
3275 return Py_BuildValue("(sssss)",
3276 u.sysname,
3277 u.nodename,
3278 u.release,
3279 u.version,
3280 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003281}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003282#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003283
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003284static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003285extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003286{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003287 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 if (PyFloat_Check(t)) {
3289 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003290 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003291 if (!intobj)
3292 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003293#if SIZEOF_TIME_T > SIZEOF_LONG
3294 intval = PyLong_AsUnsignedLongLongMask(intobj);
3295#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003297#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 Py_DECREF(intobj);
3299 if (intval == -1 && PyErr_Occurred())
3300 return -1;
3301 *sec = intval;
3302 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3303 if (*usec < 0)
3304 /* If rounding gave us a negative number,
3305 truncate. */
3306 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003307 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003308 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003309#if SIZEOF_TIME_T > SIZEOF_LONG
3310 intval = PyLong_AsUnsignedLongLongMask(t);
3311#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003312 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003313#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 if (intval == -1 && PyErr_Occurred())
3315 return -1;
3316 *sec = intval;
3317 *usec = 0;
3318 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003319}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003321PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003322"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003323utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003324Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003325second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003326
Barry Warsaw53699e91996-12-10 23:23:01 +00003327static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003328posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003329{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003330#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003331 PyObject *arg;
3332 PyUnicodeObject *obwpath;
3333 wchar_t *wpath = NULL;
3334 PyObject *oapath;
3335 char *apath;
3336 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003337 time_t atimesec, mtimesec;
3338 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003339 FILETIME atime, mtime;
3340 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003341
Victor Stinner8c62be82010-05-06 00:08:46 +00003342 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3343 wpath = PyUnicode_AS_UNICODE(obwpath);
3344 Py_BEGIN_ALLOW_THREADS
3345 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3346 NULL, OPEN_EXISTING,
3347 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3348 Py_END_ALLOW_THREADS
3349 if (hFile == INVALID_HANDLE_VALUE)
3350 return win32_error_unicode("utime", wpath);
3351 } else
3352 /* Drop the argument parsing error as narrow strings
3353 are also valid. */
3354 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003355
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 if (!wpath) {
3357 if (!PyArg_ParseTuple(args, "O&O:utime",
3358 PyUnicode_FSConverter, &oapath, &arg))
3359 return NULL;
3360 apath = PyBytes_AsString(oapath);
3361 Py_BEGIN_ALLOW_THREADS
3362 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3363 NULL, OPEN_EXISTING,
3364 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3365 Py_END_ALLOW_THREADS
3366 if (hFile == INVALID_HANDLE_VALUE) {
3367 win32_error("utime", apath);
3368 Py_DECREF(oapath);
3369 return NULL;
3370 }
3371 Py_DECREF(oapath);
3372 }
3373
3374 if (arg == Py_None) {
3375 SYSTEMTIME now;
3376 GetSystemTime(&now);
3377 if (!SystemTimeToFileTime(&now, &mtime) ||
3378 !SystemTimeToFileTime(&now, &atime)) {
3379 win32_error("utime", NULL);
3380 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003381 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003382 }
3383 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3384 PyErr_SetString(PyExc_TypeError,
3385 "utime() arg 2 must be a tuple (atime, mtime)");
3386 goto done;
3387 }
3388 else {
3389 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3390 &atimesec, &ausec) == -1)
3391 goto done;
3392 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3393 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3394 &mtimesec, &musec) == -1)
3395 goto done;
3396 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3397 }
3398 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3399 /* Avoid putting the file name into the error here,
3400 as that may confuse the user into believing that
3401 something is wrong with the file, when it also
3402 could be the time stamp that gives a problem. */
3403 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003404 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003405 }
3406 Py_INCREF(Py_None);
3407 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003408done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003409 CloseHandle(hFile);
3410 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003411#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003412
Victor Stinner8c62be82010-05-06 00:08:46 +00003413 PyObject *opath;
3414 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003415 time_t atime, mtime;
3416 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003417 int res;
3418 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003419
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003420#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003421 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003422#define ATIME buf[0].tv_sec
3423#define MTIME buf[1].tv_sec
3424#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003425/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003426 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003427#define ATIME buf.actime
3428#define MTIME buf.modtime
3429#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003430#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003431 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003432#define ATIME buf[0]
3433#define MTIME buf[1]
3434#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003435#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003436
Mark Hammond817c9292003-12-03 01:22:38 +00003437
Victor Stinner8c62be82010-05-06 00:08:46 +00003438 if (!PyArg_ParseTuple(args, "O&O:utime",
3439 PyUnicode_FSConverter, &opath, &arg))
3440 return NULL;
3441 path = PyBytes_AsString(opath);
3442 if (arg == Py_None) {
3443 /* optional time values not given */
3444 Py_BEGIN_ALLOW_THREADS
3445 res = utime(path, NULL);
3446 Py_END_ALLOW_THREADS
3447 }
3448 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3449 PyErr_SetString(PyExc_TypeError,
3450 "utime() arg 2 must be a tuple (atime, mtime)");
3451 Py_DECREF(opath);
3452 return NULL;
3453 }
3454 else {
3455 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3456 &atime, &ausec) == -1) {
3457 Py_DECREF(opath);
3458 return NULL;
3459 }
3460 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3461 &mtime, &musec) == -1) {
3462 Py_DECREF(opath);
3463 return NULL;
3464 }
3465 ATIME = atime;
3466 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003467#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003468 buf[0].tv_usec = ausec;
3469 buf[1].tv_usec = musec;
3470 Py_BEGIN_ALLOW_THREADS
3471 res = utimes(path, buf);
3472 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003473#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003474 Py_BEGIN_ALLOW_THREADS
3475 res = utime(path, UTIME_ARG);
3476 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003477#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003478 }
3479 if (res < 0) {
3480 return posix_error_with_allocated_filename(opath);
3481 }
3482 Py_DECREF(opath);
3483 Py_INCREF(Py_None);
3484 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003485#undef UTIME_ARG
3486#undef ATIME
3487#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003488#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003489}
3490
Guido van Rossum85e3b011991-06-03 12:42:10 +00003491
Guido van Rossum3b066191991-06-04 19:40:25 +00003492/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003494PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003495"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003496Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003497
Barry Warsaw53699e91996-12-10 23:23:01 +00003498static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003499posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003500{
Victor Stinner8c62be82010-05-06 00:08:46 +00003501 int sts;
3502 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3503 return NULL;
3504 _exit(sts);
3505 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003506}
3507
Martin v. Löwis114619e2002-10-07 06:44:21 +00003508#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3509static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003510free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003511{
Victor Stinner8c62be82010-05-06 00:08:46 +00003512 Py_ssize_t i;
3513 for (i = 0; i < count; i++)
3514 PyMem_Free(array[i]);
3515 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003516}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003517
Antoine Pitrou69f71142009-05-24 21:25:49 +00003518static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003519int fsconvert_strdup(PyObject *o, char**out)
3520{
Victor Stinner8c62be82010-05-06 00:08:46 +00003521 PyObject *bytes;
3522 Py_ssize_t size;
3523 if (!PyUnicode_FSConverter(o, &bytes))
3524 return 0;
3525 size = PyBytes_GET_SIZE(bytes);
3526 *out = PyMem_Malloc(size+1);
3527 if (!*out)
3528 return 0;
3529 memcpy(*out, PyBytes_AsString(bytes), size+1);
3530 Py_DECREF(bytes);
3531 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003532}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003533#endif
3534
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003535
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003536#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003537PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003538"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003539Execute an executable path with arguments, replacing current process.\n\
3540\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003541 path: path of executable file\n\
3542 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003543
Barry Warsaw53699e91996-12-10 23:23:01 +00003544static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003545posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003546{
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 PyObject *opath;
3548 char *path;
3549 PyObject *argv;
3550 char **argvlist;
3551 Py_ssize_t i, argc;
3552 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003553
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 /* execv has two arguments: (path, argv), where
3555 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003556
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 if (!PyArg_ParseTuple(args, "O&O:execv",
3558 PyUnicode_FSConverter,
3559 &opath, &argv))
3560 return NULL;
3561 path = PyBytes_AsString(opath);
3562 if (PyList_Check(argv)) {
3563 argc = PyList_Size(argv);
3564 getitem = PyList_GetItem;
3565 }
3566 else if (PyTuple_Check(argv)) {
3567 argc = PyTuple_Size(argv);
3568 getitem = PyTuple_GetItem;
3569 }
3570 else {
3571 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3572 Py_DECREF(opath);
3573 return NULL;
3574 }
3575 if (argc < 1) {
3576 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3577 Py_DECREF(opath);
3578 return NULL;
3579 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003580
Victor Stinner8c62be82010-05-06 00:08:46 +00003581 argvlist = PyMem_NEW(char *, argc+1);
3582 if (argvlist == NULL) {
3583 Py_DECREF(opath);
3584 return PyErr_NoMemory();
3585 }
3586 for (i = 0; i < argc; i++) {
3587 if (!fsconvert_strdup((*getitem)(argv, i),
3588 &argvlist[i])) {
3589 free_string_array(argvlist, i);
3590 PyErr_SetString(PyExc_TypeError,
3591 "execv() arg 2 must contain only strings");
3592 Py_DECREF(opath);
3593 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003594
Victor Stinner8c62be82010-05-06 00:08:46 +00003595 }
3596 }
3597 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003598
Victor Stinner8c62be82010-05-06 00:08:46 +00003599 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003600
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003602
Victor Stinner8c62be82010-05-06 00:08:46 +00003603 free_string_array(argvlist, argc);
3604 Py_DECREF(opath);
3605 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003606}
3607
Victor Stinner13bb71c2010-04-23 21:41:56 +00003608static char**
3609parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3610{
Victor Stinner8c62be82010-05-06 00:08:46 +00003611 char **envlist;
3612 Py_ssize_t i, pos, envc;
3613 PyObject *keys=NULL, *vals=NULL;
3614 PyObject *key, *val, *key2, *val2;
3615 char *p, *k, *v;
3616 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003617
Victor Stinner8c62be82010-05-06 00:08:46 +00003618 i = PyMapping_Size(env);
3619 if (i < 0)
3620 return NULL;
3621 envlist = PyMem_NEW(char *, i + 1);
3622 if (envlist == NULL) {
3623 PyErr_NoMemory();
3624 return NULL;
3625 }
3626 envc = 0;
3627 keys = PyMapping_Keys(env);
3628 vals = PyMapping_Values(env);
3629 if (!keys || !vals)
3630 goto error;
3631 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3632 PyErr_Format(PyExc_TypeError,
3633 "env.keys() or env.values() is not a list");
3634 goto error;
3635 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003636
Victor Stinner8c62be82010-05-06 00:08:46 +00003637 for (pos = 0; pos < i; pos++) {
3638 key = PyList_GetItem(keys, pos);
3639 val = PyList_GetItem(vals, pos);
3640 if (!key || !val)
3641 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003642
Victor Stinner8c62be82010-05-06 00:08:46 +00003643 if (PyUnicode_FSConverter(key, &key2) == 0)
3644 goto error;
3645 if (PyUnicode_FSConverter(val, &val2) == 0) {
3646 Py_DECREF(key2);
3647 goto error;
3648 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003649
3650#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003651 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3652 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003653#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003654 k = PyBytes_AsString(key2);
3655 v = PyBytes_AsString(val2);
3656 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003657
Victor Stinner8c62be82010-05-06 00:08:46 +00003658 p = PyMem_NEW(char, len);
3659 if (p == NULL) {
3660 PyErr_NoMemory();
3661 Py_DECREF(key2);
3662 Py_DECREF(val2);
3663 goto error;
3664 }
3665 PyOS_snprintf(p, len, "%s=%s", k, v);
3666 envlist[envc++] = p;
3667 Py_DECREF(key2);
3668 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003669#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003670 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003671#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003672 }
3673 Py_DECREF(vals);
3674 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003675
Victor Stinner8c62be82010-05-06 00:08:46 +00003676 envlist[envc] = 0;
3677 *envc_ptr = envc;
3678 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003679
3680error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003681 Py_XDECREF(keys);
3682 Py_XDECREF(vals);
3683 while (--envc >= 0)
3684 PyMem_DEL(envlist[envc]);
3685 PyMem_DEL(envlist);
3686 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003687}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003688
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003689PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003690"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003691Execute a path with arguments and environment, replacing current process.\n\
3692\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003693 path: path of executable file\n\
3694 args: tuple or list of arguments\n\
3695 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003696
Barry Warsaw53699e91996-12-10 23:23:01 +00003697static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003698posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003699{
Victor Stinner8c62be82010-05-06 00:08:46 +00003700 PyObject *opath;
3701 char *path;
3702 PyObject *argv, *env;
3703 char **argvlist;
3704 char **envlist;
3705 Py_ssize_t i, argc, envc;
3706 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3707 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003708
Victor Stinner8c62be82010-05-06 00:08:46 +00003709 /* execve has three arguments: (path, argv, env), where
3710 argv is a list or tuple of strings and env is a dictionary
3711 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003712
Victor Stinner8c62be82010-05-06 00:08:46 +00003713 if (!PyArg_ParseTuple(args, "O&OO:execve",
3714 PyUnicode_FSConverter,
3715 &opath, &argv, &env))
3716 return NULL;
3717 path = PyBytes_AsString(opath);
3718 if (PyList_Check(argv)) {
3719 argc = PyList_Size(argv);
3720 getitem = PyList_GetItem;
3721 }
3722 else if (PyTuple_Check(argv)) {
3723 argc = PyTuple_Size(argv);
3724 getitem = PyTuple_GetItem;
3725 }
3726 else {
3727 PyErr_SetString(PyExc_TypeError,
3728 "execve() arg 2 must be a tuple or list");
3729 goto fail_0;
3730 }
3731 if (!PyMapping_Check(env)) {
3732 PyErr_SetString(PyExc_TypeError,
3733 "execve() arg 3 must be a mapping object");
3734 goto fail_0;
3735 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003736
Victor Stinner8c62be82010-05-06 00:08:46 +00003737 argvlist = PyMem_NEW(char *, argc+1);
3738 if (argvlist == NULL) {
3739 PyErr_NoMemory();
3740 goto fail_0;
3741 }
3742 for (i = 0; i < argc; i++) {
3743 if (!fsconvert_strdup((*getitem)(argv, i),
3744 &argvlist[i]))
3745 {
3746 lastarg = i;
3747 goto fail_1;
3748 }
3749 }
3750 lastarg = argc;
3751 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003752
Victor Stinner8c62be82010-05-06 00:08:46 +00003753 envlist = parse_envlist(env, &envc);
3754 if (envlist == NULL)
3755 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003756
Victor Stinner8c62be82010-05-06 00:08:46 +00003757 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003758
Victor Stinner8c62be82010-05-06 00:08:46 +00003759 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003760
Victor Stinner8c62be82010-05-06 00:08:46 +00003761 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003762
Victor Stinner8c62be82010-05-06 00:08:46 +00003763 while (--envc >= 0)
3764 PyMem_DEL(envlist[envc]);
3765 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003766 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003767 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003768 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003769 Py_DECREF(opath);
3770 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003771}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003772#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003773
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003774
Guido van Rossuma1065681999-01-25 23:20:23 +00003775#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003776PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003777"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003778Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003779\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003780 mode: mode of process creation\n\
3781 path: path of executable file\n\
3782 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003783
3784static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003785posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003786{
Victor Stinner8c62be82010-05-06 00:08:46 +00003787 PyObject *opath;
3788 char *path;
3789 PyObject *argv;
3790 char **argvlist;
3791 int mode, i;
3792 Py_ssize_t argc;
3793 Py_intptr_t spawnval;
3794 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003795
Victor Stinner8c62be82010-05-06 00:08:46 +00003796 /* spawnv has three arguments: (mode, path, argv), where
3797 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003798
Victor Stinner8c62be82010-05-06 00:08:46 +00003799 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3800 PyUnicode_FSConverter,
3801 &opath, &argv))
3802 return NULL;
3803 path = PyBytes_AsString(opath);
3804 if (PyList_Check(argv)) {
3805 argc = PyList_Size(argv);
3806 getitem = PyList_GetItem;
3807 }
3808 else if (PyTuple_Check(argv)) {
3809 argc = PyTuple_Size(argv);
3810 getitem = PyTuple_GetItem;
3811 }
3812 else {
3813 PyErr_SetString(PyExc_TypeError,
3814 "spawnv() arg 2 must be a tuple or list");
3815 Py_DECREF(opath);
3816 return NULL;
3817 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003818
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 argvlist = PyMem_NEW(char *, argc+1);
3820 if (argvlist == NULL) {
3821 Py_DECREF(opath);
3822 return PyErr_NoMemory();
3823 }
3824 for (i = 0; i < argc; i++) {
3825 if (!fsconvert_strdup((*getitem)(argv, i),
3826 &argvlist[i])) {
3827 free_string_array(argvlist, i);
3828 PyErr_SetString(
3829 PyExc_TypeError,
3830 "spawnv() arg 2 must contain only strings");
3831 Py_DECREF(opath);
3832 return NULL;
3833 }
3834 }
3835 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003836
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003837#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003838 Py_BEGIN_ALLOW_THREADS
3839 spawnval = spawnv(mode, path, argvlist);
3840 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003841#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003842 if (mode == _OLD_P_OVERLAY)
3843 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003844
Victor Stinner8c62be82010-05-06 00:08:46 +00003845 Py_BEGIN_ALLOW_THREADS
3846 spawnval = _spawnv(mode, path, argvlist);
3847 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003848#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003849
Victor Stinner8c62be82010-05-06 00:08:46 +00003850 free_string_array(argvlist, argc);
3851 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003852
Victor Stinner8c62be82010-05-06 00:08:46 +00003853 if (spawnval == -1)
3854 return posix_error();
3855 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003856#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003857 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003858#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003859 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003860#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003861}
3862
3863
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003864PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003865"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003866Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003867\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003868 mode: mode of process creation\n\
3869 path: path of executable file\n\
3870 args: tuple or list of arguments\n\
3871 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003872
3873static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003874posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003875{
Victor Stinner8c62be82010-05-06 00:08:46 +00003876 PyObject *opath;
3877 char *path;
3878 PyObject *argv, *env;
3879 char **argvlist;
3880 char **envlist;
3881 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003882 int mode;
3883 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003884 Py_intptr_t spawnval;
3885 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3886 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003887
Victor Stinner8c62be82010-05-06 00:08:46 +00003888 /* spawnve has four arguments: (mode, path, argv, env), where
3889 argv is a list or tuple of strings and env is a dictionary
3890 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003891
Victor Stinner8c62be82010-05-06 00:08:46 +00003892 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3893 PyUnicode_FSConverter,
3894 &opath, &argv, &env))
3895 return NULL;
3896 path = PyBytes_AsString(opath);
3897 if (PyList_Check(argv)) {
3898 argc = PyList_Size(argv);
3899 getitem = PyList_GetItem;
3900 }
3901 else if (PyTuple_Check(argv)) {
3902 argc = PyTuple_Size(argv);
3903 getitem = PyTuple_GetItem;
3904 }
3905 else {
3906 PyErr_SetString(PyExc_TypeError,
3907 "spawnve() arg 2 must be a tuple or list");
3908 goto fail_0;
3909 }
3910 if (!PyMapping_Check(env)) {
3911 PyErr_SetString(PyExc_TypeError,
3912 "spawnve() arg 3 must be a mapping object");
3913 goto fail_0;
3914 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003915
Victor Stinner8c62be82010-05-06 00:08:46 +00003916 argvlist = PyMem_NEW(char *, argc+1);
3917 if (argvlist == NULL) {
3918 PyErr_NoMemory();
3919 goto fail_0;
3920 }
3921 for (i = 0; i < argc; i++) {
3922 if (!fsconvert_strdup((*getitem)(argv, i),
3923 &argvlist[i]))
3924 {
3925 lastarg = i;
3926 goto fail_1;
3927 }
3928 }
3929 lastarg = argc;
3930 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003931
Victor Stinner8c62be82010-05-06 00:08:46 +00003932 envlist = parse_envlist(env, &envc);
3933 if (envlist == NULL)
3934 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003935
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003936#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003937 Py_BEGIN_ALLOW_THREADS
3938 spawnval = spawnve(mode, path, argvlist, envlist);
3939 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003940#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003941 if (mode == _OLD_P_OVERLAY)
3942 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003943
Victor Stinner8c62be82010-05-06 00:08:46 +00003944 Py_BEGIN_ALLOW_THREADS
3945 spawnval = _spawnve(mode, path, argvlist, envlist);
3946 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003947#endif
Tim Peters25059d32001-12-07 20:35:43 +00003948
Victor Stinner8c62be82010-05-06 00:08:46 +00003949 if (spawnval == -1)
3950 (void) posix_error();
3951 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003952#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003953 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003954#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003955 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003956#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003957
Victor Stinner8c62be82010-05-06 00:08:46 +00003958 while (--envc >= 0)
3959 PyMem_DEL(envlist[envc]);
3960 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003961 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003962 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003963 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003964 Py_DECREF(opath);
3965 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003966}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003967
3968/* OS/2 supports spawnvp & spawnvpe natively */
3969#if defined(PYOS_OS2)
3970PyDoc_STRVAR(posix_spawnvp__doc__,
3971"spawnvp(mode, file, args)\n\n\
3972Execute the program 'file' in a new process, using the environment\n\
3973search path to find the file.\n\
3974\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003975 mode: mode of process creation\n\
3976 file: executable file name\n\
3977 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003978
3979static PyObject *
3980posix_spawnvp(PyObject *self, PyObject *args)
3981{
Victor Stinner8c62be82010-05-06 00:08:46 +00003982 PyObject *opath;
3983 char *path;
3984 PyObject *argv;
3985 char **argvlist;
3986 int mode, i, argc;
3987 Py_intptr_t spawnval;
3988 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003989
Victor Stinner8c62be82010-05-06 00:08:46 +00003990 /* spawnvp has three arguments: (mode, path, argv), where
3991 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003992
Victor Stinner8c62be82010-05-06 00:08:46 +00003993 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3994 PyUnicode_FSConverter,
3995 &opath, &argv))
3996 return NULL;
3997 path = PyBytes_AsString(opath);
3998 if (PyList_Check(argv)) {
3999 argc = PyList_Size(argv);
4000 getitem = PyList_GetItem;
4001 }
4002 else if (PyTuple_Check(argv)) {
4003 argc = PyTuple_Size(argv);
4004 getitem = PyTuple_GetItem;
4005 }
4006 else {
4007 PyErr_SetString(PyExc_TypeError,
4008 "spawnvp() arg 2 must be a tuple or list");
4009 Py_DECREF(opath);
4010 return NULL;
4011 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004012
Victor Stinner8c62be82010-05-06 00:08:46 +00004013 argvlist = PyMem_NEW(char *, argc+1);
4014 if (argvlist == NULL) {
4015 Py_DECREF(opath);
4016 return PyErr_NoMemory();
4017 }
4018 for (i = 0; i < argc; i++) {
4019 if (!fsconvert_strdup((*getitem)(argv, i),
4020 &argvlist[i])) {
4021 free_string_array(argvlist, i);
4022 PyErr_SetString(
4023 PyExc_TypeError,
4024 "spawnvp() arg 2 must contain only strings");
4025 Py_DECREF(opath);
4026 return NULL;
4027 }
4028 }
4029 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004030
Victor Stinner8c62be82010-05-06 00:08:46 +00004031 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004032#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004033 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004034#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004036#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004037 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004038
Victor Stinner8c62be82010-05-06 00:08:46 +00004039 free_string_array(argvlist, argc);
4040 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004041
Victor Stinner8c62be82010-05-06 00:08:46 +00004042 if (spawnval == -1)
4043 return posix_error();
4044 else
4045 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004046}
4047
4048
4049PyDoc_STRVAR(posix_spawnvpe__doc__,
4050"spawnvpe(mode, file, args, env)\n\n\
4051Execute the program 'file' in a new process, using the environment\n\
4052search path to find the file.\n\
4053\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004054 mode: mode of process creation\n\
4055 file: executable file name\n\
4056 args: tuple or list of arguments\n\
4057 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004058
4059static PyObject *
4060posix_spawnvpe(PyObject *self, PyObject *args)
4061{
Victor Stinner8c62be82010-05-06 00:08:46 +00004062 PyObject *opath
4063 char *path;
4064 PyObject *argv, *env;
4065 char **argvlist;
4066 char **envlist;
4067 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004068 int mode;
4069 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004070 Py_intptr_t spawnval;
4071 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4072 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004073
Victor Stinner8c62be82010-05-06 00:08:46 +00004074 /* spawnvpe has four arguments: (mode, path, argv, env), where
4075 argv is a list or tuple of strings and env is a dictionary
4076 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004077
Victor Stinner8c62be82010-05-06 00:08:46 +00004078 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4079 PyUnicode_FSConverter,
4080 &opath, &argv, &env))
4081 return NULL;
4082 path = PyBytes_AsString(opath);
4083 if (PyList_Check(argv)) {
4084 argc = PyList_Size(argv);
4085 getitem = PyList_GetItem;
4086 }
4087 else if (PyTuple_Check(argv)) {
4088 argc = PyTuple_Size(argv);
4089 getitem = PyTuple_GetItem;
4090 }
4091 else {
4092 PyErr_SetString(PyExc_TypeError,
4093 "spawnvpe() arg 2 must be a tuple or list");
4094 goto fail_0;
4095 }
4096 if (!PyMapping_Check(env)) {
4097 PyErr_SetString(PyExc_TypeError,
4098 "spawnvpe() arg 3 must be a mapping object");
4099 goto fail_0;
4100 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004101
Victor Stinner8c62be82010-05-06 00:08:46 +00004102 argvlist = PyMem_NEW(char *, argc+1);
4103 if (argvlist == NULL) {
4104 PyErr_NoMemory();
4105 goto fail_0;
4106 }
4107 for (i = 0; i < argc; i++) {
4108 if (!fsconvert_strdup((*getitem)(argv, i),
4109 &argvlist[i]))
4110 {
4111 lastarg = i;
4112 goto fail_1;
4113 }
4114 }
4115 lastarg = argc;
4116 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004117
Victor Stinner8c62be82010-05-06 00:08:46 +00004118 envlist = parse_envlist(env, &envc);
4119 if (envlist == NULL)
4120 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004121
Victor Stinner8c62be82010-05-06 00:08:46 +00004122 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004123#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004124 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004125#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004126 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004127#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004128 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004129
Victor Stinner8c62be82010-05-06 00:08:46 +00004130 if (spawnval == -1)
4131 (void) posix_error();
4132 else
4133 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004134
Victor Stinner8c62be82010-05-06 00:08:46 +00004135 while (--envc >= 0)
4136 PyMem_DEL(envlist[envc]);
4137 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004138 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004139 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004140 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004141 Py_DECREF(opath);
4142 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004143}
4144#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004145#endif /* HAVE_SPAWNV */
4146
4147
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004148#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004149PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004150"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004151Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4152\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004153Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004154
4155static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004156posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004157{
Victor Stinner8c62be82010-05-06 00:08:46 +00004158 pid_t pid;
4159 int result = 0;
4160 _PyImport_AcquireLock();
4161 pid = fork1();
4162 if (pid == 0) {
4163 /* child: this clobbers and resets the import lock. */
4164 PyOS_AfterFork();
4165 } else {
4166 /* parent: release the import lock. */
4167 result = _PyImport_ReleaseLock();
4168 }
4169 if (pid == -1)
4170 return posix_error();
4171 if (result < 0) {
4172 /* Don't clobber the OSError if the fork failed. */
4173 PyErr_SetString(PyExc_RuntimeError,
4174 "not holding the import lock");
4175 return NULL;
4176 }
4177 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004178}
4179#endif
4180
4181
Guido van Rossumad0ee831995-03-01 10:34:45 +00004182#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004183PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004184"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004185Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004186Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004187
Barry Warsaw53699e91996-12-10 23:23:01 +00004188static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004189posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004190{
Victor Stinner8c62be82010-05-06 00:08:46 +00004191 pid_t pid;
4192 int result = 0;
4193 _PyImport_AcquireLock();
4194 pid = fork();
4195 if (pid == 0) {
4196 /* child: this clobbers and resets the import lock. */
4197 PyOS_AfterFork();
4198 } else {
4199 /* parent: release the import lock. */
4200 result = _PyImport_ReleaseLock();
4201 }
4202 if (pid == -1)
4203 return posix_error();
4204 if (result < 0) {
4205 /* Don't clobber the OSError if the fork failed. */
4206 PyErr_SetString(PyExc_RuntimeError,
4207 "not holding the import lock");
4208 return NULL;
4209 }
4210 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004211}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004212#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004213
Neal Norwitzb59798b2003-03-21 01:43:31 +00004214/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004215/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4216#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004217#define DEV_PTY_FILE "/dev/ptc"
4218#define HAVE_DEV_PTMX
4219#else
4220#define DEV_PTY_FILE "/dev/ptmx"
4221#endif
4222
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004223#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004224#ifdef HAVE_PTY_H
4225#include <pty.h>
4226#else
4227#ifdef HAVE_LIBUTIL_H
4228#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004229#else
4230#ifdef HAVE_UTIL_H
4231#include <util.h>
4232#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004233#endif /* HAVE_LIBUTIL_H */
4234#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004235#ifdef HAVE_STROPTS_H
4236#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004237#endif
4238#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004239
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004240#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004241PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004242"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004243Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004244
4245static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004246posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004247{
Victor Stinner8c62be82010-05-06 00:08:46 +00004248 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004249#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004250 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004251#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004252#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004253 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004254#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004255 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004256#endif
4257#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004258
Thomas Wouters70c21a12000-07-14 14:28:33 +00004259#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004260 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4261 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004262#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004263 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4264 if (slave_name == NULL)
4265 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004266
Victor Stinner8c62be82010-05-06 00:08:46 +00004267 slave_fd = open(slave_name, O_RDWR);
4268 if (slave_fd < 0)
4269 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004270#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004271 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4272 if (master_fd < 0)
4273 return posix_error();
4274 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4275 /* change permission of slave */
4276 if (grantpt(master_fd) < 0) {
4277 PyOS_setsig(SIGCHLD, sig_saved);
4278 return posix_error();
4279 }
4280 /* unlock slave */
4281 if (unlockpt(master_fd) < 0) {
4282 PyOS_setsig(SIGCHLD, sig_saved);
4283 return posix_error();
4284 }
4285 PyOS_setsig(SIGCHLD, sig_saved);
4286 slave_name = ptsname(master_fd); /* get name of slave */
4287 if (slave_name == NULL)
4288 return posix_error();
4289 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4290 if (slave_fd < 0)
4291 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004292#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004293 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4294 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004295#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004296 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004297#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004298#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004299#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004300
Victor Stinner8c62be82010-05-06 00:08:46 +00004301 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004302
Fred Drake8cef4cf2000-06-28 16:40:38 +00004303}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004304#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004305
4306#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004307PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004308"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004309Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4310Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004311To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004312
4313static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004314posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004315{
Victor Stinner8c62be82010-05-06 00:08:46 +00004316 int master_fd = -1, result = 0;
4317 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004318
Victor Stinner8c62be82010-05-06 00:08:46 +00004319 _PyImport_AcquireLock();
4320 pid = forkpty(&master_fd, NULL, NULL, NULL);
4321 if (pid == 0) {
4322 /* child: this clobbers and resets the import lock. */
4323 PyOS_AfterFork();
4324 } else {
4325 /* parent: release the import lock. */
4326 result = _PyImport_ReleaseLock();
4327 }
4328 if (pid == -1)
4329 return posix_error();
4330 if (result < 0) {
4331 /* Don't clobber the OSError if the fork failed. */
4332 PyErr_SetString(PyExc_RuntimeError,
4333 "not holding the import lock");
4334 return NULL;
4335 }
4336 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004337}
4338#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004339
Guido van Rossumad0ee831995-03-01 10:34:45 +00004340#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004341PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004342"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004343Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004344
Barry Warsaw53699e91996-12-10 23:23:01 +00004345static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004346posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004347{
Victor Stinner8c62be82010-05-06 00:08:46 +00004348 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004349}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004350#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004351
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004352
Guido van Rossumad0ee831995-03-01 10:34:45 +00004353#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004354PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004355"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004356Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004357
Barry Warsaw53699e91996-12-10 23:23:01 +00004358static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004359posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004360{
Victor Stinner8c62be82010-05-06 00:08:46 +00004361 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004362}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004363#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004364
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004365
Guido van Rossumad0ee831995-03-01 10:34:45 +00004366#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004367PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004368"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004369Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004370
Barry Warsaw53699e91996-12-10 23:23:01 +00004371static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004372posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004373{
Victor Stinner8c62be82010-05-06 00:08:46 +00004374 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004375}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004376#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004377
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004379PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004380"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004381Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004382
Barry Warsaw53699e91996-12-10 23:23:01 +00004383static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004384posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004385{
Victor Stinner8c62be82010-05-06 00:08:46 +00004386 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004387}
4388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004389
Fred Drakec9680921999-12-13 16:37:25 +00004390#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004391PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004392"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004393Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004394
4395static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004396posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004397{
4398 PyObject *result = NULL;
4399
Fred Drakec9680921999-12-13 16:37:25 +00004400#ifdef NGROUPS_MAX
4401#define MAX_GROUPS NGROUPS_MAX
4402#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004404#define MAX_GROUPS 64
4405#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004406 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004407
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004408 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004409 * This is a helper variable to store the intermediate result when
4410 * that happens.
4411 *
4412 * To keep the code readable the OSX behaviour is unconditional,
4413 * according to the POSIX spec this should be safe on all unix-y
4414 * systems.
4415 */
4416 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004417 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004418
Victor Stinner8c62be82010-05-06 00:08:46 +00004419 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004420 if (n < 0) {
4421 if (errno == EINVAL) {
4422 n = getgroups(0, NULL);
4423 if (n == -1) {
4424 return posix_error();
4425 }
4426 if (n == 0) {
4427 /* Avoid malloc(0) */
4428 alt_grouplist = grouplist;
4429 } else {
4430 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4431 if (alt_grouplist == NULL) {
4432 errno = EINVAL;
4433 return posix_error();
4434 }
4435 n = getgroups(n, alt_grouplist);
4436 if (n == -1) {
4437 PyMem_Free(alt_grouplist);
4438 return posix_error();
4439 }
4440 }
4441 } else {
4442 return posix_error();
4443 }
4444 }
4445 result = PyList_New(n);
4446 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004447 int i;
4448 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004449 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004450 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004451 Py_DECREF(result);
4452 result = NULL;
4453 break;
Fred Drakec9680921999-12-13 16:37:25 +00004454 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004455 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004456 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004457 }
4458
4459 if (alt_grouplist != grouplist) {
4460 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004461 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004462
Fred Drakec9680921999-12-13 16:37:25 +00004463 return result;
4464}
4465#endif
4466
Antoine Pitroub7572f02009-12-02 20:46:48 +00004467#ifdef HAVE_INITGROUPS
4468PyDoc_STRVAR(posix_initgroups__doc__,
4469"initgroups(username, gid) -> None\n\n\
4470Call the system initgroups() to initialize the group access list with all of\n\
4471the groups of which the specified username is a member, plus the specified\n\
4472group id.");
4473
4474static PyObject *
4475posix_initgroups(PyObject *self, PyObject *args)
4476{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004477 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004478 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004479 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004480 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004481
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004482 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4483 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004484 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004485 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004486
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004487 res = initgroups(username, (gid_t) gid);
4488 Py_DECREF(oname);
4489 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004490 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004491
Victor Stinner8c62be82010-05-06 00:08:46 +00004492 Py_INCREF(Py_None);
4493 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004494}
4495#endif
4496
Martin v. Löwis606edc12002-06-13 21:09:11 +00004497#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004498PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004499"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004500Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004501
4502static PyObject *
4503posix_getpgid(PyObject *self, PyObject *args)
4504{
Victor Stinner8c62be82010-05-06 00:08:46 +00004505 pid_t pid, pgid;
4506 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4507 return NULL;
4508 pgid = getpgid(pid);
4509 if (pgid < 0)
4510 return posix_error();
4511 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004512}
4513#endif /* HAVE_GETPGID */
4514
4515
Guido van Rossumb6775db1994-08-01 11:34:53 +00004516#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004517PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004518"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004519Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004520
Barry Warsaw53699e91996-12-10 23:23:01 +00004521static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004522posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004523{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004524#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004525 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004526#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004527 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004528#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004529}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004530#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004531
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004532
Guido van Rossumb6775db1994-08-01 11:34:53 +00004533#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004534PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004535"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004536Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004537
Barry Warsaw53699e91996-12-10 23:23:01 +00004538static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004539posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004540{
Guido van Rossum64933891994-10-20 21:56:42 +00004541#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004542 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004543#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004544 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004545#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004546 return posix_error();
4547 Py_INCREF(Py_None);
4548 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004549}
4550
Guido van Rossumb6775db1994-08-01 11:34:53 +00004551#endif /* HAVE_SETPGRP */
4552
Guido van Rossumad0ee831995-03-01 10:34:45 +00004553#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004554
4555#ifdef MS_WINDOWS
4556#include <tlhelp32.h>
4557
4558static PyObject*
4559win32_getppid()
4560{
4561 HANDLE snapshot;
4562 pid_t mypid;
4563 PyObject* result = NULL;
4564 BOOL have_record;
4565 PROCESSENTRY32 pe;
4566
4567 mypid = getpid(); /* This function never fails */
4568
4569 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4570 if (snapshot == INVALID_HANDLE_VALUE)
4571 return PyErr_SetFromWindowsErr(GetLastError());
4572
4573 pe.dwSize = sizeof(pe);
4574 have_record = Process32First(snapshot, &pe);
4575 while (have_record) {
4576 if (mypid == (pid_t)pe.th32ProcessID) {
4577 /* We could cache the ulong value in a static variable. */
4578 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4579 break;
4580 }
4581
4582 have_record = Process32Next(snapshot, &pe);
4583 }
4584
4585 /* If our loop exits and our pid was not found (result will be NULL)
4586 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4587 * error anyway, so let's raise it. */
4588 if (!result)
4589 result = PyErr_SetFromWindowsErr(GetLastError());
4590
4591 CloseHandle(snapshot);
4592
4593 return result;
4594}
4595#endif /*MS_WINDOWS*/
4596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004597PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004598"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004599Return the parent's process id. If the parent process has already exited,\n\
4600Windows machines will still return its id; others systems will return the id\n\
4601of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004602
Barry Warsaw53699e91996-12-10 23:23:01 +00004603static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004604posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004605{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004606#ifdef MS_WINDOWS
4607 return win32_getppid();
4608#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004609 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004610#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004611}
4612#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004613
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004614
Fred Drake12c6e2d1999-12-14 21:25:03 +00004615#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004616PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004617"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004618Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004619
4620static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004621posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004622{
Victor Stinner8c62be82010-05-06 00:08:46 +00004623 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004624#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004625 wchar_t user_name[UNLEN + 1];
4626 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4627
4628 if (GetUserNameW(user_name, &num_chars)) {
4629 /* num_chars is the number of unicode chars plus null terminator */
4630 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004631 }
4632 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004633 result = PyErr_SetFromWindowsErr(GetLastError());
4634#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004635 char *name;
4636 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004637
Victor Stinner8c62be82010-05-06 00:08:46 +00004638 errno = 0;
4639 name = getlogin();
4640 if (name == NULL) {
4641 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004642 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004643 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004644 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004645 }
4646 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004647 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004648 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004649#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004650 return result;
4651}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004652#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004653
Guido van Rossumad0ee831995-03-01 10:34:45 +00004654#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004655PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004656"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004657Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004658
Barry Warsaw53699e91996-12-10 23:23:01 +00004659static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004660posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004661{
Victor Stinner8c62be82010-05-06 00:08:46 +00004662 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004663}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004664#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004666
Guido van Rossumad0ee831995-03-01 10:34:45 +00004667#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004668PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004669"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004670Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004671
Barry Warsaw53699e91996-12-10 23:23:01 +00004672static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004673posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004674{
Victor Stinner8c62be82010-05-06 00:08:46 +00004675 pid_t pid;
4676 int sig;
4677 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4678 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004679#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004680 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4681 APIRET rc;
4682 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004683 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004684
4685 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4686 APIRET rc;
4687 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004688 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004689
4690 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004691 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004692#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004693 if (kill(pid, sig) == -1)
4694 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004695#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004696 Py_INCREF(Py_None);
4697 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004698}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004699#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004700
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004701#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004702PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004703"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004704Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004705
4706static PyObject *
4707posix_killpg(PyObject *self, PyObject *args)
4708{
Victor Stinner8c62be82010-05-06 00:08:46 +00004709 int sig;
4710 pid_t pgid;
4711 /* XXX some man pages make the `pgid` parameter an int, others
4712 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4713 take the same type. Moreover, pid_t is always at least as wide as
4714 int (else compilation of this module fails), which is safe. */
4715 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4716 return NULL;
4717 if (killpg(pgid, sig) == -1)
4718 return posix_error();
4719 Py_INCREF(Py_None);
4720 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004721}
4722#endif
4723
Brian Curtineb24d742010-04-12 17:16:38 +00004724#ifdef MS_WINDOWS
4725PyDoc_STRVAR(win32_kill__doc__,
4726"kill(pid, sig)\n\n\
4727Kill a process with a signal.");
4728
4729static PyObject *
4730win32_kill(PyObject *self, PyObject *args)
4731{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004732 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004733 DWORD pid, sig, err;
4734 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004735
Victor Stinner8c62be82010-05-06 00:08:46 +00004736 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4737 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004738
Victor Stinner8c62be82010-05-06 00:08:46 +00004739 /* Console processes which share a common console can be sent CTRL+C or
4740 CTRL+BREAK events, provided they handle said events. */
4741 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4742 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4743 err = GetLastError();
4744 PyErr_SetFromWindowsErr(err);
4745 }
4746 else
4747 Py_RETURN_NONE;
4748 }
Brian Curtineb24d742010-04-12 17:16:38 +00004749
Victor Stinner8c62be82010-05-06 00:08:46 +00004750 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4751 attempt to open and terminate the process. */
4752 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4753 if (handle == NULL) {
4754 err = GetLastError();
4755 return PyErr_SetFromWindowsErr(err);
4756 }
Brian Curtineb24d742010-04-12 17:16:38 +00004757
Victor Stinner8c62be82010-05-06 00:08:46 +00004758 if (TerminateProcess(handle, sig) == 0) {
4759 err = GetLastError();
4760 result = PyErr_SetFromWindowsErr(err);
4761 } else {
4762 Py_INCREF(Py_None);
4763 result = Py_None;
4764 }
Brian Curtineb24d742010-04-12 17:16:38 +00004765
Victor Stinner8c62be82010-05-06 00:08:46 +00004766 CloseHandle(handle);
4767 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004768}
4769#endif /* MS_WINDOWS */
4770
Guido van Rossumc0125471996-06-28 18:55:32 +00004771#ifdef HAVE_PLOCK
4772
4773#ifdef HAVE_SYS_LOCK_H
4774#include <sys/lock.h>
4775#endif
4776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004777PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004778"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004779Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004780
Barry Warsaw53699e91996-12-10 23:23:01 +00004781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004782posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004783{
Victor Stinner8c62be82010-05-06 00:08:46 +00004784 int op;
4785 if (!PyArg_ParseTuple(args, "i:plock", &op))
4786 return NULL;
4787 if (plock(op) == -1)
4788 return posix_error();
4789 Py_INCREF(Py_None);
4790 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004791}
4792#endif
4793
Guido van Rossumb6775db1994-08-01 11:34:53 +00004794#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004795PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004796"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004797Set the current process's user id.");
4798
Barry Warsaw53699e91996-12-10 23:23:01 +00004799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004800posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004801{
Victor Stinner8c62be82010-05-06 00:08:46 +00004802 long uid_arg;
4803 uid_t uid;
4804 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4805 return NULL;
4806 uid = uid_arg;
4807 if (uid != uid_arg) {
4808 PyErr_SetString(PyExc_OverflowError, "user id too big");
4809 return NULL;
4810 }
4811 if (setuid(uid) < 0)
4812 return posix_error();
4813 Py_INCREF(Py_None);
4814 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004815}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004816#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004817
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004818
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004819#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004820PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004821"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004822Set the current process's effective user id.");
4823
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004824static PyObject *
4825posix_seteuid (PyObject *self, PyObject *args)
4826{
Victor Stinner8c62be82010-05-06 00:08:46 +00004827 long euid_arg;
4828 uid_t euid;
4829 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4830 return NULL;
4831 euid = euid_arg;
4832 if (euid != euid_arg) {
4833 PyErr_SetString(PyExc_OverflowError, "user id too big");
4834 return NULL;
4835 }
4836 if (seteuid(euid) < 0) {
4837 return posix_error();
4838 } else {
4839 Py_INCREF(Py_None);
4840 return Py_None;
4841 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004842}
4843#endif /* HAVE_SETEUID */
4844
4845#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004846PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004847"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004848Set the current process's effective group id.");
4849
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004850static PyObject *
4851posix_setegid (PyObject *self, PyObject *args)
4852{
Victor Stinner8c62be82010-05-06 00:08:46 +00004853 long egid_arg;
4854 gid_t egid;
4855 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4856 return NULL;
4857 egid = egid_arg;
4858 if (egid != egid_arg) {
4859 PyErr_SetString(PyExc_OverflowError, "group id too big");
4860 return NULL;
4861 }
4862 if (setegid(egid) < 0) {
4863 return posix_error();
4864 } else {
4865 Py_INCREF(Py_None);
4866 return Py_None;
4867 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004868}
4869#endif /* HAVE_SETEGID */
4870
4871#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004872PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004873"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004874Set the current process's real and effective user ids.");
4875
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004876static PyObject *
4877posix_setreuid (PyObject *self, PyObject *args)
4878{
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 long ruid_arg, euid_arg;
4880 uid_t ruid, euid;
4881 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4882 return NULL;
4883 if (ruid_arg == -1)
4884 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4885 else
4886 ruid = ruid_arg; /* otherwise, assign from our long */
4887 if (euid_arg == -1)
4888 euid = (uid_t)-1;
4889 else
4890 euid = euid_arg;
4891 if ((euid_arg != -1 && euid != euid_arg) ||
4892 (ruid_arg != -1 && ruid != ruid_arg)) {
4893 PyErr_SetString(PyExc_OverflowError, "user id too big");
4894 return NULL;
4895 }
4896 if (setreuid(ruid, euid) < 0) {
4897 return posix_error();
4898 } else {
4899 Py_INCREF(Py_None);
4900 return Py_None;
4901 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004902}
4903#endif /* HAVE_SETREUID */
4904
4905#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004906PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004907"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004908Set the current process's real and effective group ids.");
4909
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004910static PyObject *
4911posix_setregid (PyObject *self, PyObject *args)
4912{
Victor Stinner8c62be82010-05-06 00:08:46 +00004913 long rgid_arg, egid_arg;
4914 gid_t rgid, egid;
4915 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4916 return NULL;
4917 if (rgid_arg == -1)
4918 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4919 else
4920 rgid = rgid_arg; /* otherwise, assign from our long */
4921 if (egid_arg == -1)
4922 egid = (gid_t)-1;
4923 else
4924 egid = egid_arg;
4925 if ((egid_arg != -1 && egid != egid_arg) ||
4926 (rgid_arg != -1 && rgid != rgid_arg)) {
4927 PyErr_SetString(PyExc_OverflowError, "group id too big");
4928 return NULL;
4929 }
4930 if (setregid(rgid, egid) < 0) {
4931 return posix_error();
4932 } else {
4933 Py_INCREF(Py_None);
4934 return Py_None;
4935 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004936}
4937#endif /* HAVE_SETREGID */
4938
Guido van Rossumb6775db1994-08-01 11:34:53 +00004939#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004940PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004941"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004942Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004943
Barry Warsaw53699e91996-12-10 23:23:01 +00004944static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004945posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004946{
Victor Stinner8c62be82010-05-06 00:08:46 +00004947 long gid_arg;
4948 gid_t gid;
4949 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4950 return NULL;
4951 gid = gid_arg;
4952 if (gid != gid_arg) {
4953 PyErr_SetString(PyExc_OverflowError, "group id too big");
4954 return NULL;
4955 }
4956 if (setgid(gid) < 0)
4957 return posix_error();
4958 Py_INCREF(Py_None);
4959 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004960}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004961#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004962
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004963#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004964PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004965"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004966Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004967
4968static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004969posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004970{
Victor Stinner8c62be82010-05-06 00:08:46 +00004971 int i, len;
4972 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004973
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 if (!PySequence_Check(groups)) {
4975 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4976 return NULL;
4977 }
4978 len = PySequence_Size(groups);
4979 if (len > MAX_GROUPS) {
4980 PyErr_SetString(PyExc_ValueError, "too many groups");
4981 return NULL;
4982 }
4983 for(i = 0; i < len; i++) {
4984 PyObject *elem;
4985 elem = PySequence_GetItem(groups, i);
4986 if (!elem)
4987 return NULL;
4988 if (!PyLong_Check(elem)) {
4989 PyErr_SetString(PyExc_TypeError,
4990 "groups must be integers");
4991 Py_DECREF(elem);
4992 return NULL;
4993 } else {
4994 unsigned long x = PyLong_AsUnsignedLong(elem);
4995 if (PyErr_Occurred()) {
4996 PyErr_SetString(PyExc_TypeError,
4997 "group id too big");
4998 Py_DECREF(elem);
4999 return NULL;
5000 }
5001 grouplist[i] = x;
5002 /* read back the value to see if it fitted in gid_t */
5003 if (grouplist[i] != x) {
5004 PyErr_SetString(PyExc_TypeError,
5005 "group id too big");
5006 Py_DECREF(elem);
5007 return NULL;
5008 }
5009 }
5010 Py_DECREF(elem);
5011 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005012
Victor Stinner8c62be82010-05-06 00:08:46 +00005013 if (setgroups(len, grouplist) < 0)
5014 return posix_error();
5015 Py_INCREF(Py_None);
5016 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005017}
5018#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005019
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005020#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5021static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005022wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005023{
Victor Stinner8c62be82010-05-06 00:08:46 +00005024 PyObject *result;
5025 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005026
Victor Stinner8c62be82010-05-06 00:08:46 +00005027 if (pid == -1)
5028 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005029
Victor Stinner8c62be82010-05-06 00:08:46 +00005030 if (struct_rusage == NULL) {
5031 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5032 if (m == NULL)
5033 return NULL;
5034 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5035 Py_DECREF(m);
5036 if (struct_rusage == NULL)
5037 return NULL;
5038 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005039
Victor Stinner8c62be82010-05-06 00:08:46 +00005040 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5041 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5042 if (!result)
5043 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005044
5045#ifndef doubletime
5046#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5047#endif
5048
Victor Stinner8c62be82010-05-06 00:08:46 +00005049 PyStructSequence_SET_ITEM(result, 0,
5050 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5051 PyStructSequence_SET_ITEM(result, 1,
5052 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005053#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005054 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5055 SET_INT(result, 2, ru->ru_maxrss);
5056 SET_INT(result, 3, ru->ru_ixrss);
5057 SET_INT(result, 4, ru->ru_idrss);
5058 SET_INT(result, 5, ru->ru_isrss);
5059 SET_INT(result, 6, ru->ru_minflt);
5060 SET_INT(result, 7, ru->ru_majflt);
5061 SET_INT(result, 8, ru->ru_nswap);
5062 SET_INT(result, 9, ru->ru_inblock);
5063 SET_INT(result, 10, ru->ru_oublock);
5064 SET_INT(result, 11, ru->ru_msgsnd);
5065 SET_INT(result, 12, ru->ru_msgrcv);
5066 SET_INT(result, 13, ru->ru_nsignals);
5067 SET_INT(result, 14, ru->ru_nvcsw);
5068 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005069#undef SET_INT
5070
Victor Stinner8c62be82010-05-06 00:08:46 +00005071 if (PyErr_Occurred()) {
5072 Py_DECREF(result);
5073 return NULL;
5074 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005075
Victor Stinner8c62be82010-05-06 00:08:46 +00005076 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005077}
5078#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5079
5080#ifdef HAVE_WAIT3
5081PyDoc_STRVAR(posix_wait3__doc__,
5082"wait3(options) -> (pid, status, rusage)\n\n\
5083Wait for completion of a child process.");
5084
5085static PyObject *
5086posix_wait3(PyObject *self, PyObject *args)
5087{
Victor Stinner8c62be82010-05-06 00:08:46 +00005088 pid_t pid;
5089 int options;
5090 struct rusage ru;
5091 WAIT_TYPE status;
5092 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005093
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5095 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005096
Victor Stinner8c62be82010-05-06 00:08:46 +00005097 Py_BEGIN_ALLOW_THREADS
5098 pid = wait3(&status, options, &ru);
5099 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005100
Victor Stinner8c62be82010-05-06 00:08:46 +00005101 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005102}
5103#endif /* HAVE_WAIT3 */
5104
5105#ifdef HAVE_WAIT4
5106PyDoc_STRVAR(posix_wait4__doc__,
5107"wait4(pid, options) -> (pid, status, rusage)\n\n\
5108Wait for completion of a given child process.");
5109
5110static PyObject *
5111posix_wait4(PyObject *self, PyObject *args)
5112{
Victor Stinner8c62be82010-05-06 00:08:46 +00005113 pid_t pid;
5114 int options;
5115 struct rusage ru;
5116 WAIT_TYPE status;
5117 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005118
Victor Stinner8c62be82010-05-06 00:08:46 +00005119 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5120 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005121
Victor Stinner8c62be82010-05-06 00:08:46 +00005122 Py_BEGIN_ALLOW_THREADS
5123 pid = wait4(pid, &status, options, &ru);
5124 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005125
Victor Stinner8c62be82010-05-06 00:08:46 +00005126 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005127}
5128#endif /* HAVE_WAIT4 */
5129
Guido van Rossumb6775db1994-08-01 11:34:53 +00005130#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005131PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005132"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005133Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005134
Barry Warsaw53699e91996-12-10 23:23:01 +00005135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005136posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005137{
Victor Stinner8c62be82010-05-06 00:08:46 +00005138 pid_t pid;
5139 int options;
5140 WAIT_TYPE status;
5141 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005142
Victor Stinner8c62be82010-05-06 00:08:46 +00005143 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5144 return NULL;
5145 Py_BEGIN_ALLOW_THREADS
5146 pid = waitpid(pid, &status, options);
5147 Py_END_ALLOW_THREADS
5148 if (pid == -1)
5149 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005150
Victor Stinner8c62be82010-05-06 00:08:46 +00005151 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005152}
5153
Tim Petersab034fa2002-02-01 11:27:43 +00005154#elif defined(HAVE_CWAIT)
5155
5156/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005157PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005158"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005159"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005160
5161static PyObject *
5162posix_waitpid(PyObject *self, PyObject *args)
5163{
Victor Stinner8c62be82010-05-06 00:08:46 +00005164 Py_intptr_t pid;
5165 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005166
Victor Stinner8c62be82010-05-06 00:08:46 +00005167 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5168 return NULL;
5169 Py_BEGIN_ALLOW_THREADS
5170 pid = _cwait(&status, pid, options);
5171 Py_END_ALLOW_THREADS
5172 if (pid == -1)
5173 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005174
Victor Stinner8c62be82010-05-06 00:08:46 +00005175 /* shift the status left a byte so this is more like the POSIX waitpid */
5176 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005177}
5178#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005179
Guido van Rossumad0ee831995-03-01 10:34:45 +00005180#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005181PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005182"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005183Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005184
Barry Warsaw53699e91996-12-10 23:23:01 +00005185static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005186posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005187{
Victor Stinner8c62be82010-05-06 00:08:46 +00005188 pid_t pid;
5189 WAIT_TYPE status;
5190 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005191
Victor Stinner8c62be82010-05-06 00:08:46 +00005192 Py_BEGIN_ALLOW_THREADS
5193 pid = wait(&status);
5194 Py_END_ALLOW_THREADS
5195 if (pid == -1)
5196 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005197
Victor Stinner8c62be82010-05-06 00:08:46 +00005198 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005199}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005200#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005201
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005202
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005203PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005204"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005205Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005206
Barry Warsaw53699e91996-12-10 23:23:01 +00005207static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005208posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005209{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005210#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005211 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005212#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005213#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005214 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5215 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005216#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005217 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005218#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005219#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005220}
5221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005222
Guido van Rossumb6775db1994-08-01 11:34:53 +00005223#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005224PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005225"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005226Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005227
Barry Warsaw53699e91996-12-10 23:23:01 +00005228static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005229posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005230{
Victor Stinner8c62be82010-05-06 00:08:46 +00005231 PyObject* v;
5232 char buf[MAXPATHLEN];
5233 PyObject *opath;
5234 char *path;
5235 int n;
5236 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005237
Victor Stinner8c62be82010-05-06 00:08:46 +00005238 if (!PyArg_ParseTuple(args, "O&:readlink",
5239 PyUnicode_FSConverter, &opath))
5240 return NULL;
5241 path = PyBytes_AsString(opath);
5242 v = PySequence_GetItem(args, 0);
5243 if (v == NULL) {
5244 Py_DECREF(opath);
5245 return NULL;
5246 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005247
Victor Stinner8c62be82010-05-06 00:08:46 +00005248 if (PyUnicode_Check(v)) {
5249 arg_is_unicode = 1;
5250 }
5251 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005252
Victor Stinner8c62be82010-05-06 00:08:46 +00005253 Py_BEGIN_ALLOW_THREADS
5254 n = readlink(path, buf, (int) sizeof buf);
5255 Py_END_ALLOW_THREADS
5256 if (n < 0)
5257 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005258
Victor Stinner8c62be82010-05-06 00:08:46 +00005259 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005260 if (arg_is_unicode)
5261 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5262 else
5263 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005264}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005265#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005266
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005267
Brian Curtin52173d42010-12-02 18:29:18 +00005268#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005269PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005270"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005271Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005272
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005274posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005275{
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005277}
5278#endif /* HAVE_SYMLINK */
5279
Brian Curtind40e6f72010-07-08 21:39:08 +00005280#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5281
5282PyDoc_STRVAR(win_readlink__doc__,
5283"readlink(path) -> path\n\n\
5284Return a string representing the path to which the symbolic link points.");
5285
Brian Curtind40e6f72010-07-08 21:39:08 +00005286/* Windows readlink implementation */
5287static PyObject *
5288win_readlink(PyObject *self, PyObject *args)
5289{
5290 wchar_t *path;
5291 DWORD n_bytes_returned;
5292 DWORD io_result;
5293 PyObject *result;
5294 HANDLE reparse_point_handle;
5295
5296 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5297 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5298 wchar_t *print_name;
5299
5300 if (!PyArg_ParseTuple(args,
5301 "u:readlink",
5302 &path))
5303 return NULL;
5304
5305 /* First get a handle to the reparse point */
5306 Py_BEGIN_ALLOW_THREADS
5307 reparse_point_handle = CreateFileW(
5308 path,
5309 0,
5310 0,
5311 0,
5312 OPEN_EXISTING,
5313 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5314 0);
5315 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005316
Brian Curtind40e6f72010-07-08 21:39:08 +00005317 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5318 {
5319 return win32_error_unicode("readlink", path);
5320 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005321
Brian Curtind40e6f72010-07-08 21:39:08 +00005322 Py_BEGIN_ALLOW_THREADS
5323 /* New call DeviceIoControl to read the reparse point */
5324 io_result = DeviceIoControl(
5325 reparse_point_handle,
5326 FSCTL_GET_REPARSE_POINT,
5327 0, 0, /* in buffer */
5328 target_buffer, sizeof(target_buffer),
5329 &n_bytes_returned,
5330 0 /* we're not using OVERLAPPED_IO */
5331 );
5332 CloseHandle(reparse_point_handle);
5333 Py_END_ALLOW_THREADS
5334
5335 if (io_result==0)
5336 {
5337 return win32_error_unicode("readlink", path);
5338 }
5339
5340 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5341 {
5342 PyErr_SetString(PyExc_ValueError,
5343 "not a symbolic link");
5344 return NULL;
5345 }
Brian Curtin74e45612010-07-09 15:58:59 +00005346 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5347 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5348
5349 result = PyUnicode_FromWideChar(print_name,
5350 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005351 return result;
5352}
5353
5354#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5355
Brian Curtin52173d42010-12-02 18:29:18 +00005356#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005357
5358/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5359static int has_CreateSymbolicLinkW = 0;
5360static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5361static int
5362check_CreateSymbolicLinkW()
5363{
5364 HINSTANCE hKernel32;
5365 /* only recheck */
5366 if (has_CreateSymbolicLinkW)
5367 return has_CreateSymbolicLinkW;
5368 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005369 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5370 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005371 if (Py_CreateSymbolicLinkW)
5372 has_CreateSymbolicLinkW = 1;
5373 return has_CreateSymbolicLinkW;
5374}
5375
5376PyDoc_STRVAR(win_symlink__doc__,
5377"symlink(src, dst, target_is_directory=False)\n\n\
5378Create a symbolic link pointing to src named dst.\n\
5379target_is_directory is required if the target is to be interpreted as\n\
5380a directory.\n\
5381This function requires Windows 6.0 or greater, and raises a\n\
5382NotImplementedError otherwise.");
5383
5384static PyObject *
5385win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5386{
5387 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5388 PyObject *src, *dest;
5389 int target_is_directory = 0;
5390 DWORD res;
5391 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005392
Brian Curtind40e6f72010-07-08 21:39:08 +00005393 if (!check_CreateSymbolicLinkW())
5394 {
5395 /* raise NotImplementedError */
5396 return PyErr_Format(PyExc_NotImplementedError,
5397 "CreateSymbolicLinkW not found");
5398 }
5399 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5400 kwlist, &src, &dest, &target_is_directory))
5401 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005402
5403 if (win32_can_symlink == 0)
5404 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5405
Brian Curtind40e6f72010-07-08 21:39:08 +00005406 if (!convert_to_unicode(&src)) { return NULL; }
5407 if (!convert_to_unicode(&dest)) {
5408 Py_DECREF(src);
5409 return NULL;
5410 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005411
Brian Curtind40e6f72010-07-08 21:39:08 +00005412 /* if src is a directory, ensure target_is_directory==1 */
5413 if(
5414 GetFileAttributesExW(
5415 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5416 ))
5417 {
5418 target_is_directory = target_is_directory ||
5419 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5420 }
5421
5422 Py_BEGIN_ALLOW_THREADS
5423 res = Py_CreateSymbolicLinkW(
5424 PyUnicode_AsUnicode(dest),
5425 PyUnicode_AsUnicode(src),
5426 target_is_directory);
5427 Py_END_ALLOW_THREADS
5428 Py_DECREF(src);
5429 Py_DECREF(dest);
5430 if (!res)
5431 {
5432 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5433 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005434
Brian Curtind40e6f72010-07-08 21:39:08 +00005435 Py_INCREF(Py_None);
5436 return Py_None;
5437}
Brian Curtin52173d42010-12-02 18:29:18 +00005438#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005439
5440#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005441#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5442static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005443system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005444{
5445 ULONG value = 0;
5446
5447 Py_BEGIN_ALLOW_THREADS
5448 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5449 Py_END_ALLOW_THREADS
5450
5451 return value;
5452}
5453
5454static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005455posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005456{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005457 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005458 return Py_BuildValue("ddddd",
5459 (double)0 /* t.tms_utime / HZ */,
5460 (double)0 /* t.tms_stime / HZ */,
5461 (double)0 /* t.tms_cutime / HZ */,
5462 (double)0 /* t.tms_cstime / HZ */,
5463 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005464}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005465#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005466#define NEED_TICKS_PER_SECOND
5467static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005468static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005469posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005470{
Victor Stinner8c62be82010-05-06 00:08:46 +00005471 struct tms t;
5472 clock_t c;
5473 errno = 0;
5474 c = times(&t);
5475 if (c == (clock_t) -1)
5476 return posix_error();
5477 return Py_BuildValue("ddddd",
5478 (double)t.tms_utime / ticks_per_second,
5479 (double)t.tms_stime / ticks_per_second,
5480 (double)t.tms_cutime / ticks_per_second,
5481 (double)t.tms_cstime / ticks_per_second,
5482 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005483}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005484#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005485#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005486
5487
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005488#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005489#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005490static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005491posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005492{
Victor Stinner8c62be82010-05-06 00:08:46 +00005493 FILETIME create, exit, kernel, user;
5494 HANDLE hProc;
5495 hProc = GetCurrentProcess();
5496 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5497 /* The fields of a FILETIME structure are the hi and lo part
5498 of a 64-bit value expressed in 100 nanosecond units.
5499 1e7 is one second in such units; 1e-7 the inverse.
5500 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5501 */
5502 return Py_BuildValue(
5503 "ddddd",
5504 (double)(user.dwHighDateTime*429.4967296 +
5505 user.dwLowDateTime*1e-7),
5506 (double)(kernel.dwHighDateTime*429.4967296 +
5507 kernel.dwLowDateTime*1e-7),
5508 (double)0,
5509 (double)0,
5510 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005511}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005512#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005513
5514#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005515PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005516"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005517Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005518#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005519
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005520
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005521#ifdef HAVE_GETSID
5522PyDoc_STRVAR(posix_getsid__doc__,
5523"getsid(pid) -> sid\n\n\
5524Call the system call getsid().");
5525
5526static PyObject *
5527posix_getsid(PyObject *self, PyObject *args)
5528{
Victor Stinner8c62be82010-05-06 00:08:46 +00005529 pid_t pid;
5530 int sid;
5531 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5532 return NULL;
5533 sid = getsid(pid);
5534 if (sid < 0)
5535 return posix_error();
5536 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005537}
5538#endif /* HAVE_GETSID */
5539
5540
Guido van Rossumb6775db1994-08-01 11:34:53 +00005541#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005542PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005543"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005544Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005545
Barry Warsaw53699e91996-12-10 23:23:01 +00005546static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005547posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005548{
Victor Stinner8c62be82010-05-06 00:08:46 +00005549 if (setsid() < 0)
5550 return posix_error();
5551 Py_INCREF(Py_None);
5552 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005553}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005554#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005555
Guido van Rossumb6775db1994-08-01 11:34:53 +00005556#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005557PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005558"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005560
Barry Warsaw53699e91996-12-10 23:23:01 +00005561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005562posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005563{
Victor Stinner8c62be82010-05-06 00:08:46 +00005564 pid_t pid;
5565 int pgrp;
5566 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5567 return NULL;
5568 if (setpgid(pid, pgrp) < 0)
5569 return posix_error();
5570 Py_INCREF(Py_None);
5571 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005572}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005573#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005574
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005575
Guido van Rossumb6775db1994-08-01 11:34:53 +00005576#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005577PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005578"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005579Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005580
Barry Warsaw53699e91996-12-10 23:23:01 +00005581static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005582posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005583{
Victor Stinner8c62be82010-05-06 00:08:46 +00005584 int fd;
5585 pid_t pgid;
5586 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5587 return NULL;
5588 pgid = tcgetpgrp(fd);
5589 if (pgid < 0)
5590 return posix_error();
5591 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005592}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005593#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005594
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005595
Guido van Rossumb6775db1994-08-01 11:34:53 +00005596#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005597PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005598"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005599Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005600
Barry Warsaw53699e91996-12-10 23:23:01 +00005601static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005602posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005603{
Victor Stinner8c62be82010-05-06 00:08:46 +00005604 int fd;
5605 pid_t pgid;
5606 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5607 return NULL;
5608 if (tcsetpgrp(fd, pgid) < 0)
5609 return posix_error();
5610 Py_INCREF(Py_None);
5611 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005612}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005613#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005614
Guido van Rossum687dd131993-05-17 08:34:16 +00005615/* Functions acting on file descriptors */
5616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005617PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005618"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005619Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005620
Barry Warsaw53699e91996-12-10 23:23:01 +00005621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005622posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005623{
Victor Stinner8c62be82010-05-06 00:08:46 +00005624 PyObject *ofile;
5625 char *file;
5626 int flag;
5627 int mode = 0777;
5628 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005629
5630#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005631 PyUnicodeObject *po;
5632 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5633 Py_BEGIN_ALLOW_THREADS
5634 /* PyUnicode_AS_UNICODE OK without thread
5635 lock as it is a simple dereference. */
5636 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5637 Py_END_ALLOW_THREADS
5638 if (fd < 0)
5639 return posix_error();
5640 return PyLong_FromLong((long)fd);
5641 }
5642 /* Drop the argument parsing error as narrow strings
5643 are also valid. */
5644 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005645#endif
5646
Victor Stinner8c62be82010-05-06 00:08:46 +00005647 if (!PyArg_ParseTuple(args, "O&i|i",
5648 PyUnicode_FSConverter, &ofile,
5649 &flag, &mode))
5650 return NULL;
5651 file = PyBytes_AsString(ofile);
5652 Py_BEGIN_ALLOW_THREADS
5653 fd = open(file, flag, mode);
5654 Py_END_ALLOW_THREADS
5655 if (fd < 0)
5656 return posix_error_with_allocated_filename(ofile);
5657 Py_DECREF(ofile);
5658 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005659}
5660
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005662PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005663"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005664Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005665
Barry Warsaw53699e91996-12-10 23:23:01 +00005666static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005667posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005668{
Victor Stinner8c62be82010-05-06 00:08:46 +00005669 int fd, res;
5670 if (!PyArg_ParseTuple(args, "i:close", &fd))
5671 return NULL;
5672 if (!_PyVerify_fd(fd))
5673 return posix_error();
5674 Py_BEGIN_ALLOW_THREADS
5675 res = close(fd);
5676 Py_END_ALLOW_THREADS
5677 if (res < 0)
5678 return posix_error();
5679 Py_INCREF(Py_None);
5680 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005681}
5682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005683
Victor Stinner8c62be82010-05-06 00:08:46 +00005684PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005685"closerange(fd_low, fd_high)\n\n\
5686Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5687
5688static PyObject *
5689posix_closerange(PyObject *self, PyObject *args)
5690{
Victor Stinner8c62be82010-05-06 00:08:46 +00005691 int fd_from, fd_to, i;
5692 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5693 return NULL;
5694 Py_BEGIN_ALLOW_THREADS
5695 for (i = fd_from; i < fd_to; i++)
5696 if (_PyVerify_fd(i))
5697 close(i);
5698 Py_END_ALLOW_THREADS
5699 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005700}
5701
5702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005703PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005704"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005705Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005706
Barry Warsaw53699e91996-12-10 23:23:01 +00005707static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005708posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005709{
Victor Stinner8c62be82010-05-06 00:08:46 +00005710 int fd;
5711 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5712 return NULL;
5713 if (!_PyVerify_fd(fd))
5714 return posix_error();
5715 Py_BEGIN_ALLOW_THREADS
5716 fd = dup(fd);
5717 Py_END_ALLOW_THREADS
5718 if (fd < 0)
5719 return posix_error();
5720 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005721}
5722
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005724PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005725"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005726Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005727
Barry Warsaw53699e91996-12-10 23:23:01 +00005728static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005729posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005730{
Victor Stinner8c62be82010-05-06 00:08:46 +00005731 int fd, fd2, res;
5732 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5733 return NULL;
5734 if (!_PyVerify_fd_dup2(fd, fd2))
5735 return posix_error();
5736 Py_BEGIN_ALLOW_THREADS
5737 res = dup2(fd, fd2);
5738 Py_END_ALLOW_THREADS
5739 if (res < 0)
5740 return posix_error();
5741 Py_INCREF(Py_None);
5742 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005743}
5744
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005746PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005747"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005748Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005749
Barry Warsaw53699e91996-12-10 23:23:01 +00005750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005751posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005752{
Victor Stinner8c62be82010-05-06 00:08:46 +00005753 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005754#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005755 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005756#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005757 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005758#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005759 PyObject *posobj;
5760 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5761 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005762#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005763 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5764 switch (how) {
5765 case 0: how = SEEK_SET; break;
5766 case 1: how = SEEK_CUR; break;
5767 case 2: how = SEEK_END; break;
5768 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005769#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005770
5771#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005772 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005773#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +00005774 pos = PyLong_AsLongLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005775#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005776 if (PyErr_Occurred())
5777 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005778
Victor Stinner8c62be82010-05-06 00:08:46 +00005779 if (!_PyVerify_fd(fd))
5780 return posix_error();
5781 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005782#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005783 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005784#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005786#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 Py_END_ALLOW_THREADS
5788 if (res < 0)
5789 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005790
5791#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005792 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005793#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005794 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005795#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005796}
5797
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005799PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005800"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005802
Barry Warsaw53699e91996-12-10 23:23:01 +00005803static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005804posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005805{
Victor Stinner8c62be82010-05-06 00:08:46 +00005806 int fd, size;
5807 Py_ssize_t n;
5808 PyObject *buffer;
5809 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5810 return NULL;
5811 if (size < 0) {
5812 errno = EINVAL;
5813 return posix_error();
5814 }
5815 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5816 if (buffer == NULL)
5817 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005818 if (!_PyVerify_fd(fd)) {
5819 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005820 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005821 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005822 Py_BEGIN_ALLOW_THREADS
5823 n = read(fd, PyBytes_AS_STRING(buffer), size);
5824 Py_END_ALLOW_THREADS
5825 if (n < 0) {
5826 Py_DECREF(buffer);
5827 return posix_error();
5828 }
5829 if (n != size)
5830 _PyBytes_Resize(&buffer, n);
5831 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005832}
5833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005835PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005836"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005837Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005838
Barry Warsaw53699e91996-12-10 23:23:01 +00005839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005840posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005841{
Victor Stinner8c62be82010-05-06 00:08:46 +00005842 Py_buffer pbuf;
5843 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005844 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005845
Victor Stinner8c62be82010-05-06 00:08:46 +00005846 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5847 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005848 if (!_PyVerify_fd(fd)) {
5849 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005850 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005851 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005852 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005853 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005854#if defined(MS_WIN64) || defined(MS_WINDOWS)
5855 if (len > INT_MAX)
5856 len = INT_MAX;
5857 size = write(fd, pbuf.buf, (int)len);
5858#else
Victor Stinner72344792011-01-11 00:04:12 +00005859 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005860#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005861 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005862 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005863 if (size < 0)
5864 return posix_error();
5865 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005866}
5867
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005868#ifdef HAVE_SENDFILE
5869#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
5870static int
5871iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
5872{
5873 int i, j;
5874 *iov = PyMem_New(struct iovec, cnt);
5875 if (*iov == NULL) {
5876 PyErr_NoMemory();
5877 return 0;
5878 }
5879 *buf = PyMem_New(Py_buffer, cnt);
5880 if (*buf == NULL) {
5881 PyMem_Del(*iov);
5882 PyErr_NoMemory();
5883 return 0;
5884 }
5885
5886 for (i = 0; i < cnt; i++) {
5887 if (PyObject_GetBuffer(PySequence_GetItem(seq, i), &(*buf)[i],
5888 type) == -1) {
5889 PyMem_Del(*iov);
5890 for (j = 0; j < i; j++) {
5891 PyBuffer_Release(&(*buf)[j]);
5892 }
5893 PyMem_Del(*buf);
5894 return 0;
5895 }
5896 (*iov)[i].iov_base = (*buf)[i].buf;
5897 (*iov)[i].iov_len = (*buf)[i].len;
5898 }
5899 return 1;
5900}
5901
5902static void
5903iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
5904{
5905 int i;
5906 PyMem_Del(iov);
5907 for (i = 0; i < cnt; i++) {
5908 PyBuffer_Release(&buf[i]);
5909 }
5910 PyMem_Del(buf);
5911}
5912#endif
5913
5914PyDoc_STRVAR(posix_sendfile__doc__,
5915"sendfile(out, in, offset, nbytes) -> byteswritten\n\
5916sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
5917 -> byteswritten\n\
5918Copy nbytes bytes from file descriptor in to file descriptor out.");
5919
5920static PyObject *
5921posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
5922{
5923 int in, out;
5924 Py_ssize_t ret;
5925 off_t offset;
5926
5927#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
5928#ifndef __APPLE__
5929 Py_ssize_t len;
5930#endif
5931 PyObject *headers = NULL, *trailers = NULL;
5932 Py_buffer *hbuf, *tbuf;
5933 off_t sbytes;
5934 struct sf_hdtr sf;
5935 int flags = 0;
5936 sf.headers = NULL;
5937 sf.trailers = NULL;
Benjamin Petersonc6696d22011-02-26 21:32:16 +00005938 static char *keywords[] = {"out", "in", "offset", "count", "headers",
5939 "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005940
5941#ifdef __APPLE__
5942 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00005943 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005944#else
5945 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00005946 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005947#endif
5948 &headers, &trailers, &flags))
5949 return NULL;
5950 if (headers != NULL) {
5951 if (!PySequence_Check(headers)) {
5952 PyErr_SetString(PyExc_TypeError,
5953 "sendfile() headers must be a sequence or None");
5954 return NULL;
5955 } else {
5956 sf.hdr_cnt = PySequence_Size(headers);
5957 if (sf.hdr_cnt > 0 && !iov_setup(&(sf.headers), &hbuf,
5958 headers, sf.hdr_cnt, PyBUF_SIMPLE))
5959 return NULL;
5960 }
5961 }
5962 if (trailers != NULL) {
5963 if (!PySequence_Check(trailers)) {
5964 PyErr_SetString(PyExc_TypeError,
5965 "sendfile() trailers must be a sequence or None");
5966 return NULL;
5967 } else {
5968 sf.trl_cnt = PySequence_Size(trailers);
5969 if (sf.trl_cnt > 0 && !iov_setup(&(sf.trailers), &tbuf,
5970 trailers, sf.trl_cnt, PyBUF_SIMPLE))
5971 return NULL;
5972 }
5973 }
5974
5975 Py_BEGIN_ALLOW_THREADS
5976#ifdef __APPLE__
5977 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
5978#else
5979 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
5980#endif
5981 Py_END_ALLOW_THREADS
5982
5983 if (sf.headers != NULL)
5984 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
5985 if (sf.trailers != NULL)
5986 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
5987
5988 if (ret < 0) {
5989 if ((errno == EAGAIN) || (errno == EBUSY)) {
5990 if (sbytes != 0) {
5991 // some data has been sent
5992 goto done;
5993 }
5994 else {
5995 // no data has been sent; upper application is supposed
5996 // to retry on EAGAIN or EBUSY
5997 return posix_error();
5998 }
5999 }
6000 return posix_error();
6001 }
6002 goto done;
6003
6004done:
6005 #if !defined(HAVE_LARGEFILE_SUPPORT)
6006 return Py_BuildValue("l", sbytes);
6007 #else
6008 return Py_BuildValue("L", sbytes);
6009 #endif
6010
6011#else
6012 Py_ssize_t count;
6013 PyObject *offobj;
6014 static char *keywords[] = {"out", "in",
6015 "offset", "count", NULL};
6016 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
6017 keywords, &out, &in, &offobj, &count))
6018 return NULL;
6019#ifdef linux
6020 if (offobj == Py_None) {
6021 Py_BEGIN_ALLOW_THREADS
6022 ret = sendfile(out, in, NULL, count);
6023 Py_END_ALLOW_THREADS
6024 if (ret < 0)
6025 return posix_error();
6026 Py_INCREF(Py_None);
6027 return Py_BuildValue("nO", ret, Py_None);
6028 }
6029#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00006030 if (!_parse_off_t(offobj, &offset))
6031 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006032 Py_BEGIN_ALLOW_THREADS
6033 ret = sendfile(out, in, &offset, count);
6034 Py_END_ALLOW_THREADS
6035 if (ret < 0)
6036 return posix_error();
6037 return Py_BuildValue("n", ret);
6038#endif
6039}
6040#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006042PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006043"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006044Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006045
Barry Warsaw53699e91996-12-10 23:23:01 +00006046static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006047posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006048{
Victor Stinner8c62be82010-05-06 00:08:46 +00006049 int fd;
6050 STRUCT_STAT st;
6051 int res;
6052 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6053 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006054#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00006055 /* on OpenVMS we must ensure that all bytes are written to the file */
6056 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006057#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 if (!_PyVerify_fd(fd))
6059 return posix_error();
6060 Py_BEGIN_ALLOW_THREADS
6061 res = FSTAT(fd, &st);
6062 Py_END_ALLOW_THREADS
6063 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006064#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006065 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006066#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006067 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006068#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006069 }
Tim Peters5aa91602002-01-30 05:46:57 +00006070
Victor Stinner8c62be82010-05-06 00:08:46 +00006071 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006072}
6073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006074PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006075"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006076Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006077connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006078
6079static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006080posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006081{
Victor Stinner8c62be82010-05-06 00:08:46 +00006082 int fd;
6083 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6084 return NULL;
6085 if (!_PyVerify_fd(fd))
6086 return PyBool_FromLong(0);
6087 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006088}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006089
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006090#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006091PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006092"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006093Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006094
Barry Warsaw53699e91996-12-10 23:23:01 +00006095static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006096posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006097{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006098#if defined(PYOS_OS2)
6099 HFILE read, write;
6100 APIRET rc;
6101
Victor Stinner8c62be82010-05-06 00:08:46 +00006102 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006103 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00006104 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006105 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006106 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006107
6108 return Py_BuildValue("(ii)", read, write);
6109#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006110#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006111 int fds[2];
6112 int res;
6113 Py_BEGIN_ALLOW_THREADS
6114 res = pipe(fds);
6115 Py_END_ALLOW_THREADS
6116 if (res != 0)
6117 return posix_error();
6118 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006119#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00006120 HANDLE read, write;
6121 int read_fd, write_fd;
6122 BOOL ok;
6123 Py_BEGIN_ALLOW_THREADS
6124 ok = CreatePipe(&read, &write, NULL, 0);
6125 Py_END_ALLOW_THREADS
6126 if (!ok)
6127 return win32_error("CreatePipe", NULL);
6128 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6129 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6130 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006131#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006132#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006133}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006134#endif /* HAVE_PIPE */
6135
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006136
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006137#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006138PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006139"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006140Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006141
Barry Warsaw53699e91996-12-10 23:23:01 +00006142static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006143posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006144{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006145 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006146 char *filename;
6147 int mode = 0666;
6148 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006149 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
6150 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00006151 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006152 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006153 Py_BEGIN_ALLOW_THREADS
6154 res = mkfifo(filename, mode);
6155 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006156 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 if (res < 0)
6158 return posix_error();
6159 Py_INCREF(Py_None);
6160 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006161}
6162#endif
6163
6164
Neal Norwitz11690112002-07-30 01:08:28 +00006165#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006166PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006167"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006168Create a filesystem node (file, device special file or named pipe)\n\
6169named filename. mode specifies both the permissions to use and the\n\
6170type of node to be created, being combined (bitwise OR) with one of\n\
6171S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006172device defines the newly created device special file (probably using\n\
6173os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006174
6175
6176static PyObject *
6177posix_mknod(PyObject *self, PyObject *args)
6178{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006179 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006180 char *filename;
6181 int mode = 0600;
6182 int device = 0;
6183 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006184 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
6185 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00006186 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006187 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006188 Py_BEGIN_ALLOW_THREADS
6189 res = mknod(filename, mode, device);
6190 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006191 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006192 if (res < 0)
6193 return posix_error();
6194 Py_INCREF(Py_None);
6195 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006196}
6197#endif
6198
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006199#ifdef HAVE_DEVICE_MACROS
6200PyDoc_STRVAR(posix_major__doc__,
6201"major(device) -> major number\n\
6202Extracts a device major number from a raw device number.");
6203
6204static PyObject *
6205posix_major(PyObject *self, PyObject *args)
6206{
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 int device;
6208 if (!PyArg_ParseTuple(args, "i:major", &device))
6209 return NULL;
6210 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006211}
6212
6213PyDoc_STRVAR(posix_minor__doc__,
6214"minor(device) -> minor number\n\
6215Extracts a device minor number from a raw device number.");
6216
6217static PyObject *
6218posix_minor(PyObject *self, PyObject *args)
6219{
Victor Stinner8c62be82010-05-06 00:08:46 +00006220 int device;
6221 if (!PyArg_ParseTuple(args, "i:minor", &device))
6222 return NULL;
6223 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006224}
6225
6226PyDoc_STRVAR(posix_makedev__doc__,
6227"makedev(major, minor) -> device number\n\
6228Composes a raw device number from the major and minor device numbers.");
6229
6230static PyObject *
6231posix_makedev(PyObject *self, PyObject *args)
6232{
Victor Stinner8c62be82010-05-06 00:08:46 +00006233 int major, minor;
6234 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6235 return NULL;
6236 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006237}
6238#endif /* device macros */
6239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006240
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006241#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006242PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006243"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006244Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006245
Barry Warsaw53699e91996-12-10 23:23:01 +00006246static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006247posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006248{
Victor Stinner8c62be82010-05-06 00:08:46 +00006249 int fd;
6250 off_t length;
6251 int res;
6252 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006253
Victor Stinner8c62be82010-05-06 00:08:46 +00006254 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6255 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006256
6257#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006258 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006259#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006260 length = PyLong_Check(lenobj) ?
6261 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006262#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 if (PyErr_Occurred())
6264 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006265
Victor Stinner8c62be82010-05-06 00:08:46 +00006266 Py_BEGIN_ALLOW_THREADS
6267 res = ftruncate(fd, length);
6268 Py_END_ALLOW_THREADS
6269 if (res < 0)
6270 return posix_error();
6271 Py_INCREF(Py_None);
6272 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006273}
6274#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006275
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006276#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006277PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006278"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006279Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006280
Fred Drake762e2061999-08-26 17:23:54 +00006281/* Save putenv() parameters as values here, so we can collect them when they
6282 * get re-set with another call for the same key. */
6283static PyObject *posix_putenv_garbage;
6284
Tim Peters5aa91602002-01-30 05:46:57 +00006285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006286posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006287{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006288#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006289 wchar_t *s1, *s2;
6290 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006291#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006292 PyObject *os1, *os2;
6293 char *s1, *s2;
6294 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006295#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006296 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006297 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006298
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006299#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 if (!PyArg_ParseTuple(args,
6301 "uu:putenv",
6302 &s1, &s2))
6303 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006304#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006305 if (!PyArg_ParseTuple(args,
6306 "O&O&:putenv",
6307 PyUnicode_FSConverter, &os1,
6308 PyUnicode_FSConverter, &os2))
6309 return NULL;
6310 s1 = PyBytes_AsString(os1);
6311 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006312#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006313
6314#if defined(PYOS_OS2)
6315 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6316 APIRET rc;
6317
Guido van Rossumd48f2521997-12-05 22:19:34 +00006318 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006319 if (rc != NO_ERROR) {
6320 os2_error(rc);
6321 goto error;
6322 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006323
6324 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6325 APIRET rc;
6326
Guido van Rossumd48f2521997-12-05 22:19:34 +00006327 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006328 if (rc != NO_ERROR) {
6329 os2_error(rc);
6330 goto error;
6331 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006332 } else {
6333#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006334 /* XXX This can leak memory -- not easy to fix :-( */
6335 /* len includes space for a trailing \0; the size arg to
6336 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006337#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006338 len = wcslen(s1) + wcslen(s2) + 2;
6339 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006340#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006341 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006342 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006343#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006344 if (newstr == NULL) {
6345 PyErr_NoMemory();
6346 goto error;
6347 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006348#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006349 newenv = PyUnicode_AsUnicode(newstr);
6350 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6351 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006352 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006353 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006354 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006355#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006356 newenv = PyBytes_AS_STRING(newstr);
6357 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6358 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006359 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006360 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006362#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006363
Victor Stinner8c62be82010-05-06 00:08:46 +00006364 /* Install the first arg and newstr in posix_putenv_garbage;
6365 * this will cause previous value to be collected. This has to
6366 * happen after the real putenv() call because the old value
6367 * was still accessible until then. */
6368 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006369#ifdef MS_WINDOWS
6370 PyTuple_GET_ITEM(args, 0),
6371#else
6372 os1,
6373#endif
6374 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006375 /* really not much we can do; just leak */
6376 PyErr_Clear();
6377 }
6378 else {
6379 Py_DECREF(newstr);
6380 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006381
6382#if defined(PYOS_OS2)
6383 }
6384#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006385
Martin v. Löwis011e8422009-05-05 04:43:17 +00006386#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006387 Py_DECREF(os1);
6388 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006389#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006390 Py_RETURN_NONE;
6391
6392error:
6393#ifndef MS_WINDOWS
6394 Py_DECREF(os1);
6395 Py_DECREF(os2);
6396#endif
6397 Py_XDECREF(newstr);
6398 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006399}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006400#endif /* putenv */
6401
Guido van Rossumc524d952001-10-19 01:31:59 +00006402#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006403PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006404"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006406
6407static PyObject *
6408posix_unsetenv(PyObject *self, PyObject *args)
6409{
Victor Stinner84ae1182010-05-06 22:05:07 +00006410#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006411 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006412
Victor Stinner8c62be82010-05-06 00:08:46 +00006413 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6414 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006415#else
6416 PyObject *os1;
6417 char *s1;
6418
6419 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6420 PyUnicode_FSConverter, &os1))
6421 return NULL;
6422 s1 = PyBytes_AsString(os1);
6423#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006424
Victor Stinner8c62be82010-05-06 00:08:46 +00006425 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006426
Victor Stinner8c62be82010-05-06 00:08:46 +00006427 /* Remove the key from posix_putenv_garbage;
6428 * this will cause it to be collected. This has to
6429 * happen after the real unsetenv() call because the
6430 * old value was still accessible until then.
6431 */
6432 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006433#ifdef MS_WINDOWS
6434 PyTuple_GET_ITEM(args, 0)
6435#else
6436 os1
6437#endif
6438 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006439 /* really not much we can do; just leak */
6440 PyErr_Clear();
6441 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006442
Victor Stinner84ae1182010-05-06 22:05:07 +00006443#ifndef MS_WINDOWS
6444 Py_DECREF(os1);
6445#endif
6446 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006447}
6448#endif /* unsetenv */
6449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006450PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006451"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006452Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006453
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006454static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006455posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006456{
Victor Stinner8c62be82010-05-06 00:08:46 +00006457 int code;
6458 char *message;
6459 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6460 return NULL;
6461 message = strerror(code);
6462 if (message == NULL) {
6463 PyErr_SetString(PyExc_ValueError,
6464 "strerror() argument out of range");
6465 return NULL;
6466 }
6467 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006468}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006469
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006470
Guido van Rossumc9641791998-08-04 15:26:23 +00006471#ifdef HAVE_SYS_WAIT_H
6472
Fred Drake106c1a02002-04-23 15:58:02 +00006473#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006474PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006475"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006476Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006477
6478static PyObject *
6479posix_WCOREDUMP(PyObject *self, PyObject *args)
6480{
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 WAIT_TYPE status;
6482 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006483
Victor Stinner8c62be82010-05-06 00:08:46 +00006484 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6485 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006486
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006488}
6489#endif /* WCOREDUMP */
6490
6491#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006492PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006493"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006494Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006495job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006496
6497static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006498posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006499{
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 WAIT_TYPE status;
6501 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006502
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6504 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006505
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006507}
6508#endif /* WIFCONTINUED */
6509
Guido van Rossumc9641791998-08-04 15:26:23 +00006510#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006511PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006512"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006513Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006514
6515static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006516posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006517{
Victor Stinner8c62be82010-05-06 00:08:46 +00006518 WAIT_TYPE status;
6519 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006520
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6522 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006523
Victor Stinner8c62be82010-05-06 00:08:46 +00006524 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006525}
6526#endif /* WIFSTOPPED */
6527
6528#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006529PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006530"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006531Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006532
6533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006534posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006535{
Victor Stinner8c62be82010-05-06 00:08:46 +00006536 WAIT_TYPE status;
6537 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006538
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6540 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006541
Victor Stinner8c62be82010-05-06 00:08:46 +00006542 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006543}
6544#endif /* WIFSIGNALED */
6545
6546#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006547PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006548"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006549Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006550system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006551
6552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006553posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006554{
Victor Stinner8c62be82010-05-06 00:08:46 +00006555 WAIT_TYPE status;
6556 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006557
Victor Stinner8c62be82010-05-06 00:08:46 +00006558 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6559 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006560
Victor Stinner8c62be82010-05-06 00:08:46 +00006561 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006562}
6563#endif /* WIFEXITED */
6564
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006565#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006566PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006567"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006568Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006569
6570static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006571posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006572{
Victor Stinner8c62be82010-05-06 00:08:46 +00006573 WAIT_TYPE status;
6574 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006575
Victor Stinner8c62be82010-05-06 00:08:46 +00006576 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6577 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006578
Victor Stinner8c62be82010-05-06 00:08:46 +00006579 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006580}
6581#endif /* WEXITSTATUS */
6582
6583#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006584PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006585"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006586Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006587value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006588
6589static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006590posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006591{
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 WAIT_TYPE status;
6593 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006594
Victor Stinner8c62be82010-05-06 00:08:46 +00006595 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6596 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006597
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006599}
6600#endif /* WTERMSIG */
6601
6602#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006603PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006604"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006605Return the signal that stopped the process that provided\n\
6606the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006607
6608static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006609posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006610{
Victor Stinner8c62be82010-05-06 00:08:46 +00006611 WAIT_TYPE status;
6612 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006613
Victor Stinner8c62be82010-05-06 00:08:46 +00006614 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6615 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006616
Victor Stinner8c62be82010-05-06 00:08:46 +00006617 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006618}
6619#endif /* WSTOPSIG */
6620
6621#endif /* HAVE_SYS_WAIT_H */
6622
6623
Thomas Wouters477c8d52006-05-27 19:21:47 +00006624#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006625#ifdef _SCO_DS
6626/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6627 needed definitions in sys/statvfs.h */
6628#define _SVID3
6629#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006630#include <sys/statvfs.h>
6631
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006632static PyObject*
6633_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6635 if (v == NULL)
6636 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006637
6638#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006639 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6640 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6641 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6642 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6643 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6644 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6645 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6646 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6647 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6648 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006649#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006650 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6651 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6652 PyStructSequence_SET_ITEM(v, 2,
6653 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6654 PyStructSequence_SET_ITEM(v, 3,
6655 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6656 PyStructSequence_SET_ITEM(v, 4,
6657 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6658 PyStructSequence_SET_ITEM(v, 5,
6659 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6660 PyStructSequence_SET_ITEM(v, 6,
6661 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6662 PyStructSequence_SET_ITEM(v, 7,
6663 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6664 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6665 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006666#endif
6667
Victor Stinner8c62be82010-05-06 00:08:46 +00006668 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006669}
6670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006671PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006672"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006673Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006674
6675static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006676posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006677{
Victor Stinner8c62be82010-05-06 00:08:46 +00006678 int fd, res;
6679 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006680
Victor Stinner8c62be82010-05-06 00:08:46 +00006681 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6682 return NULL;
6683 Py_BEGIN_ALLOW_THREADS
6684 res = fstatvfs(fd, &st);
6685 Py_END_ALLOW_THREADS
6686 if (res != 0)
6687 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006688
Victor Stinner8c62be82010-05-06 00:08:46 +00006689 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006690}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006691#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006692
6693
Thomas Wouters477c8d52006-05-27 19:21:47 +00006694#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006695#include <sys/statvfs.h>
6696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006697PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006698"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006699Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006700
6701static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006702posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006703{
Victor Stinner8c62be82010-05-06 00:08:46 +00006704 char *path;
6705 int res;
6706 struct statvfs st;
6707 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6708 return NULL;
6709 Py_BEGIN_ALLOW_THREADS
6710 res = statvfs(path, &st);
6711 Py_END_ALLOW_THREADS
6712 if (res != 0)
6713 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006714
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006716}
6717#endif /* HAVE_STATVFS */
6718
Fred Drakec9680921999-12-13 16:37:25 +00006719/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6720 * It maps strings representing configuration variable names to
6721 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006722 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006723 * rarely-used constants. There are three separate tables that use
6724 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006725 *
6726 * This code is always included, even if none of the interfaces that
6727 * need it are included. The #if hackery needed to avoid it would be
6728 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006729 */
6730struct constdef {
6731 char *name;
6732 long value;
6733};
6734
Fred Drake12c6e2d1999-12-14 21:25:03 +00006735static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006736conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006737 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006738{
Christian Heimes217cfd12007-12-02 14:31:20 +00006739 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006740 *valuep = PyLong_AS_LONG(arg);
6741 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006742 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006743 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006744 /* look up the value in the table using a binary search */
6745 size_t lo = 0;
6746 size_t mid;
6747 size_t hi = tablesize;
6748 int cmp;
6749 const char *confname;
6750 if (!PyUnicode_Check(arg)) {
6751 PyErr_SetString(PyExc_TypeError,
6752 "configuration names must be strings or integers");
6753 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006754 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006755 confname = _PyUnicode_AsString(arg);
6756 if (confname == NULL)
6757 return 0;
6758 while (lo < hi) {
6759 mid = (lo + hi) / 2;
6760 cmp = strcmp(confname, table[mid].name);
6761 if (cmp < 0)
6762 hi = mid;
6763 else if (cmp > 0)
6764 lo = mid + 1;
6765 else {
6766 *valuep = table[mid].value;
6767 return 1;
6768 }
6769 }
6770 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6771 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006772 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006773}
6774
6775
6776#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6777static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006778#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006779 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006780#endif
6781#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006782 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006783#endif
Fred Drakec9680921999-12-13 16:37:25 +00006784#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006785 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006786#endif
6787#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006789#endif
6790#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006791 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006792#endif
6793#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006794 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006795#endif
6796#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006797 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006798#endif
6799#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006801#endif
6802#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006803 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006804#endif
6805#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006807#endif
6808#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006809 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006810#endif
6811#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006812 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006813#endif
6814#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006815 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006816#endif
6817#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006818 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006819#endif
6820#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006821 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006822#endif
6823#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006824 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006825#endif
6826#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006827 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006828#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006829#ifdef _PC_ACL_ENABLED
6830 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6831#endif
6832#ifdef _PC_MIN_HOLE_SIZE
6833 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6834#endif
6835#ifdef _PC_ALLOC_SIZE_MIN
6836 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6837#endif
6838#ifdef _PC_REC_INCR_XFER_SIZE
6839 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6840#endif
6841#ifdef _PC_REC_MAX_XFER_SIZE
6842 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6843#endif
6844#ifdef _PC_REC_MIN_XFER_SIZE
6845 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6846#endif
6847#ifdef _PC_REC_XFER_ALIGN
6848 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6849#endif
6850#ifdef _PC_SYMLINK_MAX
6851 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6852#endif
6853#ifdef _PC_XATTR_ENABLED
6854 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6855#endif
6856#ifdef _PC_XATTR_EXISTS
6857 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6858#endif
6859#ifdef _PC_TIMESTAMP_RESOLUTION
6860 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6861#endif
Fred Drakec9680921999-12-13 16:37:25 +00006862};
6863
Fred Drakec9680921999-12-13 16:37:25 +00006864static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006865conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006866{
6867 return conv_confname(arg, valuep, posix_constants_pathconf,
6868 sizeof(posix_constants_pathconf)
6869 / sizeof(struct constdef));
6870}
6871#endif
6872
6873#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006874PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006875"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006876Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006877If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006878
6879static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006880posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006881{
6882 PyObject *result = NULL;
6883 int name, fd;
6884
Fred Drake12c6e2d1999-12-14 21:25:03 +00006885 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6886 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006887 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006888
Stefan Krah0e803b32010-11-26 16:16:47 +00006889 errno = 0;
6890 limit = fpathconf(fd, name);
6891 if (limit == -1 && errno != 0)
6892 posix_error();
6893 else
6894 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006895 }
6896 return result;
6897}
6898#endif
6899
6900
6901#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006902PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006903"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006904Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006905If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006906
6907static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006908posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006909{
6910 PyObject *result = NULL;
6911 int name;
6912 char *path;
6913
6914 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6915 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006917
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 errno = 0;
6919 limit = pathconf(path, name);
6920 if (limit == -1 && errno != 0) {
6921 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006922 /* could be a path or name problem */
6923 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006924 else
Stefan Krah99439262010-11-26 12:58:05 +00006925 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006926 }
6927 else
6928 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006929 }
6930 return result;
6931}
6932#endif
6933
6934#ifdef HAVE_CONFSTR
6935static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006936#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006938#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006939#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006941#endif
6942#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006944#endif
Fred Draked86ed291999-12-15 15:34:33 +00006945#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006947#endif
6948#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006950#endif
6951#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006953#endif
6954#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006956#endif
Fred Drakec9680921999-12-13 16:37:25 +00006957#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006959#endif
6960#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006961 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006962#endif
6963#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006965#endif
6966#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006968#endif
6969#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006971#endif
6972#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006974#endif
6975#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006977#endif
6978#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006980#endif
Fred Draked86ed291999-12-15 15:34:33 +00006981#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006983#endif
Fred Drakec9680921999-12-13 16:37:25 +00006984#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006986#endif
Fred Draked86ed291999-12-15 15:34:33 +00006987#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006989#endif
6990#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006991 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006992#endif
6993#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006994 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006995#endif
6996#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006998#endif
Fred Drakec9680921999-12-13 16:37:25 +00006999#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007001#endif
7002#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007004#endif
7005#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007007#endif
7008#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007010#endif
7011#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
7014#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007016#endif
7017#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
7020#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007022#endif
7023#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007025#endif
7026#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007028#endif
7029#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007031#endif
7032#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007034#endif
7035#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007037#endif
7038#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007040#endif
7041#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007043#endif
7044#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007046#endif
Fred Draked86ed291999-12-15 15:34:33 +00007047#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007049#endif
7050#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007052#endif
7053#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007055#endif
7056#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007058#endif
7059#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007061#endif
7062#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007064#endif
7065#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007067#endif
7068#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007070#endif
7071#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007073#endif
7074#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007076#endif
7077#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007079#endif
7080#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007082#endif
7083#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007085#endif
Fred Drakec9680921999-12-13 16:37:25 +00007086};
7087
7088static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007089conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007090{
7091 return conv_confname(arg, valuep, posix_constants_confstr,
7092 sizeof(posix_constants_confstr)
7093 / sizeof(struct constdef));
7094}
7095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007096PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007097"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007098Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007099
7100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007101posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007102{
7103 PyObject *result = NULL;
7104 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00007105 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00007106 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007107
Victor Stinnercb043522010-09-10 23:49:04 +00007108 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
7109 return NULL;
7110
7111 errno = 0;
7112 len = confstr(name, buffer, sizeof(buffer));
7113 if (len == 0) {
7114 if (errno) {
7115 posix_error();
7116 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00007117 }
7118 else {
Victor Stinnercb043522010-09-10 23:49:04 +00007119 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00007120 }
7121 }
Victor Stinnercb043522010-09-10 23:49:04 +00007122
7123 if ((unsigned int)len >= sizeof(buffer)) {
7124 char *buf = PyMem_Malloc(len);
7125 if (buf == NULL)
7126 return PyErr_NoMemory();
7127 confstr(name, buf, len);
7128 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
7129 PyMem_Free(buf);
7130 }
7131 else
7132 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007133 return result;
7134}
7135#endif
7136
7137
7138#ifdef HAVE_SYSCONF
7139static struct constdef posix_constants_sysconf[] = {
7140#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007142#endif
7143#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007145#endif
7146#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007148#endif
7149#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007151#endif
7152#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
7155#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007157#endif
7158#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007160#endif
7161#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
7164#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007166#endif
7167#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007169#endif
Fred Draked86ed291999-12-15 15:34:33 +00007170#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007172#endif
7173#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007175#endif
Fred Drakec9680921999-12-13 16:37:25 +00007176#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007178#endif
Fred Drakec9680921999-12-13 16:37:25 +00007179#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007181#endif
7182#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007184#endif
7185#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007187#endif
7188#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007190#endif
7191#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007193#endif
Fred Draked86ed291999-12-15 15:34:33 +00007194#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007196#endif
Fred Drakec9680921999-12-13 16:37:25 +00007197#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007199#endif
7200#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007202#endif
7203#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007205#endif
7206#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007208#endif
7209#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007211#endif
Fred Draked86ed291999-12-15 15:34:33 +00007212#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007214#endif
Fred Drakec9680921999-12-13 16:37:25 +00007215#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007217#endif
7218#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007220#endif
7221#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007223#endif
7224#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007226#endif
7227#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007229#endif
7230#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
7233#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007235#endif
7236#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007238#endif
7239#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007241#endif
7242#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007244#endif
7245#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007247#endif
7248#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007250#endif
7251#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007253#endif
7254#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007256#endif
7257#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007259#endif
7260#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007262#endif
7263#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007265#endif
7266#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007268#endif
7269#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007271#endif
7272#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007274#endif
7275#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007277#endif
7278#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007280#endif
7281#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007283#endif
Fred Draked86ed291999-12-15 15:34:33 +00007284#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007286#endif
Fred Drakec9680921999-12-13 16:37:25 +00007287#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007288 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007289#endif
7290#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007291 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007292#endif
7293#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007294 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007295#endif
Fred Draked86ed291999-12-15 15:34:33 +00007296#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007298#endif
Fred Drakec9680921999-12-13 16:37:25 +00007299#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007300 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007301#endif
Fred Draked86ed291999-12-15 15:34:33 +00007302#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007303 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007304#endif
7305#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007306 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007307#endif
Fred Drakec9680921999-12-13 16:37:25 +00007308#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007310#endif
7311#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007313#endif
7314#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007316#endif
7317#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007319#endif
Fred Draked86ed291999-12-15 15:34:33 +00007320#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007321 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007322#endif
Fred Drakec9680921999-12-13 16:37:25 +00007323#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007325#endif
7326#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007328#endif
7329#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007331#endif
7332#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007334#endif
7335#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007337#endif
7338#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007339 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007340#endif
7341#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007342 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007343#endif
Fred Draked86ed291999-12-15 15:34:33 +00007344#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007345 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007346#endif
Fred Drakec9680921999-12-13 16:37:25 +00007347#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007348 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007349#endif
7350#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007352#endif
Fred Draked86ed291999-12-15 15:34:33 +00007353#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007354 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007355#endif
Fred Drakec9680921999-12-13 16:37:25 +00007356#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007358#endif
7359#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007360 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007361#endif
7362#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007363 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007364#endif
7365#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007366 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007367#endif
7368#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007369 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007370#endif
7371#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007372 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007373#endif
7374#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007375 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007376#endif
7377#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007378 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007379#endif
7380#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007382#endif
Fred Draked86ed291999-12-15 15:34:33 +00007383#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007384 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007385#endif
7386#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007387 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007388#endif
Fred Drakec9680921999-12-13 16:37:25 +00007389#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007390 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007391#endif
7392#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007393 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007394#endif
7395#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007396 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007397#endif
7398#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007399 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007400#endif
7401#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007402 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007403#endif
7404#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007405 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007406#endif
7407#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007408 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007409#endif
7410#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007412#endif
7413#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007414 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007415#endif
7416#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007417 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007418#endif
7419#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007420 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007421#endif
7422#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007423 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007424#endif
7425#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007426 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007427#endif
7428#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007429 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007430#endif
7431#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007432 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007433#endif
7434#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007435 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007436#endif
7437#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007438 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007439#endif
7440#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007441 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007442#endif
7443#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007445#endif
7446#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007447 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007448#endif
7449#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007451#endif
7452#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007454#endif
7455#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007456 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007457#endif
7458#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007459 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007460#endif
7461#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007462 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007463#endif
7464#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007465 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007466#endif
7467#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007468 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007469#endif
7470#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007471 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007472#endif
7473#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007474 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007475#endif
7476#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007477 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007478#endif
7479#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007480 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007481#endif
7482#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007483 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007484#endif
7485#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007486 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007487#endif
7488#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007489 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007490#endif
7491#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007492 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007493#endif
Fred Draked86ed291999-12-15 15:34:33 +00007494#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007495 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007496#endif
Fred Drakec9680921999-12-13 16:37:25 +00007497#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007498 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007499#endif
7500#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007501 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007502#endif
7503#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007504 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007505#endif
7506#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007507 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007508#endif
7509#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007510 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007511#endif
7512#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007513 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007514#endif
7515#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007516 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007517#endif
7518#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007519 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007520#endif
7521#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007522 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007523#endif
7524#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007525 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007526#endif
7527#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007528 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007529#endif
7530#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007531 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007532#endif
7533#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007534 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007535#endif
7536#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007537 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007538#endif
7539#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007541#endif
7542#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007543 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007544#endif
7545#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007546 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007547#endif
7548#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007549 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007550#endif
7551#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007553#endif
7554#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007555 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007556#endif
7557#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007558 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007559#endif
7560#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007561 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007562#endif
7563#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007564 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007565#endif
7566#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007568#endif
7569#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007571#endif
7572#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007573 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007574#endif
7575#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007576 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007577#endif
7578#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007579 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007580#endif
7581#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007582 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007583#endif
7584#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007585 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007586#endif
7587#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007588 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007589#endif
7590#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007591 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007592#endif
7593#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007594 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007595#endif
7596#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007597 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007598#endif
7599#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007600 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007601#endif
7602#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007603 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007604#endif
7605#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007606 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007607#endif
7608#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007609 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007610#endif
7611#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007612 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007613#endif
7614#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007615 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007616#endif
7617#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007618 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007619#endif
7620#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007621 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007622#endif
7623#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007624 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007625#endif
7626#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007628#endif
7629#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007631#endif
7632};
7633
7634static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007635conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007636{
7637 return conv_confname(arg, valuep, posix_constants_sysconf,
7638 sizeof(posix_constants_sysconf)
7639 / sizeof(struct constdef));
7640}
7641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007642PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007643"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007644Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007645
7646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007647posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007648{
7649 PyObject *result = NULL;
7650 int name;
7651
7652 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7653 int value;
7654
7655 errno = 0;
7656 value = sysconf(name);
7657 if (value == -1 && errno != 0)
7658 posix_error();
7659 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007660 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007661 }
7662 return result;
7663}
7664#endif
7665
7666
Fred Drakebec628d1999-12-15 18:31:10 +00007667/* This code is used to ensure that the tables of configuration value names
7668 * are in sorted order as required by conv_confname(), and also to build the
7669 * the exported dictionaries that are used to publish information about the
7670 * names available on the host platform.
7671 *
7672 * Sorting the table at runtime ensures that the table is properly ordered
7673 * when used, even for platforms we're not able to test on. It also makes
7674 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007675 */
Fred Drakebec628d1999-12-15 18:31:10 +00007676
7677static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007678cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007679{
7680 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007682 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007683 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007684
7685 return strcmp(c1->name, c2->name);
7686}
7687
7688static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007689setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007690 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007691{
Fred Drakebec628d1999-12-15 18:31:10 +00007692 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007693 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007694
7695 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7696 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007697 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007698 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007699
Barry Warsaw3155db32000-04-13 15:20:40 +00007700 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007701 PyObject *o = PyLong_FromLong(table[i].value);
7702 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7703 Py_XDECREF(o);
7704 Py_DECREF(d);
7705 return -1;
7706 }
7707 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007708 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007709 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007710}
7711
Fred Drakebec628d1999-12-15 18:31:10 +00007712/* Return -1 on failure, 0 on success. */
7713static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007714setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007715{
7716#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007717 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007718 sizeof(posix_constants_pathconf)
7719 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007720 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007721 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007722#endif
7723#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007724 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007725 sizeof(posix_constants_confstr)
7726 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007727 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007728 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007729#endif
7730#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007731 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007732 sizeof(posix_constants_sysconf)
7733 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007734 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007735 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007736#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007737 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007738}
Fred Draked86ed291999-12-15 15:34:33 +00007739
7740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007741PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007742"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007743Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007744in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007745
7746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007747posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007748{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007749 abort();
7750 /*NOTREACHED*/
7751 Py_FatalError("abort() called from Python code didn't abort!");
7752 return NULL;
7753}
Fred Drakebec628d1999-12-15 18:31:10 +00007754
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007755#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007756PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007757"startfile(filepath [, operation]) - Start a file with its associated\n\
7758application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007759\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007760When \"operation\" is not specified or \"open\", this acts like\n\
7761double-clicking the file in Explorer, or giving the file name as an\n\
7762argument to the DOS \"start\" command: the file is opened with whatever\n\
7763application (if any) its extension is associated.\n\
7764When another \"operation\" is given, it specifies what should be done with\n\
7765the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007766\n\
7767startfile returns as soon as the associated application is launched.\n\
7768There is no option to wait for the application to close, and no way\n\
7769to retrieve the application's exit status.\n\
7770\n\
7771The filepath is relative to the current directory. If you want to use\n\
7772an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007773the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007774
7775static PyObject *
7776win32_startfile(PyObject *self, PyObject *args)
7777{
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 PyObject *ofilepath;
7779 char *filepath;
7780 char *operation = NULL;
7781 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007782
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 PyObject *unipath, *woperation = NULL;
7784 if (!PyArg_ParseTuple(args, "U|s:startfile",
7785 &unipath, &operation)) {
7786 PyErr_Clear();
7787 goto normal;
7788 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007789
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 if (operation) {
7791 woperation = PyUnicode_DecodeASCII(operation,
7792 strlen(operation), NULL);
7793 if (!woperation) {
7794 PyErr_Clear();
7795 operation = NULL;
7796 goto normal;
7797 }
7798 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007799
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 Py_BEGIN_ALLOW_THREADS
7801 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7802 PyUnicode_AS_UNICODE(unipath),
7803 NULL, NULL, SW_SHOWNORMAL);
7804 Py_END_ALLOW_THREADS
7805
7806 Py_XDECREF(woperation);
7807 if (rc <= (HINSTANCE)32) {
7808 PyObject *errval = win32_error_unicode("startfile",
7809 PyUnicode_AS_UNICODE(unipath));
7810 return errval;
7811 }
7812 Py_INCREF(Py_None);
7813 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007814
7815normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007816 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7817 PyUnicode_FSConverter, &ofilepath,
7818 &operation))
7819 return NULL;
7820 filepath = PyBytes_AsString(ofilepath);
7821 Py_BEGIN_ALLOW_THREADS
7822 rc = ShellExecute((HWND)0, operation, filepath,
7823 NULL, NULL, SW_SHOWNORMAL);
7824 Py_END_ALLOW_THREADS
7825 if (rc <= (HINSTANCE)32) {
7826 PyObject *errval = win32_error("startfile", filepath);
7827 Py_DECREF(ofilepath);
7828 return errval;
7829 }
7830 Py_DECREF(ofilepath);
7831 Py_INCREF(Py_None);
7832 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007833}
7834#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007835
Martin v. Löwis438b5342002-12-27 10:16:42 +00007836#ifdef HAVE_GETLOADAVG
7837PyDoc_STRVAR(posix_getloadavg__doc__,
7838"getloadavg() -> (float, float, float)\n\n\
7839Return the number of processes in the system run queue averaged over\n\
7840the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7841was unobtainable");
7842
7843static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007844posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007845{
7846 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007847 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007848 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7849 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007850 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007851 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007852}
7853#endif
7854
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007855#ifdef MS_WINDOWS
7856
7857PyDoc_STRVAR(win32_urandom__doc__,
7858"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007859Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007860
7861typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7862 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7863 DWORD dwFlags );
7864typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7865 BYTE *pbBuffer );
7866
7867static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007868/* This handle is never explicitly released. Instead, the operating
7869 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007870static HCRYPTPROV hCryptProv = 0;
7871
Tim Peters4ad82172004-08-30 17:02:04 +00007872static PyObject*
7873win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007874{
Victor Stinner8c62be82010-05-06 00:08:46 +00007875 int howMany;
7876 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007877
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 /* Read arguments */
7879 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7880 return NULL;
7881 if (howMany < 0)
7882 return PyErr_Format(PyExc_ValueError,
7883 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007884
Victor Stinner8c62be82010-05-06 00:08:46 +00007885 if (hCryptProv == 0) {
7886 HINSTANCE hAdvAPI32 = NULL;
7887 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007888
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 /* Obtain handle to the DLL containing CryptoAPI
7890 This should not fail */
7891 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7892 if(hAdvAPI32 == NULL)
7893 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007894
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 /* Obtain pointers to the CryptoAPI functions
7896 This will fail on some early versions of Win95 */
7897 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7898 hAdvAPI32,
7899 "CryptAcquireContextA");
7900 if (pCryptAcquireContext == NULL)
7901 return PyErr_Format(PyExc_NotImplementedError,
7902 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007903
Victor Stinner8c62be82010-05-06 00:08:46 +00007904 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7905 hAdvAPI32, "CryptGenRandom");
7906 if (pCryptGenRandom == NULL)
7907 return PyErr_Format(PyExc_NotImplementedError,
7908 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007909
Victor Stinner8c62be82010-05-06 00:08:46 +00007910 /* Acquire context */
7911 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7912 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7913 return win32_error("CryptAcquireContext", NULL);
7914 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007915
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 /* Allocate bytes */
7917 result = PyBytes_FromStringAndSize(NULL, howMany);
7918 if (result != NULL) {
7919 /* Get random data */
7920 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7921 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7922 PyBytes_AS_STRING(result))) {
7923 Py_DECREF(result);
7924 return win32_error("CryptGenRandom", NULL);
7925 }
7926 }
7927 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007928}
7929#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007930
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007931PyDoc_STRVAR(device_encoding__doc__,
7932"device_encoding(fd) -> str\n\n\
7933Return a string describing the encoding of the device\n\
7934if the output is a terminal; else return None.");
7935
7936static PyObject *
7937device_encoding(PyObject *self, PyObject *args)
7938{
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 int fd;
7940 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7941 return NULL;
7942 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7943 Py_INCREF(Py_None);
7944 return Py_None;
7945 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007946#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007947 if (fd == 0) {
7948 char buf[100];
7949 sprintf(buf, "cp%d", GetConsoleCP());
7950 return PyUnicode_FromString(buf);
7951 }
7952 if (fd == 1 || fd == 2) {
7953 char buf[100];
7954 sprintf(buf, "cp%d", GetConsoleOutputCP());
7955 return PyUnicode_FromString(buf);
7956 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007957#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 {
7959 char *codeset = nl_langinfo(CODESET);
7960 if (codeset != NULL && codeset[0] != 0)
7961 return PyUnicode_FromString(codeset);
7962 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007963#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007964 Py_INCREF(Py_None);
7965 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007966}
7967
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007968#ifdef __VMS
7969/* Use openssl random routine */
7970#include <openssl/rand.h>
7971PyDoc_STRVAR(vms_urandom__doc__,
7972"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007973Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007974
7975static PyObject*
7976vms_urandom(PyObject *self, PyObject *args)
7977{
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 int howMany;
7979 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007980
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 /* Read arguments */
7982 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7983 return NULL;
7984 if (howMany < 0)
7985 return PyErr_Format(PyExc_ValueError,
7986 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007987
Victor Stinner8c62be82010-05-06 00:08:46 +00007988 /* Allocate bytes */
7989 result = PyBytes_FromStringAndSize(NULL, howMany);
7990 if (result != NULL) {
7991 /* Get random data */
7992 if (RAND_pseudo_bytes((unsigned char*)
7993 PyBytes_AS_STRING(result),
7994 howMany) < 0) {
7995 Py_DECREF(result);
7996 return PyErr_Format(PyExc_ValueError,
7997 "RAND_pseudo_bytes");
7998 }
7999 }
8000 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008001}
8002#endif
8003
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008004#ifdef HAVE_SETRESUID
8005PyDoc_STRVAR(posix_setresuid__doc__,
8006"setresuid(ruid, euid, suid)\n\n\
8007Set the current process's real, effective, and saved user ids.");
8008
8009static PyObject*
8010posix_setresuid (PyObject *self, PyObject *args)
8011{
Victor Stinner8c62be82010-05-06 00:08:46 +00008012 /* We assume uid_t is no larger than a long. */
8013 long ruid, euid, suid;
8014 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
8015 return NULL;
8016 if (setresuid(ruid, euid, suid) < 0)
8017 return posix_error();
8018 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008019}
8020#endif
8021
8022#ifdef HAVE_SETRESGID
8023PyDoc_STRVAR(posix_setresgid__doc__,
8024"setresgid(rgid, egid, sgid)\n\n\
8025Set the current process's real, effective, and saved group ids.");
8026
8027static PyObject*
8028posix_setresgid (PyObject *self, PyObject *args)
8029{
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 /* We assume uid_t is no larger than a long. */
8031 long rgid, egid, sgid;
8032 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
8033 return NULL;
8034 if (setresgid(rgid, egid, sgid) < 0)
8035 return posix_error();
8036 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008037}
8038#endif
8039
8040#ifdef HAVE_GETRESUID
8041PyDoc_STRVAR(posix_getresuid__doc__,
8042"getresuid() -> (ruid, euid, suid)\n\n\
8043Get tuple of the current process's real, effective, and saved user ids.");
8044
8045static PyObject*
8046posix_getresuid (PyObject *self, PyObject *noargs)
8047{
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 uid_t ruid, euid, suid;
8049 long l_ruid, l_euid, l_suid;
8050 if (getresuid(&ruid, &euid, &suid) < 0)
8051 return posix_error();
8052 /* Force the values into long's as we don't know the size of uid_t. */
8053 l_ruid = ruid;
8054 l_euid = euid;
8055 l_suid = suid;
8056 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008057}
8058#endif
8059
8060#ifdef HAVE_GETRESGID
8061PyDoc_STRVAR(posix_getresgid__doc__,
8062"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00008063Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008064
8065static PyObject*
8066posix_getresgid (PyObject *self, PyObject *noargs)
8067{
Victor Stinner8c62be82010-05-06 00:08:46 +00008068 uid_t rgid, egid, sgid;
8069 long l_rgid, l_egid, l_sgid;
8070 if (getresgid(&rgid, &egid, &sgid) < 0)
8071 return posix_error();
8072 /* Force the values into long's as we don't know the size of uid_t. */
8073 l_rgid = rgid;
8074 l_egid = egid;
8075 l_sgid = sgid;
8076 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008077}
8078#endif
8079
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008080/* Posix *at family of functions:
8081 faccessat, fchmodat, fchownat, fstatat, futimesat,
8082 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
8083 unlinkat, utimensat, mkfifoat */
8084
8085#ifdef HAVE_FACCESSAT
8086PyDoc_STRVAR(posix_faccessat__doc__,
8087"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
8088Like access() but if path is relative, it is taken as relative to dirfd.\n\
8089flags is optional and can be constructed by ORing together zero or more\n\
8090of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
8091If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8092is interpreted relative to the current working directory.");
8093
8094static PyObject *
8095posix_faccessat(PyObject *self, PyObject *args)
8096{
8097 PyObject *opath;
8098 char *path;
8099 int mode;
8100 int res;
8101 int dirfd, flags = 0;
8102 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
8103 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8104 return NULL;
8105 path = PyBytes_AsString(opath);
8106 Py_BEGIN_ALLOW_THREADS
8107 res = faccessat(dirfd, path, mode, flags);
8108 Py_END_ALLOW_THREADS
8109 Py_DECREF(opath);
8110 return PyBool_FromLong(res == 0);
8111}
8112#endif
8113
8114#ifdef HAVE_FCHMODAT
8115PyDoc_STRVAR(posix_fchmodat__doc__,
8116"fchmodat(dirfd, path, mode, flags=0)\n\n\
8117Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
8118flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8119If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8120is interpreted relative to the current working directory.");
8121
8122static PyObject *
8123posix_fchmodat(PyObject *self, PyObject *args)
8124{
8125 int dirfd, mode, res;
8126 int flags = 0;
8127 PyObject *opath;
8128 char *path;
8129
8130 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
8131 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8132 return NULL;
8133
8134 path = PyBytes_AsString(opath);
8135
8136 Py_BEGIN_ALLOW_THREADS
8137 res = fchmodat(dirfd, path, mode, flags);
8138 Py_END_ALLOW_THREADS
8139 Py_DECREF(opath);
8140 if (res < 0)
8141 return posix_error();
8142 Py_RETURN_NONE;
8143}
8144#endif /* HAVE_FCHMODAT */
8145
8146#ifdef HAVE_FCHOWNAT
8147PyDoc_STRVAR(posix_fchownat__doc__,
8148"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
8149Like chown() but if path is relative, it is taken as relative to dirfd.\n\
8150flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8151If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8152is interpreted relative to the current working directory.");
8153
8154static PyObject *
8155posix_fchownat(PyObject *self, PyObject *args)
8156{
8157 PyObject *opath;
8158 int dirfd, res;
8159 long uid, gid;
8160 int flags = 0;
8161 char *path;
8162
8163 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
8164 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
8165 return NULL;
8166
8167 path = PyBytes_AsString(opath);
8168
8169 Py_BEGIN_ALLOW_THREADS
8170 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
8171 Py_END_ALLOW_THREADS
8172 Py_DECREF(opath);
8173 if (res < 0)
8174 return posix_error();
8175 Py_RETURN_NONE;
8176}
8177#endif /* HAVE_FCHOWNAT */
8178
8179#ifdef HAVE_FSTATAT
8180PyDoc_STRVAR(posix_fstatat__doc__,
8181"fstatat(dirfd, path, flags=0) -> stat result\n\n\
8182Like stat() but if path is relative, it is taken as relative to dirfd.\n\
8183flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8184If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8185is interpreted relative to the current working directory.");
8186
8187static PyObject *
8188posix_fstatat(PyObject *self, PyObject *args)
8189{
8190 PyObject *opath;
8191 char *path;
8192 STRUCT_STAT st;
8193 int dirfd, res, flags = 0;
8194
8195 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
8196 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8197 return NULL;
8198 path = PyBytes_AsString(opath);
8199
8200 Py_BEGIN_ALLOW_THREADS
8201 res = fstatat(dirfd, path, &st, flags);
8202 Py_END_ALLOW_THREADS
8203 Py_DECREF(opath);
8204 if (res != 0)
8205 return posix_error();
8206
8207 return _pystat_fromstructstat(&st);
8208}
8209#endif
8210
8211#ifdef HAVE_FUTIMESAT
8212PyDoc_STRVAR(posix_futimesat__doc__,
8213"futimesat(dirfd, path, (atime, mtime))\n\
8214futimesat(dirfd, path, None)\n\n\
8215Like utime() but if path is relative, it is taken as relative to dirfd.\n\
8216If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8217is interpreted relative to the current working directory.");
8218
8219static PyObject *
8220posix_futimesat(PyObject *self, PyObject *args)
8221{
8222 PyObject *opath;
8223 char *path;
8224 int res, dirfd;
8225 PyObject* arg;
8226
8227 struct timeval buf[2];
8228
8229 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
8230 &dirfd, PyUnicode_FSConverter, &opath, &arg))
8231 return NULL;
8232 path = PyBytes_AsString(opath);
8233 if (arg == Py_None) {
8234 /* optional time values not given */
8235 Py_BEGIN_ALLOW_THREADS
8236 res = futimesat(dirfd, path, NULL);
8237 Py_END_ALLOW_THREADS
8238 }
8239 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
8240 PyErr_SetString(PyExc_TypeError,
8241 "futimesat() arg 3 must be a tuple (atime, mtime)");
8242 Py_DECREF(opath);
8243 return NULL;
8244 }
8245 else {
8246 if (extract_time(PyTuple_GET_ITEM(arg, 0),
8247 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
8248 Py_DECREF(opath);
8249 return NULL;
8250 }
8251 if (extract_time(PyTuple_GET_ITEM(arg, 1),
8252 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
8253 Py_DECREF(opath);
8254 return NULL;
8255 }
8256 Py_BEGIN_ALLOW_THREADS
8257 res = futimesat(dirfd, path, buf);
8258 Py_END_ALLOW_THREADS
8259 }
8260 Py_DECREF(opath);
8261 if (res < 0) {
8262 return posix_error();
8263 }
8264 Py_RETURN_NONE;
8265}
8266#endif
8267
8268#ifdef HAVE_LINKAT
8269PyDoc_STRVAR(posix_linkat__doc__,
8270"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
8271Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
8272and if dstpath is relative, it is taken as relative to dstfd.\n\
8273flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
8274If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
8275srcpath is interpreted relative to the current working directory. This\n\
8276also applies for dstpath.");
8277
8278static PyObject *
8279posix_linkat(PyObject *self, PyObject *args)
8280{
8281 PyObject *osrc, *odst;
8282 char *src, *dst;
8283 int res, srcfd, dstfd;
8284 int flags = 0;
8285
8286 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
8287 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
8288 return NULL;
8289 src = PyBytes_AsString(osrc);
8290 dst = PyBytes_AsString(odst);
8291 Py_BEGIN_ALLOW_THREADS
8292 res = linkat(srcfd, src, dstfd, dst, flags);
8293 Py_END_ALLOW_THREADS
8294 Py_DECREF(osrc);
8295 Py_DECREF(odst);
8296 if (res < 0)
8297 return posix_error();
8298 Py_RETURN_NONE;
8299}
8300#endif /* HAVE_LINKAT */
8301
8302#ifdef HAVE_MKDIRAT
8303PyDoc_STRVAR(posix_mkdirat__doc__,
8304"mkdirat(dirfd, path, mode=0o777)\n\n\
8305Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
8306If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8307is interpreted relative to the current working directory.");
8308
8309static PyObject *
8310posix_mkdirat(PyObject *self, PyObject *args)
8311{
8312 int res, dirfd;
8313 PyObject *opath;
8314 char *path;
8315 int mode = 0777;
8316
8317 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
8318 &dirfd, PyUnicode_FSConverter, &opath, &mode))
8319 return NULL;
8320 path = PyBytes_AsString(opath);
8321 Py_BEGIN_ALLOW_THREADS
8322 res = mkdirat(dirfd, path, mode);
8323 Py_END_ALLOW_THREADS
8324 Py_DECREF(opath);
8325 if (res < 0)
8326 return posix_error();
8327 Py_RETURN_NONE;
8328}
8329#endif
8330
8331#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
8332PyDoc_STRVAR(posix_mknodat__doc__,
8333"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
8334Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
8335If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8336is interpreted relative to the current working directory.");
8337
8338static PyObject *
8339posix_mknodat(PyObject *self, PyObject *args)
8340{
8341 PyObject *opath;
8342 char *filename;
8343 int mode = 0600;
8344 int device = 0;
8345 int res, dirfd;
8346 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
8347 PyUnicode_FSConverter, &opath, &mode, &device))
8348 return NULL;
8349 filename = PyBytes_AS_STRING(opath);
8350 Py_BEGIN_ALLOW_THREADS
8351 res = mknodat(dirfd, filename, mode, device);
8352 Py_END_ALLOW_THREADS
8353 Py_DECREF(opath);
8354 if (res < 0)
8355 return posix_error();
8356 Py_RETURN_NONE;
8357}
8358#endif
8359
8360#ifdef HAVE_OPENAT
8361PyDoc_STRVAR(posix_openat__doc__,
8362"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
8363Like open() but if path is relative, it is taken as relative to dirfd.\n\
8364If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8365is interpreted relative to the current working directory.");
8366
8367static PyObject *
8368posix_openat(PyObject *self, PyObject *args)
8369{
8370 PyObject *ofile;
8371 char *file;
8372 int flag, dirfd, fd;
8373 int mode = 0777;
8374
8375 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
8376 &dirfd, PyUnicode_FSConverter, &ofile,
8377 &flag, &mode))
8378 return NULL;
8379 file = PyBytes_AsString(ofile);
8380 Py_BEGIN_ALLOW_THREADS
8381 fd = openat(dirfd, file, flag, mode);
8382 Py_END_ALLOW_THREADS
8383 Py_DECREF(ofile);
8384 if (fd < 0)
8385 return posix_error();
8386 return PyLong_FromLong((long)fd);
8387}
8388#endif
8389
8390#ifdef HAVE_READLINKAT
8391PyDoc_STRVAR(posix_readlinkat__doc__,
8392"readlinkat(dirfd, path) -> path\n\n\
8393Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
8394If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8395is interpreted relative to the current working directory.");
8396
8397static PyObject *
8398posix_readlinkat(PyObject *self, PyObject *args)
8399{
8400 PyObject *v, *opath;
8401 char buf[MAXPATHLEN];
8402 char *path;
8403 int n, dirfd;
8404 int arg_is_unicode = 0;
8405
8406 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
8407 &dirfd, PyUnicode_FSConverter, &opath))
8408 return NULL;
8409 path = PyBytes_AsString(opath);
8410 v = PySequence_GetItem(args, 1);
8411 if (v == NULL) {
8412 Py_DECREF(opath);
8413 return NULL;
8414 }
8415
8416 if (PyUnicode_Check(v)) {
8417 arg_is_unicode = 1;
8418 }
8419 Py_DECREF(v);
8420
8421 Py_BEGIN_ALLOW_THREADS
8422 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
8423 Py_END_ALLOW_THREADS
8424 Py_DECREF(opath);
8425 if (n < 0)
8426 return posix_error();
8427
8428 if (arg_is_unicode)
8429 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
8430 else
8431 return PyBytes_FromStringAndSize(buf, n);
8432}
8433#endif /* HAVE_READLINKAT */
8434
8435#ifdef HAVE_RENAMEAT
8436PyDoc_STRVAR(posix_renameat__doc__,
8437"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
8438Like rename() but if oldpath is relative, it is taken as relative to\n\
8439olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
8440If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
8441oldpath is interpreted relative to the current working directory. This\n\
8442also applies for newpath.");
8443
8444static PyObject *
8445posix_renameat(PyObject *self, PyObject *args)
8446{
8447 int res;
8448 PyObject *opathold, *opathnew;
8449 char *opath, *npath;
8450 int oldfd, newfd;
8451
8452 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
8453 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
8454 return NULL;
8455 opath = PyBytes_AsString(opathold);
8456 npath = PyBytes_AsString(opathnew);
8457 Py_BEGIN_ALLOW_THREADS
8458 res = renameat(oldfd, opath, newfd, npath);
8459 Py_END_ALLOW_THREADS
8460 Py_DECREF(opathold);
8461 Py_DECREF(opathnew);
8462 if (res < 0)
8463 return posix_error();
8464 Py_RETURN_NONE;
8465}
8466#endif
8467
8468#if HAVE_SYMLINKAT
8469PyDoc_STRVAR(posix_symlinkat__doc__,
8470"symlinkat(src, dstfd, dst)\n\n\
8471Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
8472If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
8473is interpreted relative to the current working directory.");
8474
8475static PyObject *
8476posix_symlinkat(PyObject *self, PyObject *args)
8477{
8478 int res, dstfd;
8479 PyObject *osrc, *odst;
8480 char *src, *dst;
8481
8482 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
8483 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
8484 return NULL;
8485 src = PyBytes_AsString(osrc);
8486 dst = PyBytes_AsString(odst);
8487 Py_BEGIN_ALLOW_THREADS
8488 res = symlinkat(src, dstfd, dst);
8489 Py_END_ALLOW_THREADS
8490 Py_DECREF(osrc);
8491 Py_DECREF(odst);
8492 if (res < 0)
8493 return posix_error();
8494 Py_RETURN_NONE;
8495}
8496#endif /* HAVE_SYMLINKAT */
8497
8498#ifdef HAVE_UNLINKAT
8499PyDoc_STRVAR(posix_unlinkat__doc__,
8500"unlinkat(dirfd, path, flags=0)\n\n\
8501Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
8502flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
8503specified, unlinkat() behaves like rmdir().\n\
8504If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8505is interpreted relative to the current working directory.");
8506
8507static PyObject *
8508posix_unlinkat(PyObject *self, PyObject *args)
8509{
8510 int dirfd, res, flags = 0;
8511 PyObject *opath;
8512 char *path;
8513
8514 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
8515 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8516 return NULL;
8517 path = PyBytes_AsString(opath);
8518 Py_BEGIN_ALLOW_THREADS
8519 res = unlinkat(dirfd, path, flags);
8520 Py_END_ALLOW_THREADS
8521 Py_DECREF(opath);
8522 if (res < 0)
8523 return posix_error();
8524 Py_RETURN_NONE;
8525}
8526#endif
8527
8528#ifdef HAVE_UTIMENSAT
8529PyDoc_STRVAR(posix_utimensat__doc__,
8530"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
8531 (mtime_sec, mtime_nsec), flags)\n\
8532utimensat(dirfd, path, None, None, flags)\n\n\
8533Updates the timestamps of a file with nanosecond precision. If path is\n\
8534relative, it is taken as relative to dirfd.\n\
8535The second form sets atime and mtime to the current time.\n\
8536flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8537If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8538is interpreted relative to the current working directory.\n\
8539If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
8540current time.\n\
8541If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
8542
8543static PyObject *
8544posix_utimensat(PyObject *self, PyObject *args)
8545{
8546 PyObject *opath;
8547 char *path;
8548 int res, dirfd, flags = 0;
8549 PyObject *atime, *mtime;
8550
8551 struct timespec buf[2];
8552
8553 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
8554 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
8555 return NULL;
8556 path = PyBytes_AsString(opath);
8557 if (atime == Py_None && mtime == Py_None) {
8558 /* optional time values not given */
8559 Py_BEGIN_ALLOW_THREADS
8560 res = utimensat(dirfd, path, NULL, flags);
8561 Py_END_ALLOW_THREADS
8562 }
8563 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
8564 PyErr_SetString(PyExc_TypeError,
8565 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
8566 Py_DECREF(opath);
8567 return NULL;
8568 }
8569 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
8570 PyErr_SetString(PyExc_TypeError,
8571 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
8572 Py_DECREF(opath);
8573 return NULL;
8574 }
8575 else {
8576 if (!PyArg_ParseTuple(atime, "ll:utimensat",
8577 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
8578 Py_DECREF(opath);
8579 return NULL;
8580 }
8581 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
8582 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
8583 Py_DECREF(opath);
8584 return NULL;
8585 }
8586 Py_BEGIN_ALLOW_THREADS
8587 res = utimensat(dirfd, path, buf, flags);
8588 Py_END_ALLOW_THREADS
8589 }
8590 Py_DECREF(opath);
8591 if (res < 0) {
8592 return posix_error();
8593 }
8594 Py_RETURN_NONE;
8595}
8596#endif
8597
8598#ifdef HAVE_MKFIFOAT
8599PyDoc_STRVAR(posix_mkfifoat__doc__,
8600"mkfifoat(dirfd, path, mode=0o666)\n\n\
8601Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
8602If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8603is interpreted relative to the current working directory.");
8604
8605static PyObject *
8606posix_mkfifoat(PyObject *self, PyObject *args)
8607{
8608 PyObject *opath;
8609 char *filename;
8610 int mode = 0666;
8611 int res, dirfd;
8612 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
8613 &dirfd, PyUnicode_FSConverter, &opath, &mode))
8614 return NULL;
8615 filename = PyBytes_AS_STRING(opath);
8616 Py_BEGIN_ALLOW_THREADS
8617 res = mkfifoat(dirfd, filename, mode);
8618 Py_END_ALLOW_THREADS
8619 Py_DECREF(opath);
8620 if (res < 0)
8621 return posix_error();
8622 Py_RETURN_NONE;
8623}
8624#endif
8625
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008626static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008627 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008628#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008630#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008631 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00008632#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008633 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00008634#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00008636#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00008637 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00008638#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008639#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008640 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008641#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00008642#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00008643 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00008644#endif /* HAVE_LCHMOD */
8645#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008646 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00008647#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00008648#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008649 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00008650#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008651#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008652 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008653#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008654#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00008655 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00008656#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008657#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00008658 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008659#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008660#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00008661 {"getcwd", (PyCFunction)posix_getcwd_unicode,
8662 METH_NOARGS, posix_getcwd__doc__},
8663 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
8664 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008665#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008666#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00008667 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008668#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00008669 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00008670#ifdef HAVE_FDOPENDIR
8671 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
8672#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008673 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8674 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008675#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00008676 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008677#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00008678#ifdef HAVE_GETPRIORITY
8679 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
8680#endif /* HAVE_GETPRIORITY */
8681#ifdef HAVE_SETPRIORITY
8682 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
8683#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008684#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00008685 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008686#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00008687#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00008688 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00008689#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00008690 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8691 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8692 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00008693 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00008694#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00008695 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008696#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00008697#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008698 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00008699 win_symlink__doc__},
8700#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008701#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00008702 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008703#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008705#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008706 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008707#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00008708 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8709 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8710 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008711#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00008712 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008713#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00008714 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008715#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00008716 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8717 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008718#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008719#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00008720 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8721 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008722#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008723 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8724 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008725#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008726#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008727#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008729#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008730#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00008731 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008732#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008733#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00008734 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008735#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008736#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008738#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008739#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008740 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008741#endif /* HAVE_GETEGID */
8742#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008743 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008744#endif /* HAVE_GETEUID */
8745#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008746 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008747#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008748#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008749 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008750#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008751 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008752#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008753 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008754#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008755#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008756 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008757#endif /* HAVE_GETPPID */
8758#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008759 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008760#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008761#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008762 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008763#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008764#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00008765 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008766#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008767#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00008768 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008769#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008770#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008772#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00008773#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008774 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8775 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00008776 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00008777#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008778#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008779 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008780#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008781#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008782 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008783#endif /* HAVE_SETEUID */
8784#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008785 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008786#endif /* HAVE_SETEGID */
8787#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008788 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008789#endif /* HAVE_SETREUID */
8790#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008791 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008792#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008793#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008795#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008796#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008797 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008798#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00008799#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00008800 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00008801#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008802#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008803 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00008804#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008805#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008807#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008808#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008809 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008810#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008811#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00008812 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008813#endif /* HAVE_WAIT3 */
8814#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00008815 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008816#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008817#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008818 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008819#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008820#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00008821 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008822#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008823#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00008824 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008825#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008826#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008827 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008828#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008829#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008830 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008831#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008832#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008833 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008834#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00008835 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8836 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8837 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8838 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
8839 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8840 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8841 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8842 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8843 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008844#ifdef HAVE_SENDFILE
8845 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
8846 posix_sendfile__doc__},
8847#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008848 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8849 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008850#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008851 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008852#endif
8853#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00008854 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008855#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008856#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00008857 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008858#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008859#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00008860 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8861 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8862 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008863#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008864#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00008865 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008866#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008867#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008868 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008869#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008870#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008871 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008872#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008874#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00008875 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008876#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008877#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008878 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008879#endif
8880#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008881 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008882#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008883#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008884#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008885 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008886#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008887#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008888 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008889#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008890#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008891 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008892#endif /* WIFSTOPPED */
8893#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008895#endif /* WIFSIGNALED */
8896#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008897 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008898#endif /* WIFEXITED */
8899#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008900 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008901#endif /* WEXITSTATUS */
8902#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008903 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008904#endif /* WTERMSIG */
8905#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008906 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008907#endif /* WSTOPSIG */
8908#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008909#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008910 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008911#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008912#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008913 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008914#endif
Fred Drakec9680921999-12-13 16:37:25 +00008915#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008916 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008917#endif
8918#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008919 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008920#endif
8921#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008922 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008923#endif
8924#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008925 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008926#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008928#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008929 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008930 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008931 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008932#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008933#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008934 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008935#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008936 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008937 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008938 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008939 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008940 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008941 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008942#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008943 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008944#endif
8945#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008946 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008947#endif
8948#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008949 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008950#endif
8951#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008952 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008953#endif
8954
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008955/* posix *at family of functions */
8956#ifdef HAVE_FACCESSAT
8957 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
8958#endif
8959#ifdef HAVE_FCHMODAT
8960 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
8961#endif /* HAVE_FCHMODAT */
8962#ifdef HAVE_FCHOWNAT
8963 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
8964#endif /* HAVE_FCHOWNAT */
8965#ifdef HAVE_FSTATAT
8966 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
8967#endif
8968#ifdef HAVE_FUTIMESAT
8969 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
8970#endif
8971#ifdef HAVE_LINKAT
8972 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
8973#endif /* HAVE_LINKAT */
8974#ifdef HAVE_MKDIRAT
8975 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
8976#endif
8977#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
8978 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
8979#endif
8980#ifdef HAVE_OPENAT
8981 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
8982#endif
8983#ifdef HAVE_READLINKAT
8984 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
8985#endif /* HAVE_READLINKAT */
8986#ifdef HAVE_RENAMEAT
8987 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
8988#endif
8989#if HAVE_SYMLINKAT
8990 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
8991#endif /* HAVE_SYMLINKAT */
8992#ifdef HAVE_UNLINKAT
8993 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
8994#endif
8995#ifdef HAVE_UTIMENSAT
8996 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
8997#endif
8998#ifdef HAVE_MKFIFOAT
8999 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
9000#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009001 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009002};
9003
9004
Barry Warsaw4a342091996-12-19 23:50:02 +00009005static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009006ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009007{
Victor Stinner8c62be82010-05-06 00:08:46 +00009008 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009009}
9010
Guido van Rossumd48f2521997-12-05 22:19:34 +00009011#if defined(PYOS_OS2)
9012/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009013static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009014{
9015 APIRET rc;
9016 ULONG values[QSV_MAX+1];
9017 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009018 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009019
9020 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009021 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009022 Py_END_ALLOW_THREADS
9023
9024 if (rc != NO_ERROR) {
9025 os2_error(rc);
9026 return -1;
9027 }
9028
Fred Drake4d1e64b2002-04-15 19:40:07 +00009029 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9030 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9031 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9032 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9033 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9034 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9035 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009036
9037 switch (values[QSV_VERSION_MINOR]) {
9038 case 0: ver = "2.00"; break;
9039 case 10: ver = "2.10"; break;
9040 case 11: ver = "2.11"; break;
9041 case 30: ver = "3.00"; break;
9042 case 40: ver = "4.00"; break;
9043 case 50: ver = "5.00"; break;
9044 default:
Tim Peters885d4572001-11-28 20:27:42 +00009045 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00009046 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009047 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009048 ver = &tmp[0];
9049 }
9050
9051 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009052 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009053 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009054
9055 /* Add Indicator of Which Drive was Used to Boot the System */
9056 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9057 tmp[1] = ':';
9058 tmp[2] = '\0';
9059
Fred Drake4d1e64b2002-04-15 19:40:07 +00009060 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009061}
9062#endif
9063
Brian Curtin52173d42010-12-02 18:29:18 +00009064#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009065static int
Brian Curtin52173d42010-12-02 18:29:18 +00009066enable_symlink()
9067{
9068 HANDLE tok;
9069 TOKEN_PRIVILEGES tok_priv;
9070 LUID luid;
9071 int meth_idx = 0;
9072
9073 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009074 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009075
9076 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009077 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009078
9079 tok_priv.PrivilegeCount = 1;
9080 tok_priv.Privileges[0].Luid = luid;
9081 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
9082
9083 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
9084 sizeof(TOKEN_PRIVILEGES),
9085 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009086 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009087
Brian Curtin3b4499c2010-12-28 14:31:47 +00009088 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
9089 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00009090}
9091#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
9092
Barry Warsaw4a342091996-12-19 23:50:02 +00009093static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009094all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009095{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009096#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009097 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009098#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009099#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009100 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009101#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009102#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009103 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009104#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009105#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009106 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009107#endif
Fred Drakec9680921999-12-13 16:37:25 +00009108#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009109 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009110#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009111#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009112 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009113#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009114#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009115 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009116#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009117#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00009118 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009119#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009120#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00009121 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009122#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009123#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009124 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009125#endif
9126#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009127 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009128#endif
9129#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00009130 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009131#endif
9132#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00009133 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009134#endif
9135#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009136 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009137#endif
9138#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00009139 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009140#endif
9141#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009142 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009143#endif
9144#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009145 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009146#endif
9147#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009148 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009149#endif
9150#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009151 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009152#endif
9153#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009154 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009155#endif
9156#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00009157 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009158#endif
9159#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009160 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009161#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009162#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009163 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009164#endif
9165#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00009166 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009167#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009168#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009169 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009170#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009171#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009172 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009173#endif
9174#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009175 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009176#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009177#ifdef PRIO_PROCESS
9178 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
9179#endif
9180#ifdef PRIO_PGRP
9181 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
9182#endif
9183#ifdef PRIO_USER
9184 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
9185#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009186/* posix - constants for *at functions */
9187#ifdef AT_SYMLINK_NOFOLLOW
9188 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
9189#endif
9190#ifdef AT_EACCESS
9191 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
9192#endif
9193#ifdef AT_FDCWD
9194 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
9195#endif
9196#ifdef AT_REMOVEDIR
9197 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
9198#endif
9199#ifdef AT_SYMLINK_FOLLOW
9200 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
9201#endif
9202#ifdef UTIME_NOW
9203 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
9204#endif
9205#ifdef UTIME_OMIT
9206 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
9207#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009208
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009209
Tim Peters5aa91602002-01-30 05:46:57 +00009210/* MS Windows */
9211#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009212 /* Don't inherit in child processes. */
9213 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009214#endif
9215#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00009216 /* Optimize for short life (keep in memory). */
9217 /* MS forgot to define this one with a non-underscore form too. */
9218 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009219#endif
9220#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009221 /* Automatically delete when last handle is closed. */
9222 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009223#endif
9224#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00009225 /* Optimize for random access. */
9226 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009227#endif
9228#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009229 /* Optimize for sequential access. */
9230 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009231#endif
9232
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009233/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009234#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009235 /* Send a SIGIO signal whenever input or output
9236 becomes available on file descriptor */
9237 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009238#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009239#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009240 /* Direct disk access. */
9241 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009242#endif
9243#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00009244 /* Must be a directory. */
9245 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009246#endif
9247#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00009248 /* Do not follow links. */
9249 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009250#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009251#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009252 /* Do not update the access time. */
9253 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009254#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009255
Victor Stinner8c62be82010-05-06 00:08:46 +00009256 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009257#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009258 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009259#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009260#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009261 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009262#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009263#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009264 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009265#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009266#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009267 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009268#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009269#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00009270 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009271#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009272#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00009273 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009274#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009275#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009276 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009277#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009278#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00009279 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009280#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009281#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009282 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009283#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009284#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009285 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009286#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009287#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009288 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009289#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009290#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009291 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009292#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009293#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00009294 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009295#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009296#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00009297 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009298#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009299#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009300 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009301#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009302#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009303 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009304#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009305#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00009306 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009307#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009308
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009309 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009310#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009311 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009312#endif /* ST_RDONLY */
9313#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009314 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009315#endif /* ST_NOSUID */
9316
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009317 /* FreeBSD sendfile() constants */
9318#ifdef SF_NODISKIO
9319 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
9320#endif
9321#ifdef SF_MNOWAIT
9322 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
9323#endif
9324#ifdef SF_SYNC
9325 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
9326#endif
9327
Guido van Rossum246bc171999-02-01 23:54:31 +00009328#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009329#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00009330 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
9331 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
9332 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
9333 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
9334 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
9335 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
9336 if (ins(d, "P_PM", (long)P_PM)) return -1;
9337 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
9338 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
9339 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
9340 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
9341 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
9342 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
9343 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
9344 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
9345 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
9346 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
9347 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
9348 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
9349 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009350#else
Victor Stinner8c62be82010-05-06 00:08:46 +00009351 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
9352 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
9353 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
9354 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
9355 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00009356#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009357#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00009358
Guido van Rossumd48f2521997-12-05 22:19:34 +00009359#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00009360 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009361#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00009363}
9364
9365
Tim Peters5aa91602002-01-30 05:46:57 +00009366#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00009367#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009368#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009369
9370#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00009371#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009372#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00009373
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00009374#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00009375#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00009376#define MODNAME "posix"
9377#endif
9378
Martin v. Löwis1a214512008-06-11 05:26:20 +00009379static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009380 PyModuleDef_HEAD_INIT,
9381 MODNAME,
9382 posix__doc__,
9383 -1,
9384 posix_methods,
9385 NULL,
9386 NULL,
9387 NULL,
9388 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00009389};
9390
9391
Mark Hammondfe51c6d2002-08-02 02:27:13 +00009392PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009393INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00009394{
Victor Stinner8c62be82010-05-06 00:08:46 +00009395 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00009396
Brian Curtin52173d42010-12-02 18:29:18 +00009397#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009398 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00009399#endif
9400
Victor Stinner8c62be82010-05-06 00:08:46 +00009401 m = PyModule_Create(&posixmodule);
9402 if (m == NULL)
9403 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00009404
Victor Stinner8c62be82010-05-06 00:08:46 +00009405 /* Initialize environ dictionary */
9406 v = convertenviron();
9407 Py_XINCREF(v);
9408 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
9409 return NULL;
9410 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00009411
Victor Stinner8c62be82010-05-06 00:08:46 +00009412 if (all_ins(m))
9413 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00009414
Victor Stinner8c62be82010-05-06 00:08:46 +00009415 if (setup_confname_tables(m))
9416 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00009417
Victor Stinner8c62be82010-05-06 00:08:46 +00009418 Py_INCREF(PyExc_OSError);
9419 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00009420
Guido van Rossumb3d39562000-01-31 18:41:26 +00009421#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009422 if (posix_putenv_garbage == NULL)
9423 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00009424#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009425
Victor Stinner8c62be82010-05-06 00:08:46 +00009426 if (!initialized) {
9427 stat_result_desc.name = MODNAME ".stat_result";
9428 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
9429 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
9430 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
9431 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
9432 structseq_new = StatResultType.tp_new;
9433 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009434
Victor Stinner8c62be82010-05-06 00:08:46 +00009435 statvfs_result_desc.name = MODNAME ".statvfs_result";
9436 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00009437#ifdef NEED_TICKS_PER_SECOND
9438# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00009439 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00009440# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00009441 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00009442# else
Victor Stinner8c62be82010-05-06 00:08:46 +00009443 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00009444# endif
9445#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009446 }
9447 Py_INCREF((PyObject*) &StatResultType);
9448 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
9449 Py_INCREF((PyObject*) &StatVFSResultType);
9450 PyModule_AddObject(m, "statvfs_result",
9451 (PyObject*) &StatVFSResultType);
9452 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00009453
9454#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00009455 /*
9456 * Step 2 of weak-linking support on Mac OS X.
9457 *
9458 * The code below removes functions that are not available on the
9459 * currently active platform.
9460 *
9461 * This block allow one to use a python binary that was build on
9462 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9463 * OSX 10.4.
9464 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00009465#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00009466 if (fstatvfs == NULL) {
9467 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
9468 return NULL;
9469 }
9470 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00009471#endif /* HAVE_FSTATVFS */
9472
9473#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00009474 if (statvfs == NULL) {
9475 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9476 return NULL;
9477 }
9478 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00009479#endif /* HAVE_STATVFS */
9480
9481# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009482 if (lchown == NULL) {
9483 if (PyObject_DelAttrString(m, "lchown") == -1) {
9484 return NULL;
9485 }
9486 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00009487#endif /* HAVE_LCHOWN */
9488
9489
9490#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00009491 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00009492
Guido van Rossumb6775db1994-08-01 11:34:53 +00009493}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009494
9495#ifdef __cplusplus
9496}
9497#endif