blob: f873a7a774ecfc8bb99392c1c30bb1412c4ffcfe [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
Ross Lagerwall4d076da2011-03-18 06:56:53 +020062#ifdef HAVE_SYS_UIO_H
63#include <sys/uio.h>
64#endif
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_TYPES_H */
69
70#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000071#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000073
Guido van Rossum36bc6801995-06-14 22:54:23 +000074#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000075#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000079#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000081
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#ifdef HAVE_FCNTL_H
83#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000084#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000085
Guido van Rossuma6535fd2001-10-18 19:44:10 +000086#ifdef HAVE_GRP_H
87#include <grp.h>
88#endif
89
Barry Warsaw5676bd12003-01-07 20:57:09 +000090#ifdef HAVE_SYSEXITS_H
91#include <sysexits.h>
92#endif /* HAVE_SYSEXITS_H */
93
Anthony Baxter8a560de2004-10-13 15:30:56 +000094#ifdef HAVE_SYS_LOADAVG_H
95#include <sys/loadavg.h>
96#endif
97
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000098#ifdef HAVE_LANGINFO_H
99#include <langinfo.h>
100#endif
101
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000102#ifdef HAVE_SYS_SENDFILE_H
103#include <sys/sendfile.h>
104#endif
105
106#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
107#ifdef HAVE_SYS_SOCKET_H
108#include <sys/socket.h>
109#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000110#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
Ezio Melotti4969f702011-03-15 05:59:46 +02001311 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001312
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
Ross Lagerwall7807c352011-03-17 20:20:30 +02001506#if defined(HAVE_WAITID) && !defined(__APPLE__)
1507PyDoc_STRVAR(waitid_result__doc__,
1508"waitid_result: Result from waitid.\n\n\
1509This object may be accessed either as a tuple of\n\
1510 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1511or via the attributes si_pid, si_uid, and so on.\n\
1512\n\
1513See os.waitid for more information.");
1514
1515static PyStructSequence_Field waitid_result_fields[] = {
1516 {"si_pid", },
1517 {"si_uid", },
1518 {"si_signo", },
1519 {"si_status", },
1520 {"si_code", },
1521 {0}
1522};
1523
1524static PyStructSequence_Desc waitid_result_desc = {
1525 "waitid_result", /* name */
1526 waitid_result__doc__, /* doc */
1527 waitid_result_fields,
1528 5
1529};
1530static PyTypeObject WaitidResultType;
1531#endif
1532
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001533static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001534static PyTypeObject StatResultType;
1535static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001536static newfunc structseq_new;
1537
1538static PyObject *
1539statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1540{
Victor Stinner8c62be82010-05-06 00:08:46 +00001541 PyStructSequence *result;
1542 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001543
Victor Stinner8c62be82010-05-06 00:08:46 +00001544 result = (PyStructSequence*)structseq_new(type, args, kwds);
1545 if (!result)
1546 return NULL;
1547 /* If we have been initialized from a tuple,
1548 st_?time might be set to None. Initialize it
1549 from the int slots. */
1550 for (i = 7; i <= 9; i++) {
1551 if (result->ob_item[i+3] == Py_None) {
1552 Py_DECREF(Py_None);
1553 Py_INCREF(result->ob_item[i]);
1554 result->ob_item[i+3] = result->ob_item[i];
1555 }
1556 }
1557 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001558}
1559
1560
1561
1562/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001563static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001564
1565PyDoc_STRVAR(stat_float_times__doc__,
1566"stat_float_times([newval]) -> oldval\n\n\
1567Determine whether os.[lf]stat represents time stamps as float objects.\n\
1568If newval is True, future calls to stat() return floats, if it is False,\n\
1569future calls return ints. \n\
1570If newval is omitted, return the current setting.\n");
1571
1572static PyObject*
1573stat_float_times(PyObject* self, PyObject *args)
1574{
Victor Stinner8c62be82010-05-06 00:08:46 +00001575 int newval = -1;
1576 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1577 return NULL;
1578 if (newval == -1)
1579 /* Return old value */
1580 return PyBool_FromLong(_stat_float_times);
1581 _stat_float_times = newval;
1582 Py_INCREF(Py_None);
1583 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001584}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001585
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001586static void
1587fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1588{
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001590#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001592#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001594#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 if (!ival)
1596 return;
1597 if (_stat_float_times) {
1598 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1599 } else {
1600 fval = ival;
1601 Py_INCREF(fval);
1602 }
1603 PyStructSequence_SET_ITEM(v, index, ival);
1604 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001605}
1606
Tim Peters5aa91602002-01-30 05:46:57 +00001607/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001608 (used by posix_stat() and posix_fstat()) */
1609static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001610_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001611{
Victor Stinner8c62be82010-05-06 00:08:46 +00001612 unsigned long ansec, mnsec, cnsec;
1613 PyObject *v = PyStructSequence_New(&StatResultType);
1614 if (v == NULL)
1615 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001616
Victor Stinner8c62be82010-05-06 00:08:46 +00001617 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001618#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001619 PyStructSequence_SET_ITEM(v, 1,
1620 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001621#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001623#endif
1624#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001625 PyStructSequence_SET_ITEM(v, 2,
1626 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001627#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001628 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001629#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001630 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1631 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1632 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001633#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001634 PyStructSequence_SET_ITEM(v, 6,
1635 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001636#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001637 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001638#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001639
Martin v. Löwis14694662006-02-03 12:54:16 +00001640#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001641 ansec = st->st_atim.tv_nsec;
1642 mnsec = st->st_mtim.tv_nsec;
1643 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001644#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001645 ansec = st->st_atimespec.tv_nsec;
1646 mnsec = st->st_mtimespec.tv_nsec;
1647 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001648#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001649 ansec = st->st_atime_nsec;
1650 mnsec = st->st_mtime_nsec;
1651 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001652#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001653 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001654#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 fill_time(v, 7, st->st_atime, ansec);
1656 fill_time(v, 8, st->st_mtime, mnsec);
1657 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001658
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001659#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1661 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001662#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001663#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001664 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1665 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001666#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001667#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1669 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001670#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001671#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1673 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001674#endif
1675#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001676 {
1677 PyObject *val;
1678 unsigned long bsec,bnsec;
1679 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001680#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001681 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001682#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001684#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001685 if (_stat_float_times) {
1686 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1687 } else {
1688 val = PyLong_FromLong((long)bsec);
1689 }
1690 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1691 val);
1692 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001693#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001694#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001695 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1696 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001697#endif
Fred Drake699f3522000-06-29 21:12:41 +00001698
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 if (PyErr_Occurred()) {
1700 Py_DECREF(v);
1701 return NULL;
1702 }
Fred Drake699f3522000-06-29 21:12:41 +00001703
Victor Stinner8c62be82010-05-06 00:08:46 +00001704 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001705}
1706
Barry Warsaw53699e91996-12-10 23:23:01 +00001707static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001708posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001709 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001710#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001712#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001713 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001714#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001715 char *wformat,
1716 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001717{
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 STRUCT_STAT st;
1719 PyObject *opath;
1720 char *path;
1721 int res;
1722 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001723
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001724#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001725 PyUnicodeObject *po;
1726 if (PyArg_ParseTuple(args, wformat, &po)) {
1727 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001728
Victor Stinner8c62be82010-05-06 00:08:46 +00001729 Py_BEGIN_ALLOW_THREADS
1730 /* PyUnicode_AS_UNICODE result OK without
1731 thread lock as it is a simple dereference. */
1732 res = wstatfunc(wpath, &st);
1733 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001734
Victor Stinner8c62be82010-05-06 00:08:46 +00001735 if (res != 0)
1736 return win32_error_unicode("stat", wpath);
1737 return _pystat_fromstructstat(&st);
1738 }
1739 /* Drop the argument parsing error as narrow strings
1740 are also valid. */
1741 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001742#endif
1743
Victor Stinner8c62be82010-05-06 00:08:46 +00001744 if (!PyArg_ParseTuple(args, format,
1745 PyUnicode_FSConverter, &opath))
1746 return NULL;
1747 path = PyBytes_AsString(opath);
1748 Py_BEGIN_ALLOW_THREADS
1749 res = (*statfunc)(path, &st);
1750 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001751
Victor Stinner8c62be82010-05-06 00:08:46 +00001752 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001753#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001754 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001755#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001756 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001757#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 }
1759 else
1760 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001761
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 Py_DECREF(opath);
1763 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001764}
1765
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001766/* POSIX methods */
1767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001769"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001770Use the real uid/gid to test for access to a path. Note that most\n\
1771operations will use the effective uid/gid, therefore this routine can\n\
1772be used in a suid/sgid environment to test if the invoking user has the\n\
1773specified access to the path. The mode argument can be F_OK to test\n\
1774existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001775
1776static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001777posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001778{
Victor Stinner8c62be82010-05-06 00:08:46 +00001779 PyObject *opath;
1780 char *path;
1781 int mode;
1782
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001783#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 DWORD attr;
1785 PyUnicodeObject *po;
1786 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1787 Py_BEGIN_ALLOW_THREADS
1788 /* PyUnicode_AS_UNICODE OK without thread lock as
1789 it is a simple dereference. */
1790 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1791 Py_END_ALLOW_THREADS
1792 goto finish;
1793 }
1794 /* Drop the argument parsing error as narrow strings
1795 are also valid. */
1796 PyErr_Clear();
1797 if (!PyArg_ParseTuple(args, "O&i:access",
1798 PyUnicode_FSConverter, &opath, &mode))
1799 return NULL;
1800 path = PyBytes_AsString(opath);
1801 Py_BEGIN_ALLOW_THREADS
1802 attr = GetFileAttributesA(path);
1803 Py_END_ALLOW_THREADS
1804 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001805finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 if (attr == 0xFFFFFFFF)
1807 /* File does not exist, or cannot read attributes */
1808 return PyBool_FromLong(0);
1809 /* Access is possible if either write access wasn't requested, or
1810 the file isn't read-only, or if it's a directory, as there are
1811 no read-only directories on Windows. */
1812 return PyBool_FromLong(!(mode & 2)
1813 || !(attr & FILE_ATTRIBUTE_READONLY)
1814 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001815#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001816 int res;
1817 if (!PyArg_ParseTuple(args, "O&i:access",
1818 PyUnicode_FSConverter, &opath, &mode))
1819 return NULL;
1820 path = PyBytes_AsString(opath);
1821 Py_BEGIN_ALLOW_THREADS
1822 res = access(path, mode);
1823 Py_END_ALLOW_THREADS
1824 Py_DECREF(opath);
1825 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001826#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001827}
1828
Guido van Rossumd371ff11999-01-25 16:12:23 +00001829#ifndef F_OK
1830#define F_OK 0
1831#endif
1832#ifndef R_OK
1833#define R_OK 4
1834#endif
1835#ifndef W_OK
1836#define W_OK 2
1837#endif
1838#ifndef X_OK
1839#define X_OK 1
1840#endif
1841
1842#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001843PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001844"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001846
1847static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001848posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001849{
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 int id;
1851 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001852
Victor Stinner8c62be82010-05-06 00:08:46 +00001853 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1854 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001855
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001856#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001857 /* file descriptor 0 only, the default input device (stdin) */
1858 if (id == 0) {
1859 ret = ttyname();
1860 }
1861 else {
1862 ret = NULL;
1863 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001864#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001865 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001866#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001867 if (ret == NULL)
1868 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001869 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001870}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001871#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001872
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001873#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001875"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001877
1878static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001879posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001880{
Victor Stinner8c62be82010-05-06 00:08:46 +00001881 char *ret;
1882 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001883
Greg Wardb48bc172000-03-01 21:51:56 +00001884#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001886#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001887 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001888#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001889 if (ret == NULL)
1890 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001891 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001892}
1893#endif
1894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001896"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001898
Barry Warsaw53699e91996-12-10 23:23:01 +00001899static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001900posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001901{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001902#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001903 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001904#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001905 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001906#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001907 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001908#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001909 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001910#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001911}
1912
Fred Drake4d1e64b2002-04-15 19:40:07 +00001913#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001915"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001916Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001917opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001918
1919static PyObject *
1920posix_fchdir(PyObject *self, PyObject *fdobj)
1921{
Victor Stinner8c62be82010-05-06 00:08:46 +00001922 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001923}
1924#endif /* HAVE_FCHDIR */
1925
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001928"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001929Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001930
Barry Warsaw53699e91996-12-10 23:23:01 +00001931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001932posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001933{
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 PyObject *opath = NULL;
1935 char *path = NULL;
1936 int i;
1937 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001938#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001939 DWORD attr;
1940 PyUnicodeObject *po;
1941 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1942 Py_BEGIN_ALLOW_THREADS
1943 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1944 if (attr != 0xFFFFFFFF) {
1945 if (i & _S_IWRITE)
1946 attr &= ~FILE_ATTRIBUTE_READONLY;
1947 else
1948 attr |= FILE_ATTRIBUTE_READONLY;
1949 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1950 }
1951 else
1952 res = 0;
1953 Py_END_ALLOW_THREADS
1954 if (!res)
1955 return win32_error_unicode("chmod",
1956 PyUnicode_AS_UNICODE(po));
1957 Py_INCREF(Py_None);
1958 return Py_None;
1959 }
1960 /* Drop the argument parsing error as narrow strings
1961 are also valid. */
1962 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001963
Victor Stinner8c62be82010-05-06 00:08:46 +00001964 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1965 &opath, &i))
1966 return NULL;
1967 path = PyBytes_AsString(opath);
1968 Py_BEGIN_ALLOW_THREADS
1969 attr = GetFileAttributesA(path);
1970 if (attr != 0xFFFFFFFF) {
1971 if (i & _S_IWRITE)
1972 attr &= ~FILE_ATTRIBUTE_READONLY;
1973 else
1974 attr |= FILE_ATTRIBUTE_READONLY;
1975 res = SetFileAttributesA(path, attr);
1976 }
1977 else
1978 res = 0;
1979 Py_END_ALLOW_THREADS
1980 if (!res) {
1981 win32_error("chmod", path);
1982 Py_DECREF(opath);
1983 return NULL;
1984 }
1985 Py_DECREF(opath);
1986 Py_INCREF(Py_None);
1987 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001988#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001989 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1990 &opath, &i))
1991 return NULL;
1992 path = PyBytes_AsString(opath);
1993 Py_BEGIN_ALLOW_THREADS
1994 res = chmod(path, i);
1995 Py_END_ALLOW_THREADS
1996 if (res < 0)
1997 return posix_error_with_allocated_filename(opath);
1998 Py_DECREF(opath);
1999 Py_INCREF(Py_None);
2000 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002001#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002002}
2003
Christian Heimes4e30a842007-11-30 22:12:06 +00002004#ifdef HAVE_FCHMOD
2005PyDoc_STRVAR(posix_fchmod__doc__,
2006"fchmod(fd, mode)\n\n\
2007Change the access permissions of the file given by file\n\
2008descriptor fd.");
2009
2010static PyObject *
2011posix_fchmod(PyObject *self, PyObject *args)
2012{
Victor Stinner8c62be82010-05-06 00:08:46 +00002013 int fd, mode, res;
2014 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2015 return NULL;
2016 Py_BEGIN_ALLOW_THREADS
2017 res = fchmod(fd, mode);
2018 Py_END_ALLOW_THREADS
2019 if (res < 0)
2020 return posix_error();
2021 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002022}
2023#endif /* HAVE_FCHMOD */
2024
2025#ifdef HAVE_LCHMOD
2026PyDoc_STRVAR(posix_lchmod__doc__,
2027"lchmod(path, mode)\n\n\
2028Change the access permissions of a file. If path is a symlink, this\n\
2029affects the link itself rather than the target.");
2030
2031static PyObject *
2032posix_lchmod(PyObject *self, PyObject *args)
2033{
Victor Stinner8c62be82010-05-06 00:08:46 +00002034 PyObject *opath;
2035 char *path;
2036 int i;
2037 int res;
2038 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2039 &opath, &i))
2040 return NULL;
2041 path = PyBytes_AsString(opath);
2042 Py_BEGIN_ALLOW_THREADS
2043 res = lchmod(path, i);
2044 Py_END_ALLOW_THREADS
2045 if (res < 0)
2046 return posix_error_with_allocated_filename(opath);
2047 Py_DECREF(opath);
2048 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002049}
2050#endif /* HAVE_LCHMOD */
2051
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002052
Thomas Wouterscf297e42007-02-23 15:07:44 +00002053#ifdef HAVE_CHFLAGS
2054PyDoc_STRVAR(posix_chflags__doc__,
2055"chflags(path, flags)\n\n\
2056Set file flags.");
2057
2058static PyObject *
2059posix_chflags(PyObject *self, PyObject *args)
2060{
Victor Stinner8c62be82010-05-06 00:08:46 +00002061 PyObject *opath;
2062 char *path;
2063 unsigned long flags;
2064 int res;
2065 if (!PyArg_ParseTuple(args, "O&k:chflags",
2066 PyUnicode_FSConverter, &opath, &flags))
2067 return NULL;
2068 path = PyBytes_AsString(opath);
2069 Py_BEGIN_ALLOW_THREADS
2070 res = chflags(path, flags);
2071 Py_END_ALLOW_THREADS
2072 if (res < 0)
2073 return posix_error_with_allocated_filename(opath);
2074 Py_DECREF(opath);
2075 Py_INCREF(Py_None);
2076 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002077}
2078#endif /* HAVE_CHFLAGS */
2079
2080#ifdef HAVE_LCHFLAGS
2081PyDoc_STRVAR(posix_lchflags__doc__,
2082"lchflags(path, flags)\n\n\
2083Set file flags.\n\
2084This function will not follow symbolic links.");
2085
2086static PyObject *
2087posix_lchflags(PyObject *self, PyObject *args)
2088{
Victor Stinner8c62be82010-05-06 00:08:46 +00002089 PyObject *opath;
2090 char *path;
2091 unsigned long flags;
2092 int res;
2093 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2094 PyUnicode_FSConverter, &opath, &flags))
2095 return NULL;
2096 path = PyBytes_AsString(opath);
2097 Py_BEGIN_ALLOW_THREADS
2098 res = lchflags(path, flags);
2099 Py_END_ALLOW_THREADS
2100 if (res < 0)
2101 return posix_error_with_allocated_filename(opath);
2102 Py_DECREF(opath);
2103 Py_INCREF(Py_None);
2104 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002105}
2106#endif /* HAVE_LCHFLAGS */
2107
Martin v. Löwis244edc82001-10-04 22:44:26 +00002108#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002109PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002110"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002111Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002112
2113static PyObject *
2114posix_chroot(PyObject *self, PyObject *args)
2115{
Victor Stinner8c62be82010-05-06 00:08:46 +00002116 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002117}
2118#endif
2119
Guido van Rossum21142a01999-01-08 21:05:37 +00002120#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002121PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002122"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002123force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002124
2125static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002126posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002127{
Stefan Krah0e803b32010-11-26 16:16:47 +00002128 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002129}
2130#endif /* HAVE_FSYNC */
2131
Ross Lagerwall7807c352011-03-17 20:20:30 +02002132#ifdef HAVE_SYNC
2133PyDoc_STRVAR(posix_sync__doc__,
2134"sync()\n\n\
2135Force write of everything to disk.");
2136
2137static PyObject *
2138posix_sync(PyObject *self, PyObject *noargs)
2139{
2140 Py_BEGIN_ALLOW_THREADS
2141 sync();
2142 Py_END_ALLOW_THREADS
2143 Py_RETURN_NONE;
2144}
2145#endif
2146
Guido van Rossum21142a01999-01-08 21:05:37 +00002147#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002148
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002149#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002150extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2151#endif
2152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002153PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002154"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002155force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002156 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002157
2158static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002159posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002160{
Stefan Krah0e803b32010-11-26 16:16:47 +00002161 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002162}
2163#endif /* HAVE_FDATASYNC */
2164
2165
Fredrik Lundh10723342000-07-10 16:38:09 +00002166#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002167PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002168"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002169Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002170
Barry Warsaw53699e91996-12-10 23:23:01 +00002171static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002172posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002173{
Victor Stinner8c62be82010-05-06 00:08:46 +00002174 PyObject *opath;
2175 char *path;
2176 long uid, gid;
2177 int res;
2178 if (!PyArg_ParseTuple(args, "O&ll:chown",
2179 PyUnicode_FSConverter, &opath,
2180 &uid, &gid))
2181 return NULL;
2182 path = PyBytes_AsString(opath);
2183 Py_BEGIN_ALLOW_THREADS
2184 res = chown(path, (uid_t) uid, (gid_t) gid);
2185 Py_END_ALLOW_THREADS
2186 if (res < 0)
2187 return posix_error_with_allocated_filename(opath);
2188 Py_DECREF(opath);
2189 Py_INCREF(Py_None);
2190 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002191}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002192#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002193
Christian Heimes4e30a842007-11-30 22:12:06 +00002194#ifdef HAVE_FCHOWN
2195PyDoc_STRVAR(posix_fchown__doc__,
2196"fchown(fd, uid, gid)\n\n\
2197Change the owner and group id of the file given by file descriptor\n\
2198fd to the numeric uid and gid.");
2199
2200static PyObject *
2201posix_fchown(PyObject *self, PyObject *args)
2202{
Victor Stinner8c62be82010-05-06 00:08:46 +00002203 int fd;
2204 long uid, gid;
2205 int res;
2206 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2207 return NULL;
2208 Py_BEGIN_ALLOW_THREADS
2209 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2210 Py_END_ALLOW_THREADS
2211 if (res < 0)
2212 return posix_error();
2213 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002214}
2215#endif /* HAVE_FCHOWN */
2216
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002217#ifdef HAVE_LCHOWN
2218PyDoc_STRVAR(posix_lchown__doc__,
2219"lchown(path, uid, gid)\n\n\
2220Change the owner and group id of path to the numeric uid and gid.\n\
2221This function will not follow symbolic links.");
2222
2223static PyObject *
2224posix_lchown(PyObject *self, PyObject *args)
2225{
Victor Stinner8c62be82010-05-06 00:08:46 +00002226 PyObject *opath;
2227 char *path;
2228 long uid, gid;
2229 int res;
2230 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2231 PyUnicode_FSConverter, &opath,
2232 &uid, &gid))
2233 return NULL;
2234 path = PyBytes_AsString(opath);
2235 Py_BEGIN_ALLOW_THREADS
2236 res = lchown(path, (uid_t) uid, (gid_t) gid);
2237 Py_END_ALLOW_THREADS
2238 if (res < 0)
2239 return posix_error_with_allocated_filename(opath);
2240 Py_DECREF(opath);
2241 Py_INCREF(Py_None);
2242 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002243}
2244#endif /* HAVE_LCHOWN */
2245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002246
Guido van Rossum36bc6801995-06-14 22:54:23 +00002247#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002248static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002249posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002250{
Victor Stinner8c62be82010-05-06 00:08:46 +00002251 char buf[1026];
2252 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002253
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002254#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 if (!use_bytes) {
2256 wchar_t wbuf[1026];
2257 wchar_t *wbuf2 = wbuf;
2258 PyObject *resobj;
2259 DWORD len;
2260 Py_BEGIN_ALLOW_THREADS
2261 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2262 /* If the buffer is large enough, len does not include the
2263 terminating \0. If the buffer is too small, len includes
2264 the space needed for the terminator. */
2265 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2266 wbuf2 = malloc(len * sizeof(wchar_t));
2267 if (wbuf2)
2268 len = GetCurrentDirectoryW(len, wbuf2);
2269 }
2270 Py_END_ALLOW_THREADS
2271 if (!wbuf2) {
2272 PyErr_NoMemory();
2273 return NULL;
2274 }
2275 if (!len) {
2276 if (wbuf2 != wbuf) free(wbuf2);
2277 return win32_error("getcwdu", NULL);
2278 }
2279 resobj = PyUnicode_FromWideChar(wbuf2, len);
2280 if (wbuf2 != wbuf) free(wbuf2);
2281 return resobj;
2282 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002283#endif
2284
Victor Stinner8c62be82010-05-06 00:08:46 +00002285 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002286#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002287 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002288#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002289 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002290#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002291 Py_END_ALLOW_THREADS
2292 if (res == NULL)
2293 return posix_error();
2294 if (use_bytes)
2295 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002296 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002297}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002298
2299PyDoc_STRVAR(posix_getcwd__doc__,
2300"getcwd() -> path\n\n\
2301Return a unicode string representing the current working directory.");
2302
2303static PyObject *
2304posix_getcwd_unicode(PyObject *self)
2305{
2306 return posix_getcwd(0);
2307}
2308
2309PyDoc_STRVAR(posix_getcwdb__doc__,
2310"getcwdb() -> path\n\n\
2311Return a bytes string representing the current working directory.");
2312
2313static PyObject *
2314posix_getcwd_bytes(PyObject *self)
2315{
2316 return posix_getcwd(1);
2317}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002318#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002319
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002320
Guido van Rossumb6775db1994-08-01 11:34:53 +00002321#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002322PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002323"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002324Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002325
Barry Warsaw53699e91996-12-10 23:23:01 +00002326static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002327posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002328{
Victor Stinner8c62be82010-05-06 00:08:46 +00002329 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002330}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002331#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002332
Brian Curtin1b9df392010-11-24 20:24:31 +00002333#ifdef MS_WINDOWS
2334PyDoc_STRVAR(win32_link__doc__,
2335"link(src, dst)\n\n\
2336Create a hard link to a file.");
2337
2338static PyObject *
2339win32_link(PyObject *self, PyObject *args)
2340{
2341 PyObject *osrc, *odst;
2342 char *src, *dst;
2343 BOOL rslt;
2344
Brian Curtinfc889c42010-11-28 23:59:46 +00002345 PyUnicodeObject *usrc, *udst;
2346 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2347 Py_BEGIN_ALLOW_THREADS
2348 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2349 PyUnicode_AS_UNICODE(usrc), NULL);
2350 Py_END_ALLOW_THREADS
2351
2352 if (rslt == 0)
2353 return win32_error("link", NULL);
2354
2355 Py_RETURN_NONE;
2356 }
2357
2358 /* Narrow strings also valid. */
2359 PyErr_Clear();
2360
Brian Curtin1b9df392010-11-24 20:24:31 +00002361 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2362 PyUnicode_FSConverter, &odst))
2363 return NULL;
2364
2365 src = PyBytes_AsString(osrc);
2366 dst = PyBytes_AsString(odst);
2367
2368 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002369 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002370 Py_END_ALLOW_THREADS
2371
Stefan Krah30b341f2010-11-27 11:44:18 +00002372 Py_DECREF(osrc);
2373 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002374 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002375 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002376
2377 Py_RETURN_NONE;
2378}
2379#endif /* MS_WINDOWS */
2380
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002382PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002383"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002384Return a list containing the names of the entries in the directory.\n\
2385\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002386 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002387\n\
2388The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002389entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002390
Barry Warsaw53699e91996-12-10 23:23:01 +00002391static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002392posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002393{
Victor Stinner8c62be82010-05-06 00:08:46 +00002394 /* XXX Should redo this putting the (now four) versions of opendir
2395 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002396#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002397
Victor Stinner8c62be82010-05-06 00:08:46 +00002398 PyObject *d, *v;
2399 HANDLE hFindFile;
2400 BOOL result;
2401 WIN32_FIND_DATA FileData;
2402 PyObject *opath;
2403 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2404 char *bufptr = namebuf;
2405 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002406
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002407 PyObject *po = NULL;
2408 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002409 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002410 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002411
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002412 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002413 po_wchars = L".";
2414 len = 1;
2415 } else {
2416 po_wchars = PyUnicode_AS_UNICODE(po);
2417 len = PyUnicode_GET_SIZE(po);
2418 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002419 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002420 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2421 if (!wnamebuf) {
2422 PyErr_NoMemory();
2423 return NULL;
2424 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002425 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002426 if (len > 0) {
2427 Py_UNICODE wch = wnamebuf[len-1];
2428 if (wch != L'/' && wch != L'\\' && wch != L':')
2429 wnamebuf[len++] = L'\\';
2430 wcscpy(wnamebuf + len, L"*.*");
2431 }
2432 if ((d = PyList_New(0)) == NULL) {
2433 free(wnamebuf);
2434 return NULL;
2435 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002436 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002437 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002438 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002439 if (hFindFile == INVALID_HANDLE_VALUE) {
2440 int error = GetLastError();
2441 if (error == ERROR_FILE_NOT_FOUND) {
2442 free(wnamebuf);
2443 return d;
2444 }
2445 Py_DECREF(d);
2446 win32_error_unicode("FindFirstFileW", wnamebuf);
2447 free(wnamebuf);
2448 return NULL;
2449 }
2450 do {
2451 /* Skip over . and .. */
2452 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2453 wcscmp(wFileData.cFileName, L"..") != 0) {
2454 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2455 if (v == NULL) {
2456 Py_DECREF(d);
2457 d = NULL;
2458 break;
2459 }
2460 if (PyList_Append(d, v) != 0) {
2461 Py_DECREF(v);
2462 Py_DECREF(d);
2463 d = NULL;
2464 break;
2465 }
2466 Py_DECREF(v);
2467 }
2468 Py_BEGIN_ALLOW_THREADS
2469 result = FindNextFileW(hFindFile, &wFileData);
2470 Py_END_ALLOW_THREADS
2471 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2472 it got to the end of the directory. */
2473 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2474 Py_DECREF(d);
2475 win32_error_unicode("FindNextFileW", wnamebuf);
2476 FindClose(hFindFile);
2477 free(wnamebuf);
2478 return NULL;
2479 }
2480 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002481
Victor Stinner8c62be82010-05-06 00:08:46 +00002482 if (FindClose(hFindFile) == FALSE) {
2483 Py_DECREF(d);
2484 win32_error_unicode("FindClose", wnamebuf);
2485 free(wnamebuf);
2486 return NULL;
2487 }
2488 free(wnamebuf);
2489 return d;
2490 }
2491 /* Drop the argument parsing error as narrow strings
2492 are also valid. */
2493 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002494
Victor Stinner8c62be82010-05-06 00:08:46 +00002495 if (!PyArg_ParseTuple(args, "O&:listdir",
2496 PyUnicode_FSConverter, &opath))
2497 return NULL;
2498 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2499 PyErr_SetString(PyExc_ValueError, "path too long");
2500 Py_DECREF(opath);
2501 return NULL;
2502 }
2503 strcpy(namebuf, PyBytes_AsString(opath));
2504 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002505 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002506 if (len > 0) {
2507 char ch = namebuf[len-1];
2508 if (ch != SEP && ch != ALTSEP && ch != ':')
2509 namebuf[len++] = '/';
2510 strcpy(namebuf + len, "*.*");
2511 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002512
Victor Stinner8c62be82010-05-06 00:08:46 +00002513 if ((d = PyList_New(0)) == NULL)
2514 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002515
Antoine Pitroub73caab2010-08-09 23:39:31 +00002516 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002517 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002518 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002519 if (hFindFile == INVALID_HANDLE_VALUE) {
2520 int error = GetLastError();
2521 if (error == ERROR_FILE_NOT_FOUND)
2522 return d;
2523 Py_DECREF(d);
2524 return win32_error("FindFirstFile", namebuf);
2525 }
2526 do {
2527 /* Skip over . and .. */
2528 if (strcmp(FileData.cFileName, ".") != 0 &&
2529 strcmp(FileData.cFileName, "..") != 0) {
2530 v = PyBytes_FromString(FileData.cFileName);
2531 if (v == NULL) {
2532 Py_DECREF(d);
2533 d = NULL;
2534 break;
2535 }
2536 if (PyList_Append(d, v) != 0) {
2537 Py_DECREF(v);
2538 Py_DECREF(d);
2539 d = NULL;
2540 break;
2541 }
2542 Py_DECREF(v);
2543 }
2544 Py_BEGIN_ALLOW_THREADS
2545 result = FindNextFile(hFindFile, &FileData);
2546 Py_END_ALLOW_THREADS
2547 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2548 it got to the end of the directory. */
2549 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2550 Py_DECREF(d);
2551 win32_error("FindNextFile", namebuf);
2552 FindClose(hFindFile);
2553 return NULL;
2554 }
2555 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002556
Victor Stinner8c62be82010-05-06 00:08:46 +00002557 if (FindClose(hFindFile) == FALSE) {
2558 Py_DECREF(d);
2559 return win32_error("FindClose", namebuf);
2560 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002561
Victor Stinner8c62be82010-05-06 00:08:46 +00002562 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002563
Tim Peters0bb44a42000-09-15 07:44:49 +00002564#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002565
2566#ifndef MAX_PATH
2567#define MAX_PATH CCHMAXPATH
2568#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002569 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002570 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002571 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002572 PyObject *d, *v;
2573 char namebuf[MAX_PATH+5];
2574 HDIR hdir = 1;
2575 ULONG srchcnt = 1;
2576 FILEFINDBUF3 ep;
2577 APIRET rc;
2578
Victor Stinner8c62be82010-05-06 00:08:46 +00002579 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002580 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002581 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002582 name = PyBytes_AsString(oname);
2583 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002584 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002585 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002586 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002587 return NULL;
2588 }
2589 strcpy(namebuf, name);
2590 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002591 if (*pt == ALTSEP)
2592 *pt = SEP;
2593 if (namebuf[len-1] != SEP)
2594 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002595 strcpy(namebuf + len, "*.*");
2596
Neal Norwitz6c913782007-10-14 03:23:09 +00002597 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002598 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002599 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002600 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002601
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002602 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2603 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002604 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002605 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2606 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2607 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002608
2609 if (rc != NO_ERROR) {
2610 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002611 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002612 }
2613
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002614 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002615 do {
2616 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002617 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002618 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002619
2620 strcpy(namebuf, ep.achName);
2621
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002622 /* Leave Case of Name Alone -- In Native Form */
2623 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002624
Christian Heimes72b710a2008-05-26 13:28:38 +00002625 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002626 if (v == NULL) {
2627 Py_DECREF(d);
2628 d = NULL;
2629 break;
2630 }
2631 if (PyList_Append(d, v) != 0) {
2632 Py_DECREF(v);
2633 Py_DECREF(d);
2634 d = NULL;
2635 break;
2636 }
2637 Py_DECREF(v);
2638 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2639 }
2640
Victor Stinnerdcb24032010-04-22 12:08:36 +00002641 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002642 return d;
2643#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 PyObject *oname;
2645 char *name;
2646 PyObject *d, *v;
2647 DIR *dirp;
2648 struct dirent *ep;
2649 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002650
Victor Stinner8c62be82010-05-06 00:08:46 +00002651 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002652 /* v is never read, so it does not need to be initialized yet. */
2653 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002654 arg_is_unicode = 0;
2655 PyErr_Clear();
2656 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002657 oname = NULL;
2658 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002659 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002660 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002661 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002662 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002664 Py_BEGIN_ALLOW_THREADS
2665 dirp = opendir(name);
2666 Py_END_ALLOW_THREADS
2667 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002668 return posix_error_with_allocated_filename(oname);
2669 }
2670 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002671 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002672 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002673 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002674 Py_DECREF(oname);
2675 return NULL;
2676 }
2677 for (;;) {
2678 errno = 0;
2679 Py_BEGIN_ALLOW_THREADS
2680 ep = readdir(dirp);
2681 Py_END_ALLOW_THREADS
2682 if (ep == NULL) {
2683 if (errno == 0) {
2684 break;
2685 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002686 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002687 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002688 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002689 Py_DECREF(d);
2690 return posix_error_with_allocated_filename(oname);
2691 }
2692 }
2693 if (ep->d_name[0] == '.' &&
2694 (NAMLEN(ep) == 1 ||
2695 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2696 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002697 if (arg_is_unicode)
2698 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2699 else
2700 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002701 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002702 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002703 break;
2704 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002705 if (PyList_Append(d, v) != 0) {
2706 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002707 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002708 break;
2709 }
2710 Py_DECREF(v);
2711 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002712 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002713 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002714 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002715 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002716
Victor Stinner8c62be82010-05-06 00:08:46 +00002717 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002718
Tim Peters0bb44a42000-09-15 07:44:49 +00002719#endif /* which OS */
2720} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002721
Antoine Pitrou8250e232011-02-25 23:41:16 +00002722#ifdef HAVE_FDOPENDIR
2723PyDoc_STRVAR(posix_fdlistdir__doc__,
2724"fdlistdir(fd) -> list_of_strings\n\n\
2725Like listdir(), but uses a file descriptor instead.\n\
2726After succesful execution of this function, fd will be closed.");
2727
2728static PyObject *
2729posix_fdlistdir(PyObject *self, PyObject *args)
2730{
2731 PyObject *d, *v;
2732 DIR *dirp;
2733 struct dirent *ep;
2734 int fd;
2735
2736 errno = 0;
2737 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2738 return NULL;
2739 Py_BEGIN_ALLOW_THREADS
2740 dirp = fdopendir(fd);
2741 Py_END_ALLOW_THREADS
2742 if (dirp == NULL) {
2743 close(fd);
2744 return posix_error();
2745 }
2746 if ((d = PyList_New(0)) == NULL) {
2747 Py_BEGIN_ALLOW_THREADS
2748 closedir(dirp);
2749 Py_END_ALLOW_THREADS
2750 return NULL;
2751 }
2752 for (;;) {
2753 errno = 0;
2754 Py_BEGIN_ALLOW_THREADS
2755 ep = readdir(dirp);
2756 Py_END_ALLOW_THREADS
2757 if (ep == NULL) {
2758 if (errno == 0) {
2759 break;
2760 } else {
2761 Py_BEGIN_ALLOW_THREADS
2762 closedir(dirp);
2763 Py_END_ALLOW_THREADS
2764 Py_DECREF(d);
2765 return posix_error();
2766 }
2767 }
2768 if (ep->d_name[0] == '.' &&
2769 (NAMLEN(ep) == 1 ||
2770 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2771 continue;
2772 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2773 if (v == NULL) {
2774 Py_CLEAR(d);
2775 break;
2776 }
2777 if (PyList_Append(d, v) != 0) {
2778 Py_DECREF(v);
2779 Py_CLEAR(d);
2780 break;
2781 }
2782 Py_DECREF(v);
2783 }
2784 Py_BEGIN_ALLOW_THREADS
2785 closedir(dirp);
2786 Py_END_ALLOW_THREADS
2787
2788 return d;
2789}
2790#endif
2791
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002792#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002793/* A helper function for abspath on win32 */
2794static PyObject *
2795posix__getfullpathname(PyObject *self, PyObject *args)
2796{
Victor Stinner8c62be82010-05-06 00:08:46 +00002797 PyObject *opath;
2798 char *path;
2799 char outbuf[MAX_PATH*2];
2800 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002801#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002802 PyUnicodeObject *po;
2803 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2804 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2805 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2806 Py_UNICODE *wtemp;
2807 DWORD result;
2808 PyObject *v;
2809 result = GetFullPathNameW(wpath,
2810 sizeof(woutbuf)/sizeof(woutbuf[0]),
2811 woutbuf, &wtemp);
2812 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2813 woutbufp = malloc(result * sizeof(Py_UNICODE));
2814 if (!woutbufp)
2815 return PyErr_NoMemory();
2816 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2817 }
2818 if (result)
2819 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2820 else
2821 v = win32_error_unicode("GetFullPathNameW", wpath);
2822 if (woutbufp != woutbuf)
2823 free(woutbufp);
2824 return v;
2825 }
2826 /* Drop the argument parsing error as narrow strings
2827 are also valid. */
2828 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002829
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002830#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002831 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2832 PyUnicode_FSConverter, &opath))
2833 return NULL;
2834 path = PyBytes_AsString(opath);
2835 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2836 outbuf, &temp)) {
2837 win32_error("GetFullPathName", path);
2838 Py_DECREF(opath);
2839 return NULL;
2840 }
2841 Py_DECREF(opath);
2842 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2843 return PyUnicode_Decode(outbuf, strlen(outbuf),
2844 Py_FileSystemDefaultEncoding, NULL);
2845 }
2846 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002847} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002848
Brian Curtinf5e76d02010-11-24 13:14:05 +00002849/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2850static int has_GetFinalPathNameByHandle = 0;
2851static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2852 DWORD);
2853static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2854 DWORD);
2855static int
2856check_GetFinalPathNameByHandle()
2857{
2858 HINSTANCE hKernel32;
2859 /* only recheck */
2860 if (!has_GetFinalPathNameByHandle)
2861 {
2862 hKernel32 = GetModuleHandle("KERNEL32");
2863 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2864 "GetFinalPathNameByHandleA");
2865 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2866 "GetFinalPathNameByHandleW");
2867 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2868 Py_GetFinalPathNameByHandleW;
2869 }
2870 return has_GetFinalPathNameByHandle;
2871}
2872
Brian Curtind40e6f72010-07-08 21:39:08 +00002873/* A helper function for samepath on windows */
2874static PyObject *
2875posix__getfinalpathname(PyObject *self, PyObject *args)
2876{
2877 HANDLE hFile;
2878 int buf_size;
2879 wchar_t *target_path;
2880 int result_length;
2881 PyObject *result;
2882 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002883
Brian Curtin94622b02010-09-24 00:03:39 +00002884 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002885 return NULL;
2886 }
2887
2888 if(!check_GetFinalPathNameByHandle()) {
2889 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2890 NotImplementedError. */
2891 return PyErr_Format(PyExc_NotImplementedError,
2892 "GetFinalPathNameByHandle not available on this platform");
2893 }
2894
2895 hFile = CreateFileW(
2896 path,
2897 0, /* desired access */
2898 0, /* share mode */
2899 NULL, /* security attributes */
2900 OPEN_EXISTING,
2901 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2902 FILE_FLAG_BACKUP_SEMANTICS,
2903 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002904
Brian Curtind40e6f72010-07-08 21:39:08 +00002905 if(hFile == INVALID_HANDLE_VALUE) {
2906 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002907 return PyErr_Format(PyExc_RuntimeError,
2908 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002909 }
2910
2911 /* We have a good handle to the target, use it to determine the
2912 target path name. */
2913 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2914
2915 if(!buf_size)
2916 return win32_error_unicode("GetFinalPathNameByHandle", path);
2917
2918 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2919 if(!target_path)
2920 return PyErr_NoMemory();
2921
2922 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2923 buf_size, VOLUME_NAME_DOS);
2924 if(!result_length)
2925 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2926
2927 if(!CloseHandle(hFile))
2928 return win32_error_unicode("GetFinalPathNameByHandle", path);
2929
2930 target_path[result_length] = 0;
2931 result = PyUnicode_FromUnicode(target_path, result_length);
2932 free(target_path);
2933 return result;
2934
2935} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002936
2937static PyObject *
2938posix__getfileinformation(PyObject *self, PyObject *args)
2939{
2940 HANDLE hFile;
2941 BY_HANDLE_FILE_INFORMATION info;
2942 int fd;
2943
2944 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2945 return NULL;
2946
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002947 if (!_PyVerify_fd(fd))
2948 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002949
2950 hFile = (HANDLE)_get_osfhandle(fd);
2951 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002952 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002953
2954 if (!GetFileInformationByHandle(hFile, &info))
2955 return win32_error("_getfileinformation", NULL);
2956
2957 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2958 info.nFileIndexHigh,
2959 info.nFileIndexLow);
2960}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002961#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002962
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002963PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002964"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002965Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002966
Barry Warsaw53699e91996-12-10 23:23:01 +00002967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002968posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002969{
Victor Stinner8c62be82010-05-06 00:08:46 +00002970 int res;
2971 PyObject *opath;
2972 char *path;
2973 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002974
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002975#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002976 PyUnicodeObject *po;
2977 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2978 Py_BEGIN_ALLOW_THREADS
2979 /* PyUnicode_AS_UNICODE OK without thread lock as
2980 it is a simple dereference. */
2981 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2982 Py_END_ALLOW_THREADS
2983 if (!res)
2984 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2985 Py_INCREF(Py_None);
2986 return Py_None;
2987 }
2988 /* Drop the argument parsing error as narrow strings
2989 are also valid. */
2990 PyErr_Clear();
2991 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2992 PyUnicode_FSConverter, &opath, &mode))
2993 return NULL;
2994 path = PyBytes_AsString(opath);
2995 Py_BEGIN_ALLOW_THREADS
2996 /* PyUnicode_AS_UNICODE OK without thread lock as
2997 it is a simple dereference. */
2998 res = CreateDirectoryA(path, NULL);
2999 Py_END_ALLOW_THREADS
3000 if (!res) {
3001 win32_error("mkdir", path);
3002 Py_DECREF(opath);
3003 return NULL;
3004 }
3005 Py_DECREF(opath);
3006 Py_INCREF(Py_None);
3007 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003008#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003009
Victor Stinner8c62be82010-05-06 00:08:46 +00003010 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3011 PyUnicode_FSConverter, &opath, &mode))
3012 return NULL;
3013 path = PyBytes_AsString(opath);
3014 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003015#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003016 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003017#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003018 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003019#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003020 Py_END_ALLOW_THREADS
3021 if (res < 0)
3022 return posix_error_with_allocated_filename(opath);
3023 Py_DECREF(opath);
3024 Py_INCREF(Py_None);
3025 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003026#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003027}
3028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003029
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003030/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3031#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003032#include <sys/resource.h>
3033#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003034
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003035
3036#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003037PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003038"nice(inc) -> new_priority\n\n\
3039Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003040
Barry Warsaw53699e91996-12-10 23:23:01 +00003041static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003042posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003043{
Victor Stinner8c62be82010-05-06 00:08:46 +00003044 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003045
Victor Stinner8c62be82010-05-06 00:08:46 +00003046 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3047 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003048
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 /* There are two flavours of 'nice': one that returns the new
3050 priority (as required by almost all standards out there) and the
3051 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3052 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003053
Victor Stinner8c62be82010-05-06 00:08:46 +00003054 If we are of the nice family that returns the new priority, we
3055 need to clear errno before the call, and check if errno is filled
3056 before calling posix_error() on a returnvalue of -1, because the
3057 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003058
Victor Stinner8c62be82010-05-06 00:08:46 +00003059 errno = 0;
3060 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003061#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003062 if (value == 0)
3063 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003064#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003065 if (value == -1 && errno != 0)
3066 /* either nice() or getpriority() returned an error */
3067 return posix_error();
3068 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003069}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003070#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003071
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003072
3073#ifdef HAVE_GETPRIORITY
3074PyDoc_STRVAR(posix_getpriority__doc__,
3075"getpriority(which, who) -> current_priority\n\n\
3076Get program scheduling priority.");
3077
3078static PyObject *
3079posix_getpriority(PyObject *self, PyObject *args)
3080{
3081 int which, who, retval;
3082
3083 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3084 return NULL;
3085 errno = 0;
3086 Py_BEGIN_ALLOW_THREADS
3087 retval = getpriority(which, who);
3088 Py_END_ALLOW_THREADS
3089 if (errno != 0)
3090 return posix_error();
3091 return PyLong_FromLong((long)retval);
3092}
3093#endif /* HAVE_GETPRIORITY */
3094
3095
3096#ifdef HAVE_SETPRIORITY
3097PyDoc_STRVAR(posix_setpriority__doc__,
3098"setpriority(which, who, prio) -> None\n\n\
3099Set program scheduling priority.");
3100
3101static PyObject *
3102posix_setpriority(PyObject *self, PyObject *args)
3103{
3104 int which, who, prio, retval;
3105
3106 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3107 return NULL;
3108 Py_BEGIN_ALLOW_THREADS
3109 retval = setpriority(which, who, prio);
3110 Py_END_ALLOW_THREADS
3111 if (retval == -1)
3112 return posix_error();
3113 Py_RETURN_NONE;
3114}
3115#endif /* HAVE_SETPRIORITY */
3116
3117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003118PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003119"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003120Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003121
Barry Warsaw53699e91996-12-10 23:23:01 +00003122static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003123posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003124{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003125#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003126 PyObject *o1, *o2;
3127 char *p1, *p2;
3128 BOOL result;
3129 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3130 goto error;
3131 if (!convert_to_unicode(&o1))
3132 goto error;
3133 if (!convert_to_unicode(&o2)) {
3134 Py_DECREF(o1);
3135 goto error;
3136 }
3137 Py_BEGIN_ALLOW_THREADS
3138 result = MoveFileW(PyUnicode_AsUnicode(o1),
3139 PyUnicode_AsUnicode(o2));
3140 Py_END_ALLOW_THREADS
3141 Py_DECREF(o1);
3142 Py_DECREF(o2);
3143 if (!result)
3144 return win32_error("rename", NULL);
3145 Py_INCREF(Py_None);
3146 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003147error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 PyErr_Clear();
3149 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3150 return NULL;
3151 Py_BEGIN_ALLOW_THREADS
3152 result = MoveFileA(p1, p2);
3153 Py_END_ALLOW_THREADS
3154 if (!result)
3155 return win32_error("rename", NULL);
3156 Py_INCREF(Py_None);
3157 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003158#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003160#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003161}
3162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003164PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003165"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003166Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167
Barry Warsaw53699e91996-12-10 23:23:01 +00003168static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003169posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003170{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003171#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003172 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003173#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003174 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003175#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003176}
3177
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003179PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003180"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003181Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003182
Barry Warsaw53699e91996-12-10 23:23:01 +00003183static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003184posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003185{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003186#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003187 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003188#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003189 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003190#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003191}
3192
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003193
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003194#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003195PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003196"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003197Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003198
Barry Warsaw53699e91996-12-10 23:23:01 +00003199static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003200posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003201{
Victor Stinner8c62be82010-05-06 00:08:46 +00003202 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003203#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003204 wchar_t *command;
3205 if (!PyArg_ParseTuple(args, "u:system", &command))
3206 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003207
Victor Stinner8c62be82010-05-06 00:08:46 +00003208 Py_BEGIN_ALLOW_THREADS
3209 sts = _wsystem(command);
3210 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003211#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003212 PyObject *command_obj;
3213 char *command;
3214 if (!PyArg_ParseTuple(args, "O&:system",
3215 PyUnicode_FSConverter, &command_obj))
3216 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003217
Victor Stinner8c62be82010-05-06 00:08:46 +00003218 command = PyBytes_AsString(command_obj);
3219 Py_BEGIN_ALLOW_THREADS
3220 sts = system(command);
3221 Py_END_ALLOW_THREADS
3222 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003223#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003224 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003225}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003226#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003227
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003229PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003230"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003231Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003232
Barry Warsaw53699e91996-12-10 23:23:01 +00003233static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003234posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003235{
Victor Stinner8c62be82010-05-06 00:08:46 +00003236 int i;
3237 if (!PyArg_ParseTuple(args, "i:umask", &i))
3238 return NULL;
3239 i = (int)umask(i);
3240 if (i < 0)
3241 return posix_error();
3242 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003243}
3244
Brian Curtind40e6f72010-07-08 21:39:08 +00003245#ifdef MS_WINDOWS
3246
3247/* override the default DeleteFileW behavior so that directory
3248symlinks can be removed with this function, the same as with
3249Unix symlinks */
3250BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3251{
3252 WIN32_FILE_ATTRIBUTE_DATA info;
3253 WIN32_FIND_DATAW find_data;
3254 HANDLE find_data_handle;
3255 int is_directory = 0;
3256 int is_link = 0;
3257
3258 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3259 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003260
Brian Curtind40e6f72010-07-08 21:39:08 +00003261 /* Get WIN32_FIND_DATA structure for the path to determine if
3262 it is a symlink */
3263 if(is_directory &&
3264 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3265 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3266
3267 if(find_data_handle != INVALID_HANDLE_VALUE) {
3268 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3269 FindClose(find_data_handle);
3270 }
3271 }
3272 }
3273
3274 if (is_directory && is_link)
3275 return RemoveDirectoryW(lpFileName);
3276
3277 return DeleteFileW(lpFileName);
3278}
3279#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003281PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003282"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003283Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003284
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003285PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003286"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003287Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003288
Barry Warsaw53699e91996-12-10 23:23:01 +00003289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003290posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003291{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003292#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003293 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3294 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003295#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003297#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003298}
3299
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003300
Guido van Rossumb6775db1994-08-01 11:34:53 +00003301#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003302PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003303"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003304Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003305
Barry Warsaw53699e91996-12-10 23:23:01 +00003306static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003307posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003308{
Victor Stinner8c62be82010-05-06 00:08:46 +00003309 struct utsname u;
3310 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003311
Victor Stinner8c62be82010-05-06 00:08:46 +00003312 Py_BEGIN_ALLOW_THREADS
3313 res = uname(&u);
3314 Py_END_ALLOW_THREADS
3315 if (res < 0)
3316 return posix_error();
3317 return Py_BuildValue("(sssss)",
3318 u.sysname,
3319 u.nodename,
3320 u.release,
3321 u.version,
3322 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003323}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003324#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003325
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003326static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003327extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003328{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003329 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 if (PyFloat_Check(t)) {
3331 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003332 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 if (!intobj)
3334 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003335#if SIZEOF_TIME_T > SIZEOF_LONG
3336 intval = PyLong_AsUnsignedLongLongMask(intobj);
3337#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003338 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003339#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003340 Py_DECREF(intobj);
3341 if (intval == -1 && PyErr_Occurred())
3342 return -1;
3343 *sec = intval;
3344 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3345 if (*usec < 0)
3346 /* If rounding gave us a negative number,
3347 truncate. */
3348 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003349 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003351#if SIZEOF_TIME_T > SIZEOF_LONG
3352 intval = PyLong_AsUnsignedLongLongMask(t);
3353#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003354 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003355#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 if (intval == -1 && PyErr_Occurred())
3357 return -1;
3358 *sec = intval;
3359 *usec = 0;
3360 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003361}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003363PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003364"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003365utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003366Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003367second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003368
Barry Warsaw53699e91996-12-10 23:23:01 +00003369static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003370posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003371{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003372#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003373 PyObject *arg;
3374 PyUnicodeObject *obwpath;
3375 wchar_t *wpath = NULL;
3376 PyObject *oapath;
3377 char *apath;
3378 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003379 time_t atimesec, mtimesec;
3380 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003381 FILETIME atime, mtime;
3382 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003383
Victor Stinner8c62be82010-05-06 00:08:46 +00003384 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3385 wpath = PyUnicode_AS_UNICODE(obwpath);
3386 Py_BEGIN_ALLOW_THREADS
3387 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3388 NULL, OPEN_EXISTING,
3389 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3390 Py_END_ALLOW_THREADS
3391 if (hFile == INVALID_HANDLE_VALUE)
3392 return win32_error_unicode("utime", wpath);
3393 } else
3394 /* Drop the argument parsing error as narrow strings
3395 are also valid. */
3396 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003397
Victor Stinner8c62be82010-05-06 00:08:46 +00003398 if (!wpath) {
3399 if (!PyArg_ParseTuple(args, "O&O:utime",
3400 PyUnicode_FSConverter, &oapath, &arg))
3401 return NULL;
3402 apath = PyBytes_AsString(oapath);
3403 Py_BEGIN_ALLOW_THREADS
3404 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3405 NULL, OPEN_EXISTING,
3406 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3407 Py_END_ALLOW_THREADS
3408 if (hFile == INVALID_HANDLE_VALUE) {
3409 win32_error("utime", apath);
3410 Py_DECREF(oapath);
3411 return NULL;
3412 }
3413 Py_DECREF(oapath);
3414 }
3415
3416 if (arg == Py_None) {
3417 SYSTEMTIME now;
3418 GetSystemTime(&now);
3419 if (!SystemTimeToFileTime(&now, &mtime) ||
3420 !SystemTimeToFileTime(&now, &atime)) {
3421 win32_error("utime", NULL);
3422 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003423 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003424 }
3425 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3426 PyErr_SetString(PyExc_TypeError,
3427 "utime() arg 2 must be a tuple (atime, mtime)");
3428 goto done;
3429 }
3430 else {
3431 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3432 &atimesec, &ausec) == -1)
3433 goto done;
3434 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3435 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3436 &mtimesec, &musec) == -1)
3437 goto done;
3438 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3439 }
3440 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3441 /* Avoid putting the file name into the error here,
3442 as that may confuse the user into believing that
3443 something is wrong with the file, when it also
3444 could be the time stamp that gives a problem. */
3445 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003446 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003447 }
3448 Py_INCREF(Py_None);
3449 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003450done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003451 CloseHandle(hFile);
3452 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003453#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003454
Victor Stinner8c62be82010-05-06 00:08:46 +00003455 PyObject *opath;
3456 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003457 time_t atime, mtime;
3458 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003459 int res;
3460 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003461
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003462#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003464#define ATIME buf[0].tv_sec
3465#define MTIME buf[1].tv_sec
3466#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003467/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003468 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003469#define ATIME buf.actime
3470#define MTIME buf.modtime
3471#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003472#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003473 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003474#define ATIME buf[0]
3475#define MTIME buf[1]
3476#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003477#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003478
Mark Hammond817c9292003-12-03 01:22:38 +00003479
Victor Stinner8c62be82010-05-06 00:08:46 +00003480 if (!PyArg_ParseTuple(args, "O&O:utime",
3481 PyUnicode_FSConverter, &opath, &arg))
3482 return NULL;
3483 path = PyBytes_AsString(opath);
3484 if (arg == Py_None) {
3485 /* optional time values not given */
3486 Py_BEGIN_ALLOW_THREADS
3487 res = utime(path, NULL);
3488 Py_END_ALLOW_THREADS
3489 }
3490 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3491 PyErr_SetString(PyExc_TypeError,
3492 "utime() arg 2 must be a tuple (atime, mtime)");
3493 Py_DECREF(opath);
3494 return NULL;
3495 }
3496 else {
3497 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3498 &atime, &ausec) == -1) {
3499 Py_DECREF(opath);
3500 return NULL;
3501 }
3502 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3503 &mtime, &musec) == -1) {
3504 Py_DECREF(opath);
3505 return NULL;
3506 }
3507 ATIME = atime;
3508 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003509#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003510 buf[0].tv_usec = ausec;
3511 buf[1].tv_usec = musec;
3512 Py_BEGIN_ALLOW_THREADS
3513 res = utimes(path, buf);
3514 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003515#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003516 Py_BEGIN_ALLOW_THREADS
3517 res = utime(path, UTIME_ARG);
3518 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003519#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003520 }
3521 if (res < 0) {
3522 return posix_error_with_allocated_filename(opath);
3523 }
3524 Py_DECREF(opath);
3525 Py_INCREF(Py_None);
3526 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003527#undef UTIME_ARG
3528#undef ATIME
3529#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003530#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003531}
3532
Ross Lagerwall7807c352011-03-17 20:20:30 +02003533#ifdef HAVE_FUTIMES
3534PyDoc_STRVAR(posix_futimes__doc__,
3535"futimes(fd, (atime, mtime))\n\
3536futimes(fd, None)\n\n\
3537Set the access and modified time of the file specified by the file\n\
3538descriptor fd to the given values. If the second form is used, set the\n\
3539access and modified times to the current time.");
3540
3541static PyObject *
3542posix_futimes(PyObject *self, PyObject *args)
3543{
3544 int res, fd;
3545 PyObject* arg;
3546 struct timeval buf[2];
3547 long ausec, musec;
3548
3549 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3550 return NULL;
3551
3552 if (arg == Py_None) {
3553 /* optional time values not given */
3554 Py_BEGIN_ALLOW_THREADS
3555 res = futimes(fd, NULL);
3556 Py_END_ALLOW_THREADS
3557 }
3558 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3559 PyErr_SetString(PyExc_TypeError,
3560 "futimes() arg 2 must be a tuple (atime, mtime)");
3561 return NULL;
3562 }
3563 else {
3564 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3565 &(buf[0].tv_sec), &ausec) == -1) {
3566 return NULL;
3567 }
3568 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3569 &(buf[1].tv_sec), &musec) == -1) {
3570 return NULL;
3571 }
3572 buf[0].tv_usec = ausec;
3573 buf[1].tv_usec = musec;
3574 Py_BEGIN_ALLOW_THREADS
3575 res = futimes(fd, buf);
3576 Py_END_ALLOW_THREADS
3577 }
3578 if (res < 0)
3579 return posix_error();
3580 Py_RETURN_NONE;
3581}
3582#endif
3583
3584#ifdef HAVE_LUTIMES
3585PyDoc_STRVAR(posix_lutimes__doc__,
3586"lutimes(path, (atime, mtime))\n\
3587lutimes(path, None)\n\n\
3588Like utime(), but if path is a symbolic link, it is not dereferenced.");
3589
3590static PyObject *
3591posix_lutimes(PyObject *self, PyObject *args)
3592{
3593 PyObject *opath, *arg;
3594 const char *path;
3595 int res;
3596 struct timeval buf[2];
3597 long ausec, musec;
3598
3599 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3600 PyUnicode_FSConverter, &opath, &arg))
3601 return NULL;
3602 path = PyBytes_AsString(opath);
3603 if (arg == Py_None) {
3604 /* optional time values not given */
3605 Py_BEGIN_ALLOW_THREADS
3606 res = lutimes(path, NULL);
3607 Py_END_ALLOW_THREADS
3608 }
3609 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3610 PyErr_SetString(PyExc_TypeError,
3611 "lutimes() arg 2 must be a tuple (atime, mtime)");
3612 Py_DECREF(opath);
3613 return NULL;
3614 }
3615 else {
3616 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3617 &(buf[0].tv_sec), &ausec) == -1) {
3618 Py_DECREF(opath);
3619 return NULL;
3620 }
3621 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3622 &(buf[1].tv_sec), &musec) == -1) {
3623 Py_DECREF(opath);
3624 return NULL;
3625 }
3626 buf[0].tv_usec = ausec;
3627 buf[1].tv_usec = musec;
3628 Py_BEGIN_ALLOW_THREADS
3629 res = lutimes(path, buf);
3630 Py_END_ALLOW_THREADS
3631 }
3632 Py_DECREF(opath);
3633 if (res < 0)
3634 return posix_error();
3635 Py_RETURN_NONE;
3636}
3637#endif
3638
3639#ifdef HAVE_FUTIMENS
3640PyDoc_STRVAR(posix_futimens__doc__,
3641"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3642futimens(fd, None, None)\n\n\
3643Updates the timestamps of a file specified by the file descriptor fd, with\n\
3644nanosecond precision.\n\
3645The second form sets atime and mtime to the current time.\n\
3646If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3647current time.\n\
3648If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3649
3650static PyObject *
3651posix_futimens(PyObject *self, PyObject *args)
3652{
3653 int res, fd;
3654 PyObject *atime, *mtime;
3655 struct timespec buf[2];
3656
3657 if (!PyArg_ParseTuple(args, "iOO:futimens",
3658 &fd, &atime, &mtime))
3659 return NULL;
3660 if (atime == Py_None && mtime == Py_None) {
3661 /* optional time values not given */
3662 Py_BEGIN_ALLOW_THREADS
3663 res = futimens(fd, NULL);
3664 Py_END_ALLOW_THREADS
3665 }
3666 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3667 PyErr_SetString(PyExc_TypeError,
3668 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3669 return NULL;
3670 }
3671 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3672 PyErr_SetString(PyExc_TypeError,
3673 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3674 return NULL;
3675 }
3676 else {
3677 if (!PyArg_ParseTuple(atime, "ll:futimens",
3678 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3679 return NULL;
3680 }
3681 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3682 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3683 return NULL;
3684 }
3685 Py_BEGIN_ALLOW_THREADS
3686 res = futimens(fd, buf);
3687 Py_END_ALLOW_THREADS
3688 }
3689 if (res < 0)
3690 return posix_error();
3691 Py_RETURN_NONE;
3692}
3693#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003694
Guido van Rossum3b066191991-06-04 19:40:25 +00003695/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003697PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003698"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003699Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003700
Barry Warsaw53699e91996-12-10 23:23:01 +00003701static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003702posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003703{
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 int sts;
3705 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3706 return NULL;
3707 _exit(sts);
3708 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003709}
3710
Martin v. Löwis114619e2002-10-07 06:44:21 +00003711#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3712static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003713free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003714{
Victor Stinner8c62be82010-05-06 00:08:46 +00003715 Py_ssize_t i;
3716 for (i = 0; i < count; i++)
3717 PyMem_Free(array[i]);
3718 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003719}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003720
Antoine Pitrou69f71142009-05-24 21:25:49 +00003721static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003722int fsconvert_strdup(PyObject *o, char**out)
3723{
Victor Stinner8c62be82010-05-06 00:08:46 +00003724 PyObject *bytes;
3725 Py_ssize_t size;
3726 if (!PyUnicode_FSConverter(o, &bytes))
3727 return 0;
3728 size = PyBytes_GET_SIZE(bytes);
3729 *out = PyMem_Malloc(size+1);
3730 if (!*out)
3731 return 0;
3732 memcpy(*out, PyBytes_AsString(bytes), size+1);
3733 Py_DECREF(bytes);
3734 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003735}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003736#endif
3737
Ross Lagerwall7807c352011-03-17 20:20:30 +02003738#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003739static char**
3740parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3741{
Victor Stinner8c62be82010-05-06 00:08:46 +00003742 char **envlist;
3743 Py_ssize_t i, pos, envc;
3744 PyObject *keys=NULL, *vals=NULL;
3745 PyObject *key, *val, *key2, *val2;
3746 char *p, *k, *v;
3747 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003748
Victor Stinner8c62be82010-05-06 00:08:46 +00003749 i = PyMapping_Size(env);
3750 if (i < 0)
3751 return NULL;
3752 envlist = PyMem_NEW(char *, i + 1);
3753 if (envlist == NULL) {
3754 PyErr_NoMemory();
3755 return NULL;
3756 }
3757 envc = 0;
3758 keys = PyMapping_Keys(env);
3759 vals = PyMapping_Values(env);
3760 if (!keys || !vals)
3761 goto error;
3762 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3763 PyErr_Format(PyExc_TypeError,
3764 "env.keys() or env.values() is not a list");
3765 goto error;
3766 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003767
Victor Stinner8c62be82010-05-06 00:08:46 +00003768 for (pos = 0; pos < i; pos++) {
3769 key = PyList_GetItem(keys, pos);
3770 val = PyList_GetItem(vals, pos);
3771 if (!key || !val)
3772 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003773
Victor Stinner8c62be82010-05-06 00:08:46 +00003774 if (PyUnicode_FSConverter(key, &key2) == 0)
3775 goto error;
3776 if (PyUnicode_FSConverter(val, &val2) == 0) {
3777 Py_DECREF(key2);
3778 goto error;
3779 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003780
3781#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003782 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3783 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003784#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003785 k = PyBytes_AsString(key2);
3786 v = PyBytes_AsString(val2);
3787 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003788
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 p = PyMem_NEW(char, len);
3790 if (p == NULL) {
3791 PyErr_NoMemory();
3792 Py_DECREF(key2);
3793 Py_DECREF(val2);
3794 goto error;
3795 }
3796 PyOS_snprintf(p, len, "%s=%s", k, v);
3797 envlist[envc++] = p;
3798 Py_DECREF(key2);
3799 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003800#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003801 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003802#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003803 }
3804 Py_DECREF(vals);
3805 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003806
Victor Stinner8c62be82010-05-06 00:08:46 +00003807 envlist[envc] = 0;
3808 *envc_ptr = envc;
3809 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003810
3811error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003812 Py_XDECREF(keys);
3813 Py_XDECREF(vals);
3814 while (--envc >= 0)
3815 PyMem_DEL(envlist[envc]);
3816 PyMem_DEL(envlist);
3817 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003818}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003819
Ross Lagerwall7807c352011-03-17 20:20:30 +02003820static char**
3821parse_arglist(PyObject* argv, Py_ssize_t *argc)
3822{
3823 int i;
3824 char **argvlist = PyMem_NEW(char *, *argc+1);
3825 if (argvlist == NULL) {
3826 PyErr_NoMemory();
3827 return NULL;
3828 }
3829 for (i = 0; i < *argc; i++) {
3830 if (!fsconvert_strdup(PySequence_ITEM(argv, i),
3831 &argvlist[i]))
3832 {
3833 *argc = i;
3834 goto fail;
3835 }
3836 }
3837 argvlist[*argc] = NULL;
3838 return argvlist;
3839fail:
3840 free_string_array(argvlist, *argc);
3841 return NULL;
3842}
3843#endif
3844
3845#ifdef HAVE_EXECV
3846PyDoc_STRVAR(posix_execv__doc__,
3847"execv(path, args)\n\n\
3848Execute an executable path with arguments, replacing current process.\n\
3849\n\
3850 path: path of executable file\n\
3851 args: tuple or list of strings");
3852
3853static PyObject *
3854posix_execv(PyObject *self, PyObject *args)
3855{
3856 PyObject *opath;
3857 char *path;
3858 PyObject *argv;
3859 char **argvlist;
3860 Py_ssize_t argc;
3861
3862 /* execv has two arguments: (path, argv), where
3863 argv is a list or tuple of strings. */
3864
3865 if (!PyArg_ParseTuple(args, "O&O:execv",
3866 PyUnicode_FSConverter,
3867 &opath, &argv))
3868 return NULL;
3869 path = PyBytes_AsString(opath);
3870 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3871 PyErr_SetString(PyExc_TypeError,
3872 "execv() arg 2 must be a tuple or list");
3873 Py_DECREF(opath);
3874 return NULL;
3875 }
3876 argc = PySequence_Size(argv);
3877 if (argc < 1) {
3878 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3879 Py_DECREF(opath);
3880 return NULL;
3881 }
3882
3883 argvlist = parse_arglist(argv, &argc);
3884 if (argvlist == NULL) {
3885 Py_DECREF(opath);
3886 return NULL;
3887 }
3888
3889 execv(path, argvlist);
3890
3891 /* If we get here it's definitely an error */
3892
3893 free_string_array(argvlist, argc);
3894 Py_DECREF(opath);
3895 return posix_error();
3896}
3897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003898PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003899"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003900Execute a path with arguments and environment, replacing current process.\n\
3901\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003902 path: path of executable file\n\
3903 args: tuple or list of arguments\n\
3904 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003905
Barry Warsaw53699e91996-12-10 23:23:01 +00003906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003907posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003908{
Victor Stinner8c62be82010-05-06 00:08:46 +00003909 PyObject *opath;
3910 char *path;
3911 PyObject *argv, *env;
3912 char **argvlist;
3913 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003914 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003915
Victor Stinner8c62be82010-05-06 00:08:46 +00003916 /* execve has three arguments: (path, argv, env), where
3917 argv is a list or tuple of strings and env is a dictionary
3918 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003919
Victor Stinner8c62be82010-05-06 00:08:46 +00003920 if (!PyArg_ParseTuple(args, "O&OO:execve",
3921 PyUnicode_FSConverter,
3922 &opath, &argv, &env))
3923 return NULL;
3924 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003925 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003926 PyErr_SetString(PyExc_TypeError,
3927 "execve() arg 2 must be a tuple or list");
3928 goto fail_0;
3929 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003930 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 if (!PyMapping_Check(env)) {
3932 PyErr_SetString(PyExc_TypeError,
3933 "execve() arg 3 must be a mapping object");
3934 goto fail_0;
3935 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003936
Ross Lagerwall7807c352011-03-17 20:20:30 +02003937 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00003938 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003939 goto fail_0;
3940 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003941
Victor Stinner8c62be82010-05-06 00:08:46 +00003942 envlist = parse_envlist(env, &envc);
3943 if (envlist == NULL)
3944 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003945
Victor Stinner8c62be82010-05-06 00:08:46 +00003946 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003947
Victor Stinner8c62be82010-05-06 00:08:46 +00003948 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003949
Victor Stinner8c62be82010-05-06 00:08:46 +00003950 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003951
Victor Stinner8c62be82010-05-06 00:08:46 +00003952 while (--envc >= 0)
3953 PyMem_DEL(envlist[envc]);
3954 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003955 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02003956 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003957 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003958 Py_DECREF(opath);
3959 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003960}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003961#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003962
Ross Lagerwall7807c352011-03-17 20:20:30 +02003963#ifdef HAVE_FEXECVE
3964PyDoc_STRVAR(posix_fexecve__doc__,
3965"fexecve(fd, args, env)\n\n\
3966Execute the program specified by a file descriptor with arguments and\n\
3967environment, replacing the current process.\n\
3968\n\
3969 fd: file descriptor of executable\n\
3970 args: tuple or list of arguments\n\
3971 env: dictionary of strings mapping to strings");
3972
3973static PyObject *
3974posix_fexecve(PyObject *self, PyObject *args)
3975{
3976 int fd;
3977 PyObject *argv, *env;
3978 char **argvlist;
3979 char **envlist;
3980 Py_ssize_t argc, envc;
3981
3982 if (!PyArg_ParseTuple(args, "iOO:fexecve",
3983 &fd, &argv, &env))
3984 return NULL;
3985 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3986 PyErr_SetString(PyExc_TypeError,
3987 "fexecve() arg 2 must be a tuple or list");
3988 return NULL;
3989 }
3990 argc = PySequence_Size(argv);
3991 if (!PyMapping_Check(env)) {
3992 PyErr_SetString(PyExc_TypeError,
3993 "fexecve() arg 3 must be a mapping object");
3994 return NULL;
3995 }
3996
3997 argvlist = parse_arglist(argv, &argc);
3998 if (argvlist == NULL)
3999 return NULL;
4000
4001 envlist = parse_envlist(env, &envc);
4002 if (envlist == NULL)
4003 goto fail;
4004
4005 fexecve(fd, argvlist, envlist);
4006
4007 /* If we get here it's definitely an error */
4008
4009 (void) posix_error();
4010
4011 while (--envc >= 0)
4012 PyMem_DEL(envlist[envc]);
4013 PyMem_DEL(envlist);
4014 fail:
4015 free_string_array(argvlist, argc);
4016 return NULL;
4017}
4018#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004019
Guido van Rossuma1065681999-01-25 23:20:23 +00004020#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004021PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004022"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004023Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004024\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004025 mode: mode of process creation\n\
4026 path: path of executable file\n\
4027 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004028
4029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004030posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004031{
Victor Stinner8c62be82010-05-06 00:08:46 +00004032 PyObject *opath;
4033 char *path;
4034 PyObject *argv;
4035 char **argvlist;
4036 int mode, i;
4037 Py_ssize_t argc;
4038 Py_intptr_t spawnval;
4039 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004040
Victor Stinner8c62be82010-05-06 00:08:46 +00004041 /* spawnv has three arguments: (mode, path, argv), where
4042 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004043
Victor Stinner8c62be82010-05-06 00:08:46 +00004044 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4045 PyUnicode_FSConverter,
4046 &opath, &argv))
4047 return NULL;
4048 path = PyBytes_AsString(opath);
4049 if (PyList_Check(argv)) {
4050 argc = PyList_Size(argv);
4051 getitem = PyList_GetItem;
4052 }
4053 else if (PyTuple_Check(argv)) {
4054 argc = PyTuple_Size(argv);
4055 getitem = PyTuple_GetItem;
4056 }
4057 else {
4058 PyErr_SetString(PyExc_TypeError,
4059 "spawnv() arg 2 must be a tuple or list");
4060 Py_DECREF(opath);
4061 return NULL;
4062 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004063
Victor Stinner8c62be82010-05-06 00:08:46 +00004064 argvlist = PyMem_NEW(char *, argc+1);
4065 if (argvlist == NULL) {
4066 Py_DECREF(opath);
4067 return PyErr_NoMemory();
4068 }
4069 for (i = 0; i < argc; i++) {
4070 if (!fsconvert_strdup((*getitem)(argv, i),
4071 &argvlist[i])) {
4072 free_string_array(argvlist, i);
4073 PyErr_SetString(
4074 PyExc_TypeError,
4075 "spawnv() arg 2 must contain only strings");
4076 Py_DECREF(opath);
4077 return NULL;
4078 }
4079 }
4080 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004081
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004082#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004083 Py_BEGIN_ALLOW_THREADS
4084 spawnval = spawnv(mode, path, argvlist);
4085 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004086#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004087 if (mode == _OLD_P_OVERLAY)
4088 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004089
Victor Stinner8c62be82010-05-06 00:08:46 +00004090 Py_BEGIN_ALLOW_THREADS
4091 spawnval = _spawnv(mode, path, argvlist);
4092 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004093#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004094
Victor Stinner8c62be82010-05-06 00:08:46 +00004095 free_string_array(argvlist, argc);
4096 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004097
Victor Stinner8c62be82010-05-06 00:08:46 +00004098 if (spawnval == -1)
4099 return posix_error();
4100 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004101#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004102 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004103#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004104 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004105#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004106}
4107
4108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004109PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004110"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004111Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004112\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004113 mode: mode of process creation\n\
4114 path: path of executable file\n\
4115 args: tuple or list of arguments\n\
4116 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004117
4118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004119posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004120{
Victor Stinner8c62be82010-05-06 00:08:46 +00004121 PyObject *opath;
4122 char *path;
4123 PyObject *argv, *env;
4124 char **argvlist;
4125 char **envlist;
4126 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004127 int mode;
4128 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004129 Py_intptr_t spawnval;
4130 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4131 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004132
Victor Stinner8c62be82010-05-06 00:08:46 +00004133 /* spawnve has four arguments: (mode, path, argv, env), where
4134 argv is a list or tuple of strings and env is a dictionary
4135 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004136
Victor Stinner8c62be82010-05-06 00:08:46 +00004137 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4138 PyUnicode_FSConverter,
4139 &opath, &argv, &env))
4140 return NULL;
4141 path = PyBytes_AsString(opath);
4142 if (PyList_Check(argv)) {
4143 argc = PyList_Size(argv);
4144 getitem = PyList_GetItem;
4145 }
4146 else if (PyTuple_Check(argv)) {
4147 argc = PyTuple_Size(argv);
4148 getitem = PyTuple_GetItem;
4149 }
4150 else {
4151 PyErr_SetString(PyExc_TypeError,
4152 "spawnve() arg 2 must be a tuple or list");
4153 goto fail_0;
4154 }
4155 if (!PyMapping_Check(env)) {
4156 PyErr_SetString(PyExc_TypeError,
4157 "spawnve() arg 3 must be a mapping object");
4158 goto fail_0;
4159 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004160
Victor Stinner8c62be82010-05-06 00:08:46 +00004161 argvlist = PyMem_NEW(char *, argc+1);
4162 if (argvlist == NULL) {
4163 PyErr_NoMemory();
4164 goto fail_0;
4165 }
4166 for (i = 0; i < argc; i++) {
4167 if (!fsconvert_strdup((*getitem)(argv, i),
4168 &argvlist[i]))
4169 {
4170 lastarg = i;
4171 goto fail_1;
4172 }
4173 }
4174 lastarg = argc;
4175 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004176
Victor Stinner8c62be82010-05-06 00:08:46 +00004177 envlist = parse_envlist(env, &envc);
4178 if (envlist == NULL)
4179 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004180
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004181#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004182 Py_BEGIN_ALLOW_THREADS
4183 spawnval = spawnve(mode, path, argvlist, envlist);
4184 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004185#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004186 if (mode == _OLD_P_OVERLAY)
4187 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004188
Victor Stinner8c62be82010-05-06 00:08:46 +00004189 Py_BEGIN_ALLOW_THREADS
4190 spawnval = _spawnve(mode, path, argvlist, envlist);
4191 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004192#endif
Tim Peters25059d32001-12-07 20:35:43 +00004193
Victor Stinner8c62be82010-05-06 00:08:46 +00004194 if (spawnval == -1)
4195 (void) posix_error();
4196 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004197#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004198 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004199#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004200 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004201#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004202
Victor Stinner8c62be82010-05-06 00:08:46 +00004203 while (--envc >= 0)
4204 PyMem_DEL(envlist[envc]);
4205 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004206 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004207 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004208 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004209 Py_DECREF(opath);
4210 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004211}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004212
4213/* OS/2 supports spawnvp & spawnvpe natively */
4214#if defined(PYOS_OS2)
4215PyDoc_STRVAR(posix_spawnvp__doc__,
4216"spawnvp(mode, file, args)\n\n\
4217Execute the program 'file' in a new process, using the environment\n\
4218search path to find the file.\n\
4219\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004220 mode: mode of process creation\n\
4221 file: executable file name\n\
4222 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004223
4224static PyObject *
4225posix_spawnvp(PyObject *self, PyObject *args)
4226{
Victor Stinner8c62be82010-05-06 00:08:46 +00004227 PyObject *opath;
4228 char *path;
4229 PyObject *argv;
4230 char **argvlist;
4231 int mode, i, argc;
4232 Py_intptr_t spawnval;
4233 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004234
Victor Stinner8c62be82010-05-06 00:08:46 +00004235 /* spawnvp has three arguments: (mode, path, argv), where
4236 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004237
Victor Stinner8c62be82010-05-06 00:08:46 +00004238 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4239 PyUnicode_FSConverter,
4240 &opath, &argv))
4241 return NULL;
4242 path = PyBytes_AsString(opath);
4243 if (PyList_Check(argv)) {
4244 argc = PyList_Size(argv);
4245 getitem = PyList_GetItem;
4246 }
4247 else if (PyTuple_Check(argv)) {
4248 argc = PyTuple_Size(argv);
4249 getitem = PyTuple_GetItem;
4250 }
4251 else {
4252 PyErr_SetString(PyExc_TypeError,
4253 "spawnvp() arg 2 must be a tuple or list");
4254 Py_DECREF(opath);
4255 return NULL;
4256 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004257
Victor Stinner8c62be82010-05-06 00:08:46 +00004258 argvlist = PyMem_NEW(char *, argc+1);
4259 if (argvlist == NULL) {
4260 Py_DECREF(opath);
4261 return PyErr_NoMemory();
4262 }
4263 for (i = 0; i < argc; i++) {
4264 if (!fsconvert_strdup((*getitem)(argv, i),
4265 &argvlist[i])) {
4266 free_string_array(argvlist, i);
4267 PyErr_SetString(
4268 PyExc_TypeError,
4269 "spawnvp() arg 2 must contain only strings");
4270 Py_DECREF(opath);
4271 return NULL;
4272 }
4273 }
4274 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004275
Victor Stinner8c62be82010-05-06 00:08:46 +00004276 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004277#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004278 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004279#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004280 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004281#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004283
Victor Stinner8c62be82010-05-06 00:08:46 +00004284 free_string_array(argvlist, argc);
4285 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004286
Victor Stinner8c62be82010-05-06 00:08:46 +00004287 if (spawnval == -1)
4288 return posix_error();
4289 else
4290 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004291}
4292
4293
4294PyDoc_STRVAR(posix_spawnvpe__doc__,
4295"spawnvpe(mode, file, args, env)\n\n\
4296Execute the program 'file' in a new process, using the environment\n\
4297search path to find the file.\n\
4298\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004299 mode: mode of process creation\n\
4300 file: executable file name\n\
4301 args: tuple or list of arguments\n\
4302 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004303
4304static PyObject *
4305posix_spawnvpe(PyObject *self, PyObject *args)
4306{
Victor Stinner8c62be82010-05-06 00:08:46 +00004307 PyObject *opath
4308 char *path;
4309 PyObject *argv, *env;
4310 char **argvlist;
4311 char **envlist;
4312 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004313 int mode;
4314 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004315 Py_intptr_t spawnval;
4316 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4317 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004318
Victor Stinner8c62be82010-05-06 00:08:46 +00004319 /* spawnvpe has four arguments: (mode, path, argv, env), where
4320 argv is a list or tuple of strings and env is a dictionary
4321 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004322
Victor Stinner8c62be82010-05-06 00:08:46 +00004323 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4324 PyUnicode_FSConverter,
4325 &opath, &argv, &env))
4326 return NULL;
4327 path = PyBytes_AsString(opath);
4328 if (PyList_Check(argv)) {
4329 argc = PyList_Size(argv);
4330 getitem = PyList_GetItem;
4331 }
4332 else if (PyTuple_Check(argv)) {
4333 argc = PyTuple_Size(argv);
4334 getitem = PyTuple_GetItem;
4335 }
4336 else {
4337 PyErr_SetString(PyExc_TypeError,
4338 "spawnvpe() arg 2 must be a tuple or list");
4339 goto fail_0;
4340 }
4341 if (!PyMapping_Check(env)) {
4342 PyErr_SetString(PyExc_TypeError,
4343 "spawnvpe() arg 3 must be a mapping object");
4344 goto fail_0;
4345 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004346
Victor Stinner8c62be82010-05-06 00:08:46 +00004347 argvlist = PyMem_NEW(char *, argc+1);
4348 if (argvlist == NULL) {
4349 PyErr_NoMemory();
4350 goto fail_0;
4351 }
4352 for (i = 0; i < argc; i++) {
4353 if (!fsconvert_strdup((*getitem)(argv, i),
4354 &argvlist[i]))
4355 {
4356 lastarg = i;
4357 goto fail_1;
4358 }
4359 }
4360 lastarg = argc;
4361 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004362
Victor Stinner8c62be82010-05-06 00:08:46 +00004363 envlist = parse_envlist(env, &envc);
4364 if (envlist == NULL)
4365 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004366
Victor Stinner8c62be82010-05-06 00:08:46 +00004367 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004368#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004369 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004370#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004372#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004374
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 if (spawnval == -1)
4376 (void) posix_error();
4377 else
4378 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004379
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 while (--envc >= 0)
4381 PyMem_DEL(envlist[envc]);
4382 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004383 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004384 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004385 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004386 Py_DECREF(opath);
4387 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004388}
4389#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004390#endif /* HAVE_SPAWNV */
4391
4392
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004393#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004394PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004395"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004396Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4397\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004398Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004399
4400static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004401posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004402{
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 pid_t pid;
4404 int result = 0;
4405 _PyImport_AcquireLock();
4406 pid = fork1();
4407 if (pid == 0) {
4408 /* child: this clobbers and resets the import lock. */
4409 PyOS_AfterFork();
4410 } else {
4411 /* parent: release the import lock. */
4412 result = _PyImport_ReleaseLock();
4413 }
4414 if (pid == -1)
4415 return posix_error();
4416 if (result < 0) {
4417 /* Don't clobber the OSError if the fork failed. */
4418 PyErr_SetString(PyExc_RuntimeError,
4419 "not holding the import lock");
4420 return NULL;
4421 }
4422 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004423}
4424#endif
4425
4426
Guido van Rossumad0ee831995-03-01 10:34:45 +00004427#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004428PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004429"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004430Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004431Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004432
Barry Warsaw53699e91996-12-10 23:23:01 +00004433static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004434posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004435{
Victor Stinner8c62be82010-05-06 00:08:46 +00004436 pid_t pid;
4437 int result = 0;
4438 _PyImport_AcquireLock();
4439 pid = fork();
4440 if (pid == 0) {
4441 /* child: this clobbers and resets the import lock. */
4442 PyOS_AfterFork();
4443 } else {
4444 /* parent: release the import lock. */
4445 result = _PyImport_ReleaseLock();
4446 }
4447 if (pid == -1)
4448 return posix_error();
4449 if (result < 0) {
4450 /* Don't clobber the OSError if the fork failed. */
4451 PyErr_SetString(PyExc_RuntimeError,
4452 "not holding the import lock");
4453 return NULL;
4454 }
4455 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004456}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004457#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004458
Neal Norwitzb59798b2003-03-21 01:43:31 +00004459/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004460/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4461#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004462#define DEV_PTY_FILE "/dev/ptc"
4463#define HAVE_DEV_PTMX
4464#else
4465#define DEV_PTY_FILE "/dev/ptmx"
4466#endif
4467
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004468#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004469#ifdef HAVE_PTY_H
4470#include <pty.h>
4471#else
4472#ifdef HAVE_LIBUTIL_H
4473#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004474#else
4475#ifdef HAVE_UTIL_H
4476#include <util.h>
4477#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004478#endif /* HAVE_LIBUTIL_H */
4479#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004480#ifdef HAVE_STROPTS_H
4481#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004482#endif
4483#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004484
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004485#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004486PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004487"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004488Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004489
4490static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004491posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004492{
Victor Stinner8c62be82010-05-06 00:08:46 +00004493 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004494#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004495 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004496#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004497#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004498 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004499#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004500 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004501#endif
4502#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004503
Thomas Wouters70c21a12000-07-14 14:28:33 +00004504#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004505 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4506 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004507#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004508 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4509 if (slave_name == NULL)
4510 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004511
Victor Stinner8c62be82010-05-06 00:08:46 +00004512 slave_fd = open(slave_name, O_RDWR);
4513 if (slave_fd < 0)
4514 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004515#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004516 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4517 if (master_fd < 0)
4518 return posix_error();
4519 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4520 /* change permission of slave */
4521 if (grantpt(master_fd) < 0) {
4522 PyOS_setsig(SIGCHLD, sig_saved);
4523 return posix_error();
4524 }
4525 /* unlock slave */
4526 if (unlockpt(master_fd) < 0) {
4527 PyOS_setsig(SIGCHLD, sig_saved);
4528 return posix_error();
4529 }
4530 PyOS_setsig(SIGCHLD, sig_saved);
4531 slave_name = ptsname(master_fd); /* get name of slave */
4532 if (slave_name == NULL)
4533 return posix_error();
4534 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4535 if (slave_fd < 0)
4536 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004537#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004538 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4539 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004540#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004542#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004543#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004544#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004545
Victor Stinner8c62be82010-05-06 00:08:46 +00004546 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004547
Fred Drake8cef4cf2000-06-28 16:40:38 +00004548}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004549#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004550
4551#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004552PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004553"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004554Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4555Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004556To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004557
4558static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004559posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004560{
Victor Stinner8c62be82010-05-06 00:08:46 +00004561 int master_fd = -1, result = 0;
4562 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004563
Victor Stinner8c62be82010-05-06 00:08:46 +00004564 _PyImport_AcquireLock();
4565 pid = forkpty(&master_fd, NULL, NULL, NULL);
4566 if (pid == 0) {
4567 /* child: this clobbers and resets the import lock. */
4568 PyOS_AfterFork();
4569 } else {
4570 /* parent: release the import lock. */
4571 result = _PyImport_ReleaseLock();
4572 }
4573 if (pid == -1)
4574 return posix_error();
4575 if (result < 0) {
4576 /* Don't clobber the OSError if the fork failed. */
4577 PyErr_SetString(PyExc_RuntimeError,
4578 "not holding the import lock");
4579 return NULL;
4580 }
4581 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004582}
4583#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004584
Ross Lagerwall7807c352011-03-17 20:20:30 +02004585
Guido van Rossumad0ee831995-03-01 10:34:45 +00004586#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004587PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004588"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004589Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004590
Barry Warsaw53699e91996-12-10 23:23:01 +00004591static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004592posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004593{
Victor Stinner8c62be82010-05-06 00:08:46 +00004594 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004595}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004596#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004597
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004598
Guido van Rossumad0ee831995-03-01 10:34:45 +00004599#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004600PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004601"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004602Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004603
Barry Warsaw53699e91996-12-10 23:23:01 +00004604static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004605posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004606{
Victor Stinner8c62be82010-05-06 00:08:46 +00004607 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004608}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004609#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004611
Guido van Rossumad0ee831995-03-01 10:34:45 +00004612#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004613PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004614"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004615Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004616
Barry Warsaw53699e91996-12-10 23:23:01 +00004617static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004618posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004619{
Victor Stinner8c62be82010-05-06 00:08:46 +00004620 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004621}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004622#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004625PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004626"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004628
Barry Warsaw53699e91996-12-10 23:23:01 +00004629static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004630posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004631{
Victor Stinner8c62be82010-05-06 00:08:46 +00004632 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004633}
4634
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004635
Fred Drakec9680921999-12-13 16:37:25 +00004636#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004637PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004638"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004639Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004640
4641static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004642posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004643{
4644 PyObject *result = NULL;
4645
Fred Drakec9680921999-12-13 16:37:25 +00004646#ifdef NGROUPS_MAX
4647#define MAX_GROUPS NGROUPS_MAX
4648#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004649 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004650#define MAX_GROUPS 64
4651#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004652 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004653
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004654 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004655 * This is a helper variable to store the intermediate result when
4656 * that happens.
4657 *
4658 * To keep the code readable the OSX behaviour is unconditional,
4659 * according to the POSIX spec this should be safe on all unix-y
4660 * systems.
4661 */
4662 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004663 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004664
Victor Stinner8c62be82010-05-06 00:08:46 +00004665 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004666 if (n < 0) {
4667 if (errno == EINVAL) {
4668 n = getgroups(0, NULL);
4669 if (n == -1) {
4670 return posix_error();
4671 }
4672 if (n == 0) {
4673 /* Avoid malloc(0) */
4674 alt_grouplist = grouplist;
4675 } else {
4676 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4677 if (alt_grouplist == NULL) {
4678 errno = EINVAL;
4679 return posix_error();
4680 }
4681 n = getgroups(n, alt_grouplist);
4682 if (n == -1) {
4683 PyMem_Free(alt_grouplist);
4684 return posix_error();
4685 }
4686 }
4687 } else {
4688 return posix_error();
4689 }
4690 }
4691 result = PyList_New(n);
4692 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004693 int i;
4694 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004695 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004696 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004697 Py_DECREF(result);
4698 result = NULL;
4699 break;
Fred Drakec9680921999-12-13 16:37:25 +00004700 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004701 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004702 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004703 }
4704
4705 if (alt_grouplist != grouplist) {
4706 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004707 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004708
Fred Drakec9680921999-12-13 16:37:25 +00004709 return result;
4710}
4711#endif
4712
Antoine Pitroub7572f02009-12-02 20:46:48 +00004713#ifdef HAVE_INITGROUPS
4714PyDoc_STRVAR(posix_initgroups__doc__,
4715"initgroups(username, gid) -> None\n\n\
4716Call the system initgroups() to initialize the group access list with all of\n\
4717the groups of which the specified username is a member, plus the specified\n\
4718group id.");
4719
4720static PyObject *
4721posix_initgroups(PyObject *self, PyObject *args)
4722{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004723 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004724 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004725 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004726 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004727
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004728 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4729 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004730 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004731 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004732
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004733 res = initgroups(username, (gid_t) gid);
4734 Py_DECREF(oname);
4735 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004736 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004737
Victor Stinner8c62be82010-05-06 00:08:46 +00004738 Py_INCREF(Py_None);
4739 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004740}
4741#endif
4742
Martin v. Löwis606edc12002-06-13 21:09:11 +00004743#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004744PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004745"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004746Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004747
4748static PyObject *
4749posix_getpgid(PyObject *self, PyObject *args)
4750{
Victor Stinner8c62be82010-05-06 00:08:46 +00004751 pid_t pid, pgid;
4752 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4753 return NULL;
4754 pgid = getpgid(pid);
4755 if (pgid < 0)
4756 return posix_error();
4757 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004758}
4759#endif /* HAVE_GETPGID */
4760
4761
Guido van Rossumb6775db1994-08-01 11:34:53 +00004762#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004763PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004764"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004765Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004766
Barry Warsaw53699e91996-12-10 23:23:01 +00004767static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004768posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004769{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004770#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004771 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004772#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004773 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004774#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004775}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004776#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004777
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004778
Guido van Rossumb6775db1994-08-01 11:34:53 +00004779#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004780PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004781"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004782Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004783
Barry Warsaw53699e91996-12-10 23:23:01 +00004784static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004785posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004786{
Guido van Rossum64933891994-10-20 21:56:42 +00004787#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004788 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004789#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004790 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004791#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004792 return posix_error();
4793 Py_INCREF(Py_None);
4794 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004795}
4796
Guido van Rossumb6775db1994-08-01 11:34:53 +00004797#endif /* HAVE_SETPGRP */
4798
Guido van Rossumad0ee831995-03-01 10:34:45 +00004799#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004800
4801#ifdef MS_WINDOWS
4802#include <tlhelp32.h>
4803
4804static PyObject*
4805win32_getppid()
4806{
4807 HANDLE snapshot;
4808 pid_t mypid;
4809 PyObject* result = NULL;
4810 BOOL have_record;
4811 PROCESSENTRY32 pe;
4812
4813 mypid = getpid(); /* This function never fails */
4814
4815 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4816 if (snapshot == INVALID_HANDLE_VALUE)
4817 return PyErr_SetFromWindowsErr(GetLastError());
4818
4819 pe.dwSize = sizeof(pe);
4820 have_record = Process32First(snapshot, &pe);
4821 while (have_record) {
4822 if (mypid == (pid_t)pe.th32ProcessID) {
4823 /* We could cache the ulong value in a static variable. */
4824 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4825 break;
4826 }
4827
4828 have_record = Process32Next(snapshot, &pe);
4829 }
4830
4831 /* If our loop exits and our pid was not found (result will be NULL)
4832 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4833 * error anyway, so let's raise it. */
4834 if (!result)
4835 result = PyErr_SetFromWindowsErr(GetLastError());
4836
4837 CloseHandle(snapshot);
4838
4839 return result;
4840}
4841#endif /*MS_WINDOWS*/
4842
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004843PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004844"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004845Return the parent's process id. If the parent process has already exited,\n\
4846Windows machines will still return its id; others systems will return the id\n\
4847of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004848
Barry Warsaw53699e91996-12-10 23:23:01 +00004849static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004850posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004851{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004852#ifdef MS_WINDOWS
4853 return win32_getppid();
4854#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004855 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004856#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004857}
4858#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004859
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004860
Fred Drake12c6e2d1999-12-14 21:25:03 +00004861#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004862PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004863"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004864Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004865
4866static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004867posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004868{
Victor Stinner8c62be82010-05-06 00:08:46 +00004869 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004870#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004871 wchar_t user_name[UNLEN + 1];
4872 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4873
4874 if (GetUserNameW(user_name, &num_chars)) {
4875 /* num_chars is the number of unicode chars plus null terminator */
4876 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004877 }
4878 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004879 result = PyErr_SetFromWindowsErr(GetLastError());
4880#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004881 char *name;
4882 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004883
Victor Stinner8c62be82010-05-06 00:08:46 +00004884 errno = 0;
4885 name = getlogin();
4886 if (name == NULL) {
4887 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004888 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004889 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004890 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004891 }
4892 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004893 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004894 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004895#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004896 return result;
4897}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004898#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004899
Guido van Rossumad0ee831995-03-01 10:34:45 +00004900#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004901PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004902"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004903Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004904
Barry Warsaw53699e91996-12-10 23:23:01 +00004905static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004906posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004907{
Victor Stinner8c62be82010-05-06 00:08:46 +00004908 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004909}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004910#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004911
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004912
Guido van Rossumad0ee831995-03-01 10:34:45 +00004913#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004914PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004915"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004916Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004917
Barry Warsaw53699e91996-12-10 23:23:01 +00004918static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004919posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004920{
Victor Stinner8c62be82010-05-06 00:08:46 +00004921 pid_t pid;
4922 int sig;
4923 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4924 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004925#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004926 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4927 APIRET rc;
4928 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004929 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004930
4931 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4932 APIRET rc;
4933 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004934 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004935
4936 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004937 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004938#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004939 if (kill(pid, sig) == -1)
4940 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004941#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 Py_INCREF(Py_None);
4943 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004944}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004945#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004946
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004947#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004948PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004949"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004950Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004951
4952static PyObject *
4953posix_killpg(PyObject *self, PyObject *args)
4954{
Victor Stinner8c62be82010-05-06 00:08:46 +00004955 int sig;
4956 pid_t pgid;
4957 /* XXX some man pages make the `pgid` parameter an int, others
4958 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4959 take the same type. Moreover, pid_t is always at least as wide as
4960 int (else compilation of this module fails), which is safe. */
4961 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4962 return NULL;
4963 if (killpg(pgid, sig) == -1)
4964 return posix_error();
4965 Py_INCREF(Py_None);
4966 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004967}
4968#endif
4969
Brian Curtineb24d742010-04-12 17:16:38 +00004970#ifdef MS_WINDOWS
4971PyDoc_STRVAR(win32_kill__doc__,
4972"kill(pid, sig)\n\n\
4973Kill a process with a signal.");
4974
4975static PyObject *
4976win32_kill(PyObject *self, PyObject *args)
4977{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004978 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004979 DWORD pid, sig, err;
4980 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004981
Victor Stinner8c62be82010-05-06 00:08:46 +00004982 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4983 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004984
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 /* Console processes which share a common console can be sent CTRL+C or
4986 CTRL+BREAK events, provided they handle said events. */
4987 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4988 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4989 err = GetLastError();
4990 PyErr_SetFromWindowsErr(err);
4991 }
4992 else
4993 Py_RETURN_NONE;
4994 }
Brian Curtineb24d742010-04-12 17:16:38 +00004995
Victor Stinner8c62be82010-05-06 00:08:46 +00004996 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4997 attempt to open and terminate the process. */
4998 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4999 if (handle == NULL) {
5000 err = GetLastError();
5001 return PyErr_SetFromWindowsErr(err);
5002 }
Brian Curtineb24d742010-04-12 17:16:38 +00005003
Victor Stinner8c62be82010-05-06 00:08:46 +00005004 if (TerminateProcess(handle, sig) == 0) {
5005 err = GetLastError();
5006 result = PyErr_SetFromWindowsErr(err);
5007 } else {
5008 Py_INCREF(Py_None);
5009 result = Py_None;
5010 }
Brian Curtineb24d742010-04-12 17:16:38 +00005011
Victor Stinner8c62be82010-05-06 00:08:46 +00005012 CloseHandle(handle);
5013 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005014}
5015#endif /* MS_WINDOWS */
5016
Guido van Rossumc0125471996-06-28 18:55:32 +00005017#ifdef HAVE_PLOCK
5018
5019#ifdef HAVE_SYS_LOCK_H
5020#include <sys/lock.h>
5021#endif
5022
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005023PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005024"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005026
Barry Warsaw53699e91996-12-10 23:23:01 +00005027static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005028posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005029{
Victor Stinner8c62be82010-05-06 00:08:46 +00005030 int op;
5031 if (!PyArg_ParseTuple(args, "i:plock", &op))
5032 return NULL;
5033 if (plock(op) == -1)
5034 return posix_error();
5035 Py_INCREF(Py_None);
5036 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005037}
5038#endif
5039
Guido van Rossumb6775db1994-08-01 11:34:53 +00005040#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005041PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005042"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005043Set the current process's user id.");
5044
Barry Warsaw53699e91996-12-10 23:23:01 +00005045static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005046posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005047{
Victor Stinner8c62be82010-05-06 00:08:46 +00005048 long uid_arg;
5049 uid_t uid;
5050 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5051 return NULL;
5052 uid = uid_arg;
5053 if (uid != uid_arg) {
5054 PyErr_SetString(PyExc_OverflowError, "user id too big");
5055 return NULL;
5056 }
5057 if (setuid(uid) < 0)
5058 return posix_error();
5059 Py_INCREF(Py_None);
5060 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005061}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005062#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005063
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005064
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005065#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005066PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005067"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005068Set the current process's effective user id.");
5069
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005070static PyObject *
5071posix_seteuid (PyObject *self, PyObject *args)
5072{
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 long euid_arg;
5074 uid_t euid;
5075 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5076 return NULL;
5077 euid = euid_arg;
5078 if (euid != euid_arg) {
5079 PyErr_SetString(PyExc_OverflowError, "user id too big");
5080 return NULL;
5081 }
5082 if (seteuid(euid) < 0) {
5083 return posix_error();
5084 } else {
5085 Py_INCREF(Py_None);
5086 return Py_None;
5087 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005088}
5089#endif /* HAVE_SETEUID */
5090
5091#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005092PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005093"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005094Set the current process's effective group id.");
5095
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005096static PyObject *
5097posix_setegid (PyObject *self, PyObject *args)
5098{
Victor Stinner8c62be82010-05-06 00:08:46 +00005099 long egid_arg;
5100 gid_t egid;
5101 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5102 return NULL;
5103 egid = egid_arg;
5104 if (egid != egid_arg) {
5105 PyErr_SetString(PyExc_OverflowError, "group id too big");
5106 return NULL;
5107 }
5108 if (setegid(egid) < 0) {
5109 return posix_error();
5110 } else {
5111 Py_INCREF(Py_None);
5112 return Py_None;
5113 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005114}
5115#endif /* HAVE_SETEGID */
5116
5117#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005118PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005119"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005120Set the current process's real and effective user ids.");
5121
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005122static PyObject *
5123posix_setreuid (PyObject *self, PyObject *args)
5124{
Victor Stinner8c62be82010-05-06 00:08:46 +00005125 long ruid_arg, euid_arg;
5126 uid_t ruid, euid;
5127 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5128 return NULL;
5129 if (ruid_arg == -1)
5130 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5131 else
5132 ruid = ruid_arg; /* otherwise, assign from our long */
5133 if (euid_arg == -1)
5134 euid = (uid_t)-1;
5135 else
5136 euid = euid_arg;
5137 if ((euid_arg != -1 && euid != euid_arg) ||
5138 (ruid_arg != -1 && ruid != ruid_arg)) {
5139 PyErr_SetString(PyExc_OverflowError, "user id too big");
5140 return NULL;
5141 }
5142 if (setreuid(ruid, euid) < 0) {
5143 return posix_error();
5144 } else {
5145 Py_INCREF(Py_None);
5146 return Py_None;
5147 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005148}
5149#endif /* HAVE_SETREUID */
5150
5151#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005152PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005153"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005154Set the current process's real and effective group ids.");
5155
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005156static PyObject *
5157posix_setregid (PyObject *self, PyObject *args)
5158{
Victor Stinner8c62be82010-05-06 00:08:46 +00005159 long rgid_arg, egid_arg;
5160 gid_t rgid, egid;
5161 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5162 return NULL;
5163 if (rgid_arg == -1)
5164 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5165 else
5166 rgid = rgid_arg; /* otherwise, assign from our long */
5167 if (egid_arg == -1)
5168 egid = (gid_t)-1;
5169 else
5170 egid = egid_arg;
5171 if ((egid_arg != -1 && egid != egid_arg) ||
5172 (rgid_arg != -1 && rgid != rgid_arg)) {
5173 PyErr_SetString(PyExc_OverflowError, "group id too big");
5174 return NULL;
5175 }
5176 if (setregid(rgid, egid) < 0) {
5177 return posix_error();
5178 } else {
5179 Py_INCREF(Py_None);
5180 return Py_None;
5181 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005182}
5183#endif /* HAVE_SETREGID */
5184
Guido van Rossumb6775db1994-08-01 11:34:53 +00005185#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005186PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005187"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005188Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005189
Barry Warsaw53699e91996-12-10 23:23:01 +00005190static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005191posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005192{
Victor Stinner8c62be82010-05-06 00:08:46 +00005193 long gid_arg;
5194 gid_t gid;
5195 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5196 return NULL;
5197 gid = gid_arg;
5198 if (gid != gid_arg) {
5199 PyErr_SetString(PyExc_OverflowError, "group id too big");
5200 return NULL;
5201 }
5202 if (setgid(gid) < 0)
5203 return posix_error();
5204 Py_INCREF(Py_None);
5205 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005206}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005207#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005208
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005209#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005210PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005211"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005212Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005213
5214static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005215posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005216{
Victor Stinner8c62be82010-05-06 00:08:46 +00005217 int i, len;
5218 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005219
Victor Stinner8c62be82010-05-06 00:08:46 +00005220 if (!PySequence_Check(groups)) {
5221 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5222 return NULL;
5223 }
5224 len = PySequence_Size(groups);
5225 if (len > MAX_GROUPS) {
5226 PyErr_SetString(PyExc_ValueError, "too many groups");
5227 return NULL;
5228 }
5229 for(i = 0; i < len; i++) {
5230 PyObject *elem;
5231 elem = PySequence_GetItem(groups, i);
5232 if (!elem)
5233 return NULL;
5234 if (!PyLong_Check(elem)) {
5235 PyErr_SetString(PyExc_TypeError,
5236 "groups must be integers");
5237 Py_DECREF(elem);
5238 return NULL;
5239 } else {
5240 unsigned long x = PyLong_AsUnsignedLong(elem);
5241 if (PyErr_Occurred()) {
5242 PyErr_SetString(PyExc_TypeError,
5243 "group id too big");
5244 Py_DECREF(elem);
5245 return NULL;
5246 }
5247 grouplist[i] = x;
5248 /* read back the value to see if it fitted in gid_t */
5249 if (grouplist[i] != x) {
5250 PyErr_SetString(PyExc_TypeError,
5251 "group id too big");
5252 Py_DECREF(elem);
5253 return NULL;
5254 }
5255 }
5256 Py_DECREF(elem);
5257 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005258
Victor Stinner8c62be82010-05-06 00:08:46 +00005259 if (setgroups(len, grouplist) < 0)
5260 return posix_error();
5261 Py_INCREF(Py_None);
5262 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005263}
5264#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005265
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005266#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5267static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005268wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005269{
Victor Stinner8c62be82010-05-06 00:08:46 +00005270 PyObject *result;
5271 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005272
Victor Stinner8c62be82010-05-06 00:08:46 +00005273 if (pid == -1)
5274 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005275
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 if (struct_rusage == NULL) {
5277 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5278 if (m == NULL)
5279 return NULL;
5280 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5281 Py_DECREF(m);
5282 if (struct_rusage == NULL)
5283 return NULL;
5284 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005285
Victor Stinner8c62be82010-05-06 00:08:46 +00005286 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5287 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5288 if (!result)
5289 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005290
5291#ifndef doubletime
5292#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5293#endif
5294
Victor Stinner8c62be82010-05-06 00:08:46 +00005295 PyStructSequence_SET_ITEM(result, 0,
5296 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5297 PyStructSequence_SET_ITEM(result, 1,
5298 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005299#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005300 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5301 SET_INT(result, 2, ru->ru_maxrss);
5302 SET_INT(result, 3, ru->ru_ixrss);
5303 SET_INT(result, 4, ru->ru_idrss);
5304 SET_INT(result, 5, ru->ru_isrss);
5305 SET_INT(result, 6, ru->ru_minflt);
5306 SET_INT(result, 7, ru->ru_majflt);
5307 SET_INT(result, 8, ru->ru_nswap);
5308 SET_INT(result, 9, ru->ru_inblock);
5309 SET_INT(result, 10, ru->ru_oublock);
5310 SET_INT(result, 11, ru->ru_msgsnd);
5311 SET_INT(result, 12, ru->ru_msgrcv);
5312 SET_INT(result, 13, ru->ru_nsignals);
5313 SET_INT(result, 14, ru->ru_nvcsw);
5314 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005315#undef SET_INT
5316
Victor Stinner8c62be82010-05-06 00:08:46 +00005317 if (PyErr_Occurred()) {
5318 Py_DECREF(result);
5319 return NULL;
5320 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005321
Victor Stinner8c62be82010-05-06 00:08:46 +00005322 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005323}
5324#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5325
5326#ifdef HAVE_WAIT3
5327PyDoc_STRVAR(posix_wait3__doc__,
5328"wait3(options) -> (pid, status, rusage)\n\n\
5329Wait for completion of a child process.");
5330
5331static PyObject *
5332posix_wait3(PyObject *self, PyObject *args)
5333{
Victor Stinner8c62be82010-05-06 00:08:46 +00005334 pid_t pid;
5335 int options;
5336 struct rusage ru;
5337 WAIT_TYPE status;
5338 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005339
Victor Stinner8c62be82010-05-06 00:08:46 +00005340 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5341 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005342
Victor Stinner8c62be82010-05-06 00:08:46 +00005343 Py_BEGIN_ALLOW_THREADS
5344 pid = wait3(&status, options, &ru);
5345 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005346
Victor Stinner8c62be82010-05-06 00:08:46 +00005347 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005348}
5349#endif /* HAVE_WAIT3 */
5350
5351#ifdef HAVE_WAIT4
5352PyDoc_STRVAR(posix_wait4__doc__,
5353"wait4(pid, options) -> (pid, status, rusage)\n\n\
5354Wait for completion of a given child process.");
5355
5356static PyObject *
5357posix_wait4(PyObject *self, PyObject *args)
5358{
Victor Stinner8c62be82010-05-06 00:08:46 +00005359 pid_t pid;
5360 int options;
5361 struct rusage ru;
5362 WAIT_TYPE status;
5363 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005364
Victor Stinner8c62be82010-05-06 00:08:46 +00005365 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5366 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005367
Victor Stinner8c62be82010-05-06 00:08:46 +00005368 Py_BEGIN_ALLOW_THREADS
5369 pid = wait4(pid, &status, options, &ru);
5370 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005371
Victor Stinner8c62be82010-05-06 00:08:46 +00005372 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005373}
5374#endif /* HAVE_WAIT4 */
5375
Ross Lagerwall7807c352011-03-17 20:20:30 +02005376#if defined(HAVE_WAITID) && !defined(__APPLE__)
5377PyDoc_STRVAR(posix_waitid__doc__,
5378"waitid(idtype, id, options) -> waitid_result\n\n\
5379Wait for the completion of one or more child processes.\n\n\
5380idtype can be P_PID, P_PGID or P_ALL.\n\
5381id specifies the pid to wait on.\n\
5382options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
5383or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
5384Returns either waitid_result or None if WNOHANG is specified and there are\n\
5385no children in a waitable state.");
5386
5387static PyObject *
5388posix_waitid(PyObject *self, PyObject *args)
5389{
5390 PyObject *result;
5391 idtype_t idtype;
5392 id_t id;
5393 int options, res;
5394 siginfo_t si;
5395 si.si_pid = 0;
5396 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
5397 return NULL;
5398 Py_BEGIN_ALLOW_THREADS
5399 res = waitid(idtype, id, &si, options);
5400 Py_END_ALLOW_THREADS
5401 if (res == -1)
5402 return posix_error();
5403
5404 if (si.si_pid == 0)
5405 Py_RETURN_NONE;
5406
5407 result = PyStructSequence_New(&WaitidResultType);
5408 if (!result)
5409 return NULL;
5410
5411 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
5412 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
5413 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
5414 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
5415 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
5416 if (PyErr_Occurred()) {
5417 Py_DECREF(result);
5418 return NULL;
5419 }
5420
5421 return result;
5422}
5423#endif
5424
Guido van Rossumb6775db1994-08-01 11:34:53 +00005425#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005426PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005427"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005428Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005429
Barry Warsaw53699e91996-12-10 23:23:01 +00005430static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005431posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005432{
Victor Stinner8c62be82010-05-06 00:08:46 +00005433 pid_t pid;
5434 int options;
5435 WAIT_TYPE status;
5436 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005437
Victor Stinner8c62be82010-05-06 00:08:46 +00005438 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5439 return NULL;
5440 Py_BEGIN_ALLOW_THREADS
5441 pid = waitpid(pid, &status, options);
5442 Py_END_ALLOW_THREADS
5443 if (pid == -1)
5444 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005445
Victor Stinner8c62be82010-05-06 00:08:46 +00005446 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005447}
5448
Tim Petersab034fa2002-02-01 11:27:43 +00005449#elif defined(HAVE_CWAIT)
5450
5451/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005452PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005453"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005454"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005455
5456static PyObject *
5457posix_waitpid(PyObject *self, PyObject *args)
5458{
Victor Stinner8c62be82010-05-06 00:08:46 +00005459 Py_intptr_t pid;
5460 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005461
Victor Stinner8c62be82010-05-06 00:08:46 +00005462 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5463 return NULL;
5464 Py_BEGIN_ALLOW_THREADS
5465 pid = _cwait(&status, pid, options);
5466 Py_END_ALLOW_THREADS
5467 if (pid == -1)
5468 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005469
Victor Stinner8c62be82010-05-06 00:08:46 +00005470 /* shift the status left a byte so this is more like the POSIX waitpid */
5471 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005472}
5473#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005474
Guido van Rossumad0ee831995-03-01 10:34:45 +00005475#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005476PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005477"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005478Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005479
Barry Warsaw53699e91996-12-10 23:23:01 +00005480static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005481posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005482{
Victor Stinner8c62be82010-05-06 00:08:46 +00005483 pid_t pid;
5484 WAIT_TYPE status;
5485 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005486
Victor Stinner8c62be82010-05-06 00:08:46 +00005487 Py_BEGIN_ALLOW_THREADS
5488 pid = wait(&status);
5489 Py_END_ALLOW_THREADS
5490 if (pid == -1)
5491 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005492
Victor Stinner8c62be82010-05-06 00:08:46 +00005493 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005494}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005495#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005496
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005498PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005499"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005501
Barry Warsaw53699e91996-12-10 23:23:01 +00005502static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005503posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005504{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005505#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005506 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005507#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005508#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005509 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5510 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005511#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005512 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005513#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005514#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005515}
5516
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005517
Guido van Rossumb6775db1994-08-01 11:34:53 +00005518#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005519PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005520"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005521Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005522
Barry Warsaw53699e91996-12-10 23:23:01 +00005523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005524posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005525{
Victor Stinner8c62be82010-05-06 00:08:46 +00005526 PyObject* v;
5527 char buf[MAXPATHLEN];
5528 PyObject *opath;
5529 char *path;
5530 int n;
5531 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005532
Victor Stinner8c62be82010-05-06 00:08:46 +00005533 if (!PyArg_ParseTuple(args, "O&:readlink",
5534 PyUnicode_FSConverter, &opath))
5535 return NULL;
5536 path = PyBytes_AsString(opath);
5537 v = PySequence_GetItem(args, 0);
5538 if (v == NULL) {
5539 Py_DECREF(opath);
5540 return NULL;
5541 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005542
Victor Stinner8c62be82010-05-06 00:08:46 +00005543 if (PyUnicode_Check(v)) {
5544 arg_is_unicode = 1;
5545 }
5546 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005547
Victor Stinner8c62be82010-05-06 00:08:46 +00005548 Py_BEGIN_ALLOW_THREADS
5549 n = readlink(path, buf, (int) sizeof buf);
5550 Py_END_ALLOW_THREADS
5551 if (n < 0)
5552 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005553
Victor Stinner8c62be82010-05-06 00:08:46 +00005554 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005555 if (arg_is_unicode)
5556 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5557 else
5558 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005559}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005560#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005561
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005562
Brian Curtin52173d42010-12-02 18:29:18 +00005563#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005564PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005565"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005566Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005567
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005569posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005570{
Victor Stinner8c62be82010-05-06 00:08:46 +00005571 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005572}
5573#endif /* HAVE_SYMLINK */
5574
Brian Curtind40e6f72010-07-08 21:39:08 +00005575#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5576
5577PyDoc_STRVAR(win_readlink__doc__,
5578"readlink(path) -> path\n\n\
5579Return a string representing the path to which the symbolic link points.");
5580
Brian Curtind40e6f72010-07-08 21:39:08 +00005581/* Windows readlink implementation */
5582static PyObject *
5583win_readlink(PyObject *self, PyObject *args)
5584{
5585 wchar_t *path;
5586 DWORD n_bytes_returned;
5587 DWORD io_result;
5588 PyObject *result;
5589 HANDLE reparse_point_handle;
5590
5591 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5592 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5593 wchar_t *print_name;
5594
5595 if (!PyArg_ParseTuple(args,
5596 "u:readlink",
5597 &path))
5598 return NULL;
5599
5600 /* First get a handle to the reparse point */
5601 Py_BEGIN_ALLOW_THREADS
5602 reparse_point_handle = CreateFileW(
5603 path,
5604 0,
5605 0,
5606 0,
5607 OPEN_EXISTING,
5608 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5609 0);
5610 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005611
Brian Curtind40e6f72010-07-08 21:39:08 +00005612 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5613 {
5614 return win32_error_unicode("readlink", path);
5615 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005616
Brian Curtind40e6f72010-07-08 21:39:08 +00005617 Py_BEGIN_ALLOW_THREADS
5618 /* New call DeviceIoControl to read the reparse point */
5619 io_result = DeviceIoControl(
5620 reparse_point_handle,
5621 FSCTL_GET_REPARSE_POINT,
5622 0, 0, /* in buffer */
5623 target_buffer, sizeof(target_buffer),
5624 &n_bytes_returned,
5625 0 /* we're not using OVERLAPPED_IO */
5626 );
5627 CloseHandle(reparse_point_handle);
5628 Py_END_ALLOW_THREADS
5629
5630 if (io_result==0)
5631 {
5632 return win32_error_unicode("readlink", path);
5633 }
5634
5635 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5636 {
5637 PyErr_SetString(PyExc_ValueError,
5638 "not a symbolic link");
5639 return NULL;
5640 }
Brian Curtin74e45612010-07-09 15:58:59 +00005641 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5642 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5643
5644 result = PyUnicode_FromWideChar(print_name,
5645 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005646 return result;
5647}
5648
5649#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5650
Brian Curtin52173d42010-12-02 18:29:18 +00005651#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005652
5653/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5654static int has_CreateSymbolicLinkW = 0;
5655static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5656static int
5657check_CreateSymbolicLinkW()
5658{
5659 HINSTANCE hKernel32;
5660 /* only recheck */
5661 if (has_CreateSymbolicLinkW)
5662 return has_CreateSymbolicLinkW;
5663 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005664 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5665 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005666 if (Py_CreateSymbolicLinkW)
5667 has_CreateSymbolicLinkW = 1;
5668 return has_CreateSymbolicLinkW;
5669}
5670
5671PyDoc_STRVAR(win_symlink__doc__,
5672"symlink(src, dst, target_is_directory=False)\n\n\
5673Create a symbolic link pointing to src named dst.\n\
5674target_is_directory is required if the target is to be interpreted as\n\
5675a directory.\n\
5676This function requires Windows 6.0 or greater, and raises a\n\
5677NotImplementedError otherwise.");
5678
5679static PyObject *
5680win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5681{
5682 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5683 PyObject *src, *dest;
5684 int target_is_directory = 0;
5685 DWORD res;
5686 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005687
Brian Curtind40e6f72010-07-08 21:39:08 +00005688 if (!check_CreateSymbolicLinkW())
5689 {
5690 /* raise NotImplementedError */
5691 return PyErr_Format(PyExc_NotImplementedError,
5692 "CreateSymbolicLinkW not found");
5693 }
5694 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5695 kwlist, &src, &dest, &target_is_directory))
5696 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005697
5698 if (win32_can_symlink == 0)
5699 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5700
Brian Curtind40e6f72010-07-08 21:39:08 +00005701 if (!convert_to_unicode(&src)) { return NULL; }
5702 if (!convert_to_unicode(&dest)) {
5703 Py_DECREF(src);
5704 return NULL;
5705 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005706
Brian Curtind40e6f72010-07-08 21:39:08 +00005707 /* if src is a directory, ensure target_is_directory==1 */
5708 if(
5709 GetFileAttributesExW(
5710 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5711 ))
5712 {
5713 target_is_directory = target_is_directory ||
5714 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5715 }
5716
5717 Py_BEGIN_ALLOW_THREADS
5718 res = Py_CreateSymbolicLinkW(
5719 PyUnicode_AsUnicode(dest),
5720 PyUnicode_AsUnicode(src),
5721 target_is_directory);
5722 Py_END_ALLOW_THREADS
5723 Py_DECREF(src);
5724 Py_DECREF(dest);
5725 if (!res)
5726 {
5727 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5728 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005729
Brian Curtind40e6f72010-07-08 21:39:08 +00005730 Py_INCREF(Py_None);
5731 return Py_None;
5732}
Brian Curtin52173d42010-12-02 18:29:18 +00005733#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005734
5735#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005736#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5737static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005738system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005739{
5740 ULONG value = 0;
5741
5742 Py_BEGIN_ALLOW_THREADS
5743 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5744 Py_END_ALLOW_THREADS
5745
5746 return value;
5747}
5748
5749static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005750posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005751{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005752 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005753 return Py_BuildValue("ddddd",
5754 (double)0 /* t.tms_utime / HZ */,
5755 (double)0 /* t.tms_stime / HZ */,
5756 (double)0 /* t.tms_cutime / HZ */,
5757 (double)0 /* t.tms_cstime / HZ */,
5758 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005759}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005760#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005761#define NEED_TICKS_PER_SECOND
5762static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005763static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005764posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005765{
Victor Stinner8c62be82010-05-06 00:08:46 +00005766 struct tms t;
5767 clock_t c;
5768 errno = 0;
5769 c = times(&t);
5770 if (c == (clock_t) -1)
5771 return posix_error();
5772 return Py_BuildValue("ddddd",
5773 (double)t.tms_utime / ticks_per_second,
5774 (double)t.tms_stime / ticks_per_second,
5775 (double)t.tms_cutime / ticks_per_second,
5776 (double)t.tms_cstime / ticks_per_second,
5777 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005778}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005779#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005780#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005781
5782
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005783#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005784#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005785static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005786posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005787{
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 FILETIME create, exit, kernel, user;
5789 HANDLE hProc;
5790 hProc = GetCurrentProcess();
5791 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5792 /* The fields of a FILETIME structure are the hi and lo part
5793 of a 64-bit value expressed in 100 nanosecond units.
5794 1e7 is one second in such units; 1e-7 the inverse.
5795 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5796 */
5797 return Py_BuildValue(
5798 "ddddd",
5799 (double)(user.dwHighDateTime*429.4967296 +
5800 user.dwLowDateTime*1e-7),
5801 (double)(kernel.dwHighDateTime*429.4967296 +
5802 kernel.dwLowDateTime*1e-7),
5803 (double)0,
5804 (double)0,
5805 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005806}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005807#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005808
5809#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005810PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005811"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005812Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005813#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005814
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005815
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005816#ifdef HAVE_GETSID
5817PyDoc_STRVAR(posix_getsid__doc__,
5818"getsid(pid) -> sid\n\n\
5819Call the system call getsid().");
5820
5821static PyObject *
5822posix_getsid(PyObject *self, PyObject *args)
5823{
Victor Stinner8c62be82010-05-06 00:08:46 +00005824 pid_t pid;
5825 int sid;
5826 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5827 return NULL;
5828 sid = getsid(pid);
5829 if (sid < 0)
5830 return posix_error();
5831 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005832}
5833#endif /* HAVE_GETSID */
5834
5835
Guido van Rossumb6775db1994-08-01 11:34:53 +00005836#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005837PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005838"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005839Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005840
Barry Warsaw53699e91996-12-10 23:23:01 +00005841static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005842posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005843{
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 if (setsid() < 0)
5845 return posix_error();
5846 Py_INCREF(Py_None);
5847 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005848}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005849#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005850
Guido van Rossumb6775db1994-08-01 11:34:53 +00005851#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005852PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005853"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005854Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005855
Barry Warsaw53699e91996-12-10 23:23:01 +00005856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005857posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005858{
Victor Stinner8c62be82010-05-06 00:08:46 +00005859 pid_t pid;
5860 int pgrp;
5861 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5862 return NULL;
5863 if (setpgid(pid, pgrp) < 0)
5864 return posix_error();
5865 Py_INCREF(Py_None);
5866 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005867}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005868#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005870
Guido van Rossumb6775db1994-08-01 11:34:53 +00005871#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005872PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005873"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005874Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005875
Barry Warsaw53699e91996-12-10 23:23:01 +00005876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005877posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005878{
Victor Stinner8c62be82010-05-06 00:08:46 +00005879 int fd;
5880 pid_t pgid;
5881 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5882 return NULL;
5883 pgid = tcgetpgrp(fd);
5884 if (pgid < 0)
5885 return posix_error();
5886 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005887}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005888#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005889
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005890
Guido van Rossumb6775db1994-08-01 11:34:53 +00005891#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005892PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005893"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005894Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005895
Barry Warsaw53699e91996-12-10 23:23:01 +00005896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005897posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005898{
Victor Stinner8c62be82010-05-06 00:08:46 +00005899 int fd;
5900 pid_t pgid;
5901 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5902 return NULL;
5903 if (tcsetpgrp(fd, pgid) < 0)
5904 return posix_error();
5905 Py_INCREF(Py_None);
5906 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005907}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005908#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005909
Guido van Rossum687dd131993-05-17 08:34:16 +00005910/* Functions acting on file descriptors */
5911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005912PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005913"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005914Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005915
Barry Warsaw53699e91996-12-10 23:23:01 +00005916static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005917posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005918{
Victor Stinner8c62be82010-05-06 00:08:46 +00005919 PyObject *ofile;
5920 char *file;
5921 int flag;
5922 int mode = 0777;
5923 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005924
5925#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005926 PyUnicodeObject *po;
5927 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5928 Py_BEGIN_ALLOW_THREADS
5929 /* PyUnicode_AS_UNICODE OK without thread
5930 lock as it is a simple dereference. */
5931 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5932 Py_END_ALLOW_THREADS
5933 if (fd < 0)
5934 return posix_error();
5935 return PyLong_FromLong((long)fd);
5936 }
5937 /* Drop the argument parsing error as narrow strings
5938 are also valid. */
5939 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005940#endif
5941
Victor Stinner8c62be82010-05-06 00:08:46 +00005942 if (!PyArg_ParseTuple(args, "O&i|i",
5943 PyUnicode_FSConverter, &ofile,
5944 &flag, &mode))
5945 return NULL;
5946 file = PyBytes_AsString(ofile);
5947 Py_BEGIN_ALLOW_THREADS
5948 fd = open(file, flag, mode);
5949 Py_END_ALLOW_THREADS
5950 if (fd < 0)
5951 return posix_error_with_allocated_filename(ofile);
5952 Py_DECREF(ofile);
5953 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005954}
5955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005957PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005958"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005959Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005960
Barry Warsaw53699e91996-12-10 23:23:01 +00005961static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005962posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005963{
Victor Stinner8c62be82010-05-06 00:08:46 +00005964 int fd, res;
5965 if (!PyArg_ParseTuple(args, "i:close", &fd))
5966 return NULL;
5967 if (!_PyVerify_fd(fd))
5968 return posix_error();
5969 Py_BEGIN_ALLOW_THREADS
5970 res = close(fd);
5971 Py_END_ALLOW_THREADS
5972 if (res < 0)
5973 return posix_error();
5974 Py_INCREF(Py_None);
5975 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005976}
5977
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005978
Victor Stinner8c62be82010-05-06 00:08:46 +00005979PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005980"closerange(fd_low, fd_high)\n\n\
5981Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5982
5983static PyObject *
5984posix_closerange(PyObject *self, PyObject *args)
5985{
Victor Stinner8c62be82010-05-06 00:08:46 +00005986 int fd_from, fd_to, i;
5987 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5988 return NULL;
5989 Py_BEGIN_ALLOW_THREADS
5990 for (i = fd_from; i < fd_to; i++)
5991 if (_PyVerify_fd(i))
5992 close(i);
5993 Py_END_ALLOW_THREADS
5994 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005995}
5996
5997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005998PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005999"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006000Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006001
Barry Warsaw53699e91996-12-10 23:23:01 +00006002static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006003posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006004{
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 int fd;
6006 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6007 return NULL;
6008 if (!_PyVerify_fd(fd))
6009 return posix_error();
6010 Py_BEGIN_ALLOW_THREADS
6011 fd = dup(fd);
6012 Py_END_ALLOW_THREADS
6013 if (fd < 0)
6014 return posix_error();
6015 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006016}
6017
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006018
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006019PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006020"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006021Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006022
Barry Warsaw53699e91996-12-10 23:23:01 +00006023static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006024posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006025{
Victor Stinner8c62be82010-05-06 00:08:46 +00006026 int fd, fd2, res;
6027 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6028 return NULL;
6029 if (!_PyVerify_fd_dup2(fd, fd2))
6030 return posix_error();
6031 Py_BEGIN_ALLOW_THREADS
6032 res = dup2(fd, fd2);
6033 Py_END_ALLOW_THREADS
6034 if (res < 0)
6035 return posix_error();
6036 Py_INCREF(Py_None);
6037 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006038}
6039
Ross Lagerwall7807c352011-03-17 20:20:30 +02006040#ifdef HAVE_LOCKF
6041PyDoc_STRVAR(posix_lockf__doc__,
6042"lockf(fd, cmd, len)\n\n\
6043Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6044fd is an open file descriptor.\n\
6045cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6046F_TEST.\n\
6047len specifies the section of the file to lock.");
6048
6049static PyObject *
6050posix_lockf(PyObject *self, PyObject *args)
6051{
6052 int fd, cmd, res;
6053 off_t len;
6054 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6055 &fd, &cmd, _parse_off_t, &len))
6056 return NULL;
6057
6058 Py_BEGIN_ALLOW_THREADS
6059 res = lockf(fd, cmd, len);
6060 Py_END_ALLOW_THREADS
6061
6062 if (res < 0)
6063 return posix_error();
6064
6065 Py_RETURN_NONE;
6066}
6067#endif
6068
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006069
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006070PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006071"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006072Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006073
Barry Warsaw53699e91996-12-10 23:23:01 +00006074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006075posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006076{
Victor Stinner8c62be82010-05-06 00:08:46 +00006077 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006078#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006079 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006080#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006081 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006082#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006083 PyObject *posobj;
6084 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006085 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006086#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006087 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6088 switch (how) {
6089 case 0: how = SEEK_SET; break;
6090 case 1: how = SEEK_CUR; break;
6091 case 2: how = SEEK_END; break;
6092 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006093#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006094
Ross Lagerwall8e749672011-03-17 21:54:07 +02006095#if !defined(HAVE_LARGEFILE_SUPPORT)
6096 pos = PyLong_AsLong(posobj);
6097#else
6098 pos = PyLong_AsLongLong(posobj);
6099#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006100 if (PyErr_Occurred())
6101 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006102
Victor Stinner8c62be82010-05-06 00:08:46 +00006103 if (!_PyVerify_fd(fd))
6104 return posix_error();
6105 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006106#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006107 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006108#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006109 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006110#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006111 Py_END_ALLOW_THREADS
6112 if (res < 0)
6113 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006114
6115#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006116 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006117#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006119#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006120}
6121
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006123PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006124"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006125Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006126
Barry Warsaw53699e91996-12-10 23:23:01 +00006127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006128posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006129{
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 int fd, size;
6131 Py_ssize_t n;
6132 PyObject *buffer;
6133 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6134 return NULL;
6135 if (size < 0) {
6136 errno = EINVAL;
6137 return posix_error();
6138 }
6139 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6140 if (buffer == NULL)
6141 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006142 if (!_PyVerify_fd(fd)) {
6143 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006144 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006145 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006146 Py_BEGIN_ALLOW_THREADS
6147 n = read(fd, PyBytes_AS_STRING(buffer), size);
6148 Py_END_ALLOW_THREADS
6149 if (n < 0) {
6150 Py_DECREF(buffer);
6151 return posix_error();
6152 }
6153 if (n != size)
6154 _PyBytes_Resize(&buffer, n);
6155 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006156}
6157
Ross Lagerwall7807c352011-03-17 20:20:30 +02006158#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6159 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006160static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006161iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6162{
6163 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006164 Py_ssize_t blen, total = 0;
6165
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006166 *iov = PyMem_New(struct iovec, cnt);
6167 if (*iov == NULL) {
6168 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006169 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006170 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006171
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006172 *buf = PyMem_New(Py_buffer, cnt);
6173 if (*buf == NULL) {
6174 PyMem_Del(*iov);
6175 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006176 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006177 }
6178
6179 for (i = 0; i < cnt; i++) {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006180 if (PyObject_GetBuffer(PySequence_GetItem(seq, i),
6181 &(*buf)[i], type) == -1) {
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006182 PyMem_Del(*iov);
6183 for (j = 0; j < i; j++) {
6184 PyBuffer_Release(&(*buf)[j]);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006185 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006186 PyMem_Del(*buf);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006187 total = 0;
6188 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006189 }
6190 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006191 blen = (*buf)[i].len;
6192 (*iov)[i].iov_len = blen;
6193 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006194 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006195 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006196}
6197
6198static void
6199iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6200{
6201 int i;
6202 PyMem_Del(iov);
6203 for (i = 0; i < cnt; i++) {
6204 PyBuffer_Release(&buf[i]);
6205 }
6206 PyMem_Del(buf);
6207}
6208#endif
6209
Ross Lagerwall7807c352011-03-17 20:20:30 +02006210#ifdef HAVE_READV
6211PyDoc_STRVAR(posix_readv__doc__,
6212"readv(fd, buffers) -> bytesread\n\n\
6213Read from a file descriptor into a number of writable buffers. buffers\n\
6214is an arbitrary sequence of writable buffers.\n\
6215Returns the total number of bytes read.");
6216
6217static PyObject *
6218posix_readv(PyObject *self, PyObject *args)
6219{
6220 int fd, cnt;
6221 Py_ssize_t n;
6222 PyObject *seq;
6223 struct iovec *iov;
6224 Py_buffer *buf;
6225
6226 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6227 return NULL;
6228 if (!PySequence_Check(seq)) {
6229 PyErr_SetString(PyExc_TypeError,
6230 "readv() arg 2 must be a sequence");
6231 return NULL;
6232 }
6233 cnt = PySequence_Size(seq);
6234
6235 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6236 return NULL;
6237
6238 Py_BEGIN_ALLOW_THREADS
6239 n = readv(fd, iov, cnt);
6240 Py_END_ALLOW_THREADS
6241
6242 iov_cleanup(iov, buf, cnt);
6243 return PyLong_FromSsize_t(n);
6244}
6245#endif
6246
6247#ifdef HAVE_PREAD
6248PyDoc_STRVAR(posix_pread__doc__,
6249"pread(fd, buffersize, offset) -> string\n\n\
6250Read from a file descriptor, fd, at a position of offset. It will read up\n\
6251to buffersize number of bytes. The file offset remains unchanged.");
6252
6253static PyObject *
6254posix_pread(PyObject *self, PyObject *args)
6255{
6256 int fd, size;
6257 off_t offset;
6258 Py_ssize_t n;
6259 PyObject *buffer;
6260 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6261 return NULL;
6262
6263 if (size < 0) {
6264 errno = EINVAL;
6265 return posix_error();
6266 }
6267 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6268 if (buffer == NULL)
6269 return NULL;
6270 if (!_PyVerify_fd(fd)) {
6271 Py_DECREF(buffer);
6272 return posix_error();
6273 }
6274 Py_BEGIN_ALLOW_THREADS
6275 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6276 Py_END_ALLOW_THREADS
6277 if (n < 0) {
6278 Py_DECREF(buffer);
6279 return posix_error();
6280 }
6281 if (n != size)
6282 _PyBytes_Resize(&buffer, n);
6283 return buffer;
6284}
6285#endif
6286
6287PyDoc_STRVAR(posix_write__doc__,
6288"write(fd, string) -> byteswritten\n\n\
6289Write a string to a file descriptor.");
6290
6291static PyObject *
6292posix_write(PyObject *self, PyObject *args)
6293{
6294 Py_buffer pbuf;
6295 int fd;
6296 Py_ssize_t size, len;
6297
6298 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
6299 return NULL;
6300 if (!_PyVerify_fd(fd)) {
6301 PyBuffer_Release(&pbuf);
6302 return posix_error();
6303 }
6304 len = pbuf.len;
6305 Py_BEGIN_ALLOW_THREADS
6306#if defined(MS_WIN64) || defined(MS_WINDOWS)
6307 if (len > INT_MAX)
6308 len = INT_MAX;
6309 size = write(fd, pbuf.buf, (int)len);
6310#else
6311 size = write(fd, pbuf.buf, len);
6312#endif
6313 Py_END_ALLOW_THREADS
6314 PyBuffer_Release(&pbuf);
6315 if (size < 0)
6316 return posix_error();
6317 return PyLong_FromSsize_t(size);
6318}
6319
6320#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006321PyDoc_STRVAR(posix_sendfile__doc__,
6322"sendfile(out, in, offset, nbytes) -> byteswritten\n\
6323sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
6324 -> byteswritten\n\
6325Copy nbytes bytes from file descriptor in to file descriptor out.");
6326
6327static PyObject *
6328posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
6329{
6330 int in, out;
6331 Py_ssize_t ret;
6332 off_t offset;
6333
6334#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
6335#ifndef __APPLE__
6336 Py_ssize_t len;
6337#endif
6338 PyObject *headers = NULL, *trailers = NULL;
6339 Py_buffer *hbuf, *tbuf;
6340 off_t sbytes;
6341 struct sf_hdtr sf;
6342 int flags = 0;
6343 sf.headers = NULL;
6344 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00006345 static char *keywords[] = {"out", "in",
6346 "offset", "count",
6347 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006348
6349#ifdef __APPLE__
6350 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006351 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006352#else
6353 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006354 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006355#endif
6356 &headers, &trailers, &flags))
6357 return NULL;
6358 if (headers != NULL) {
6359 if (!PySequence_Check(headers)) {
6360 PyErr_SetString(PyExc_TypeError,
6361 "sendfile() headers must be a sequence or None");
6362 return NULL;
6363 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006364 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006365 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006366 if (sf.hdr_cnt > 0 &&
6367 !(i = iov_setup(&(sf.headers), &hbuf,
6368 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006369 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006370#ifdef __APPLE__
6371 sbytes += i;
6372#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006373 }
6374 }
6375 if (trailers != NULL) {
6376 if (!PySequence_Check(trailers)) {
6377 PyErr_SetString(PyExc_TypeError,
6378 "sendfile() trailers must be a sequence or None");
6379 return NULL;
6380 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006381 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006382 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006383 if (sf.trl_cnt > 0 &&
6384 !(i = iov_setup(&(sf.trailers), &tbuf,
6385 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006386 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006387#ifdef __APPLE__
6388 sbytes += i;
6389#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006390 }
6391 }
6392
6393 Py_BEGIN_ALLOW_THREADS
6394#ifdef __APPLE__
6395 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
6396#else
6397 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
6398#endif
6399 Py_END_ALLOW_THREADS
6400
6401 if (sf.headers != NULL)
6402 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
6403 if (sf.trailers != NULL)
6404 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
6405
6406 if (ret < 0) {
6407 if ((errno == EAGAIN) || (errno == EBUSY)) {
6408 if (sbytes != 0) {
6409 // some data has been sent
6410 goto done;
6411 }
6412 else {
6413 // no data has been sent; upper application is supposed
6414 // to retry on EAGAIN or EBUSY
6415 return posix_error();
6416 }
6417 }
6418 return posix_error();
6419 }
6420 goto done;
6421
6422done:
6423 #if !defined(HAVE_LARGEFILE_SUPPORT)
6424 return Py_BuildValue("l", sbytes);
6425 #else
6426 return Py_BuildValue("L", sbytes);
6427 #endif
6428
6429#else
6430 Py_ssize_t count;
6431 PyObject *offobj;
6432 static char *keywords[] = {"out", "in",
6433 "offset", "count", NULL};
6434 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
6435 keywords, &out, &in, &offobj, &count))
6436 return NULL;
6437#ifdef linux
6438 if (offobj == Py_None) {
6439 Py_BEGIN_ALLOW_THREADS
6440 ret = sendfile(out, in, NULL, count);
6441 Py_END_ALLOW_THREADS
6442 if (ret < 0)
6443 return posix_error();
6444 Py_INCREF(Py_None);
6445 return Py_BuildValue("nO", ret, Py_None);
6446 }
6447#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00006448 if (!_parse_off_t(offobj, &offset))
6449 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006450 Py_BEGIN_ALLOW_THREADS
6451 ret = sendfile(out, in, &offset, count);
6452 Py_END_ALLOW_THREADS
6453 if (ret < 0)
6454 return posix_error();
6455 return Py_BuildValue("n", ret);
6456#endif
6457}
6458#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006460PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006461"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006462Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006463
Barry Warsaw53699e91996-12-10 23:23:01 +00006464static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006465posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006466{
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 int fd;
6468 STRUCT_STAT st;
6469 int res;
6470 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6471 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006472#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00006473 /* on OpenVMS we must ensure that all bytes are written to the file */
6474 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006475#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 if (!_PyVerify_fd(fd))
6477 return posix_error();
6478 Py_BEGIN_ALLOW_THREADS
6479 res = FSTAT(fd, &st);
6480 Py_END_ALLOW_THREADS
6481 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006482#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006483 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006484#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006486#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 }
Tim Peters5aa91602002-01-30 05:46:57 +00006488
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006490}
6491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006492PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006493"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006494Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006495connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006496
6497static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006498posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006499{
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 int fd;
6501 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6502 return NULL;
6503 if (!_PyVerify_fd(fd))
6504 return PyBool_FromLong(0);
6505 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006506}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006507
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006508#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006509PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006510"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006511Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006512
Barry Warsaw53699e91996-12-10 23:23:01 +00006513static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006514posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006515{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006516#if defined(PYOS_OS2)
6517 HFILE read, write;
6518 APIRET rc;
6519
Victor Stinner8c62be82010-05-06 00:08:46 +00006520 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006521 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00006522 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006523 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006524 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006525
6526 return Py_BuildValue("(ii)", read, write);
6527#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006528#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006529 int fds[2];
6530 int res;
6531 Py_BEGIN_ALLOW_THREADS
6532 res = pipe(fds);
6533 Py_END_ALLOW_THREADS
6534 if (res != 0)
6535 return posix_error();
6536 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006537#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 HANDLE read, write;
6539 int read_fd, write_fd;
6540 BOOL ok;
6541 Py_BEGIN_ALLOW_THREADS
6542 ok = CreatePipe(&read, &write, NULL, 0);
6543 Py_END_ALLOW_THREADS
6544 if (!ok)
6545 return win32_error("CreatePipe", NULL);
6546 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6547 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6548 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006549#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006550#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006551}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006552#endif /* HAVE_PIPE */
6553
Ross Lagerwall7807c352011-03-17 20:20:30 +02006554#ifdef HAVE_WRITEV
6555PyDoc_STRVAR(posix_writev__doc__,
6556"writev(fd, buffers) -> byteswritten\n\n\
6557Write the contents of buffers to a file descriptor, where buffers is an\n\
6558arbitrary sequence of buffers.\n\
6559Returns the total bytes written.");
6560
6561static PyObject *
6562posix_writev(PyObject *self, PyObject *args)
6563{
6564 int fd, cnt;
6565 Py_ssize_t res;
6566 PyObject *seq;
6567 struct iovec *iov;
6568 Py_buffer *buf;
6569 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
6570 return NULL;
6571 if (!PySequence_Check(seq)) {
6572 PyErr_SetString(PyExc_TypeError,
6573 "writev() arg 2 must be a sequence");
6574 return NULL;
6575 }
6576 cnt = PySequence_Size(seq);
6577
6578 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
6579 return NULL;
6580 }
6581
6582 Py_BEGIN_ALLOW_THREADS
6583 res = writev(fd, iov, cnt);
6584 Py_END_ALLOW_THREADS
6585
6586 iov_cleanup(iov, buf, cnt);
6587 return PyLong_FromSsize_t(res);
6588}
6589#endif
6590
6591#ifdef HAVE_PWRITE
6592PyDoc_STRVAR(posix_pwrite__doc__,
6593"pwrite(fd, string, offset) -> byteswritten\n\n\
6594Write string to a file descriptor, fd, from offset, leaving the file\n\
6595offset unchanged.");
6596
6597static PyObject *
6598posix_pwrite(PyObject *self, PyObject *args)
6599{
6600 Py_buffer pbuf;
6601 int fd;
6602 off_t offset;
6603 Py_ssize_t size;
6604
6605 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
6606 return NULL;
6607
6608 if (!_PyVerify_fd(fd)) {
6609 PyBuffer_Release(&pbuf);
6610 return posix_error();
6611 }
6612 Py_BEGIN_ALLOW_THREADS
6613 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
6614 Py_END_ALLOW_THREADS
6615 PyBuffer_Release(&pbuf);
6616 if (size < 0)
6617 return posix_error();
6618 return PyLong_FromSsize_t(size);
6619}
6620#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006621
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006622#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006623PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006624"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006625Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006626
Barry Warsaw53699e91996-12-10 23:23:01 +00006627static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006628posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006629{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006630 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 char *filename;
6632 int mode = 0666;
6633 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006634 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
6635 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006637 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006638 Py_BEGIN_ALLOW_THREADS
6639 res = mkfifo(filename, mode);
6640 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006641 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006642 if (res < 0)
6643 return posix_error();
6644 Py_INCREF(Py_None);
6645 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006646}
6647#endif
6648
6649
Neal Norwitz11690112002-07-30 01:08:28 +00006650#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006651PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006652"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006653Create a filesystem node (file, device special file or named pipe)\n\
6654named filename. mode specifies both the permissions to use and the\n\
6655type of node to be created, being combined (bitwise OR) with one of\n\
6656S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006657device defines the newly created device special file (probably using\n\
6658os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006659
6660
6661static PyObject *
6662posix_mknod(PyObject *self, PyObject *args)
6663{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006664 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006665 char *filename;
6666 int mode = 0600;
6667 int device = 0;
6668 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006669 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
6670 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00006671 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006672 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 Py_BEGIN_ALLOW_THREADS
6674 res = mknod(filename, mode, device);
6675 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006676 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006677 if (res < 0)
6678 return posix_error();
6679 Py_INCREF(Py_None);
6680 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006681}
6682#endif
6683
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006684#ifdef HAVE_DEVICE_MACROS
6685PyDoc_STRVAR(posix_major__doc__,
6686"major(device) -> major number\n\
6687Extracts a device major number from a raw device number.");
6688
6689static PyObject *
6690posix_major(PyObject *self, PyObject *args)
6691{
Victor Stinner8c62be82010-05-06 00:08:46 +00006692 int device;
6693 if (!PyArg_ParseTuple(args, "i:major", &device))
6694 return NULL;
6695 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006696}
6697
6698PyDoc_STRVAR(posix_minor__doc__,
6699"minor(device) -> minor number\n\
6700Extracts a device minor number from a raw device number.");
6701
6702static PyObject *
6703posix_minor(PyObject *self, PyObject *args)
6704{
Victor Stinner8c62be82010-05-06 00:08:46 +00006705 int device;
6706 if (!PyArg_ParseTuple(args, "i:minor", &device))
6707 return NULL;
6708 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006709}
6710
6711PyDoc_STRVAR(posix_makedev__doc__,
6712"makedev(major, minor) -> device number\n\
6713Composes a raw device number from the major and minor device numbers.");
6714
6715static PyObject *
6716posix_makedev(PyObject *self, PyObject *args)
6717{
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 int major, minor;
6719 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6720 return NULL;
6721 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006722}
6723#endif /* device macros */
6724
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006725
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006726#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006727PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006728"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006729Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006730
Barry Warsaw53699e91996-12-10 23:23:01 +00006731static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006732posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006733{
Victor Stinner8c62be82010-05-06 00:08:46 +00006734 int fd;
6735 off_t length;
6736 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006737
Ross Lagerwall7807c352011-03-17 20:20:30 +02006738 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00006739 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006740
Victor Stinner8c62be82010-05-06 00:08:46 +00006741 Py_BEGIN_ALLOW_THREADS
6742 res = ftruncate(fd, length);
6743 Py_END_ALLOW_THREADS
6744 if (res < 0)
6745 return posix_error();
6746 Py_INCREF(Py_None);
6747 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006748}
6749#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006750
Ross Lagerwall7807c352011-03-17 20:20:30 +02006751#ifdef HAVE_TRUNCATE
6752PyDoc_STRVAR(posix_truncate__doc__,
6753"truncate(path, length)\n\n\
6754Truncate the file given by path to length bytes.");
6755
6756static PyObject *
6757posix_truncate(PyObject *self, PyObject *args)
6758{
6759 PyObject *opath;
6760 const char *path;
6761 off_t length;
6762 int res;
6763
6764 if (!PyArg_ParseTuple(args, "O&O&:truncate",
6765 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
6766 return NULL;
6767 path = PyBytes_AsString(opath);
6768
6769 Py_BEGIN_ALLOW_THREADS
6770 res = truncate(path, length);
6771 Py_END_ALLOW_THREADS
6772 Py_DECREF(opath);
6773 if (res < 0)
6774 return posix_error();
6775 Py_RETURN_NONE;
6776}
6777#endif
6778
6779#ifdef HAVE_POSIX_FALLOCATE
6780PyDoc_STRVAR(posix_posix_fallocate__doc__,
6781"posix_fallocate(fd, offset, len)\n\n\
6782Ensures that enough disk space is allocated for the file specified by fd\n\
6783starting from offset and continuing for len bytes.");
6784
6785static PyObject *
6786posix_posix_fallocate(PyObject *self, PyObject *args)
6787{
6788 off_t len, offset;
6789 int res, fd;
6790
6791 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
6792 &fd, _parse_off_t, &offset, _parse_off_t, &len))
6793 return NULL;
6794
6795 Py_BEGIN_ALLOW_THREADS
6796 res = posix_fallocate(fd, offset, len);
6797 Py_END_ALLOW_THREADS
6798 if (res != 0) {
6799 errno = res;
6800 return posix_error();
6801 }
6802 Py_RETURN_NONE;
6803}
6804#endif
6805
6806#ifdef HAVE_POSIX_FADVISE
6807PyDoc_STRVAR(posix_posix_fadvise__doc__,
6808"posix_fadvise(fd, offset, len, advice)\n\n\
6809Announces an intention to access data in a specific pattern thus allowing\n\
6810the kernel to make optimizations.\n\
6811The advice applies to the region of the file specified by fd starting at\n\
6812offset and continuing for len bytes.\n\
6813advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
6814POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
6815POSIX_FADV_DONTNEED.");
6816
6817static PyObject *
6818posix_posix_fadvise(PyObject *self, PyObject *args)
6819{
6820 off_t len, offset;
6821 int res, fd, advice;
6822
6823 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
6824 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
6825 return NULL;
6826
6827 Py_BEGIN_ALLOW_THREADS
6828 res = posix_fadvise(fd, offset, len, advice);
6829 Py_END_ALLOW_THREADS
6830 if (res != 0) {
6831 errno = res;
6832 return posix_error();
6833 }
6834 Py_RETURN_NONE;
6835}
6836#endif
6837
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006838#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006839PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006840"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006841Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006842
Fred Drake762e2061999-08-26 17:23:54 +00006843/* Save putenv() parameters as values here, so we can collect them when they
6844 * get re-set with another call for the same key. */
6845static PyObject *posix_putenv_garbage;
6846
Tim Peters5aa91602002-01-30 05:46:57 +00006847static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006848posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006849{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006850#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 wchar_t *s1, *s2;
6852 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006853#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006854 PyObject *os1, *os2;
6855 char *s1, *s2;
6856 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006857#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006858 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006860
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006861#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 if (!PyArg_ParseTuple(args,
6863 "uu:putenv",
6864 &s1, &s2))
6865 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006866#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 if (!PyArg_ParseTuple(args,
6868 "O&O&:putenv",
6869 PyUnicode_FSConverter, &os1,
6870 PyUnicode_FSConverter, &os2))
6871 return NULL;
6872 s1 = PyBytes_AsString(os1);
6873 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006874#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006875
6876#if defined(PYOS_OS2)
6877 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6878 APIRET rc;
6879
Guido van Rossumd48f2521997-12-05 22:19:34 +00006880 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006881 if (rc != NO_ERROR) {
6882 os2_error(rc);
6883 goto error;
6884 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006885
6886 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6887 APIRET rc;
6888
Guido van Rossumd48f2521997-12-05 22:19:34 +00006889 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006890 if (rc != NO_ERROR) {
6891 os2_error(rc);
6892 goto error;
6893 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006894 } else {
6895#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006896 /* XXX This can leak memory -- not easy to fix :-( */
6897 /* len includes space for a trailing \0; the size arg to
6898 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006899#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006900 len = wcslen(s1) + wcslen(s2) + 2;
6901 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006902#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006903 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006905#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006906 if (newstr == NULL) {
6907 PyErr_NoMemory();
6908 goto error;
6909 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006910#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 newenv = PyUnicode_AsUnicode(newstr);
6912 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6913 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006914 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006915 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006917#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 newenv = PyBytes_AS_STRING(newstr);
6919 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6920 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006922 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006923 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006924#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006925
Victor Stinner8c62be82010-05-06 00:08:46 +00006926 /* Install the first arg and newstr in posix_putenv_garbage;
6927 * this will cause previous value to be collected. This has to
6928 * happen after the real putenv() call because the old value
6929 * was still accessible until then. */
6930 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006931#ifdef MS_WINDOWS
6932 PyTuple_GET_ITEM(args, 0),
6933#else
6934 os1,
6935#endif
6936 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 /* really not much we can do; just leak */
6938 PyErr_Clear();
6939 }
6940 else {
6941 Py_DECREF(newstr);
6942 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006943
6944#if defined(PYOS_OS2)
6945 }
6946#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006947
Martin v. Löwis011e8422009-05-05 04:43:17 +00006948#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 Py_DECREF(os1);
6950 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006951#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006952 Py_RETURN_NONE;
6953
6954error:
6955#ifndef MS_WINDOWS
6956 Py_DECREF(os1);
6957 Py_DECREF(os2);
6958#endif
6959 Py_XDECREF(newstr);
6960 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006961}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006962#endif /* putenv */
6963
Guido van Rossumc524d952001-10-19 01:31:59 +00006964#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006965PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006966"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006967Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006968
6969static PyObject *
6970posix_unsetenv(PyObject *self, PyObject *args)
6971{
Victor Stinner84ae1182010-05-06 22:05:07 +00006972#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006974
Victor Stinner8c62be82010-05-06 00:08:46 +00006975 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6976 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006977#else
6978 PyObject *os1;
6979 char *s1;
6980
6981 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6982 PyUnicode_FSConverter, &os1))
6983 return NULL;
6984 s1 = PyBytes_AsString(os1);
6985#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006986
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006988
Victor Stinner8c62be82010-05-06 00:08:46 +00006989 /* Remove the key from posix_putenv_garbage;
6990 * this will cause it to be collected. This has to
6991 * happen after the real unsetenv() call because the
6992 * old value was still accessible until then.
6993 */
6994 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006995#ifdef MS_WINDOWS
6996 PyTuple_GET_ITEM(args, 0)
6997#else
6998 os1
6999#endif
7000 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007001 /* really not much we can do; just leak */
7002 PyErr_Clear();
7003 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007004
Victor Stinner84ae1182010-05-06 22:05:07 +00007005#ifndef MS_WINDOWS
7006 Py_DECREF(os1);
7007#endif
7008 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007009}
7010#endif /* unsetenv */
7011
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007012PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007013"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007014Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007015
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007016static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007017posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007018{
Victor Stinner8c62be82010-05-06 00:08:46 +00007019 int code;
7020 char *message;
7021 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7022 return NULL;
7023 message = strerror(code);
7024 if (message == NULL) {
7025 PyErr_SetString(PyExc_ValueError,
7026 "strerror() argument out of range");
7027 return NULL;
7028 }
7029 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007030}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007031
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007032
Guido van Rossumc9641791998-08-04 15:26:23 +00007033#ifdef HAVE_SYS_WAIT_H
7034
Fred Drake106c1a02002-04-23 15:58:02 +00007035#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007036PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007037"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007038Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007039
7040static PyObject *
7041posix_WCOREDUMP(PyObject *self, PyObject *args)
7042{
Victor Stinner8c62be82010-05-06 00:08:46 +00007043 WAIT_TYPE status;
7044 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007045
Victor Stinner8c62be82010-05-06 00:08:46 +00007046 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7047 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007048
Victor Stinner8c62be82010-05-06 00:08:46 +00007049 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007050}
7051#endif /* WCOREDUMP */
7052
7053#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007054PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007055"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007056Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007057job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007058
7059static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007060posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007061{
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 WAIT_TYPE status;
7063 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007064
Victor Stinner8c62be82010-05-06 00:08:46 +00007065 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7066 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007067
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007069}
7070#endif /* WIFCONTINUED */
7071
Guido van Rossumc9641791998-08-04 15:26:23 +00007072#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007073PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007074"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007075Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007076
7077static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007078posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007079{
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 WAIT_TYPE status;
7081 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007082
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7084 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007085
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007087}
7088#endif /* WIFSTOPPED */
7089
7090#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007091PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007092"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007093Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007094
7095static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007096posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007097{
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 WAIT_TYPE status;
7099 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007100
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7102 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007103
Victor Stinner8c62be82010-05-06 00:08:46 +00007104 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007105}
7106#endif /* WIFSIGNALED */
7107
7108#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007109PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007110"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007111Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007112system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007113
7114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007115posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007116{
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 WAIT_TYPE status;
7118 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007119
Victor Stinner8c62be82010-05-06 00:08:46 +00007120 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7121 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007122
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007124}
7125#endif /* WIFEXITED */
7126
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007127#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007128PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007129"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007130Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007131
7132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007133posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007134{
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 WAIT_TYPE status;
7136 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007137
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7139 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007140
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007142}
7143#endif /* WEXITSTATUS */
7144
7145#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007146PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007147"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007148Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007149value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007150
7151static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007152posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007153{
Victor Stinner8c62be82010-05-06 00:08:46 +00007154 WAIT_TYPE status;
7155 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007156
Victor Stinner8c62be82010-05-06 00:08:46 +00007157 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7158 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007159
Victor Stinner8c62be82010-05-06 00:08:46 +00007160 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007161}
7162#endif /* WTERMSIG */
7163
7164#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007165PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007166"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007167Return the signal that stopped the process that provided\n\
7168the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007169
7170static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007171posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007172{
Victor Stinner8c62be82010-05-06 00:08:46 +00007173 WAIT_TYPE status;
7174 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007175
Victor Stinner8c62be82010-05-06 00:08:46 +00007176 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7177 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007178
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007180}
7181#endif /* WSTOPSIG */
7182
7183#endif /* HAVE_SYS_WAIT_H */
7184
7185
Thomas Wouters477c8d52006-05-27 19:21:47 +00007186#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007187#ifdef _SCO_DS
7188/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7189 needed definitions in sys/statvfs.h */
7190#define _SVID3
7191#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007192#include <sys/statvfs.h>
7193
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007194static PyObject*
7195_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007196 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7197 if (v == NULL)
7198 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007199
7200#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7202 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7203 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7204 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7205 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7206 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7207 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7208 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7209 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7210 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007211#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7213 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7214 PyStructSequence_SET_ITEM(v, 2,
7215 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7216 PyStructSequence_SET_ITEM(v, 3,
7217 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7218 PyStructSequence_SET_ITEM(v, 4,
7219 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7220 PyStructSequence_SET_ITEM(v, 5,
7221 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7222 PyStructSequence_SET_ITEM(v, 6,
7223 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7224 PyStructSequence_SET_ITEM(v, 7,
7225 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7226 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7227 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007228#endif
7229
Victor Stinner8c62be82010-05-06 00:08:46 +00007230 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007231}
7232
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007233PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007234"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007235Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007236
7237static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007238posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007239{
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 int fd, res;
7241 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007242
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7244 return NULL;
7245 Py_BEGIN_ALLOW_THREADS
7246 res = fstatvfs(fd, &st);
7247 Py_END_ALLOW_THREADS
7248 if (res != 0)
7249 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007250
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007252}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007253#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007254
7255
Thomas Wouters477c8d52006-05-27 19:21:47 +00007256#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007257#include <sys/statvfs.h>
7258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007259PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007260"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007261Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007262
7263static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007264posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007265{
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 char *path;
7267 int res;
7268 struct statvfs st;
7269 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7270 return NULL;
7271 Py_BEGIN_ALLOW_THREADS
7272 res = statvfs(path, &st);
7273 Py_END_ALLOW_THREADS
7274 if (res != 0)
7275 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007276
Victor Stinner8c62be82010-05-06 00:08:46 +00007277 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007278}
7279#endif /* HAVE_STATVFS */
7280
Fred Drakec9680921999-12-13 16:37:25 +00007281/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7282 * It maps strings representing configuration variable names to
7283 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007284 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007285 * rarely-used constants. There are three separate tables that use
7286 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007287 *
7288 * This code is always included, even if none of the interfaces that
7289 * need it are included. The #if hackery needed to avoid it would be
7290 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007291 */
7292struct constdef {
7293 char *name;
7294 long value;
7295};
7296
Fred Drake12c6e2d1999-12-14 21:25:03 +00007297static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007298conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00007299 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007300{
Christian Heimes217cfd12007-12-02 14:31:20 +00007301 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007302 *valuep = PyLong_AS_LONG(arg);
7303 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007304 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00007305 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00007306 /* look up the value in the table using a binary search */
7307 size_t lo = 0;
7308 size_t mid;
7309 size_t hi = tablesize;
7310 int cmp;
7311 const char *confname;
7312 if (!PyUnicode_Check(arg)) {
7313 PyErr_SetString(PyExc_TypeError,
7314 "configuration names must be strings or integers");
7315 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007316 }
Stefan Krah0e803b32010-11-26 16:16:47 +00007317 confname = _PyUnicode_AsString(arg);
7318 if (confname == NULL)
7319 return 0;
7320 while (lo < hi) {
7321 mid = (lo + hi) / 2;
7322 cmp = strcmp(confname, table[mid].name);
7323 if (cmp < 0)
7324 hi = mid;
7325 else if (cmp > 0)
7326 lo = mid + 1;
7327 else {
7328 *valuep = table[mid].value;
7329 return 1;
7330 }
7331 }
7332 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7333 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007334 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00007335}
7336
7337
7338#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7339static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007340#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007341 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007342#endif
7343#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007345#endif
Fred Drakec9680921999-12-13 16:37:25 +00007346#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007348#endif
7349#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00007350 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007351#endif
7352#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00007353 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007354#endif
7355#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007357#endif
7358#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007359 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007360#endif
7361#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00007362 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007363#endif
7364#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007365 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007366#endif
7367#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007369#endif
7370#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007372#endif
7373#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007375#endif
7376#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007377 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007378#endif
7379#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007381#endif
7382#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007384#endif
7385#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007386 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007387#endif
7388#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007389 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007390#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00007391#ifdef _PC_ACL_ENABLED
7392 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
7393#endif
7394#ifdef _PC_MIN_HOLE_SIZE
7395 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
7396#endif
7397#ifdef _PC_ALLOC_SIZE_MIN
7398 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
7399#endif
7400#ifdef _PC_REC_INCR_XFER_SIZE
7401 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
7402#endif
7403#ifdef _PC_REC_MAX_XFER_SIZE
7404 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
7405#endif
7406#ifdef _PC_REC_MIN_XFER_SIZE
7407 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
7408#endif
7409#ifdef _PC_REC_XFER_ALIGN
7410 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
7411#endif
7412#ifdef _PC_SYMLINK_MAX
7413 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
7414#endif
7415#ifdef _PC_XATTR_ENABLED
7416 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
7417#endif
7418#ifdef _PC_XATTR_EXISTS
7419 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
7420#endif
7421#ifdef _PC_TIMESTAMP_RESOLUTION
7422 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
7423#endif
Fred Drakec9680921999-12-13 16:37:25 +00007424};
7425
Fred Drakec9680921999-12-13 16:37:25 +00007426static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007427conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007428{
7429 return conv_confname(arg, valuep, posix_constants_pathconf,
7430 sizeof(posix_constants_pathconf)
7431 / sizeof(struct constdef));
7432}
7433#endif
7434
7435#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007436PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007437"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007438Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007439If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007440
7441static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007442posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007443{
7444 PyObject *result = NULL;
7445 int name, fd;
7446
Fred Drake12c6e2d1999-12-14 21:25:03 +00007447 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7448 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007449 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007450
Stefan Krah0e803b32010-11-26 16:16:47 +00007451 errno = 0;
7452 limit = fpathconf(fd, name);
7453 if (limit == -1 && errno != 0)
7454 posix_error();
7455 else
7456 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007457 }
7458 return result;
7459}
7460#endif
7461
7462
7463#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007464PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007465"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007466Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007467If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007468
7469static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007470posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007471{
7472 PyObject *result = NULL;
7473 int name;
7474 char *path;
7475
7476 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7477 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007478 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007479
Victor Stinner8c62be82010-05-06 00:08:46 +00007480 errno = 0;
7481 limit = pathconf(path, name);
7482 if (limit == -1 && errno != 0) {
7483 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00007484 /* could be a path or name problem */
7485 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007486 else
Stefan Krah99439262010-11-26 12:58:05 +00007487 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00007488 }
7489 else
7490 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007491 }
7492 return result;
7493}
7494#endif
7495
7496#ifdef HAVE_CONFSTR
7497static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007498#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00007499 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007500#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00007501#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007502 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007503#endif
7504#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007505 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007506#endif
Fred Draked86ed291999-12-15 15:34:33 +00007507#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007508 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007509#endif
7510#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007512#endif
7513#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007514 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007515#endif
7516#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007517 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007518#endif
Fred Drakec9680921999-12-13 16:37:25 +00007519#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007520 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007521#endif
7522#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007523 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007524#endif
7525#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007526 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007527#endif
7528#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007529 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007530#endif
7531#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007532 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007533#endif
7534#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007535 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007536#endif
7537#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007539#endif
7540#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007541 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007542#endif
Fred Draked86ed291999-12-15 15:34:33 +00007543#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007545#endif
Fred Drakec9680921999-12-13 16:37:25 +00007546#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00007547 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007548#endif
Fred Draked86ed291999-12-15 15:34:33 +00007549#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007551#endif
7552#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007553 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007554#endif
7555#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007556 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007557#endif
7558#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007559 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007560#endif
Fred Drakec9680921999-12-13 16:37:25 +00007561#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007562 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007563#endif
7564#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007566#endif
7567#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007568 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007569#endif
7570#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007572#endif
7573#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007574 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007575#endif
7576#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007578#endif
7579#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007580 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007581#endif
7582#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007583 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007584#endif
7585#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007586 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007587#endif
7588#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007589 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007590#endif
7591#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007592 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007593#endif
7594#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007596#endif
7597#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007599#endif
7600#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007602#endif
7603#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007604 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007605#endif
7606#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007608#endif
Fred Draked86ed291999-12-15 15:34:33 +00007609#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007611#endif
7612#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007614#endif
7615#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007617#endif
7618#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007619 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007620#endif
7621#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007623#endif
7624#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007626#endif
7627#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007628 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007629#endif
7630#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00007631 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007632#endif
7633#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007635#endif
7636#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007637 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007638#endif
7639#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007641#endif
7642#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007643 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007644#endif
7645#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00007646 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007647#endif
Fred Drakec9680921999-12-13 16:37:25 +00007648};
7649
7650static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007651conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007652{
7653 return conv_confname(arg, valuep, posix_constants_confstr,
7654 sizeof(posix_constants_confstr)
7655 / sizeof(struct constdef));
7656}
7657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007658PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007659"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007660Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007661
7662static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007663posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007664{
7665 PyObject *result = NULL;
7666 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00007667 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00007668 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007669
Victor Stinnercb043522010-09-10 23:49:04 +00007670 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
7671 return NULL;
7672
7673 errno = 0;
7674 len = confstr(name, buffer, sizeof(buffer));
7675 if (len == 0) {
7676 if (errno) {
7677 posix_error();
7678 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00007679 }
7680 else {
Victor Stinnercb043522010-09-10 23:49:04 +00007681 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00007682 }
7683 }
Victor Stinnercb043522010-09-10 23:49:04 +00007684
7685 if ((unsigned int)len >= sizeof(buffer)) {
7686 char *buf = PyMem_Malloc(len);
7687 if (buf == NULL)
7688 return PyErr_NoMemory();
7689 confstr(name, buf, len);
7690 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
7691 PyMem_Free(buf);
7692 }
7693 else
7694 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007695 return result;
7696}
7697#endif
7698
7699
7700#ifdef HAVE_SYSCONF
7701static struct constdef posix_constants_sysconf[] = {
7702#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007703 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007704#endif
7705#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00007706 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007707#endif
7708#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007709 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007710#endif
7711#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007713#endif
7714#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007716#endif
7717#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00007718 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007719#endif
7720#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00007721 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007722#endif
7723#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007724 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007725#endif
7726#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007728#endif
7729#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007731#endif
Fred Draked86ed291999-12-15 15:34:33 +00007732#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007734#endif
7735#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00007736 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007737#endif
Fred Drakec9680921999-12-13 16:37:25 +00007738#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007739 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007740#endif
Fred Drakec9680921999-12-13 16:37:25 +00007741#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007742 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007743#endif
7744#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007745 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007746#endif
7747#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007748 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007749#endif
7750#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007752#endif
7753#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007754 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007755#endif
Fred Draked86ed291999-12-15 15:34:33 +00007756#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007758#endif
Fred Drakec9680921999-12-13 16:37:25 +00007759#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007761#endif
7762#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007764#endif
7765#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007767#endif
7768#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007770#endif
7771#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007773#endif
Fred Draked86ed291999-12-15 15:34:33 +00007774#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00007775 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007776#endif
Fred Drakec9680921999-12-13 16:37:25 +00007777#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007779#endif
7780#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007782#endif
7783#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007785#endif
7786#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007788#endif
7789#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007791#endif
7792#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007794#endif
7795#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007796 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007797#endif
7798#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007799 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007800#endif
7801#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007803#endif
7804#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007806#endif
7807#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007809#endif
7810#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007811 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007812#endif
7813#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007815#endif
7816#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007818#endif
7819#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007821#endif
7822#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007824#endif
7825#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007826 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007827#endif
7828#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007830#endif
7831#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007833#endif
7834#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007835 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007836#endif
7837#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007839#endif
7840#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007842#endif
7843#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007845#endif
Fred Draked86ed291999-12-15 15:34:33 +00007846#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007848#endif
Fred Drakec9680921999-12-13 16:37:25 +00007849#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007851#endif
7852#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007854#endif
7855#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007857#endif
Fred Draked86ed291999-12-15 15:34:33 +00007858#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007860#endif
Fred Drakec9680921999-12-13 16:37:25 +00007861#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007863#endif
Fred Draked86ed291999-12-15 15:34:33 +00007864#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007866#endif
7867#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007869#endif
Fred Drakec9680921999-12-13 16:37:25 +00007870#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007872#endif
7873#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007875#endif
7876#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007878#endif
7879#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007881#endif
Fred Draked86ed291999-12-15 15:34:33 +00007882#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007884#endif
Fred Drakec9680921999-12-13 16:37:25 +00007885#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007887#endif
7888#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007890#endif
7891#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007893#endif
7894#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007896#endif
7897#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007899#endif
7900#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007902#endif
7903#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007904 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007905#endif
Fred Draked86ed291999-12-15 15:34:33 +00007906#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007907 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007908#endif
Fred Drakec9680921999-12-13 16:37:25 +00007909#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007910 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007911#endif
7912#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007913 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007914#endif
Fred Draked86ed291999-12-15 15:34:33 +00007915#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007917#endif
Fred Drakec9680921999-12-13 16:37:25 +00007918#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007919 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007920#endif
7921#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007923#endif
7924#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007926#endif
7927#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007929#endif
7930#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007932#endif
7933#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007935#endif
7936#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007938#endif
7939#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007940 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007941#endif
7942#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007944#endif
Fred Draked86ed291999-12-15 15:34:33 +00007945#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007946 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007947#endif
7948#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007949 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007950#endif
Fred Drakec9680921999-12-13 16:37:25 +00007951#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007952 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007953#endif
7954#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007955 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007956#endif
7957#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007959#endif
7960#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007962#endif
7963#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007964 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007965#endif
7966#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007967 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007968#endif
7969#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007970 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007971#endif
7972#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007973 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007974#endif
7975#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007976 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007977#endif
7978#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007980#endif
7981#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007983#endif
7984#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007985 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007986#endif
7987#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007988 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007989#endif
7990#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007992#endif
7993#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007995#endif
7996#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007998#endif
7999#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008001#endif
8002#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008003 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
8005#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008006 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008007#endif
8008#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008009 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008010#endif
8011#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008012 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008013#endif
8014#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008016#endif
8017#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008018 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008019#endif
8020#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008022#endif
8023#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008025#endif
8026#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008028#endif
8029#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008031#endif
8032#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008034#endif
8035#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008037#endif
8038#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008040#endif
8041#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
8050#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008052#endif
8053#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008054 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008055#endif
Fred Draked86ed291999-12-15 15:34:33 +00008056#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008058#endif
Fred Drakec9680921999-12-13 16:37:25 +00008059#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008060 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008061#endif
8062#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008063 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008064#endif
8065#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008067#endif
8068#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008070#endif
8071#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008073#endif
8074#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008075 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008076#endif
8077#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008079#endif
8080#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008082#endif
8083#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008085#endif
8086#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
8092#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008093 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008094#endif
8095#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008096 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008097#endif
8098#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008099 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008100#endif
8101#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008103#endif
8104#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008105 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008106#endif
8107#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008108 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008109#endif
8110#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008111 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008112#endif
8113#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008114 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008115#endif
8116#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008117 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
8119#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008121#endif
8122#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
8128#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008130#endif
8131#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
8134#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008135 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008138 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
8146#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008148#endif
8149#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008151#endif
8152#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008153 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008154#endif
8155#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008156 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008157#endif
8158#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008159 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008160#endif
8161#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008162 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008163#endif
8164#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008165 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008166#endif
8167#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008169#endif
8170#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008171 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008172#endif
8173#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008174 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008175#endif
8176#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008178#endif
8179#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008180 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008181#endif
8182#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008183 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008184#endif
8185#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008186 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008187#endif
8188#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008189 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008190#endif
8191#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008192 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008193#endif
8194};
8195
8196static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008197conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008198{
8199 return conv_confname(arg, valuep, posix_constants_sysconf,
8200 sizeof(posix_constants_sysconf)
8201 / sizeof(struct constdef));
8202}
8203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008204PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008205"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008206Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008207
8208static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008209posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008210{
8211 PyObject *result = NULL;
8212 int name;
8213
8214 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8215 int value;
8216
8217 errno = 0;
8218 value = sysconf(name);
8219 if (value == -1 && errno != 0)
8220 posix_error();
8221 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008222 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008223 }
8224 return result;
8225}
8226#endif
8227
8228
Fred Drakebec628d1999-12-15 18:31:10 +00008229/* This code is used to ensure that the tables of configuration value names
8230 * are in sorted order as required by conv_confname(), and also to build the
8231 * the exported dictionaries that are used to publish information about the
8232 * names available on the host platform.
8233 *
8234 * Sorting the table at runtime ensures that the table is properly ordered
8235 * when used, even for platforms we're not able to test on. It also makes
8236 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008237 */
Fred Drakebec628d1999-12-15 18:31:10 +00008238
8239static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008240cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008241{
8242 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008244 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008245 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008246
8247 return strcmp(c1->name, c2->name);
8248}
8249
8250static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008251setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008253{
Fred Drakebec628d1999-12-15 18:31:10 +00008254 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008255 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008256
8257 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8258 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008259 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00008260 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008261
Barry Warsaw3155db32000-04-13 15:20:40 +00008262 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008263 PyObject *o = PyLong_FromLong(table[i].value);
8264 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8265 Py_XDECREF(o);
8266 Py_DECREF(d);
8267 return -1;
8268 }
8269 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008270 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008271 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008272}
8273
Fred Drakebec628d1999-12-15 18:31:10 +00008274/* Return -1 on failure, 0 on success. */
8275static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008276setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008277{
8278#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008279 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008280 sizeof(posix_constants_pathconf)
8281 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008282 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008283 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008284#endif
8285#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008286 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008287 sizeof(posix_constants_confstr)
8288 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008289 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008290 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008291#endif
8292#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008293 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008294 sizeof(posix_constants_sysconf)
8295 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008296 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008297 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008298#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008299 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008300}
Fred Draked86ed291999-12-15 15:34:33 +00008301
8302
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008303PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008304"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008305Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008306in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008307
8308static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008309posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008310{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008311 abort();
8312 /*NOTREACHED*/
8313 Py_FatalError("abort() called from Python code didn't abort!");
8314 return NULL;
8315}
Fred Drakebec628d1999-12-15 18:31:10 +00008316
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008317#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008318PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008319"startfile(filepath [, operation]) - Start a file with its associated\n\
8320application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008321\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008322When \"operation\" is not specified or \"open\", this acts like\n\
8323double-clicking the file in Explorer, or giving the file name as an\n\
8324argument to the DOS \"start\" command: the file is opened with whatever\n\
8325application (if any) its extension is associated.\n\
8326When another \"operation\" is given, it specifies what should be done with\n\
8327the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008328\n\
8329startfile returns as soon as the associated application is launched.\n\
8330There is no option to wait for the application to close, and no way\n\
8331to retrieve the application's exit status.\n\
8332\n\
8333The filepath is relative to the current directory. If you want to use\n\
8334an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008335the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008336
8337static PyObject *
8338win32_startfile(PyObject *self, PyObject *args)
8339{
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 PyObject *ofilepath;
8341 char *filepath;
8342 char *operation = NULL;
8343 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00008344
Victor Stinner8c62be82010-05-06 00:08:46 +00008345 PyObject *unipath, *woperation = NULL;
8346 if (!PyArg_ParseTuple(args, "U|s:startfile",
8347 &unipath, &operation)) {
8348 PyErr_Clear();
8349 goto normal;
8350 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008351
Victor Stinner8c62be82010-05-06 00:08:46 +00008352 if (operation) {
8353 woperation = PyUnicode_DecodeASCII(operation,
8354 strlen(operation), NULL);
8355 if (!woperation) {
8356 PyErr_Clear();
8357 operation = NULL;
8358 goto normal;
8359 }
8360 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008361
Victor Stinner8c62be82010-05-06 00:08:46 +00008362 Py_BEGIN_ALLOW_THREADS
8363 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8364 PyUnicode_AS_UNICODE(unipath),
8365 NULL, NULL, SW_SHOWNORMAL);
8366 Py_END_ALLOW_THREADS
8367
8368 Py_XDECREF(woperation);
8369 if (rc <= (HINSTANCE)32) {
8370 PyObject *errval = win32_error_unicode("startfile",
8371 PyUnicode_AS_UNICODE(unipath));
8372 return errval;
8373 }
8374 Py_INCREF(Py_None);
8375 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008376
8377normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00008378 if (!PyArg_ParseTuple(args, "O&|s:startfile",
8379 PyUnicode_FSConverter, &ofilepath,
8380 &operation))
8381 return NULL;
8382 filepath = PyBytes_AsString(ofilepath);
8383 Py_BEGIN_ALLOW_THREADS
8384 rc = ShellExecute((HWND)0, operation, filepath,
8385 NULL, NULL, SW_SHOWNORMAL);
8386 Py_END_ALLOW_THREADS
8387 if (rc <= (HINSTANCE)32) {
8388 PyObject *errval = win32_error("startfile", filepath);
8389 Py_DECREF(ofilepath);
8390 return errval;
8391 }
8392 Py_DECREF(ofilepath);
8393 Py_INCREF(Py_None);
8394 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008395}
8396#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008397
Martin v. Löwis438b5342002-12-27 10:16:42 +00008398#ifdef HAVE_GETLOADAVG
8399PyDoc_STRVAR(posix_getloadavg__doc__,
8400"getloadavg() -> (float, float, float)\n\n\
8401Return the number of processes in the system run queue averaged over\n\
8402the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8403was unobtainable");
8404
8405static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008406posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008407{
8408 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008409 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008410 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8411 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008412 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00008413 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008414}
8415#endif
8416
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008417#ifdef MS_WINDOWS
8418
8419PyDoc_STRVAR(win32_urandom__doc__,
8420"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008421Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008422
8423typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8424 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8425 DWORD dwFlags );
8426typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8427 BYTE *pbBuffer );
8428
8429static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00008430/* This handle is never explicitly released. Instead, the operating
8431 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008432static HCRYPTPROV hCryptProv = 0;
8433
Tim Peters4ad82172004-08-30 17:02:04 +00008434static PyObject*
8435win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008436{
Victor Stinner8c62be82010-05-06 00:08:46 +00008437 int howMany;
8438 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008439
Victor Stinner8c62be82010-05-06 00:08:46 +00008440 /* Read arguments */
8441 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8442 return NULL;
8443 if (howMany < 0)
8444 return PyErr_Format(PyExc_ValueError,
8445 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008446
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 if (hCryptProv == 0) {
8448 HINSTANCE hAdvAPI32 = NULL;
8449 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008450
Victor Stinner8c62be82010-05-06 00:08:46 +00008451 /* Obtain handle to the DLL containing CryptoAPI
8452 This should not fail */
8453 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8454 if(hAdvAPI32 == NULL)
8455 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008456
Victor Stinner8c62be82010-05-06 00:08:46 +00008457 /* Obtain pointers to the CryptoAPI functions
8458 This will fail on some early versions of Win95 */
8459 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8460 hAdvAPI32,
8461 "CryptAcquireContextA");
8462 if (pCryptAcquireContext == NULL)
8463 return PyErr_Format(PyExc_NotImplementedError,
8464 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008465
Victor Stinner8c62be82010-05-06 00:08:46 +00008466 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8467 hAdvAPI32, "CryptGenRandom");
8468 if (pCryptGenRandom == NULL)
8469 return PyErr_Format(PyExc_NotImplementedError,
8470 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008471
Victor Stinner8c62be82010-05-06 00:08:46 +00008472 /* Acquire context */
8473 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8474 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8475 return win32_error("CryptAcquireContext", NULL);
8476 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008477
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 /* Allocate bytes */
8479 result = PyBytes_FromStringAndSize(NULL, howMany);
8480 if (result != NULL) {
8481 /* Get random data */
8482 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
8483 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8484 PyBytes_AS_STRING(result))) {
8485 Py_DECREF(result);
8486 return win32_error("CryptGenRandom", NULL);
8487 }
8488 }
8489 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008490}
8491#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008492
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008493PyDoc_STRVAR(device_encoding__doc__,
8494"device_encoding(fd) -> str\n\n\
8495Return a string describing the encoding of the device\n\
8496if the output is a terminal; else return None.");
8497
8498static PyObject *
8499device_encoding(PyObject *self, PyObject *args)
8500{
Victor Stinner8c62be82010-05-06 00:08:46 +00008501 int fd;
8502 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
8503 return NULL;
8504 if (!_PyVerify_fd(fd) || !isatty(fd)) {
8505 Py_INCREF(Py_None);
8506 return Py_None;
8507 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008508#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 if (fd == 0) {
8510 char buf[100];
8511 sprintf(buf, "cp%d", GetConsoleCP());
8512 return PyUnicode_FromString(buf);
8513 }
8514 if (fd == 1 || fd == 2) {
8515 char buf[100];
8516 sprintf(buf, "cp%d", GetConsoleOutputCP());
8517 return PyUnicode_FromString(buf);
8518 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008519#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00008520 {
8521 char *codeset = nl_langinfo(CODESET);
8522 if (codeset != NULL && codeset[0] != 0)
8523 return PyUnicode_FromString(codeset);
8524 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008525#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008526 Py_INCREF(Py_None);
8527 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008528}
8529
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008530#ifdef __VMS
8531/* Use openssl random routine */
8532#include <openssl/rand.h>
8533PyDoc_STRVAR(vms_urandom__doc__,
8534"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008535Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008536
8537static PyObject*
8538vms_urandom(PyObject *self, PyObject *args)
8539{
Victor Stinner8c62be82010-05-06 00:08:46 +00008540 int howMany;
8541 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008542
Victor Stinner8c62be82010-05-06 00:08:46 +00008543 /* Read arguments */
8544 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8545 return NULL;
8546 if (howMany < 0)
8547 return PyErr_Format(PyExc_ValueError,
8548 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008549
Victor Stinner8c62be82010-05-06 00:08:46 +00008550 /* Allocate bytes */
8551 result = PyBytes_FromStringAndSize(NULL, howMany);
8552 if (result != NULL) {
8553 /* Get random data */
8554 if (RAND_pseudo_bytes((unsigned char*)
8555 PyBytes_AS_STRING(result),
8556 howMany) < 0) {
8557 Py_DECREF(result);
8558 return PyErr_Format(PyExc_ValueError,
8559 "RAND_pseudo_bytes");
8560 }
8561 }
8562 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008563}
8564#endif
8565
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008566#ifdef HAVE_SETRESUID
8567PyDoc_STRVAR(posix_setresuid__doc__,
8568"setresuid(ruid, euid, suid)\n\n\
8569Set the current process's real, effective, and saved user ids.");
8570
8571static PyObject*
8572posix_setresuid (PyObject *self, PyObject *args)
8573{
Victor Stinner8c62be82010-05-06 00:08:46 +00008574 /* We assume uid_t is no larger than a long. */
8575 long ruid, euid, suid;
8576 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
8577 return NULL;
8578 if (setresuid(ruid, euid, suid) < 0)
8579 return posix_error();
8580 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008581}
8582#endif
8583
8584#ifdef HAVE_SETRESGID
8585PyDoc_STRVAR(posix_setresgid__doc__,
8586"setresgid(rgid, egid, sgid)\n\n\
8587Set the current process's real, effective, and saved group ids.");
8588
8589static PyObject*
8590posix_setresgid (PyObject *self, PyObject *args)
8591{
Victor Stinner8c62be82010-05-06 00:08:46 +00008592 /* We assume uid_t is no larger than a long. */
8593 long rgid, egid, sgid;
8594 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
8595 return NULL;
8596 if (setresgid(rgid, egid, sgid) < 0)
8597 return posix_error();
8598 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008599}
8600#endif
8601
8602#ifdef HAVE_GETRESUID
8603PyDoc_STRVAR(posix_getresuid__doc__,
8604"getresuid() -> (ruid, euid, suid)\n\n\
8605Get tuple of the current process's real, effective, and saved user ids.");
8606
8607static PyObject*
8608posix_getresuid (PyObject *self, PyObject *noargs)
8609{
Victor Stinner8c62be82010-05-06 00:08:46 +00008610 uid_t ruid, euid, suid;
8611 long l_ruid, l_euid, l_suid;
8612 if (getresuid(&ruid, &euid, &suid) < 0)
8613 return posix_error();
8614 /* Force the values into long's as we don't know the size of uid_t. */
8615 l_ruid = ruid;
8616 l_euid = euid;
8617 l_suid = suid;
8618 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008619}
8620#endif
8621
8622#ifdef HAVE_GETRESGID
8623PyDoc_STRVAR(posix_getresgid__doc__,
8624"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00008625Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008626
8627static PyObject*
8628posix_getresgid (PyObject *self, PyObject *noargs)
8629{
Victor Stinner8c62be82010-05-06 00:08:46 +00008630 uid_t rgid, egid, sgid;
8631 long l_rgid, l_egid, l_sgid;
8632 if (getresgid(&rgid, &egid, &sgid) < 0)
8633 return posix_error();
8634 /* Force the values into long's as we don't know the size of uid_t. */
8635 l_rgid = rgid;
8636 l_egid = egid;
8637 l_sgid = sgid;
8638 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008639}
8640#endif
8641
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008642/* Posix *at family of functions:
8643 faccessat, fchmodat, fchownat, fstatat, futimesat,
8644 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
8645 unlinkat, utimensat, mkfifoat */
8646
8647#ifdef HAVE_FACCESSAT
8648PyDoc_STRVAR(posix_faccessat__doc__,
8649"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
8650Like access() but if path is relative, it is taken as relative to dirfd.\n\
8651flags is optional and can be constructed by ORing together zero or more\n\
8652of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
8653If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8654is interpreted relative to the current working directory.");
8655
8656static PyObject *
8657posix_faccessat(PyObject *self, PyObject *args)
8658{
8659 PyObject *opath;
8660 char *path;
8661 int mode;
8662 int res;
8663 int dirfd, flags = 0;
8664 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
8665 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8666 return NULL;
8667 path = PyBytes_AsString(opath);
8668 Py_BEGIN_ALLOW_THREADS
8669 res = faccessat(dirfd, path, mode, flags);
8670 Py_END_ALLOW_THREADS
8671 Py_DECREF(opath);
8672 return PyBool_FromLong(res == 0);
8673}
8674#endif
8675
8676#ifdef HAVE_FCHMODAT
8677PyDoc_STRVAR(posix_fchmodat__doc__,
8678"fchmodat(dirfd, path, mode, flags=0)\n\n\
8679Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
8680flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8681If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8682is interpreted relative to the current working directory.");
8683
8684static PyObject *
8685posix_fchmodat(PyObject *self, PyObject *args)
8686{
8687 int dirfd, mode, res;
8688 int flags = 0;
8689 PyObject *opath;
8690 char *path;
8691
8692 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
8693 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8694 return NULL;
8695
8696 path = PyBytes_AsString(opath);
8697
8698 Py_BEGIN_ALLOW_THREADS
8699 res = fchmodat(dirfd, path, mode, flags);
8700 Py_END_ALLOW_THREADS
8701 Py_DECREF(opath);
8702 if (res < 0)
8703 return posix_error();
8704 Py_RETURN_NONE;
8705}
8706#endif /* HAVE_FCHMODAT */
8707
8708#ifdef HAVE_FCHOWNAT
8709PyDoc_STRVAR(posix_fchownat__doc__,
8710"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
8711Like chown() but if path is relative, it is taken as relative to dirfd.\n\
8712flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8713If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8714is interpreted relative to the current working directory.");
8715
8716static PyObject *
8717posix_fchownat(PyObject *self, PyObject *args)
8718{
8719 PyObject *opath;
8720 int dirfd, res;
8721 long uid, gid;
8722 int flags = 0;
8723 char *path;
8724
8725 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
8726 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
8727 return NULL;
8728
8729 path = PyBytes_AsString(opath);
8730
8731 Py_BEGIN_ALLOW_THREADS
8732 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
8733 Py_END_ALLOW_THREADS
8734 Py_DECREF(opath);
8735 if (res < 0)
8736 return posix_error();
8737 Py_RETURN_NONE;
8738}
8739#endif /* HAVE_FCHOWNAT */
8740
8741#ifdef HAVE_FSTATAT
8742PyDoc_STRVAR(posix_fstatat__doc__,
8743"fstatat(dirfd, path, flags=0) -> stat result\n\n\
8744Like stat() but if path is relative, it is taken as relative to dirfd.\n\
8745flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8746If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8747is interpreted relative to the current working directory.");
8748
8749static PyObject *
8750posix_fstatat(PyObject *self, PyObject *args)
8751{
8752 PyObject *opath;
8753 char *path;
8754 STRUCT_STAT st;
8755 int dirfd, res, flags = 0;
8756
8757 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
8758 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8759 return NULL;
8760 path = PyBytes_AsString(opath);
8761
8762 Py_BEGIN_ALLOW_THREADS
8763 res = fstatat(dirfd, path, &st, flags);
8764 Py_END_ALLOW_THREADS
8765 Py_DECREF(opath);
8766 if (res != 0)
8767 return posix_error();
8768
8769 return _pystat_fromstructstat(&st);
8770}
8771#endif
8772
8773#ifdef HAVE_FUTIMESAT
8774PyDoc_STRVAR(posix_futimesat__doc__,
8775"futimesat(dirfd, path, (atime, mtime))\n\
8776futimesat(dirfd, path, None)\n\n\
8777Like utime() but if path is relative, it is taken as relative to dirfd.\n\
8778If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8779is interpreted relative to the current working directory.");
8780
8781static PyObject *
8782posix_futimesat(PyObject *self, PyObject *args)
8783{
8784 PyObject *opath;
8785 char *path;
8786 int res, dirfd;
8787 PyObject* arg;
8788
8789 struct timeval buf[2];
8790
8791 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
8792 &dirfd, PyUnicode_FSConverter, &opath, &arg))
8793 return NULL;
8794 path = PyBytes_AsString(opath);
8795 if (arg == Py_None) {
8796 /* optional time values not given */
8797 Py_BEGIN_ALLOW_THREADS
8798 res = futimesat(dirfd, path, NULL);
8799 Py_END_ALLOW_THREADS
8800 }
8801 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
8802 PyErr_SetString(PyExc_TypeError,
8803 "futimesat() arg 3 must be a tuple (atime, mtime)");
8804 Py_DECREF(opath);
8805 return NULL;
8806 }
8807 else {
8808 if (extract_time(PyTuple_GET_ITEM(arg, 0),
8809 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
8810 Py_DECREF(opath);
8811 return NULL;
8812 }
8813 if (extract_time(PyTuple_GET_ITEM(arg, 1),
8814 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
8815 Py_DECREF(opath);
8816 return NULL;
8817 }
8818 Py_BEGIN_ALLOW_THREADS
8819 res = futimesat(dirfd, path, buf);
8820 Py_END_ALLOW_THREADS
8821 }
8822 Py_DECREF(opath);
8823 if (res < 0) {
8824 return posix_error();
8825 }
8826 Py_RETURN_NONE;
8827}
8828#endif
8829
8830#ifdef HAVE_LINKAT
8831PyDoc_STRVAR(posix_linkat__doc__,
8832"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
8833Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
8834and if dstpath is relative, it is taken as relative to dstfd.\n\
8835flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
8836If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
8837srcpath is interpreted relative to the current working directory. This\n\
8838also applies for dstpath.");
8839
8840static PyObject *
8841posix_linkat(PyObject *self, PyObject *args)
8842{
8843 PyObject *osrc, *odst;
8844 char *src, *dst;
8845 int res, srcfd, dstfd;
8846 int flags = 0;
8847
8848 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
8849 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
8850 return NULL;
8851 src = PyBytes_AsString(osrc);
8852 dst = PyBytes_AsString(odst);
8853 Py_BEGIN_ALLOW_THREADS
8854 res = linkat(srcfd, src, dstfd, dst, flags);
8855 Py_END_ALLOW_THREADS
8856 Py_DECREF(osrc);
8857 Py_DECREF(odst);
8858 if (res < 0)
8859 return posix_error();
8860 Py_RETURN_NONE;
8861}
8862#endif /* HAVE_LINKAT */
8863
8864#ifdef HAVE_MKDIRAT
8865PyDoc_STRVAR(posix_mkdirat__doc__,
8866"mkdirat(dirfd, path, mode=0o777)\n\n\
8867Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
8868If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8869is interpreted relative to the current working directory.");
8870
8871static PyObject *
8872posix_mkdirat(PyObject *self, PyObject *args)
8873{
8874 int res, dirfd;
8875 PyObject *opath;
8876 char *path;
8877 int mode = 0777;
8878
8879 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
8880 &dirfd, PyUnicode_FSConverter, &opath, &mode))
8881 return NULL;
8882 path = PyBytes_AsString(opath);
8883 Py_BEGIN_ALLOW_THREADS
8884 res = mkdirat(dirfd, path, mode);
8885 Py_END_ALLOW_THREADS
8886 Py_DECREF(opath);
8887 if (res < 0)
8888 return posix_error();
8889 Py_RETURN_NONE;
8890}
8891#endif
8892
8893#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
8894PyDoc_STRVAR(posix_mknodat__doc__,
8895"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
8896Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
8897If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8898is interpreted relative to the current working directory.");
8899
8900static PyObject *
8901posix_mknodat(PyObject *self, PyObject *args)
8902{
8903 PyObject *opath;
8904 char *filename;
8905 int mode = 0600;
8906 int device = 0;
8907 int res, dirfd;
8908 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
8909 PyUnicode_FSConverter, &opath, &mode, &device))
8910 return NULL;
8911 filename = PyBytes_AS_STRING(opath);
8912 Py_BEGIN_ALLOW_THREADS
8913 res = mknodat(dirfd, filename, mode, device);
8914 Py_END_ALLOW_THREADS
8915 Py_DECREF(opath);
8916 if (res < 0)
8917 return posix_error();
8918 Py_RETURN_NONE;
8919}
8920#endif
8921
8922#ifdef HAVE_OPENAT
8923PyDoc_STRVAR(posix_openat__doc__,
8924"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
8925Like open() but if path is relative, it is taken as relative to dirfd.\n\
8926If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8927is interpreted relative to the current working directory.");
8928
8929static PyObject *
8930posix_openat(PyObject *self, PyObject *args)
8931{
8932 PyObject *ofile;
8933 char *file;
8934 int flag, dirfd, fd;
8935 int mode = 0777;
8936
8937 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
8938 &dirfd, PyUnicode_FSConverter, &ofile,
8939 &flag, &mode))
8940 return NULL;
8941 file = PyBytes_AsString(ofile);
8942 Py_BEGIN_ALLOW_THREADS
8943 fd = openat(dirfd, file, flag, mode);
8944 Py_END_ALLOW_THREADS
8945 Py_DECREF(ofile);
8946 if (fd < 0)
8947 return posix_error();
8948 return PyLong_FromLong((long)fd);
8949}
8950#endif
8951
8952#ifdef HAVE_READLINKAT
8953PyDoc_STRVAR(posix_readlinkat__doc__,
8954"readlinkat(dirfd, path) -> path\n\n\
8955Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
8956If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8957is interpreted relative to the current working directory.");
8958
8959static PyObject *
8960posix_readlinkat(PyObject *self, PyObject *args)
8961{
8962 PyObject *v, *opath;
8963 char buf[MAXPATHLEN];
8964 char *path;
8965 int n, dirfd;
8966 int arg_is_unicode = 0;
8967
8968 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
8969 &dirfd, PyUnicode_FSConverter, &opath))
8970 return NULL;
8971 path = PyBytes_AsString(opath);
8972 v = PySequence_GetItem(args, 1);
8973 if (v == NULL) {
8974 Py_DECREF(opath);
8975 return NULL;
8976 }
8977
8978 if (PyUnicode_Check(v)) {
8979 arg_is_unicode = 1;
8980 }
8981 Py_DECREF(v);
8982
8983 Py_BEGIN_ALLOW_THREADS
8984 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
8985 Py_END_ALLOW_THREADS
8986 Py_DECREF(opath);
8987 if (n < 0)
8988 return posix_error();
8989
8990 if (arg_is_unicode)
8991 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
8992 else
8993 return PyBytes_FromStringAndSize(buf, n);
8994}
8995#endif /* HAVE_READLINKAT */
8996
8997#ifdef HAVE_RENAMEAT
8998PyDoc_STRVAR(posix_renameat__doc__,
8999"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9000Like rename() but if oldpath is relative, it is taken as relative to\n\
9001olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9002If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9003oldpath is interpreted relative to the current working directory. This\n\
9004also applies for newpath.");
9005
9006static PyObject *
9007posix_renameat(PyObject *self, PyObject *args)
9008{
9009 int res;
9010 PyObject *opathold, *opathnew;
9011 char *opath, *npath;
9012 int oldfd, newfd;
9013
9014 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9015 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9016 return NULL;
9017 opath = PyBytes_AsString(opathold);
9018 npath = PyBytes_AsString(opathnew);
9019 Py_BEGIN_ALLOW_THREADS
9020 res = renameat(oldfd, opath, newfd, npath);
9021 Py_END_ALLOW_THREADS
9022 Py_DECREF(opathold);
9023 Py_DECREF(opathnew);
9024 if (res < 0)
9025 return posix_error();
9026 Py_RETURN_NONE;
9027}
9028#endif
9029
9030#if HAVE_SYMLINKAT
9031PyDoc_STRVAR(posix_symlinkat__doc__,
9032"symlinkat(src, dstfd, dst)\n\n\
9033Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9034If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9035is interpreted relative to the current working directory.");
9036
9037static PyObject *
9038posix_symlinkat(PyObject *self, PyObject *args)
9039{
9040 int res, dstfd;
9041 PyObject *osrc, *odst;
9042 char *src, *dst;
9043
9044 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9045 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9046 return NULL;
9047 src = PyBytes_AsString(osrc);
9048 dst = PyBytes_AsString(odst);
9049 Py_BEGIN_ALLOW_THREADS
9050 res = symlinkat(src, dstfd, dst);
9051 Py_END_ALLOW_THREADS
9052 Py_DECREF(osrc);
9053 Py_DECREF(odst);
9054 if (res < 0)
9055 return posix_error();
9056 Py_RETURN_NONE;
9057}
9058#endif /* HAVE_SYMLINKAT */
9059
9060#ifdef HAVE_UNLINKAT
9061PyDoc_STRVAR(posix_unlinkat__doc__,
9062"unlinkat(dirfd, path, flags=0)\n\n\
9063Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9064flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9065specified, unlinkat() behaves like rmdir().\n\
9066If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9067is interpreted relative to the current working directory.");
9068
9069static PyObject *
9070posix_unlinkat(PyObject *self, PyObject *args)
9071{
9072 int dirfd, res, flags = 0;
9073 PyObject *opath;
9074 char *path;
9075
9076 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9077 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9078 return NULL;
9079 path = PyBytes_AsString(opath);
9080 Py_BEGIN_ALLOW_THREADS
9081 res = unlinkat(dirfd, path, flags);
9082 Py_END_ALLOW_THREADS
9083 Py_DECREF(opath);
9084 if (res < 0)
9085 return posix_error();
9086 Py_RETURN_NONE;
9087}
9088#endif
9089
9090#ifdef HAVE_UTIMENSAT
9091PyDoc_STRVAR(posix_utimensat__doc__,
9092"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9093 (mtime_sec, mtime_nsec), flags)\n\
9094utimensat(dirfd, path, None, None, flags)\n\n\
9095Updates the timestamps of a file with nanosecond precision. If path is\n\
9096relative, it is taken as relative to dirfd.\n\
9097The second form sets atime and mtime to the current time.\n\
9098flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9099If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9100is interpreted relative to the current working directory.\n\
9101If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9102current time.\n\
9103If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9104
9105static PyObject *
9106posix_utimensat(PyObject *self, PyObject *args)
9107{
9108 PyObject *opath;
9109 char *path;
9110 int res, dirfd, flags = 0;
9111 PyObject *atime, *mtime;
9112
9113 struct timespec buf[2];
9114
9115 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9116 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9117 return NULL;
9118 path = PyBytes_AsString(opath);
9119 if (atime == Py_None && mtime == Py_None) {
9120 /* optional time values not given */
9121 Py_BEGIN_ALLOW_THREADS
9122 res = utimensat(dirfd, path, NULL, flags);
9123 Py_END_ALLOW_THREADS
9124 }
9125 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9126 PyErr_SetString(PyExc_TypeError,
9127 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9128 Py_DECREF(opath);
9129 return NULL;
9130 }
9131 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9132 PyErr_SetString(PyExc_TypeError,
9133 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9134 Py_DECREF(opath);
9135 return NULL;
9136 }
9137 else {
9138 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9139 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9140 Py_DECREF(opath);
9141 return NULL;
9142 }
9143 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9144 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9145 Py_DECREF(opath);
9146 return NULL;
9147 }
9148 Py_BEGIN_ALLOW_THREADS
9149 res = utimensat(dirfd, path, buf, flags);
9150 Py_END_ALLOW_THREADS
9151 }
9152 Py_DECREF(opath);
9153 if (res < 0) {
9154 return posix_error();
9155 }
9156 Py_RETURN_NONE;
9157}
9158#endif
9159
9160#ifdef HAVE_MKFIFOAT
9161PyDoc_STRVAR(posix_mkfifoat__doc__,
9162"mkfifoat(dirfd, path, mode=0o666)\n\n\
9163Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9164If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9165is interpreted relative to the current working directory.");
9166
9167static PyObject *
9168posix_mkfifoat(PyObject *self, PyObject *args)
9169{
9170 PyObject *opath;
9171 char *filename;
9172 int mode = 0666;
9173 int res, dirfd;
9174 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9175 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9176 return NULL;
9177 filename = PyBytes_AS_STRING(opath);
9178 Py_BEGIN_ALLOW_THREADS
9179 res = mkfifoat(dirfd, filename, mode);
9180 Py_END_ALLOW_THREADS
9181 Py_DECREF(opath);
9182 if (res < 0)
9183 return posix_error();
9184 Py_RETURN_NONE;
9185}
9186#endif
9187
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009188static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009189 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009190#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009191 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009192#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009193 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009194#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009195 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009196#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009197 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009198#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009199 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009200#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009201#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009202 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009203#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009204#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009205 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009206#endif /* HAVE_LCHMOD */
9207#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009208 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009209#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009210#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009211 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009212#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009213#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009214 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009215#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009216#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009218#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009219#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009220 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009221#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009222#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009223 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9224 METH_NOARGS, posix_getcwd__doc__},
9225 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9226 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009227#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009228#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009229 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009230#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009231 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009232#ifdef HAVE_FDOPENDIR
9233 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
9234#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009235 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
9236 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009237#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00009238 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009239#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009240#ifdef HAVE_GETPRIORITY
9241 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
9242#endif /* HAVE_GETPRIORITY */
9243#ifdef HAVE_SETPRIORITY
9244 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
9245#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009246#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009247 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009248#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00009249#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009250 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00009251#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009252 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
9253 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
9254 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00009255 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00009256#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00009257 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009258#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00009259#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009260 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00009261 win_symlink__doc__},
9262#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009263#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00009264 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009265#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009266 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009267#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009268 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009269#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00009270 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
9271 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
9272 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009273#ifdef HAVE_FUTIMES
9274 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
9275#endif
9276#ifdef HAVE_LUTIMES
9277 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
9278#endif
9279#ifdef HAVE_FUTIMENS
9280 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
9281#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009282#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00009283 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009284#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00009285 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009286#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00009287 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
9288 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009289#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009290#ifdef HAVE_FEXECVE
9291 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
9292#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00009293#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00009294 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
9295 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009296#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00009297 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
9298 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009299#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00009300#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009301#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00009302 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009303#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009304#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009306#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009307#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00009308 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009309#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00009310#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009311 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00009312#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009313#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009314 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009315#endif /* HAVE_GETEGID */
9316#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009318#endif /* HAVE_GETEUID */
9319#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009320 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009321#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00009322#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009323 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009324#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009325 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009326#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009327 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009328#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009329#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009330 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009331#endif /* HAVE_GETPPID */
9332#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009333 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009334#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00009335#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009336 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00009337#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00009338#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00009339 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009340#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009341#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00009342 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009343#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00009344#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009345 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00009346#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00009347#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009348 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
9349 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00009350 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00009351#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009352#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009353 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009354#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009355#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009356 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009357#endif /* HAVE_SETEUID */
9358#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009359 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009360#endif /* HAVE_SETEGID */
9361#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009363#endif /* HAVE_SETREUID */
9364#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009365 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009366#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009367#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009368 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009369#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009370#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009371 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009372#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00009373#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009374 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00009375#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00009376#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00009378#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009379#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009380 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009381#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009382#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009383 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009384#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009385#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00009386 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009387#endif /* HAVE_WAIT3 */
9388#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00009389 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009390#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009391#if defined(HAVE_WAITID) && !defined(__APPLE__)
9392 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
9393#endif
Tim Petersab034fa2002-02-01 11:27:43 +00009394#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009395 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009396#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009397#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009398 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009399#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009400#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009401 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009402#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009403#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009405#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009406#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009407 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009408#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009409#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009410 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009411#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00009412 {"open", posix_open, METH_VARARGS, posix_open__doc__},
9413 {"close", posix_close, METH_VARARGS, posix_close__doc__},
9414 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
9415 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
9416 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
9417 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009418#ifdef HAVE_LOCKF
9419 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
9420#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009421 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
9422 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009423#ifdef HAVE_READV
9424 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
9425#endif
9426#ifdef HAVE_PREAD
9427 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
9428#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009429 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009430#ifdef HAVE_WRITEV
9431 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
9432#endif
9433#ifdef HAVE_PWRITE
9434 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
9435#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009436#ifdef HAVE_SENDFILE
9437 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
9438 posix_sendfile__doc__},
9439#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009440 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
9441 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009442#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009443 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009444#endif
9445#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00009446 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009447#endif
Neal Norwitz11690112002-07-30 01:08:28 +00009448#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00009449 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00009450#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009451#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00009452 {"major", posix_major, METH_VARARGS, posix_major__doc__},
9453 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
9454 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009455#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009456#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00009457 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009458#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009459#ifdef HAVE_TRUNCATE
9460 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
9461#endif
9462#ifdef HAVE_POSIX_FALLOCATE
9463 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
9464#endif
9465#ifdef HAVE_POSIX_FADVISE
9466 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
9467#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009468#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009469 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009470#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009471#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009472 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00009473#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009474 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009475#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00009476 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009477#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009478#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009479 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009480#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009481#ifdef HAVE_SYNC
9482 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
9483#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009484#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009485 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009486#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00009487#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009488#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00009489 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00009490#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009491#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009492 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009493#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00009494#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00009495 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009496#endif /* WIFSTOPPED */
9497#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00009498 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009499#endif /* WIFSIGNALED */
9500#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00009501 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009502#endif /* WIFEXITED */
9503#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00009504 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009505#endif /* WEXITSTATUS */
9506#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009507 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009508#endif /* WTERMSIG */
9509#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009510 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009511#endif /* WSTOPSIG */
9512#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00009513#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009514 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009515#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00009516#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009517 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009518#endif
Fred Drakec9680921999-12-13 16:37:25 +00009519#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00009520 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009521#endif
9522#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009523 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009524#endif
9525#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009526 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009527#endif
9528#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009529 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009530#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009531 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009532#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009533 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00009534 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00009535 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00009536#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009537#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00009538 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00009539#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009540 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009541 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009542 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009543 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00009544 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009545 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009546#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009547 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009548#endif
9549#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009550 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009551#endif
9552#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009553 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009554#endif
9555#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009556 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009557#endif
9558
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009559/* posix *at family of functions */
9560#ifdef HAVE_FACCESSAT
9561 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
9562#endif
9563#ifdef HAVE_FCHMODAT
9564 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
9565#endif /* HAVE_FCHMODAT */
9566#ifdef HAVE_FCHOWNAT
9567 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
9568#endif /* HAVE_FCHOWNAT */
9569#ifdef HAVE_FSTATAT
9570 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
9571#endif
9572#ifdef HAVE_FUTIMESAT
9573 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
9574#endif
9575#ifdef HAVE_LINKAT
9576 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
9577#endif /* HAVE_LINKAT */
9578#ifdef HAVE_MKDIRAT
9579 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
9580#endif
9581#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9582 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
9583#endif
9584#ifdef HAVE_OPENAT
9585 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
9586#endif
9587#ifdef HAVE_READLINKAT
9588 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
9589#endif /* HAVE_READLINKAT */
9590#ifdef HAVE_RENAMEAT
9591 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
9592#endif
9593#if HAVE_SYMLINKAT
9594 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
9595#endif /* HAVE_SYMLINKAT */
9596#ifdef HAVE_UNLINKAT
9597 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
9598#endif
9599#ifdef HAVE_UTIMENSAT
9600 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
9601#endif
9602#ifdef HAVE_MKFIFOAT
9603 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
9604#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009605 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009606};
9607
9608
Barry Warsaw4a342091996-12-19 23:50:02 +00009609static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009610ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009611{
Victor Stinner8c62be82010-05-06 00:08:46 +00009612 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009613}
9614
Guido van Rossumd48f2521997-12-05 22:19:34 +00009615#if defined(PYOS_OS2)
9616/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009617static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009618{
9619 APIRET rc;
9620 ULONG values[QSV_MAX+1];
9621 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009622 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009623
9624 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009625 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009626 Py_END_ALLOW_THREADS
9627
9628 if (rc != NO_ERROR) {
9629 os2_error(rc);
9630 return -1;
9631 }
9632
Fred Drake4d1e64b2002-04-15 19:40:07 +00009633 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9634 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9635 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9636 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9637 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9638 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9639 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009640
9641 switch (values[QSV_VERSION_MINOR]) {
9642 case 0: ver = "2.00"; break;
9643 case 10: ver = "2.10"; break;
9644 case 11: ver = "2.11"; break;
9645 case 30: ver = "3.00"; break;
9646 case 40: ver = "4.00"; break;
9647 case 50: ver = "5.00"; break;
9648 default:
Tim Peters885d4572001-11-28 20:27:42 +00009649 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00009650 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009651 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009652 ver = &tmp[0];
9653 }
9654
9655 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009656 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009657 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009658
9659 /* Add Indicator of Which Drive was Used to Boot the System */
9660 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9661 tmp[1] = ':';
9662 tmp[2] = '\0';
9663
Fred Drake4d1e64b2002-04-15 19:40:07 +00009664 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009665}
9666#endif
9667
Brian Curtin52173d42010-12-02 18:29:18 +00009668#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009669static int
Brian Curtin52173d42010-12-02 18:29:18 +00009670enable_symlink()
9671{
9672 HANDLE tok;
9673 TOKEN_PRIVILEGES tok_priv;
9674 LUID luid;
9675 int meth_idx = 0;
9676
9677 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009678 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009679
9680 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009681 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009682
9683 tok_priv.PrivilegeCount = 1;
9684 tok_priv.Privileges[0].Luid = luid;
9685 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
9686
9687 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
9688 sizeof(TOKEN_PRIVILEGES),
9689 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009690 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009691
Brian Curtin3b4499c2010-12-28 14:31:47 +00009692 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
9693 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00009694}
9695#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
9696
Barry Warsaw4a342091996-12-19 23:50:02 +00009697static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009698all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009699{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009700#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009701 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009702#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009703#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009704 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009705#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009706#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009707 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009708#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009709#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009710 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009711#endif
Fred Drakec9680921999-12-13 16:37:25 +00009712#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009713 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009714#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009715#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009716 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009717#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009718#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009719 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009720#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009721#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00009722 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009723#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009724#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00009725 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009726#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009727#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009728 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009729#endif
9730#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009731 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009732#endif
9733#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00009734 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009735#endif
9736#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00009737 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009738#endif
9739#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009740 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009741#endif
9742#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00009743 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009744#endif
9745#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009746 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009747#endif
9748#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009749 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009750#endif
9751#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009752 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009753#endif
9754#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009755 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009756#endif
9757#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009758 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009759#endif
9760#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00009761 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009762#endif
9763#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009764 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009765#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009766#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009767 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009768#endif
9769#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00009770 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009771#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009772#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009773 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009774#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009775#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009776 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009777#endif
9778#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009779 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009780#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009781#ifdef PRIO_PROCESS
9782 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
9783#endif
9784#ifdef PRIO_PGRP
9785 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
9786#endif
9787#ifdef PRIO_USER
9788 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
9789#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009790/* posix - constants for *at functions */
9791#ifdef AT_SYMLINK_NOFOLLOW
9792 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
9793#endif
9794#ifdef AT_EACCESS
9795 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
9796#endif
9797#ifdef AT_FDCWD
9798 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
9799#endif
9800#ifdef AT_REMOVEDIR
9801 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
9802#endif
9803#ifdef AT_SYMLINK_FOLLOW
9804 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
9805#endif
9806#ifdef UTIME_NOW
9807 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
9808#endif
9809#ifdef UTIME_OMIT
9810 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
9811#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009812
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009813
Tim Peters5aa91602002-01-30 05:46:57 +00009814/* MS Windows */
9815#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009816 /* Don't inherit in child processes. */
9817 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009818#endif
9819#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00009820 /* Optimize for short life (keep in memory). */
9821 /* MS forgot to define this one with a non-underscore form too. */
9822 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009823#endif
9824#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009825 /* Automatically delete when last handle is closed. */
9826 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009827#endif
9828#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00009829 /* Optimize for random access. */
9830 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009831#endif
9832#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009833 /* Optimize for sequential access. */
9834 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009835#endif
9836
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009837/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009838#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009839 /* Send a SIGIO signal whenever input or output
9840 becomes available on file descriptor */
9841 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009842#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009843#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009844 /* Direct disk access. */
9845 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009846#endif
9847#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00009848 /* Must be a directory. */
9849 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009850#endif
9851#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00009852 /* Do not follow links. */
9853 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009854#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009855#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009856 /* Do not update the access time. */
9857 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009858#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009859
Victor Stinner8c62be82010-05-06 00:08:46 +00009860 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009861#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009862 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009863#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009864#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009865 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009866#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009867#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009868 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009869#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009870#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009871 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009872#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009873#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00009874 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009875#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009876#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00009877 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009878#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009879#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009880 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009881#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009882#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00009883 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009884#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009885#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009886 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009887#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009888#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009889 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009890#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009891#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009892 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009893#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009894#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009895 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009896#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009897#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00009898 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009899#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009900#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00009901 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009902#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009903#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009904 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009905#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009906#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009907 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009908#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009909#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00009910 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009911#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009912
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009913 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009914#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009915 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009916#endif /* ST_RDONLY */
9917#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009918 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009919#endif /* ST_NOSUID */
9920
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009921 /* FreeBSD sendfile() constants */
9922#ifdef SF_NODISKIO
9923 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
9924#endif
9925#ifdef SF_MNOWAIT
9926 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
9927#endif
9928#ifdef SF_SYNC
9929 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
9930#endif
9931
Ross Lagerwall7807c352011-03-17 20:20:30 +02009932 /* constants for posix_fadvise */
9933#ifdef POSIX_FADV_NORMAL
9934 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
9935#endif
9936#ifdef POSIX_FADV_SEQUENTIAL
9937 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
9938#endif
9939#ifdef POSIX_FADV_RANDOM
9940 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
9941#endif
9942#ifdef POSIX_FADV_NOREUSE
9943 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
9944#endif
9945#ifdef POSIX_FADV_WILLNEED
9946 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
9947#endif
9948#ifdef POSIX_FADV_DONTNEED
9949 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
9950#endif
9951
9952 /* constants for waitid */
9953#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
9954 if (ins(d, "P_PID", (long)P_PID)) return -1;
9955 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
9956 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
9957#endif
9958#ifdef WEXITED
9959 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
9960#endif
9961#ifdef WNOWAIT
9962 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
9963#endif
9964#ifdef WSTOPPED
9965 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
9966#endif
9967#ifdef CLD_EXITED
9968 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
9969#endif
9970#ifdef CLD_DUMPED
9971 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
9972#endif
9973#ifdef CLD_TRAPPED
9974 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
9975#endif
9976#ifdef CLD_CONTINUED
9977 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
9978#endif
9979
9980 /* constants for lockf */
9981#ifdef F_LOCK
9982 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
9983#endif
9984#ifdef F_TLOCK
9985 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
9986#endif
9987#ifdef F_ULOCK
9988 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
9989#endif
9990#ifdef F_TEST
9991 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
9992#endif
9993
9994 /* constants for futimens */
9995#ifdef UTIME_NOW
9996 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
9997#endif
9998#ifdef UTIME_OMIT
9999 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10000#endif
10001
Guido van Rossum246bc171999-02-01 23:54:31 +000010002#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010003#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010004 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10005 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10006 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10007 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10008 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10009 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10010 if (ins(d, "P_PM", (long)P_PM)) return -1;
10011 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10012 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10013 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10014 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10015 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10016 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10017 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10018 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10019 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10020 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10021 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10022 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10023 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010024#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010025 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10026 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10027 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10028 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10029 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010030#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010031#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010032
Guido van Rossumd48f2521997-12-05 22:19:34 +000010033#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010034 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010035#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010036 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010037}
10038
10039
Tim Peters5aa91602002-01-30 05:46:57 +000010040#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010041#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010042#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010043
10044#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010045#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010046#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010048#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010049#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010050#define MODNAME "posix"
10051#endif
10052
Martin v. Löwis1a214512008-06-11 05:26:20 +000010053static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010054 PyModuleDef_HEAD_INIT,
10055 MODNAME,
10056 posix__doc__,
10057 -1,
10058 posix_methods,
10059 NULL,
10060 NULL,
10061 NULL,
10062 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010063};
10064
10065
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010066PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010067INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010068{
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010070
Brian Curtin52173d42010-12-02 18:29:18 +000010071#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010072 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010073#endif
10074
Victor Stinner8c62be82010-05-06 00:08:46 +000010075 m = PyModule_Create(&posixmodule);
10076 if (m == NULL)
10077 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010078
Victor Stinner8c62be82010-05-06 00:08:46 +000010079 /* Initialize environ dictionary */
10080 v = convertenviron();
10081 Py_XINCREF(v);
10082 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10083 return NULL;
10084 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010085
Victor Stinner8c62be82010-05-06 00:08:46 +000010086 if (all_ins(m))
10087 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010088
Victor Stinner8c62be82010-05-06 00:08:46 +000010089 if (setup_confname_tables(m))
10090 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010091
Victor Stinner8c62be82010-05-06 00:08:46 +000010092 Py_INCREF(PyExc_OSError);
10093 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010094
Guido van Rossumb3d39562000-01-31 18:41:26 +000010095#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010096 if (posix_putenv_garbage == NULL)
10097 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010098#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010099
Victor Stinner8c62be82010-05-06 00:08:46 +000010100 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010101#if defined(HAVE_WAITID) && !defined(__APPLE__)
10102 waitid_result_desc.name = MODNAME ".waitid_result";
10103 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10104#endif
10105
Victor Stinner8c62be82010-05-06 00:08:46 +000010106 stat_result_desc.name = MODNAME ".stat_result";
10107 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10108 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10109 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10110 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10111 structseq_new = StatResultType.tp_new;
10112 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010113
Victor Stinner8c62be82010-05-06 00:08:46 +000010114 statvfs_result_desc.name = MODNAME ".statvfs_result";
10115 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010116#ifdef NEED_TICKS_PER_SECOND
10117# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010118 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010119# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010120 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010121# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010122 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010123# endif
10124#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010125 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010126#if defined(HAVE_WAITID) && !defined(__APPLE__)
10127 Py_INCREF((PyObject*) &WaitidResultType);
10128 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10129#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010130 Py_INCREF((PyObject*) &StatResultType);
10131 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10132 Py_INCREF((PyObject*) &StatVFSResultType);
10133 PyModule_AddObject(m, "statvfs_result",
10134 (PyObject*) &StatVFSResultType);
10135 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010136
10137#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010138 /*
10139 * Step 2 of weak-linking support on Mac OS X.
10140 *
10141 * The code below removes functions that are not available on the
10142 * currently active platform.
10143 *
10144 * This block allow one to use a python binary that was build on
10145 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10146 * OSX 10.4.
10147 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010148#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010149 if (fstatvfs == NULL) {
10150 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10151 return NULL;
10152 }
10153 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010154#endif /* HAVE_FSTATVFS */
10155
10156#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010157 if (statvfs == NULL) {
10158 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10159 return NULL;
10160 }
10161 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010162#endif /* HAVE_STATVFS */
10163
10164# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010165 if (lchown == NULL) {
10166 if (PyObject_DelAttrString(m, "lchown") == -1) {
10167 return NULL;
10168 }
10169 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010170#endif /* HAVE_LCHOWN */
10171
10172
10173#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000010174 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010175
Guido van Rossumb6775db1994-08-01 11:34:53 +000010176}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010177
10178#ifdef __cplusplus
10179}
10180#endif