blob: 8f17bf41348fe30f7492b22163a14739038ac66e [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++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003830 PyObject* item = PySequence_ITEM(argv, i);
3831 if (item == NULL)
3832 goto fail;
3833 if (!fsconvert_strdup(item, &argvlist[i])) {
3834 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003835 goto fail;
3836 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003837 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003838 }
3839 argvlist[*argc] = NULL;
3840 return argvlist;
3841fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003842 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003843 free_string_array(argvlist, *argc);
3844 return NULL;
3845}
3846#endif
3847
3848#ifdef HAVE_EXECV
3849PyDoc_STRVAR(posix_execv__doc__,
3850"execv(path, args)\n\n\
3851Execute an executable path with arguments, replacing current process.\n\
3852\n\
3853 path: path of executable file\n\
3854 args: tuple or list of strings");
3855
3856static PyObject *
3857posix_execv(PyObject *self, PyObject *args)
3858{
3859 PyObject *opath;
3860 char *path;
3861 PyObject *argv;
3862 char **argvlist;
3863 Py_ssize_t argc;
3864
3865 /* execv has two arguments: (path, argv), where
3866 argv is a list or tuple of strings. */
3867
3868 if (!PyArg_ParseTuple(args, "O&O:execv",
3869 PyUnicode_FSConverter,
3870 &opath, &argv))
3871 return NULL;
3872 path = PyBytes_AsString(opath);
3873 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3874 PyErr_SetString(PyExc_TypeError,
3875 "execv() arg 2 must be a tuple or list");
3876 Py_DECREF(opath);
3877 return NULL;
3878 }
3879 argc = PySequence_Size(argv);
3880 if (argc < 1) {
3881 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3882 Py_DECREF(opath);
3883 return NULL;
3884 }
3885
3886 argvlist = parse_arglist(argv, &argc);
3887 if (argvlist == NULL) {
3888 Py_DECREF(opath);
3889 return NULL;
3890 }
3891
3892 execv(path, argvlist);
3893
3894 /* If we get here it's definitely an error */
3895
3896 free_string_array(argvlist, argc);
3897 Py_DECREF(opath);
3898 return posix_error();
3899}
3900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003901PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003902"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003903Execute a path with arguments and environment, replacing current process.\n\
3904\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003905 path: path of executable file\n\
3906 args: tuple or list of arguments\n\
3907 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003908
Barry Warsaw53699e91996-12-10 23:23:01 +00003909static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003910posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003911{
Victor Stinner8c62be82010-05-06 00:08:46 +00003912 PyObject *opath;
3913 char *path;
3914 PyObject *argv, *env;
3915 char **argvlist;
3916 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003917 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003918
Victor Stinner8c62be82010-05-06 00:08:46 +00003919 /* execve has three arguments: (path, argv, env), where
3920 argv is a list or tuple of strings and env is a dictionary
3921 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003922
Victor Stinner8c62be82010-05-06 00:08:46 +00003923 if (!PyArg_ParseTuple(args, "O&OO:execve",
3924 PyUnicode_FSConverter,
3925 &opath, &argv, &env))
3926 return NULL;
3927 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003928 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003929 PyErr_SetString(PyExc_TypeError,
3930 "execve() arg 2 must be a tuple or list");
3931 goto fail_0;
3932 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003933 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00003934 if (!PyMapping_Check(env)) {
3935 PyErr_SetString(PyExc_TypeError,
3936 "execve() arg 3 must be a mapping object");
3937 goto fail_0;
3938 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003939
Ross Lagerwall7807c352011-03-17 20:20:30 +02003940 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00003941 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003942 goto fail_0;
3943 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003944
Victor Stinner8c62be82010-05-06 00:08:46 +00003945 envlist = parse_envlist(env, &envc);
3946 if (envlist == NULL)
3947 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003948
Victor Stinner8c62be82010-05-06 00:08:46 +00003949 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003950
Victor Stinner8c62be82010-05-06 00:08:46 +00003951 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003952
Victor Stinner8c62be82010-05-06 00:08:46 +00003953 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003954
Victor Stinner8c62be82010-05-06 00:08:46 +00003955 while (--envc >= 0)
3956 PyMem_DEL(envlist[envc]);
3957 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003958 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02003959 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003960 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003961 Py_DECREF(opath);
3962 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003963}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003964#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003965
Ross Lagerwall7807c352011-03-17 20:20:30 +02003966#ifdef HAVE_FEXECVE
3967PyDoc_STRVAR(posix_fexecve__doc__,
3968"fexecve(fd, args, env)\n\n\
3969Execute the program specified by a file descriptor with arguments and\n\
3970environment, replacing the current process.\n\
3971\n\
3972 fd: file descriptor of executable\n\
3973 args: tuple or list of arguments\n\
3974 env: dictionary of strings mapping to strings");
3975
3976static PyObject *
3977posix_fexecve(PyObject *self, PyObject *args)
3978{
3979 int fd;
3980 PyObject *argv, *env;
3981 char **argvlist;
3982 char **envlist;
3983 Py_ssize_t argc, envc;
3984
3985 if (!PyArg_ParseTuple(args, "iOO:fexecve",
3986 &fd, &argv, &env))
3987 return NULL;
3988 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3989 PyErr_SetString(PyExc_TypeError,
3990 "fexecve() arg 2 must be a tuple or list");
3991 return NULL;
3992 }
3993 argc = PySequence_Size(argv);
3994 if (!PyMapping_Check(env)) {
3995 PyErr_SetString(PyExc_TypeError,
3996 "fexecve() arg 3 must be a mapping object");
3997 return NULL;
3998 }
3999
4000 argvlist = parse_arglist(argv, &argc);
4001 if (argvlist == NULL)
4002 return NULL;
4003
4004 envlist = parse_envlist(env, &envc);
4005 if (envlist == NULL)
4006 goto fail;
4007
4008 fexecve(fd, argvlist, envlist);
4009
4010 /* If we get here it's definitely an error */
4011
4012 (void) posix_error();
4013
4014 while (--envc >= 0)
4015 PyMem_DEL(envlist[envc]);
4016 PyMem_DEL(envlist);
4017 fail:
4018 free_string_array(argvlist, argc);
4019 return NULL;
4020}
4021#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004022
Guido van Rossuma1065681999-01-25 23:20:23 +00004023#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004024PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004025"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004026Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004027\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004028 mode: mode of process creation\n\
4029 path: path of executable file\n\
4030 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004031
4032static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004033posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004034{
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 PyObject *opath;
4036 char *path;
4037 PyObject *argv;
4038 char **argvlist;
4039 int mode, i;
4040 Py_ssize_t argc;
4041 Py_intptr_t spawnval;
4042 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004043
Victor Stinner8c62be82010-05-06 00:08:46 +00004044 /* spawnv has three arguments: (mode, path, argv), where
4045 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004046
Victor Stinner8c62be82010-05-06 00:08:46 +00004047 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4048 PyUnicode_FSConverter,
4049 &opath, &argv))
4050 return NULL;
4051 path = PyBytes_AsString(opath);
4052 if (PyList_Check(argv)) {
4053 argc = PyList_Size(argv);
4054 getitem = PyList_GetItem;
4055 }
4056 else if (PyTuple_Check(argv)) {
4057 argc = PyTuple_Size(argv);
4058 getitem = PyTuple_GetItem;
4059 }
4060 else {
4061 PyErr_SetString(PyExc_TypeError,
4062 "spawnv() arg 2 must be a tuple or list");
4063 Py_DECREF(opath);
4064 return NULL;
4065 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004066
Victor Stinner8c62be82010-05-06 00:08:46 +00004067 argvlist = PyMem_NEW(char *, argc+1);
4068 if (argvlist == NULL) {
4069 Py_DECREF(opath);
4070 return PyErr_NoMemory();
4071 }
4072 for (i = 0; i < argc; i++) {
4073 if (!fsconvert_strdup((*getitem)(argv, i),
4074 &argvlist[i])) {
4075 free_string_array(argvlist, i);
4076 PyErr_SetString(
4077 PyExc_TypeError,
4078 "spawnv() arg 2 must contain only strings");
4079 Py_DECREF(opath);
4080 return NULL;
4081 }
4082 }
4083 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004084
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004085#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004086 Py_BEGIN_ALLOW_THREADS
4087 spawnval = spawnv(mode, path, argvlist);
4088 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004089#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004090 if (mode == _OLD_P_OVERLAY)
4091 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004092
Victor Stinner8c62be82010-05-06 00:08:46 +00004093 Py_BEGIN_ALLOW_THREADS
4094 spawnval = _spawnv(mode, path, argvlist);
4095 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004096#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004097
Victor Stinner8c62be82010-05-06 00:08:46 +00004098 free_string_array(argvlist, argc);
4099 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004100
Victor Stinner8c62be82010-05-06 00:08:46 +00004101 if (spawnval == -1)
4102 return posix_error();
4103 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004104#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004105 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004106#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004107 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004108#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004109}
4110
4111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004112PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004113"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004114Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004115\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004116 mode: mode of process creation\n\
4117 path: path of executable file\n\
4118 args: tuple or list of arguments\n\
4119 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004120
4121static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004122posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004123{
Victor Stinner8c62be82010-05-06 00:08:46 +00004124 PyObject *opath;
4125 char *path;
4126 PyObject *argv, *env;
4127 char **argvlist;
4128 char **envlist;
4129 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004130 int mode;
4131 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004132 Py_intptr_t spawnval;
4133 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4134 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004135
Victor Stinner8c62be82010-05-06 00:08:46 +00004136 /* spawnve has four arguments: (mode, path, argv, env), where
4137 argv is a list or tuple of strings and env is a dictionary
4138 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004139
Victor Stinner8c62be82010-05-06 00:08:46 +00004140 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4141 PyUnicode_FSConverter,
4142 &opath, &argv, &env))
4143 return NULL;
4144 path = PyBytes_AsString(opath);
4145 if (PyList_Check(argv)) {
4146 argc = PyList_Size(argv);
4147 getitem = PyList_GetItem;
4148 }
4149 else if (PyTuple_Check(argv)) {
4150 argc = PyTuple_Size(argv);
4151 getitem = PyTuple_GetItem;
4152 }
4153 else {
4154 PyErr_SetString(PyExc_TypeError,
4155 "spawnve() arg 2 must be a tuple or list");
4156 goto fail_0;
4157 }
4158 if (!PyMapping_Check(env)) {
4159 PyErr_SetString(PyExc_TypeError,
4160 "spawnve() arg 3 must be a mapping object");
4161 goto fail_0;
4162 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004163
Victor Stinner8c62be82010-05-06 00:08:46 +00004164 argvlist = PyMem_NEW(char *, argc+1);
4165 if (argvlist == NULL) {
4166 PyErr_NoMemory();
4167 goto fail_0;
4168 }
4169 for (i = 0; i < argc; i++) {
4170 if (!fsconvert_strdup((*getitem)(argv, i),
4171 &argvlist[i]))
4172 {
4173 lastarg = i;
4174 goto fail_1;
4175 }
4176 }
4177 lastarg = argc;
4178 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004179
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 envlist = parse_envlist(env, &envc);
4181 if (envlist == NULL)
4182 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004183
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004184#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004185 Py_BEGIN_ALLOW_THREADS
4186 spawnval = spawnve(mode, path, argvlist, envlist);
4187 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004188#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004189 if (mode == _OLD_P_OVERLAY)
4190 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004191
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 Py_BEGIN_ALLOW_THREADS
4193 spawnval = _spawnve(mode, path, argvlist, envlist);
4194 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004195#endif
Tim Peters25059d32001-12-07 20:35:43 +00004196
Victor Stinner8c62be82010-05-06 00:08:46 +00004197 if (spawnval == -1)
4198 (void) posix_error();
4199 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004200#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004201 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004202#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004203 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004204#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004205
Victor Stinner8c62be82010-05-06 00:08:46 +00004206 while (--envc >= 0)
4207 PyMem_DEL(envlist[envc]);
4208 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004209 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004210 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004211 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004212 Py_DECREF(opath);
4213 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004214}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004215
4216/* OS/2 supports spawnvp & spawnvpe natively */
4217#if defined(PYOS_OS2)
4218PyDoc_STRVAR(posix_spawnvp__doc__,
4219"spawnvp(mode, file, args)\n\n\
4220Execute the program 'file' in a new process, using the environment\n\
4221search path to find the file.\n\
4222\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004223 mode: mode of process creation\n\
4224 file: executable file name\n\
4225 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004226
4227static PyObject *
4228posix_spawnvp(PyObject *self, PyObject *args)
4229{
Victor Stinner8c62be82010-05-06 00:08:46 +00004230 PyObject *opath;
4231 char *path;
4232 PyObject *argv;
4233 char **argvlist;
4234 int mode, i, argc;
4235 Py_intptr_t spawnval;
4236 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004237
Victor Stinner8c62be82010-05-06 00:08:46 +00004238 /* spawnvp has three arguments: (mode, path, argv), where
4239 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004240
Victor Stinner8c62be82010-05-06 00:08:46 +00004241 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4242 PyUnicode_FSConverter,
4243 &opath, &argv))
4244 return NULL;
4245 path = PyBytes_AsString(opath);
4246 if (PyList_Check(argv)) {
4247 argc = PyList_Size(argv);
4248 getitem = PyList_GetItem;
4249 }
4250 else if (PyTuple_Check(argv)) {
4251 argc = PyTuple_Size(argv);
4252 getitem = PyTuple_GetItem;
4253 }
4254 else {
4255 PyErr_SetString(PyExc_TypeError,
4256 "spawnvp() arg 2 must be a tuple or list");
4257 Py_DECREF(opath);
4258 return NULL;
4259 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004260
Victor Stinner8c62be82010-05-06 00:08:46 +00004261 argvlist = PyMem_NEW(char *, argc+1);
4262 if (argvlist == NULL) {
4263 Py_DECREF(opath);
4264 return PyErr_NoMemory();
4265 }
4266 for (i = 0; i < argc; i++) {
4267 if (!fsconvert_strdup((*getitem)(argv, i),
4268 &argvlist[i])) {
4269 free_string_array(argvlist, i);
4270 PyErr_SetString(
4271 PyExc_TypeError,
4272 "spawnvp() arg 2 must contain only strings");
4273 Py_DECREF(opath);
4274 return NULL;
4275 }
4276 }
4277 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004278
Victor Stinner8c62be82010-05-06 00:08:46 +00004279 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004280#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004281 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004282#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004283 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004284#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004285 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004286
Victor Stinner8c62be82010-05-06 00:08:46 +00004287 free_string_array(argvlist, argc);
4288 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004289
Victor Stinner8c62be82010-05-06 00:08:46 +00004290 if (spawnval == -1)
4291 return posix_error();
4292 else
4293 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004294}
4295
4296
4297PyDoc_STRVAR(posix_spawnvpe__doc__,
4298"spawnvpe(mode, file, args, env)\n\n\
4299Execute the program 'file' in a new process, using the environment\n\
4300search path to find the file.\n\
4301\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 mode: mode of process creation\n\
4303 file: executable file name\n\
4304 args: tuple or list of arguments\n\
4305 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004306
4307static PyObject *
4308posix_spawnvpe(PyObject *self, PyObject *args)
4309{
Victor Stinner8c62be82010-05-06 00:08:46 +00004310 PyObject *opath
4311 char *path;
4312 PyObject *argv, *env;
4313 char **argvlist;
4314 char **envlist;
4315 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004316 int mode;
4317 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004318 Py_intptr_t spawnval;
4319 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4320 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004321
Victor Stinner8c62be82010-05-06 00:08:46 +00004322 /* spawnvpe has four arguments: (mode, path, argv, env), where
4323 argv is a list or tuple of strings and env is a dictionary
4324 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004325
Victor Stinner8c62be82010-05-06 00:08:46 +00004326 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4327 PyUnicode_FSConverter,
4328 &opath, &argv, &env))
4329 return NULL;
4330 path = PyBytes_AsString(opath);
4331 if (PyList_Check(argv)) {
4332 argc = PyList_Size(argv);
4333 getitem = PyList_GetItem;
4334 }
4335 else if (PyTuple_Check(argv)) {
4336 argc = PyTuple_Size(argv);
4337 getitem = PyTuple_GetItem;
4338 }
4339 else {
4340 PyErr_SetString(PyExc_TypeError,
4341 "spawnvpe() arg 2 must be a tuple or list");
4342 goto fail_0;
4343 }
4344 if (!PyMapping_Check(env)) {
4345 PyErr_SetString(PyExc_TypeError,
4346 "spawnvpe() arg 3 must be a mapping object");
4347 goto fail_0;
4348 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004349
Victor Stinner8c62be82010-05-06 00:08:46 +00004350 argvlist = PyMem_NEW(char *, argc+1);
4351 if (argvlist == NULL) {
4352 PyErr_NoMemory();
4353 goto fail_0;
4354 }
4355 for (i = 0; i < argc; i++) {
4356 if (!fsconvert_strdup((*getitem)(argv, i),
4357 &argvlist[i]))
4358 {
4359 lastarg = i;
4360 goto fail_1;
4361 }
4362 }
4363 lastarg = argc;
4364 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004365
Victor Stinner8c62be82010-05-06 00:08:46 +00004366 envlist = parse_envlist(env, &envc);
4367 if (envlist == NULL)
4368 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004369
Victor Stinner8c62be82010-05-06 00:08:46 +00004370 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004371#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004372 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004373#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004374 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004375#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004376 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004377
Victor Stinner8c62be82010-05-06 00:08:46 +00004378 if (spawnval == -1)
4379 (void) posix_error();
4380 else
4381 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004382
Victor Stinner8c62be82010-05-06 00:08:46 +00004383 while (--envc >= 0)
4384 PyMem_DEL(envlist[envc]);
4385 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004386 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004388 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004389 Py_DECREF(opath);
4390 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004391}
4392#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004393#endif /* HAVE_SPAWNV */
4394
4395
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004396#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004397PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004398"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004399Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4400\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004401Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004402
4403static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004404posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004405{
Victor Stinner8c62be82010-05-06 00:08:46 +00004406 pid_t pid;
4407 int result = 0;
4408 _PyImport_AcquireLock();
4409 pid = fork1();
4410 if (pid == 0) {
4411 /* child: this clobbers and resets the import lock. */
4412 PyOS_AfterFork();
4413 } else {
4414 /* parent: release the import lock. */
4415 result = _PyImport_ReleaseLock();
4416 }
4417 if (pid == -1)
4418 return posix_error();
4419 if (result < 0) {
4420 /* Don't clobber the OSError if the fork failed. */
4421 PyErr_SetString(PyExc_RuntimeError,
4422 "not holding the import lock");
4423 return NULL;
4424 }
4425 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004426}
4427#endif
4428
4429
Guido van Rossumad0ee831995-03-01 10:34:45 +00004430#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004431PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004432"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004433Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004434Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004435
Barry Warsaw53699e91996-12-10 23:23:01 +00004436static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004437posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004438{
Victor Stinner8c62be82010-05-06 00:08:46 +00004439 pid_t pid;
4440 int result = 0;
4441 _PyImport_AcquireLock();
4442 pid = fork();
4443 if (pid == 0) {
4444 /* child: this clobbers and resets the import lock. */
4445 PyOS_AfterFork();
4446 } else {
4447 /* parent: release the import lock. */
4448 result = _PyImport_ReleaseLock();
4449 }
4450 if (pid == -1)
4451 return posix_error();
4452 if (result < 0) {
4453 /* Don't clobber the OSError if the fork failed. */
4454 PyErr_SetString(PyExc_RuntimeError,
4455 "not holding the import lock");
4456 return NULL;
4457 }
4458 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004459}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004460#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004461
Neal Norwitzb59798b2003-03-21 01:43:31 +00004462/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004463/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4464#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004465#define DEV_PTY_FILE "/dev/ptc"
4466#define HAVE_DEV_PTMX
4467#else
4468#define DEV_PTY_FILE "/dev/ptmx"
4469#endif
4470
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004471#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004472#ifdef HAVE_PTY_H
4473#include <pty.h>
4474#else
4475#ifdef HAVE_LIBUTIL_H
4476#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004477#else
4478#ifdef HAVE_UTIL_H
4479#include <util.h>
4480#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004481#endif /* HAVE_LIBUTIL_H */
4482#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004483#ifdef HAVE_STROPTS_H
4484#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004485#endif
4486#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004487
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004488#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004489PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004490"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004491Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004492
4493static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004494posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004495{
Victor Stinner8c62be82010-05-06 00:08:46 +00004496 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004497#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004498 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004499#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004500#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004501 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004502#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004503 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004504#endif
4505#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004506
Thomas Wouters70c21a12000-07-14 14:28:33 +00004507#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004508 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4509 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004510#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004511 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4512 if (slave_name == NULL)
4513 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004514
Victor Stinner8c62be82010-05-06 00:08:46 +00004515 slave_fd = open(slave_name, O_RDWR);
4516 if (slave_fd < 0)
4517 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004518#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004519 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4520 if (master_fd < 0)
4521 return posix_error();
4522 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4523 /* change permission of slave */
4524 if (grantpt(master_fd) < 0) {
4525 PyOS_setsig(SIGCHLD, sig_saved);
4526 return posix_error();
4527 }
4528 /* unlock slave */
4529 if (unlockpt(master_fd) < 0) {
4530 PyOS_setsig(SIGCHLD, sig_saved);
4531 return posix_error();
4532 }
4533 PyOS_setsig(SIGCHLD, sig_saved);
4534 slave_name = ptsname(master_fd); /* get name of slave */
4535 if (slave_name == NULL)
4536 return posix_error();
4537 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4538 if (slave_fd < 0)
4539 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004540#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4542 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004543#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004544 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004545#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004546#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004547#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004548
Victor Stinner8c62be82010-05-06 00:08:46 +00004549 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004550
Fred Drake8cef4cf2000-06-28 16:40:38 +00004551}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004552#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004553
4554#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004555PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004556"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004557Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4558Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004559To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004560
4561static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004562posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004563{
Victor Stinner8c62be82010-05-06 00:08:46 +00004564 int master_fd = -1, result = 0;
4565 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004566
Victor Stinner8c62be82010-05-06 00:08:46 +00004567 _PyImport_AcquireLock();
4568 pid = forkpty(&master_fd, NULL, NULL, NULL);
4569 if (pid == 0) {
4570 /* child: this clobbers and resets the import lock. */
4571 PyOS_AfterFork();
4572 } else {
4573 /* parent: release the import lock. */
4574 result = _PyImport_ReleaseLock();
4575 }
4576 if (pid == -1)
4577 return posix_error();
4578 if (result < 0) {
4579 /* Don't clobber the OSError if the fork failed. */
4580 PyErr_SetString(PyExc_RuntimeError,
4581 "not holding the import lock");
4582 return NULL;
4583 }
4584 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004585}
4586#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004587
Ross Lagerwall7807c352011-03-17 20:20:30 +02004588
Guido van Rossumad0ee831995-03-01 10:34:45 +00004589#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004590PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004591"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004592Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004593
Barry Warsaw53699e91996-12-10 23:23:01 +00004594static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004595posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004596{
Victor Stinner8c62be82010-05-06 00:08:46 +00004597 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004598}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004599#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004600
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004601
Guido van Rossumad0ee831995-03-01 10:34:45 +00004602#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004603PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004604"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004605Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004606
Barry Warsaw53699e91996-12-10 23:23:01 +00004607static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004608posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004609{
Victor Stinner8c62be82010-05-06 00:08:46 +00004610 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004611}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004612#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004613
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004614
Guido van Rossumad0ee831995-03-01 10:34:45 +00004615#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004616PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004617"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004618Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004619
Barry Warsaw53699e91996-12-10 23:23:01 +00004620static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004621posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004622{
Victor Stinner8c62be82010-05-06 00:08:46 +00004623 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004624}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004625#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004628PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004629"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004630Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004631
Barry Warsaw53699e91996-12-10 23:23:01 +00004632static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004633posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004634{
Victor Stinner8c62be82010-05-06 00:08:46 +00004635 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004636}
4637
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004638
Fred Drakec9680921999-12-13 16:37:25 +00004639#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004640PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004641"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004642Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004643
4644static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004645posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004646{
4647 PyObject *result = NULL;
4648
Fred Drakec9680921999-12-13 16:37:25 +00004649#ifdef NGROUPS_MAX
4650#define MAX_GROUPS NGROUPS_MAX
4651#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004652 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004653#define MAX_GROUPS 64
4654#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004655 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004656
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004657 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004658 * This is a helper variable to store the intermediate result when
4659 * that happens.
4660 *
4661 * To keep the code readable the OSX behaviour is unconditional,
4662 * according to the POSIX spec this should be safe on all unix-y
4663 * systems.
4664 */
4665 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004666 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004667
Victor Stinner8c62be82010-05-06 00:08:46 +00004668 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004669 if (n < 0) {
4670 if (errno == EINVAL) {
4671 n = getgroups(0, NULL);
4672 if (n == -1) {
4673 return posix_error();
4674 }
4675 if (n == 0) {
4676 /* Avoid malloc(0) */
4677 alt_grouplist = grouplist;
4678 } else {
4679 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4680 if (alt_grouplist == NULL) {
4681 errno = EINVAL;
4682 return posix_error();
4683 }
4684 n = getgroups(n, alt_grouplist);
4685 if (n == -1) {
4686 PyMem_Free(alt_grouplist);
4687 return posix_error();
4688 }
4689 }
4690 } else {
4691 return posix_error();
4692 }
4693 }
4694 result = PyList_New(n);
4695 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004696 int i;
4697 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004698 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004699 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004700 Py_DECREF(result);
4701 result = NULL;
4702 break;
Fred Drakec9680921999-12-13 16:37:25 +00004703 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004704 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004705 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004706 }
4707
4708 if (alt_grouplist != grouplist) {
4709 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004710 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004711
Fred Drakec9680921999-12-13 16:37:25 +00004712 return result;
4713}
4714#endif
4715
Antoine Pitroub7572f02009-12-02 20:46:48 +00004716#ifdef HAVE_INITGROUPS
4717PyDoc_STRVAR(posix_initgroups__doc__,
4718"initgroups(username, gid) -> None\n\n\
4719Call the system initgroups() to initialize the group access list with all of\n\
4720the groups of which the specified username is a member, plus the specified\n\
4721group id.");
4722
4723static PyObject *
4724posix_initgroups(PyObject *self, PyObject *args)
4725{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004726 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004727 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004728 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004729 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004730
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004731 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4732 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004733 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004734 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004735
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004736 res = initgroups(username, (gid_t) gid);
4737 Py_DECREF(oname);
4738 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004739 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004740
Victor Stinner8c62be82010-05-06 00:08:46 +00004741 Py_INCREF(Py_None);
4742 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004743}
4744#endif
4745
Martin v. Löwis606edc12002-06-13 21:09:11 +00004746#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004747PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004748"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004749Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004750
4751static PyObject *
4752posix_getpgid(PyObject *self, PyObject *args)
4753{
Victor Stinner8c62be82010-05-06 00:08:46 +00004754 pid_t pid, pgid;
4755 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4756 return NULL;
4757 pgid = getpgid(pid);
4758 if (pgid < 0)
4759 return posix_error();
4760 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004761}
4762#endif /* HAVE_GETPGID */
4763
4764
Guido van Rossumb6775db1994-08-01 11:34:53 +00004765#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004766PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004767"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004768Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004769
Barry Warsaw53699e91996-12-10 23:23:01 +00004770static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004771posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004772{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004773#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004774 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004775#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004776 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004777#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004778}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004779#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004780
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004781
Guido van Rossumb6775db1994-08-01 11:34:53 +00004782#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004783PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004784"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004785Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004786
Barry Warsaw53699e91996-12-10 23:23:01 +00004787static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004788posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004789{
Guido van Rossum64933891994-10-20 21:56:42 +00004790#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004791 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004792#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004793 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004794#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004795 return posix_error();
4796 Py_INCREF(Py_None);
4797 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004798}
4799
Guido van Rossumb6775db1994-08-01 11:34:53 +00004800#endif /* HAVE_SETPGRP */
4801
Guido van Rossumad0ee831995-03-01 10:34:45 +00004802#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004803
4804#ifdef MS_WINDOWS
4805#include <tlhelp32.h>
4806
4807static PyObject*
4808win32_getppid()
4809{
4810 HANDLE snapshot;
4811 pid_t mypid;
4812 PyObject* result = NULL;
4813 BOOL have_record;
4814 PROCESSENTRY32 pe;
4815
4816 mypid = getpid(); /* This function never fails */
4817
4818 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4819 if (snapshot == INVALID_HANDLE_VALUE)
4820 return PyErr_SetFromWindowsErr(GetLastError());
4821
4822 pe.dwSize = sizeof(pe);
4823 have_record = Process32First(snapshot, &pe);
4824 while (have_record) {
4825 if (mypid == (pid_t)pe.th32ProcessID) {
4826 /* We could cache the ulong value in a static variable. */
4827 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4828 break;
4829 }
4830
4831 have_record = Process32Next(snapshot, &pe);
4832 }
4833
4834 /* If our loop exits and our pid was not found (result will be NULL)
4835 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4836 * error anyway, so let's raise it. */
4837 if (!result)
4838 result = PyErr_SetFromWindowsErr(GetLastError());
4839
4840 CloseHandle(snapshot);
4841
4842 return result;
4843}
4844#endif /*MS_WINDOWS*/
4845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004846PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004847"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004848Return the parent's process id. If the parent process has already exited,\n\
4849Windows machines will still return its id; others systems will return the id\n\
4850of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004851
Barry Warsaw53699e91996-12-10 23:23:01 +00004852static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004853posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004854{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004855#ifdef MS_WINDOWS
4856 return win32_getppid();
4857#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004858 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004859#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004860}
4861#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004862
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004863
Fred Drake12c6e2d1999-12-14 21:25:03 +00004864#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004865PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004866"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004867Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004868
4869static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004870posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004871{
Victor Stinner8c62be82010-05-06 00:08:46 +00004872 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004873#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004874 wchar_t user_name[UNLEN + 1];
4875 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4876
4877 if (GetUserNameW(user_name, &num_chars)) {
4878 /* num_chars is the number of unicode chars plus null terminator */
4879 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004880 }
4881 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004882 result = PyErr_SetFromWindowsErr(GetLastError());
4883#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004884 char *name;
4885 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004886
Victor Stinner8c62be82010-05-06 00:08:46 +00004887 errno = 0;
4888 name = getlogin();
4889 if (name == NULL) {
4890 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004891 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004892 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004893 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004894 }
4895 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004896 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004897 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004898#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004899 return result;
4900}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004901#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004902
Guido van Rossumad0ee831995-03-01 10:34:45 +00004903#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004904PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004905"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004906Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004907
Barry Warsaw53699e91996-12-10 23:23:01 +00004908static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004909posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004910{
Victor Stinner8c62be82010-05-06 00:08:46 +00004911 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004912}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004913#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004915
Guido van Rossumad0ee831995-03-01 10:34:45 +00004916#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004917PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004918"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004919Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004920
Barry Warsaw53699e91996-12-10 23:23:01 +00004921static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004922posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004923{
Victor Stinner8c62be82010-05-06 00:08:46 +00004924 pid_t pid;
4925 int sig;
4926 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4927 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004928#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004929 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4930 APIRET rc;
4931 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004932 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004933
4934 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4935 APIRET rc;
4936 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004937 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004938
4939 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004940 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004941#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 if (kill(pid, sig) == -1)
4943 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004944#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004945 Py_INCREF(Py_None);
4946 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004947}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004948#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004949
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004950#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004951PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004952"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004953Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004954
4955static PyObject *
4956posix_killpg(PyObject *self, PyObject *args)
4957{
Victor Stinner8c62be82010-05-06 00:08:46 +00004958 int sig;
4959 pid_t pgid;
4960 /* XXX some man pages make the `pgid` parameter an int, others
4961 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4962 take the same type. Moreover, pid_t is always at least as wide as
4963 int (else compilation of this module fails), which is safe. */
4964 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4965 return NULL;
4966 if (killpg(pgid, sig) == -1)
4967 return posix_error();
4968 Py_INCREF(Py_None);
4969 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004970}
4971#endif
4972
Brian Curtineb24d742010-04-12 17:16:38 +00004973#ifdef MS_WINDOWS
4974PyDoc_STRVAR(win32_kill__doc__,
4975"kill(pid, sig)\n\n\
4976Kill a process with a signal.");
4977
4978static PyObject *
4979win32_kill(PyObject *self, PyObject *args)
4980{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004981 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004982 DWORD pid, sig, err;
4983 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004984
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4986 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004987
Victor Stinner8c62be82010-05-06 00:08:46 +00004988 /* Console processes which share a common console can be sent CTRL+C or
4989 CTRL+BREAK events, provided they handle said events. */
4990 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4991 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4992 err = GetLastError();
4993 PyErr_SetFromWindowsErr(err);
4994 }
4995 else
4996 Py_RETURN_NONE;
4997 }
Brian Curtineb24d742010-04-12 17:16:38 +00004998
Victor Stinner8c62be82010-05-06 00:08:46 +00004999 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5000 attempt to open and terminate the process. */
5001 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5002 if (handle == NULL) {
5003 err = GetLastError();
5004 return PyErr_SetFromWindowsErr(err);
5005 }
Brian Curtineb24d742010-04-12 17:16:38 +00005006
Victor Stinner8c62be82010-05-06 00:08:46 +00005007 if (TerminateProcess(handle, sig) == 0) {
5008 err = GetLastError();
5009 result = PyErr_SetFromWindowsErr(err);
5010 } else {
5011 Py_INCREF(Py_None);
5012 result = Py_None;
5013 }
Brian Curtineb24d742010-04-12 17:16:38 +00005014
Victor Stinner8c62be82010-05-06 00:08:46 +00005015 CloseHandle(handle);
5016 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005017}
5018#endif /* MS_WINDOWS */
5019
Guido van Rossumc0125471996-06-28 18:55:32 +00005020#ifdef HAVE_PLOCK
5021
5022#ifdef HAVE_SYS_LOCK_H
5023#include <sys/lock.h>
5024#endif
5025
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005026PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005027"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005028Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005029
Barry Warsaw53699e91996-12-10 23:23:01 +00005030static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005031posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005032{
Victor Stinner8c62be82010-05-06 00:08:46 +00005033 int op;
5034 if (!PyArg_ParseTuple(args, "i:plock", &op))
5035 return NULL;
5036 if (plock(op) == -1)
5037 return posix_error();
5038 Py_INCREF(Py_None);
5039 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005040}
5041#endif
5042
Guido van Rossumb6775db1994-08-01 11:34:53 +00005043#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005044PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005045"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005046Set the current process's user id.");
5047
Barry Warsaw53699e91996-12-10 23:23:01 +00005048static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005049posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005050{
Victor Stinner8c62be82010-05-06 00:08:46 +00005051 long uid_arg;
5052 uid_t uid;
5053 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5054 return NULL;
5055 uid = uid_arg;
5056 if (uid != uid_arg) {
5057 PyErr_SetString(PyExc_OverflowError, "user id too big");
5058 return NULL;
5059 }
5060 if (setuid(uid) < 0)
5061 return posix_error();
5062 Py_INCREF(Py_None);
5063 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005064}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005065#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005066
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005067
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005068#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005069PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005070"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005071Set the current process's effective user id.");
5072
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005073static PyObject *
5074posix_seteuid (PyObject *self, PyObject *args)
5075{
Victor Stinner8c62be82010-05-06 00:08:46 +00005076 long euid_arg;
5077 uid_t euid;
5078 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5079 return NULL;
5080 euid = euid_arg;
5081 if (euid != euid_arg) {
5082 PyErr_SetString(PyExc_OverflowError, "user id too big");
5083 return NULL;
5084 }
5085 if (seteuid(euid) < 0) {
5086 return posix_error();
5087 } else {
5088 Py_INCREF(Py_None);
5089 return Py_None;
5090 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005091}
5092#endif /* HAVE_SETEUID */
5093
5094#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005095PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005096"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005097Set the current process's effective group id.");
5098
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005099static PyObject *
5100posix_setegid (PyObject *self, PyObject *args)
5101{
Victor Stinner8c62be82010-05-06 00:08:46 +00005102 long egid_arg;
5103 gid_t egid;
5104 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5105 return NULL;
5106 egid = egid_arg;
5107 if (egid != egid_arg) {
5108 PyErr_SetString(PyExc_OverflowError, "group id too big");
5109 return NULL;
5110 }
5111 if (setegid(egid) < 0) {
5112 return posix_error();
5113 } else {
5114 Py_INCREF(Py_None);
5115 return Py_None;
5116 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005117}
5118#endif /* HAVE_SETEGID */
5119
5120#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005121PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005122"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005123Set the current process's real and effective user ids.");
5124
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005125static PyObject *
5126posix_setreuid (PyObject *self, PyObject *args)
5127{
Victor Stinner8c62be82010-05-06 00:08:46 +00005128 long ruid_arg, euid_arg;
5129 uid_t ruid, euid;
5130 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5131 return NULL;
5132 if (ruid_arg == -1)
5133 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5134 else
5135 ruid = ruid_arg; /* otherwise, assign from our long */
5136 if (euid_arg == -1)
5137 euid = (uid_t)-1;
5138 else
5139 euid = euid_arg;
5140 if ((euid_arg != -1 && euid != euid_arg) ||
5141 (ruid_arg != -1 && ruid != ruid_arg)) {
5142 PyErr_SetString(PyExc_OverflowError, "user id too big");
5143 return NULL;
5144 }
5145 if (setreuid(ruid, euid) < 0) {
5146 return posix_error();
5147 } else {
5148 Py_INCREF(Py_None);
5149 return Py_None;
5150 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005151}
5152#endif /* HAVE_SETREUID */
5153
5154#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005155PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005156"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005157Set the current process's real and effective group ids.");
5158
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005159static PyObject *
5160posix_setregid (PyObject *self, PyObject *args)
5161{
Victor Stinner8c62be82010-05-06 00:08:46 +00005162 long rgid_arg, egid_arg;
5163 gid_t rgid, egid;
5164 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5165 return NULL;
5166 if (rgid_arg == -1)
5167 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5168 else
5169 rgid = rgid_arg; /* otherwise, assign from our long */
5170 if (egid_arg == -1)
5171 egid = (gid_t)-1;
5172 else
5173 egid = egid_arg;
5174 if ((egid_arg != -1 && egid != egid_arg) ||
5175 (rgid_arg != -1 && rgid != rgid_arg)) {
5176 PyErr_SetString(PyExc_OverflowError, "group id too big");
5177 return NULL;
5178 }
5179 if (setregid(rgid, egid) < 0) {
5180 return posix_error();
5181 } else {
5182 Py_INCREF(Py_None);
5183 return Py_None;
5184 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005185}
5186#endif /* HAVE_SETREGID */
5187
Guido van Rossumb6775db1994-08-01 11:34:53 +00005188#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005189PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005190"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005191Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005192
Barry Warsaw53699e91996-12-10 23:23:01 +00005193static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005194posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005195{
Victor Stinner8c62be82010-05-06 00:08:46 +00005196 long gid_arg;
5197 gid_t gid;
5198 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5199 return NULL;
5200 gid = gid_arg;
5201 if (gid != gid_arg) {
5202 PyErr_SetString(PyExc_OverflowError, "group id too big");
5203 return NULL;
5204 }
5205 if (setgid(gid) < 0)
5206 return posix_error();
5207 Py_INCREF(Py_None);
5208 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005209}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005210#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005211
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005212#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005213PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005214"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005215Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005216
5217static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005218posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005219{
Victor Stinner8c62be82010-05-06 00:08:46 +00005220 int i, len;
5221 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005222
Victor Stinner8c62be82010-05-06 00:08:46 +00005223 if (!PySequence_Check(groups)) {
5224 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5225 return NULL;
5226 }
5227 len = PySequence_Size(groups);
5228 if (len > MAX_GROUPS) {
5229 PyErr_SetString(PyExc_ValueError, "too many groups");
5230 return NULL;
5231 }
5232 for(i = 0; i < len; i++) {
5233 PyObject *elem;
5234 elem = PySequence_GetItem(groups, i);
5235 if (!elem)
5236 return NULL;
5237 if (!PyLong_Check(elem)) {
5238 PyErr_SetString(PyExc_TypeError,
5239 "groups must be integers");
5240 Py_DECREF(elem);
5241 return NULL;
5242 } else {
5243 unsigned long x = PyLong_AsUnsignedLong(elem);
5244 if (PyErr_Occurred()) {
5245 PyErr_SetString(PyExc_TypeError,
5246 "group id too big");
5247 Py_DECREF(elem);
5248 return NULL;
5249 }
5250 grouplist[i] = x;
5251 /* read back the value to see if it fitted in gid_t */
5252 if (grouplist[i] != x) {
5253 PyErr_SetString(PyExc_TypeError,
5254 "group id too big");
5255 Py_DECREF(elem);
5256 return NULL;
5257 }
5258 }
5259 Py_DECREF(elem);
5260 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005261
Victor Stinner8c62be82010-05-06 00:08:46 +00005262 if (setgroups(len, grouplist) < 0)
5263 return posix_error();
5264 Py_INCREF(Py_None);
5265 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005266}
5267#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005268
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005269#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5270static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005271wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005272{
Victor Stinner8c62be82010-05-06 00:08:46 +00005273 PyObject *result;
5274 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005275
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 if (pid == -1)
5277 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005278
Victor Stinner8c62be82010-05-06 00:08:46 +00005279 if (struct_rusage == NULL) {
5280 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5281 if (m == NULL)
5282 return NULL;
5283 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5284 Py_DECREF(m);
5285 if (struct_rusage == NULL)
5286 return NULL;
5287 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005288
Victor Stinner8c62be82010-05-06 00:08:46 +00005289 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5290 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5291 if (!result)
5292 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005293
5294#ifndef doubletime
5295#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5296#endif
5297
Victor Stinner8c62be82010-05-06 00:08:46 +00005298 PyStructSequence_SET_ITEM(result, 0,
5299 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5300 PyStructSequence_SET_ITEM(result, 1,
5301 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005302#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005303 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5304 SET_INT(result, 2, ru->ru_maxrss);
5305 SET_INT(result, 3, ru->ru_ixrss);
5306 SET_INT(result, 4, ru->ru_idrss);
5307 SET_INT(result, 5, ru->ru_isrss);
5308 SET_INT(result, 6, ru->ru_minflt);
5309 SET_INT(result, 7, ru->ru_majflt);
5310 SET_INT(result, 8, ru->ru_nswap);
5311 SET_INT(result, 9, ru->ru_inblock);
5312 SET_INT(result, 10, ru->ru_oublock);
5313 SET_INT(result, 11, ru->ru_msgsnd);
5314 SET_INT(result, 12, ru->ru_msgrcv);
5315 SET_INT(result, 13, ru->ru_nsignals);
5316 SET_INT(result, 14, ru->ru_nvcsw);
5317 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005318#undef SET_INT
5319
Victor Stinner8c62be82010-05-06 00:08:46 +00005320 if (PyErr_Occurred()) {
5321 Py_DECREF(result);
5322 return NULL;
5323 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005324
Victor Stinner8c62be82010-05-06 00:08:46 +00005325 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005326}
5327#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5328
5329#ifdef HAVE_WAIT3
5330PyDoc_STRVAR(posix_wait3__doc__,
5331"wait3(options) -> (pid, status, rusage)\n\n\
5332Wait for completion of a child process.");
5333
5334static PyObject *
5335posix_wait3(PyObject *self, PyObject *args)
5336{
Victor Stinner8c62be82010-05-06 00:08:46 +00005337 pid_t pid;
5338 int options;
5339 struct rusage ru;
5340 WAIT_TYPE status;
5341 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005342
Victor Stinner8c62be82010-05-06 00:08:46 +00005343 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5344 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005345
Victor Stinner8c62be82010-05-06 00:08:46 +00005346 Py_BEGIN_ALLOW_THREADS
5347 pid = wait3(&status, options, &ru);
5348 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005349
Victor Stinner8c62be82010-05-06 00:08:46 +00005350 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005351}
5352#endif /* HAVE_WAIT3 */
5353
5354#ifdef HAVE_WAIT4
5355PyDoc_STRVAR(posix_wait4__doc__,
5356"wait4(pid, options) -> (pid, status, rusage)\n\n\
5357Wait for completion of a given child process.");
5358
5359static PyObject *
5360posix_wait4(PyObject *self, PyObject *args)
5361{
Victor Stinner8c62be82010-05-06 00:08:46 +00005362 pid_t pid;
5363 int options;
5364 struct rusage ru;
5365 WAIT_TYPE status;
5366 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005367
Victor Stinner8c62be82010-05-06 00:08:46 +00005368 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5369 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005370
Victor Stinner8c62be82010-05-06 00:08:46 +00005371 Py_BEGIN_ALLOW_THREADS
5372 pid = wait4(pid, &status, options, &ru);
5373 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005374
Victor Stinner8c62be82010-05-06 00:08:46 +00005375 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005376}
5377#endif /* HAVE_WAIT4 */
5378
Ross Lagerwall7807c352011-03-17 20:20:30 +02005379#if defined(HAVE_WAITID) && !defined(__APPLE__)
5380PyDoc_STRVAR(posix_waitid__doc__,
5381"waitid(idtype, id, options) -> waitid_result\n\n\
5382Wait for the completion of one or more child processes.\n\n\
5383idtype can be P_PID, P_PGID or P_ALL.\n\
5384id specifies the pid to wait on.\n\
5385options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
5386or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
5387Returns either waitid_result or None if WNOHANG is specified and there are\n\
5388no children in a waitable state.");
5389
5390static PyObject *
5391posix_waitid(PyObject *self, PyObject *args)
5392{
5393 PyObject *result;
5394 idtype_t idtype;
5395 id_t id;
5396 int options, res;
5397 siginfo_t si;
5398 si.si_pid = 0;
5399 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
5400 return NULL;
5401 Py_BEGIN_ALLOW_THREADS
5402 res = waitid(idtype, id, &si, options);
5403 Py_END_ALLOW_THREADS
5404 if (res == -1)
5405 return posix_error();
5406
5407 if (si.si_pid == 0)
5408 Py_RETURN_NONE;
5409
5410 result = PyStructSequence_New(&WaitidResultType);
5411 if (!result)
5412 return NULL;
5413
5414 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
5415 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
5416 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
5417 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
5418 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
5419 if (PyErr_Occurred()) {
5420 Py_DECREF(result);
5421 return NULL;
5422 }
5423
5424 return result;
5425}
5426#endif
5427
Guido van Rossumb6775db1994-08-01 11:34:53 +00005428#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005429PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005430"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005431Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005432
Barry Warsaw53699e91996-12-10 23:23:01 +00005433static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005434posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005435{
Victor Stinner8c62be82010-05-06 00:08:46 +00005436 pid_t pid;
5437 int options;
5438 WAIT_TYPE status;
5439 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005440
Victor Stinner8c62be82010-05-06 00:08:46 +00005441 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5442 return NULL;
5443 Py_BEGIN_ALLOW_THREADS
5444 pid = waitpid(pid, &status, options);
5445 Py_END_ALLOW_THREADS
5446 if (pid == -1)
5447 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005448
Victor Stinner8c62be82010-05-06 00:08:46 +00005449 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005450}
5451
Tim Petersab034fa2002-02-01 11:27:43 +00005452#elif defined(HAVE_CWAIT)
5453
5454/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005455PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005456"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005457"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005458
5459static PyObject *
5460posix_waitpid(PyObject *self, PyObject *args)
5461{
Victor Stinner8c62be82010-05-06 00:08:46 +00005462 Py_intptr_t pid;
5463 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005464
Victor Stinner8c62be82010-05-06 00:08:46 +00005465 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5466 return NULL;
5467 Py_BEGIN_ALLOW_THREADS
5468 pid = _cwait(&status, pid, options);
5469 Py_END_ALLOW_THREADS
5470 if (pid == -1)
5471 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005472
Victor Stinner8c62be82010-05-06 00:08:46 +00005473 /* shift the status left a byte so this is more like the POSIX waitpid */
5474 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005475}
5476#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005477
Guido van Rossumad0ee831995-03-01 10:34:45 +00005478#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005479PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005480"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005481Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005482
Barry Warsaw53699e91996-12-10 23:23:01 +00005483static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005484posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005485{
Victor Stinner8c62be82010-05-06 00:08:46 +00005486 pid_t pid;
5487 WAIT_TYPE status;
5488 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005489
Victor Stinner8c62be82010-05-06 00:08:46 +00005490 Py_BEGIN_ALLOW_THREADS
5491 pid = wait(&status);
5492 Py_END_ALLOW_THREADS
5493 if (pid == -1)
5494 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005495
Victor Stinner8c62be82010-05-06 00:08:46 +00005496 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005497}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005498#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005499
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005501PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005502"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005504
Barry Warsaw53699e91996-12-10 23:23:01 +00005505static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005506posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005507{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005508#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005509 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005510#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005511#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005512 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5513 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005514#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005515 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005516#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005517#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005518}
5519
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005520
Guido van Rossumb6775db1994-08-01 11:34:53 +00005521#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005522PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005523"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005525
Barry Warsaw53699e91996-12-10 23:23:01 +00005526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005527posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005528{
Victor Stinner8c62be82010-05-06 00:08:46 +00005529 PyObject* v;
5530 char buf[MAXPATHLEN];
5531 PyObject *opath;
5532 char *path;
5533 int n;
5534 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005535
Victor Stinner8c62be82010-05-06 00:08:46 +00005536 if (!PyArg_ParseTuple(args, "O&:readlink",
5537 PyUnicode_FSConverter, &opath))
5538 return NULL;
5539 path = PyBytes_AsString(opath);
5540 v = PySequence_GetItem(args, 0);
5541 if (v == NULL) {
5542 Py_DECREF(opath);
5543 return NULL;
5544 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005545
Victor Stinner8c62be82010-05-06 00:08:46 +00005546 if (PyUnicode_Check(v)) {
5547 arg_is_unicode = 1;
5548 }
5549 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005550
Victor Stinner8c62be82010-05-06 00:08:46 +00005551 Py_BEGIN_ALLOW_THREADS
5552 n = readlink(path, buf, (int) sizeof buf);
5553 Py_END_ALLOW_THREADS
5554 if (n < 0)
5555 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005556
Victor Stinner8c62be82010-05-06 00:08:46 +00005557 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005558 if (arg_is_unicode)
5559 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5560 else
5561 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005562}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005563#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005565
Brian Curtin52173d42010-12-02 18:29:18 +00005566#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005567PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005568"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005569Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005570
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005571static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005572posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005573{
Victor Stinner8c62be82010-05-06 00:08:46 +00005574 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005575}
5576#endif /* HAVE_SYMLINK */
5577
Brian Curtind40e6f72010-07-08 21:39:08 +00005578#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5579
5580PyDoc_STRVAR(win_readlink__doc__,
5581"readlink(path) -> path\n\n\
5582Return a string representing the path to which the symbolic link points.");
5583
Brian Curtind40e6f72010-07-08 21:39:08 +00005584/* Windows readlink implementation */
5585static PyObject *
5586win_readlink(PyObject *self, PyObject *args)
5587{
5588 wchar_t *path;
5589 DWORD n_bytes_returned;
5590 DWORD io_result;
5591 PyObject *result;
5592 HANDLE reparse_point_handle;
5593
5594 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5595 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5596 wchar_t *print_name;
5597
5598 if (!PyArg_ParseTuple(args,
5599 "u:readlink",
5600 &path))
5601 return NULL;
5602
5603 /* First get a handle to the reparse point */
5604 Py_BEGIN_ALLOW_THREADS
5605 reparse_point_handle = CreateFileW(
5606 path,
5607 0,
5608 0,
5609 0,
5610 OPEN_EXISTING,
5611 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5612 0);
5613 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005614
Brian Curtind40e6f72010-07-08 21:39:08 +00005615 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5616 {
5617 return win32_error_unicode("readlink", path);
5618 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005619
Brian Curtind40e6f72010-07-08 21:39:08 +00005620 Py_BEGIN_ALLOW_THREADS
5621 /* New call DeviceIoControl to read the reparse point */
5622 io_result = DeviceIoControl(
5623 reparse_point_handle,
5624 FSCTL_GET_REPARSE_POINT,
5625 0, 0, /* in buffer */
5626 target_buffer, sizeof(target_buffer),
5627 &n_bytes_returned,
5628 0 /* we're not using OVERLAPPED_IO */
5629 );
5630 CloseHandle(reparse_point_handle);
5631 Py_END_ALLOW_THREADS
5632
5633 if (io_result==0)
5634 {
5635 return win32_error_unicode("readlink", path);
5636 }
5637
5638 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5639 {
5640 PyErr_SetString(PyExc_ValueError,
5641 "not a symbolic link");
5642 return NULL;
5643 }
Brian Curtin74e45612010-07-09 15:58:59 +00005644 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5645 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5646
5647 result = PyUnicode_FromWideChar(print_name,
5648 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005649 return result;
5650}
5651
5652#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5653
Brian Curtin52173d42010-12-02 18:29:18 +00005654#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005655
5656/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5657static int has_CreateSymbolicLinkW = 0;
5658static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5659static int
5660check_CreateSymbolicLinkW()
5661{
5662 HINSTANCE hKernel32;
5663 /* only recheck */
5664 if (has_CreateSymbolicLinkW)
5665 return has_CreateSymbolicLinkW;
5666 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005667 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5668 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005669 if (Py_CreateSymbolicLinkW)
5670 has_CreateSymbolicLinkW = 1;
5671 return has_CreateSymbolicLinkW;
5672}
5673
5674PyDoc_STRVAR(win_symlink__doc__,
5675"symlink(src, dst, target_is_directory=False)\n\n\
5676Create a symbolic link pointing to src named dst.\n\
5677target_is_directory is required if the target is to be interpreted as\n\
5678a directory.\n\
5679This function requires Windows 6.0 or greater, and raises a\n\
5680NotImplementedError otherwise.");
5681
5682static PyObject *
5683win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5684{
5685 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5686 PyObject *src, *dest;
5687 int target_is_directory = 0;
5688 DWORD res;
5689 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005690
Brian Curtind40e6f72010-07-08 21:39:08 +00005691 if (!check_CreateSymbolicLinkW())
5692 {
5693 /* raise NotImplementedError */
5694 return PyErr_Format(PyExc_NotImplementedError,
5695 "CreateSymbolicLinkW not found");
5696 }
5697 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5698 kwlist, &src, &dest, &target_is_directory))
5699 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005700
5701 if (win32_can_symlink == 0)
5702 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5703
Brian Curtind40e6f72010-07-08 21:39:08 +00005704 if (!convert_to_unicode(&src)) { return NULL; }
5705 if (!convert_to_unicode(&dest)) {
5706 Py_DECREF(src);
5707 return NULL;
5708 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005709
Brian Curtind40e6f72010-07-08 21:39:08 +00005710 /* if src is a directory, ensure target_is_directory==1 */
5711 if(
5712 GetFileAttributesExW(
5713 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5714 ))
5715 {
5716 target_is_directory = target_is_directory ||
5717 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5718 }
5719
5720 Py_BEGIN_ALLOW_THREADS
5721 res = Py_CreateSymbolicLinkW(
5722 PyUnicode_AsUnicode(dest),
5723 PyUnicode_AsUnicode(src),
5724 target_is_directory);
5725 Py_END_ALLOW_THREADS
5726 Py_DECREF(src);
5727 Py_DECREF(dest);
5728 if (!res)
5729 {
5730 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5731 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005732
Brian Curtind40e6f72010-07-08 21:39:08 +00005733 Py_INCREF(Py_None);
5734 return Py_None;
5735}
Brian Curtin52173d42010-12-02 18:29:18 +00005736#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005737
5738#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005739#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5740static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005741system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005742{
5743 ULONG value = 0;
5744
5745 Py_BEGIN_ALLOW_THREADS
5746 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5747 Py_END_ALLOW_THREADS
5748
5749 return value;
5750}
5751
5752static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005753posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005754{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005755 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005756 return Py_BuildValue("ddddd",
5757 (double)0 /* t.tms_utime / HZ */,
5758 (double)0 /* t.tms_stime / HZ */,
5759 (double)0 /* t.tms_cutime / HZ */,
5760 (double)0 /* t.tms_cstime / HZ */,
5761 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005762}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005763#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005764#define NEED_TICKS_PER_SECOND
5765static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005766static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005767posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005768{
Victor Stinner8c62be82010-05-06 00:08:46 +00005769 struct tms t;
5770 clock_t c;
5771 errno = 0;
5772 c = times(&t);
5773 if (c == (clock_t) -1)
5774 return posix_error();
5775 return Py_BuildValue("ddddd",
5776 (double)t.tms_utime / ticks_per_second,
5777 (double)t.tms_stime / ticks_per_second,
5778 (double)t.tms_cutime / ticks_per_second,
5779 (double)t.tms_cstime / ticks_per_second,
5780 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005781}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005782#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005783#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005784
5785
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005786#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005787#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005788static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005789posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005790{
Victor Stinner8c62be82010-05-06 00:08:46 +00005791 FILETIME create, exit, kernel, user;
5792 HANDLE hProc;
5793 hProc = GetCurrentProcess();
5794 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5795 /* The fields of a FILETIME structure are the hi and lo part
5796 of a 64-bit value expressed in 100 nanosecond units.
5797 1e7 is one second in such units; 1e-7 the inverse.
5798 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5799 */
5800 return Py_BuildValue(
5801 "ddddd",
5802 (double)(user.dwHighDateTime*429.4967296 +
5803 user.dwLowDateTime*1e-7),
5804 (double)(kernel.dwHighDateTime*429.4967296 +
5805 kernel.dwLowDateTime*1e-7),
5806 (double)0,
5807 (double)0,
5808 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005809}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005810#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005811
5812#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005813PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005814"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005815Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005816#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005817
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005818
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005819#ifdef HAVE_GETSID
5820PyDoc_STRVAR(posix_getsid__doc__,
5821"getsid(pid) -> sid\n\n\
5822Call the system call getsid().");
5823
5824static PyObject *
5825posix_getsid(PyObject *self, PyObject *args)
5826{
Victor Stinner8c62be82010-05-06 00:08:46 +00005827 pid_t pid;
5828 int sid;
5829 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5830 return NULL;
5831 sid = getsid(pid);
5832 if (sid < 0)
5833 return posix_error();
5834 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005835}
5836#endif /* HAVE_GETSID */
5837
5838
Guido van Rossumb6775db1994-08-01 11:34:53 +00005839#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005840PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005841"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005842Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005843
Barry Warsaw53699e91996-12-10 23:23:01 +00005844static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005845posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005846{
Victor Stinner8c62be82010-05-06 00:08:46 +00005847 if (setsid() < 0)
5848 return posix_error();
5849 Py_INCREF(Py_None);
5850 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005851}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005852#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005853
Guido van Rossumb6775db1994-08-01 11:34:53 +00005854#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005855PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005856"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005857Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005858
Barry Warsaw53699e91996-12-10 23:23:01 +00005859static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005860posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005861{
Victor Stinner8c62be82010-05-06 00:08:46 +00005862 pid_t pid;
5863 int pgrp;
5864 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5865 return NULL;
5866 if (setpgid(pid, pgrp) < 0)
5867 return posix_error();
5868 Py_INCREF(Py_None);
5869 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005870}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005871#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005872
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005873
Guido van Rossumb6775db1994-08-01 11:34:53 +00005874#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005875PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005876"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005877Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005878
Barry Warsaw53699e91996-12-10 23:23:01 +00005879static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005880posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005881{
Victor Stinner8c62be82010-05-06 00:08:46 +00005882 int fd;
5883 pid_t pgid;
5884 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5885 return NULL;
5886 pgid = tcgetpgrp(fd);
5887 if (pgid < 0)
5888 return posix_error();
5889 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005890}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005891#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005892
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005893
Guido van Rossumb6775db1994-08-01 11:34:53 +00005894#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005895PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005896"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005897Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005898
Barry Warsaw53699e91996-12-10 23:23:01 +00005899static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005900posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005901{
Victor Stinner8c62be82010-05-06 00:08:46 +00005902 int fd;
5903 pid_t pgid;
5904 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5905 return NULL;
5906 if (tcsetpgrp(fd, pgid) < 0)
5907 return posix_error();
5908 Py_INCREF(Py_None);
5909 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005910}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005911#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005912
Guido van Rossum687dd131993-05-17 08:34:16 +00005913/* Functions acting on file descriptors */
5914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005915PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005916"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005917Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005918
Barry Warsaw53699e91996-12-10 23:23:01 +00005919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005920posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005921{
Victor Stinner8c62be82010-05-06 00:08:46 +00005922 PyObject *ofile;
5923 char *file;
5924 int flag;
5925 int mode = 0777;
5926 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005927
5928#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 PyUnicodeObject *po;
5930 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5931 Py_BEGIN_ALLOW_THREADS
5932 /* PyUnicode_AS_UNICODE OK without thread
5933 lock as it is a simple dereference. */
5934 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5935 Py_END_ALLOW_THREADS
5936 if (fd < 0)
5937 return posix_error();
5938 return PyLong_FromLong((long)fd);
5939 }
5940 /* Drop the argument parsing error as narrow strings
5941 are also valid. */
5942 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005943#endif
5944
Victor Stinner8c62be82010-05-06 00:08:46 +00005945 if (!PyArg_ParseTuple(args, "O&i|i",
5946 PyUnicode_FSConverter, &ofile,
5947 &flag, &mode))
5948 return NULL;
5949 file = PyBytes_AsString(ofile);
5950 Py_BEGIN_ALLOW_THREADS
5951 fd = open(file, flag, mode);
5952 Py_END_ALLOW_THREADS
5953 if (fd < 0)
5954 return posix_error_with_allocated_filename(ofile);
5955 Py_DECREF(ofile);
5956 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005957}
5958
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005959
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005960PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005961"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005962Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005963
Barry Warsaw53699e91996-12-10 23:23:01 +00005964static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005965posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005966{
Victor Stinner8c62be82010-05-06 00:08:46 +00005967 int fd, res;
5968 if (!PyArg_ParseTuple(args, "i:close", &fd))
5969 return NULL;
5970 if (!_PyVerify_fd(fd))
5971 return posix_error();
5972 Py_BEGIN_ALLOW_THREADS
5973 res = close(fd);
5974 Py_END_ALLOW_THREADS
5975 if (res < 0)
5976 return posix_error();
5977 Py_INCREF(Py_None);
5978 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005979}
5980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005981
Victor Stinner8c62be82010-05-06 00:08:46 +00005982PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005983"closerange(fd_low, fd_high)\n\n\
5984Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5985
5986static PyObject *
5987posix_closerange(PyObject *self, PyObject *args)
5988{
Victor Stinner8c62be82010-05-06 00:08:46 +00005989 int fd_from, fd_to, i;
5990 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5991 return NULL;
5992 Py_BEGIN_ALLOW_THREADS
5993 for (i = fd_from; i < fd_to; i++)
5994 if (_PyVerify_fd(i))
5995 close(i);
5996 Py_END_ALLOW_THREADS
5997 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005998}
5999
6000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006001PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006002"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006003Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006004
Barry Warsaw53699e91996-12-10 23:23:01 +00006005static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006006posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006007{
Victor Stinner8c62be82010-05-06 00:08:46 +00006008 int fd;
6009 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6010 return NULL;
6011 if (!_PyVerify_fd(fd))
6012 return posix_error();
6013 Py_BEGIN_ALLOW_THREADS
6014 fd = dup(fd);
6015 Py_END_ALLOW_THREADS
6016 if (fd < 0)
6017 return posix_error();
6018 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006019}
6020
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006022PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006023"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006024Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006025
Barry Warsaw53699e91996-12-10 23:23:01 +00006026static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006027posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006028{
Victor Stinner8c62be82010-05-06 00:08:46 +00006029 int fd, fd2, res;
6030 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6031 return NULL;
6032 if (!_PyVerify_fd_dup2(fd, fd2))
6033 return posix_error();
6034 Py_BEGIN_ALLOW_THREADS
6035 res = dup2(fd, fd2);
6036 Py_END_ALLOW_THREADS
6037 if (res < 0)
6038 return posix_error();
6039 Py_INCREF(Py_None);
6040 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006041}
6042
Ross Lagerwall7807c352011-03-17 20:20:30 +02006043#ifdef HAVE_LOCKF
6044PyDoc_STRVAR(posix_lockf__doc__,
6045"lockf(fd, cmd, len)\n\n\
6046Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6047fd is an open file descriptor.\n\
6048cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6049F_TEST.\n\
6050len specifies the section of the file to lock.");
6051
6052static PyObject *
6053posix_lockf(PyObject *self, PyObject *args)
6054{
6055 int fd, cmd, res;
6056 off_t len;
6057 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6058 &fd, &cmd, _parse_off_t, &len))
6059 return NULL;
6060
6061 Py_BEGIN_ALLOW_THREADS
6062 res = lockf(fd, cmd, len);
6063 Py_END_ALLOW_THREADS
6064
6065 if (res < 0)
6066 return posix_error();
6067
6068 Py_RETURN_NONE;
6069}
6070#endif
6071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006073PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006074"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006075Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006076
Barry Warsaw53699e91996-12-10 23:23:01 +00006077static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006078posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006079{
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006081#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006082 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006083#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006084 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006085#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006086 PyObject *posobj;
6087 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006088 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006089#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006090 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6091 switch (how) {
6092 case 0: how = SEEK_SET; break;
6093 case 1: how = SEEK_CUR; break;
6094 case 2: how = SEEK_END; break;
6095 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006096#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006097
Ross Lagerwall8e749672011-03-17 21:54:07 +02006098#if !defined(HAVE_LARGEFILE_SUPPORT)
6099 pos = PyLong_AsLong(posobj);
6100#else
6101 pos = PyLong_AsLongLong(posobj);
6102#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006103 if (PyErr_Occurred())
6104 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006105
Victor Stinner8c62be82010-05-06 00:08:46 +00006106 if (!_PyVerify_fd(fd))
6107 return posix_error();
6108 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006109#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006110 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006111#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006112 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006113#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006114 Py_END_ALLOW_THREADS
6115 if (res < 0)
6116 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006117
6118#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006119 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006120#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006122#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006123}
6124
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006126PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006127"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006128Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006129
Barry Warsaw53699e91996-12-10 23:23:01 +00006130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006131posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006132{
Victor Stinner8c62be82010-05-06 00:08:46 +00006133 int fd, size;
6134 Py_ssize_t n;
6135 PyObject *buffer;
6136 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6137 return NULL;
6138 if (size < 0) {
6139 errno = EINVAL;
6140 return posix_error();
6141 }
6142 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6143 if (buffer == NULL)
6144 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006145 if (!_PyVerify_fd(fd)) {
6146 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006147 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006148 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006149 Py_BEGIN_ALLOW_THREADS
6150 n = read(fd, PyBytes_AS_STRING(buffer), size);
6151 Py_END_ALLOW_THREADS
6152 if (n < 0) {
6153 Py_DECREF(buffer);
6154 return posix_error();
6155 }
6156 if (n != size)
6157 _PyBytes_Resize(&buffer, n);
6158 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006159}
6160
Ross Lagerwall7807c352011-03-17 20:20:30 +02006161#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6162 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006163static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006164iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6165{
6166 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006167 Py_ssize_t blen, total = 0;
6168
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006169 *iov = PyMem_New(struct iovec, cnt);
6170 if (*iov == NULL) {
6171 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006172 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006173 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006174
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006175 *buf = PyMem_New(Py_buffer, cnt);
6176 if (*buf == NULL) {
6177 PyMem_Del(*iov);
6178 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006179 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006180 }
6181
6182 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006183 PyObject *item = PySequence_GetItem(seq, i);
6184 if (item == NULL)
6185 goto fail;
6186 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6187 Py_DECREF(item);
6188 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006189 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006190 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006191 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006192 blen = (*buf)[i].len;
6193 (*iov)[i].iov_len = blen;
6194 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006195 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006196 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006197
6198fail:
6199 PyMem_Del(*iov);
6200 for (j = 0; j < i; j++) {
6201 PyBuffer_Release(&(*buf)[j]);
6202 }
6203 PyMem_Del(*buf);
6204 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006205}
6206
6207static void
6208iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6209{
6210 int i;
6211 PyMem_Del(iov);
6212 for (i = 0; i < cnt; i++) {
6213 PyBuffer_Release(&buf[i]);
6214 }
6215 PyMem_Del(buf);
6216}
6217#endif
6218
Ross Lagerwall7807c352011-03-17 20:20:30 +02006219#ifdef HAVE_READV
6220PyDoc_STRVAR(posix_readv__doc__,
6221"readv(fd, buffers) -> bytesread\n\n\
6222Read from a file descriptor into a number of writable buffers. buffers\n\
6223is an arbitrary sequence of writable buffers.\n\
6224Returns the total number of bytes read.");
6225
6226static PyObject *
6227posix_readv(PyObject *self, PyObject *args)
6228{
6229 int fd, cnt;
6230 Py_ssize_t n;
6231 PyObject *seq;
6232 struct iovec *iov;
6233 Py_buffer *buf;
6234
6235 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6236 return NULL;
6237 if (!PySequence_Check(seq)) {
6238 PyErr_SetString(PyExc_TypeError,
6239 "readv() arg 2 must be a sequence");
6240 return NULL;
6241 }
6242 cnt = PySequence_Size(seq);
6243
6244 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6245 return NULL;
6246
6247 Py_BEGIN_ALLOW_THREADS
6248 n = readv(fd, iov, cnt);
6249 Py_END_ALLOW_THREADS
6250
6251 iov_cleanup(iov, buf, cnt);
6252 return PyLong_FromSsize_t(n);
6253}
6254#endif
6255
6256#ifdef HAVE_PREAD
6257PyDoc_STRVAR(posix_pread__doc__,
6258"pread(fd, buffersize, offset) -> string\n\n\
6259Read from a file descriptor, fd, at a position of offset. It will read up\n\
6260to buffersize number of bytes. The file offset remains unchanged.");
6261
6262static PyObject *
6263posix_pread(PyObject *self, PyObject *args)
6264{
6265 int fd, size;
6266 off_t offset;
6267 Py_ssize_t n;
6268 PyObject *buffer;
6269 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6270 return NULL;
6271
6272 if (size < 0) {
6273 errno = EINVAL;
6274 return posix_error();
6275 }
6276 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6277 if (buffer == NULL)
6278 return NULL;
6279 if (!_PyVerify_fd(fd)) {
6280 Py_DECREF(buffer);
6281 return posix_error();
6282 }
6283 Py_BEGIN_ALLOW_THREADS
6284 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6285 Py_END_ALLOW_THREADS
6286 if (n < 0) {
6287 Py_DECREF(buffer);
6288 return posix_error();
6289 }
6290 if (n != size)
6291 _PyBytes_Resize(&buffer, n);
6292 return buffer;
6293}
6294#endif
6295
6296PyDoc_STRVAR(posix_write__doc__,
6297"write(fd, string) -> byteswritten\n\n\
6298Write a string to a file descriptor.");
6299
6300static PyObject *
6301posix_write(PyObject *self, PyObject *args)
6302{
6303 Py_buffer pbuf;
6304 int fd;
6305 Py_ssize_t size, len;
6306
6307 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
6308 return NULL;
6309 if (!_PyVerify_fd(fd)) {
6310 PyBuffer_Release(&pbuf);
6311 return posix_error();
6312 }
6313 len = pbuf.len;
6314 Py_BEGIN_ALLOW_THREADS
6315#if defined(MS_WIN64) || defined(MS_WINDOWS)
6316 if (len > INT_MAX)
6317 len = INT_MAX;
6318 size = write(fd, pbuf.buf, (int)len);
6319#else
6320 size = write(fd, pbuf.buf, len);
6321#endif
6322 Py_END_ALLOW_THREADS
6323 PyBuffer_Release(&pbuf);
6324 if (size < 0)
6325 return posix_error();
6326 return PyLong_FromSsize_t(size);
6327}
6328
6329#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006330PyDoc_STRVAR(posix_sendfile__doc__,
6331"sendfile(out, in, offset, nbytes) -> byteswritten\n\
6332sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
6333 -> byteswritten\n\
6334Copy nbytes bytes from file descriptor in to file descriptor out.");
6335
6336static PyObject *
6337posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
6338{
6339 int in, out;
6340 Py_ssize_t ret;
6341 off_t offset;
6342
6343#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
6344#ifndef __APPLE__
6345 Py_ssize_t len;
6346#endif
6347 PyObject *headers = NULL, *trailers = NULL;
6348 Py_buffer *hbuf, *tbuf;
6349 off_t sbytes;
6350 struct sf_hdtr sf;
6351 int flags = 0;
6352 sf.headers = NULL;
6353 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00006354 static char *keywords[] = {"out", "in",
6355 "offset", "count",
6356 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006357
6358#ifdef __APPLE__
6359 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006360 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006361#else
6362 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006363 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006364#endif
6365 &headers, &trailers, &flags))
6366 return NULL;
6367 if (headers != NULL) {
6368 if (!PySequence_Check(headers)) {
6369 PyErr_SetString(PyExc_TypeError,
6370 "sendfile() headers must be a sequence or None");
6371 return NULL;
6372 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006373 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006374 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006375 if (sf.hdr_cnt > 0 &&
6376 !(i = iov_setup(&(sf.headers), &hbuf,
6377 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006378 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006379#ifdef __APPLE__
6380 sbytes += i;
6381#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006382 }
6383 }
6384 if (trailers != NULL) {
6385 if (!PySequence_Check(trailers)) {
6386 PyErr_SetString(PyExc_TypeError,
6387 "sendfile() trailers must be a sequence or None");
6388 return NULL;
6389 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006390 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006391 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006392 if (sf.trl_cnt > 0 &&
6393 !(i = iov_setup(&(sf.trailers), &tbuf,
6394 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006395 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006396#ifdef __APPLE__
6397 sbytes += i;
6398#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006399 }
6400 }
6401
6402 Py_BEGIN_ALLOW_THREADS
6403#ifdef __APPLE__
6404 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
6405#else
6406 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
6407#endif
6408 Py_END_ALLOW_THREADS
6409
6410 if (sf.headers != NULL)
6411 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
6412 if (sf.trailers != NULL)
6413 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
6414
6415 if (ret < 0) {
6416 if ((errno == EAGAIN) || (errno == EBUSY)) {
6417 if (sbytes != 0) {
6418 // some data has been sent
6419 goto done;
6420 }
6421 else {
6422 // no data has been sent; upper application is supposed
6423 // to retry on EAGAIN or EBUSY
6424 return posix_error();
6425 }
6426 }
6427 return posix_error();
6428 }
6429 goto done;
6430
6431done:
6432 #if !defined(HAVE_LARGEFILE_SUPPORT)
6433 return Py_BuildValue("l", sbytes);
6434 #else
6435 return Py_BuildValue("L", sbytes);
6436 #endif
6437
6438#else
6439 Py_ssize_t count;
6440 PyObject *offobj;
6441 static char *keywords[] = {"out", "in",
6442 "offset", "count", NULL};
6443 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
6444 keywords, &out, &in, &offobj, &count))
6445 return NULL;
6446#ifdef linux
6447 if (offobj == Py_None) {
6448 Py_BEGIN_ALLOW_THREADS
6449 ret = sendfile(out, in, NULL, count);
6450 Py_END_ALLOW_THREADS
6451 if (ret < 0)
6452 return posix_error();
6453 Py_INCREF(Py_None);
6454 return Py_BuildValue("nO", ret, Py_None);
6455 }
6456#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00006457 if (!_parse_off_t(offobj, &offset))
6458 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006459 Py_BEGIN_ALLOW_THREADS
6460 ret = sendfile(out, in, &offset, count);
6461 Py_END_ALLOW_THREADS
6462 if (ret < 0)
6463 return posix_error();
6464 return Py_BuildValue("n", ret);
6465#endif
6466}
6467#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006469PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006470"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006471Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006472
Barry Warsaw53699e91996-12-10 23:23:01 +00006473static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006474posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006475{
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 int fd;
6477 STRUCT_STAT st;
6478 int res;
6479 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6480 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006481#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00006482 /* on OpenVMS we must ensure that all bytes are written to the file */
6483 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006484#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 if (!_PyVerify_fd(fd))
6486 return posix_error();
6487 Py_BEGIN_ALLOW_THREADS
6488 res = FSTAT(fd, &st);
6489 Py_END_ALLOW_THREADS
6490 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006491#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006492 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006493#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006494 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006495#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006496 }
Tim Peters5aa91602002-01-30 05:46:57 +00006497
Victor Stinner8c62be82010-05-06 00:08:46 +00006498 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006499}
6500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006501PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006502"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006503Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006504connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006505
6506static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006507posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006508{
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 int fd;
6510 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6511 return NULL;
6512 if (!_PyVerify_fd(fd))
6513 return PyBool_FromLong(0);
6514 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006515}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006516
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006517#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006518PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006519"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006520Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006521
Barry Warsaw53699e91996-12-10 23:23:01 +00006522static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006523posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006524{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006525#if defined(PYOS_OS2)
6526 HFILE read, write;
6527 APIRET rc;
6528
Victor Stinner8c62be82010-05-06 00:08:46 +00006529 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006530 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00006531 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006532 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006533 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006534
6535 return Py_BuildValue("(ii)", read, write);
6536#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006537#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 int fds[2];
6539 int res;
6540 Py_BEGIN_ALLOW_THREADS
6541 res = pipe(fds);
6542 Py_END_ALLOW_THREADS
6543 if (res != 0)
6544 return posix_error();
6545 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006546#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00006547 HANDLE read, write;
6548 int read_fd, write_fd;
6549 BOOL ok;
6550 Py_BEGIN_ALLOW_THREADS
6551 ok = CreatePipe(&read, &write, NULL, 0);
6552 Py_END_ALLOW_THREADS
6553 if (!ok)
6554 return win32_error("CreatePipe", NULL);
6555 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6556 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6557 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006558#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006559#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006560}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006561#endif /* HAVE_PIPE */
6562
Ross Lagerwall7807c352011-03-17 20:20:30 +02006563#ifdef HAVE_WRITEV
6564PyDoc_STRVAR(posix_writev__doc__,
6565"writev(fd, buffers) -> byteswritten\n\n\
6566Write the contents of buffers to a file descriptor, where buffers is an\n\
6567arbitrary sequence of buffers.\n\
6568Returns the total bytes written.");
6569
6570static PyObject *
6571posix_writev(PyObject *self, PyObject *args)
6572{
6573 int fd, cnt;
6574 Py_ssize_t res;
6575 PyObject *seq;
6576 struct iovec *iov;
6577 Py_buffer *buf;
6578 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
6579 return NULL;
6580 if (!PySequence_Check(seq)) {
6581 PyErr_SetString(PyExc_TypeError,
6582 "writev() arg 2 must be a sequence");
6583 return NULL;
6584 }
6585 cnt = PySequence_Size(seq);
6586
6587 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
6588 return NULL;
6589 }
6590
6591 Py_BEGIN_ALLOW_THREADS
6592 res = writev(fd, iov, cnt);
6593 Py_END_ALLOW_THREADS
6594
6595 iov_cleanup(iov, buf, cnt);
6596 return PyLong_FromSsize_t(res);
6597}
6598#endif
6599
6600#ifdef HAVE_PWRITE
6601PyDoc_STRVAR(posix_pwrite__doc__,
6602"pwrite(fd, string, offset) -> byteswritten\n\n\
6603Write string to a file descriptor, fd, from offset, leaving the file\n\
6604offset unchanged.");
6605
6606static PyObject *
6607posix_pwrite(PyObject *self, PyObject *args)
6608{
6609 Py_buffer pbuf;
6610 int fd;
6611 off_t offset;
6612 Py_ssize_t size;
6613
6614 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
6615 return NULL;
6616
6617 if (!_PyVerify_fd(fd)) {
6618 PyBuffer_Release(&pbuf);
6619 return posix_error();
6620 }
6621 Py_BEGIN_ALLOW_THREADS
6622 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
6623 Py_END_ALLOW_THREADS
6624 PyBuffer_Release(&pbuf);
6625 if (size < 0)
6626 return posix_error();
6627 return PyLong_FromSsize_t(size);
6628}
6629#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006630
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006631#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006632PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006633"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006634Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006635
Barry Warsaw53699e91996-12-10 23:23:01 +00006636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006637posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006638{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006639 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 char *filename;
6641 int mode = 0666;
6642 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006643 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
6644 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006646 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006647 Py_BEGIN_ALLOW_THREADS
6648 res = mkfifo(filename, mode);
6649 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006650 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006651 if (res < 0)
6652 return posix_error();
6653 Py_INCREF(Py_None);
6654 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006655}
6656#endif
6657
6658
Neal Norwitz11690112002-07-30 01:08:28 +00006659#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006660PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006661"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006662Create a filesystem node (file, device special file or named pipe)\n\
6663named filename. mode specifies both the permissions to use and the\n\
6664type of node to be created, being combined (bitwise OR) with one of\n\
6665S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006666device defines the newly created device special file (probably using\n\
6667os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006668
6669
6670static PyObject *
6671posix_mknod(PyObject *self, PyObject *args)
6672{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006673 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 char *filename;
6675 int mode = 0600;
6676 int device = 0;
6677 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006678 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
6679 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00006680 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006681 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 Py_BEGIN_ALLOW_THREADS
6683 res = mknod(filename, mode, device);
6684 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006685 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006686 if (res < 0)
6687 return posix_error();
6688 Py_INCREF(Py_None);
6689 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006690}
6691#endif
6692
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006693#ifdef HAVE_DEVICE_MACROS
6694PyDoc_STRVAR(posix_major__doc__,
6695"major(device) -> major number\n\
6696Extracts a device major number from a raw device number.");
6697
6698static PyObject *
6699posix_major(PyObject *self, PyObject *args)
6700{
Victor Stinner8c62be82010-05-06 00:08:46 +00006701 int device;
6702 if (!PyArg_ParseTuple(args, "i:major", &device))
6703 return NULL;
6704 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006705}
6706
6707PyDoc_STRVAR(posix_minor__doc__,
6708"minor(device) -> minor number\n\
6709Extracts a device minor number from a raw device number.");
6710
6711static PyObject *
6712posix_minor(PyObject *self, PyObject *args)
6713{
Victor Stinner8c62be82010-05-06 00:08:46 +00006714 int device;
6715 if (!PyArg_ParseTuple(args, "i:minor", &device))
6716 return NULL;
6717 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006718}
6719
6720PyDoc_STRVAR(posix_makedev__doc__,
6721"makedev(major, minor) -> device number\n\
6722Composes a raw device number from the major and minor device numbers.");
6723
6724static PyObject *
6725posix_makedev(PyObject *self, PyObject *args)
6726{
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 int major, minor;
6728 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6729 return NULL;
6730 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006731}
6732#endif /* device macros */
6733
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006734
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006735#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006736PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006737"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006738Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006739
Barry Warsaw53699e91996-12-10 23:23:01 +00006740static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006741posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006742{
Victor Stinner8c62be82010-05-06 00:08:46 +00006743 int fd;
6744 off_t length;
6745 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006746
Ross Lagerwall7807c352011-03-17 20:20:30 +02006747 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00006748 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006749
Victor Stinner8c62be82010-05-06 00:08:46 +00006750 Py_BEGIN_ALLOW_THREADS
6751 res = ftruncate(fd, length);
6752 Py_END_ALLOW_THREADS
6753 if (res < 0)
6754 return posix_error();
6755 Py_INCREF(Py_None);
6756 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006757}
6758#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006759
Ross Lagerwall7807c352011-03-17 20:20:30 +02006760#ifdef HAVE_TRUNCATE
6761PyDoc_STRVAR(posix_truncate__doc__,
6762"truncate(path, length)\n\n\
6763Truncate the file given by path to length bytes.");
6764
6765static PyObject *
6766posix_truncate(PyObject *self, PyObject *args)
6767{
6768 PyObject *opath;
6769 const char *path;
6770 off_t length;
6771 int res;
6772
6773 if (!PyArg_ParseTuple(args, "O&O&:truncate",
6774 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
6775 return NULL;
6776 path = PyBytes_AsString(opath);
6777
6778 Py_BEGIN_ALLOW_THREADS
6779 res = truncate(path, length);
6780 Py_END_ALLOW_THREADS
6781 Py_DECREF(opath);
6782 if (res < 0)
6783 return posix_error();
6784 Py_RETURN_NONE;
6785}
6786#endif
6787
6788#ifdef HAVE_POSIX_FALLOCATE
6789PyDoc_STRVAR(posix_posix_fallocate__doc__,
6790"posix_fallocate(fd, offset, len)\n\n\
6791Ensures that enough disk space is allocated for the file specified by fd\n\
6792starting from offset and continuing for len bytes.");
6793
6794static PyObject *
6795posix_posix_fallocate(PyObject *self, PyObject *args)
6796{
6797 off_t len, offset;
6798 int res, fd;
6799
6800 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
6801 &fd, _parse_off_t, &offset, _parse_off_t, &len))
6802 return NULL;
6803
6804 Py_BEGIN_ALLOW_THREADS
6805 res = posix_fallocate(fd, offset, len);
6806 Py_END_ALLOW_THREADS
6807 if (res != 0) {
6808 errno = res;
6809 return posix_error();
6810 }
6811 Py_RETURN_NONE;
6812}
6813#endif
6814
6815#ifdef HAVE_POSIX_FADVISE
6816PyDoc_STRVAR(posix_posix_fadvise__doc__,
6817"posix_fadvise(fd, offset, len, advice)\n\n\
6818Announces an intention to access data in a specific pattern thus allowing\n\
6819the kernel to make optimizations.\n\
6820The advice applies to the region of the file specified by fd starting at\n\
6821offset and continuing for len bytes.\n\
6822advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
6823POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
6824POSIX_FADV_DONTNEED.");
6825
6826static PyObject *
6827posix_posix_fadvise(PyObject *self, PyObject *args)
6828{
6829 off_t len, offset;
6830 int res, fd, advice;
6831
6832 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
6833 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
6834 return NULL;
6835
6836 Py_BEGIN_ALLOW_THREADS
6837 res = posix_fadvise(fd, offset, len, advice);
6838 Py_END_ALLOW_THREADS
6839 if (res != 0) {
6840 errno = res;
6841 return posix_error();
6842 }
6843 Py_RETURN_NONE;
6844}
6845#endif
6846
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006847#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006848PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006849"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006850Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006851
Fred Drake762e2061999-08-26 17:23:54 +00006852/* Save putenv() parameters as values here, so we can collect them when they
6853 * get re-set with another call for the same key. */
6854static PyObject *posix_putenv_garbage;
6855
Tim Peters5aa91602002-01-30 05:46:57 +00006856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006857posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006858{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006859#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 wchar_t *s1, *s2;
6861 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006862#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006863 PyObject *os1, *os2;
6864 char *s1, *s2;
6865 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006866#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006867 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006869
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006870#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 if (!PyArg_ParseTuple(args,
6872 "uu:putenv",
6873 &s1, &s2))
6874 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006875#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006876 if (!PyArg_ParseTuple(args,
6877 "O&O&:putenv",
6878 PyUnicode_FSConverter, &os1,
6879 PyUnicode_FSConverter, &os2))
6880 return NULL;
6881 s1 = PyBytes_AsString(os1);
6882 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006883#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006884
6885#if defined(PYOS_OS2)
6886 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6887 APIRET rc;
6888
Guido van Rossumd48f2521997-12-05 22:19:34 +00006889 rc = DosSetExtLIBPATH(s2, BEGIN_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
6895 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6896 APIRET rc;
6897
Guido van Rossumd48f2521997-12-05 22:19:34 +00006898 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006899 if (rc != NO_ERROR) {
6900 os2_error(rc);
6901 goto error;
6902 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006903 } else {
6904#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006905 /* XXX This can leak memory -- not easy to fix :-( */
6906 /* len includes space for a trailing \0; the size arg to
6907 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006908#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006909 len = wcslen(s1) + wcslen(s2) + 2;
6910 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006911#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006912 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006914#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006915 if (newstr == NULL) {
6916 PyErr_NoMemory();
6917 goto error;
6918 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006919#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 newenv = PyUnicode_AsUnicode(newstr);
6921 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6922 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006923 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006924 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006926#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 newenv = PyBytes_AS_STRING(newstr);
6928 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6929 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006930 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006931 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006932 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006933#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006934
Victor Stinner8c62be82010-05-06 00:08:46 +00006935 /* Install the first arg and newstr in posix_putenv_garbage;
6936 * this will cause previous value to be collected. This has to
6937 * happen after the real putenv() call because the old value
6938 * was still accessible until then. */
6939 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006940#ifdef MS_WINDOWS
6941 PyTuple_GET_ITEM(args, 0),
6942#else
6943 os1,
6944#endif
6945 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 /* really not much we can do; just leak */
6947 PyErr_Clear();
6948 }
6949 else {
6950 Py_DECREF(newstr);
6951 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006952
6953#if defined(PYOS_OS2)
6954 }
6955#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006956
Martin v. Löwis011e8422009-05-05 04:43:17 +00006957#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 Py_DECREF(os1);
6959 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006960#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006961 Py_RETURN_NONE;
6962
6963error:
6964#ifndef MS_WINDOWS
6965 Py_DECREF(os1);
6966 Py_DECREF(os2);
6967#endif
6968 Py_XDECREF(newstr);
6969 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006970}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006971#endif /* putenv */
6972
Guido van Rossumc524d952001-10-19 01:31:59 +00006973#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006974PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006975"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006976Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006977
6978static PyObject *
6979posix_unsetenv(PyObject *self, PyObject *args)
6980{
Victor Stinner84ae1182010-05-06 22:05:07 +00006981#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006983
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6985 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006986#else
6987 PyObject *os1;
6988 char *s1;
6989
6990 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6991 PyUnicode_FSConverter, &os1))
6992 return NULL;
6993 s1 = PyBytes_AsString(os1);
6994#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006995
Victor Stinner8c62be82010-05-06 00:08:46 +00006996 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006997
Victor Stinner8c62be82010-05-06 00:08:46 +00006998 /* Remove the key from posix_putenv_garbage;
6999 * this will cause it to be collected. This has to
7000 * happen after the real unsetenv() call because the
7001 * old value was still accessible until then.
7002 */
7003 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007004#ifdef MS_WINDOWS
7005 PyTuple_GET_ITEM(args, 0)
7006#else
7007 os1
7008#endif
7009 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007010 /* really not much we can do; just leak */
7011 PyErr_Clear();
7012 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007013
Victor Stinner84ae1182010-05-06 22:05:07 +00007014#ifndef MS_WINDOWS
7015 Py_DECREF(os1);
7016#endif
7017 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007018}
7019#endif /* unsetenv */
7020
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007021PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007022"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007023Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007024
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007026posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007027{
Victor Stinner8c62be82010-05-06 00:08:46 +00007028 int code;
7029 char *message;
7030 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7031 return NULL;
7032 message = strerror(code);
7033 if (message == NULL) {
7034 PyErr_SetString(PyExc_ValueError,
7035 "strerror() argument out of range");
7036 return NULL;
7037 }
7038 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007039}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007040
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007041
Guido van Rossumc9641791998-08-04 15:26:23 +00007042#ifdef HAVE_SYS_WAIT_H
7043
Fred Drake106c1a02002-04-23 15:58:02 +00007044#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007045PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007046"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007047Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007048
7049static PyObject *
7050posix_WCOREDUMP(PyObject *self, PyObject *args)
7051{
Victor Stinner8c62be82010-05-06 00:08:46 +00007052 WAIT_TYPE status;
7053 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007054
Victor Stinner8c62be82010-05-06 00:08:46 +00007055 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7056 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007057
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007059}
7060#endif /* WCOREDUMP */
7061
7062#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007063PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007064"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007065Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007066job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007067
7068static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007069posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007070{
Victor Stinner8c62be82010-05-06 00:08:46 +00007071 WAIT_TYPE status;
7072 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007073
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7075 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007076
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007078}
7079#endif /* WIFCONTINUED */
7080
Guido van Rossumc9641791998-08-04 15:26:23 +00007081#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007082PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007083"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007084Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007085
7086static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007087posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007088{
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 WAIT_TYPE status;
7090 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007091
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7093 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007094
Victor Stinner8c62be82010-05-06 00:08:46 +00007095 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007096}
7097#endif /* WIFSTOPPED */
7098
7099#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007100PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007101"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007102Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007103
7104static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007105posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007106{
Victor Stinner8c62be82010-05-06 00:08:46 +00007107 WAIT_TYPE status;
7108 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007109
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7111 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007112
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007114}
7115#endif /* WIFSIGNALED */
7116
7117#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007118PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007119"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007120Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007121system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007122
7123static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007124posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007125{
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 WAIT_TYPE status;
7127 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007128
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7130 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007131
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007133}
7134#endif /* WIFEXITED */
7135
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007136#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007137PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007138"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007139Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007140
7141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007142posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007143{
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 WAIT_TYPE status;
7145 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007146
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7148 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007149
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007151}
7152#endif /* WEXITSTATUS */
7153
7154#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007155PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007156"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007157Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007158value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007159
7160static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007161posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007162{
Victor Stinner8c62be82010-05-06 00:08:46 +00007163 WAIT_TYPE status;
7164 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007165
Victor Stinner8c62be82010-05-06 00:08:46 +00007166 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7167 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007168
Victor Stinner8c62be82010-05-06 00:08:46 +00007169 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007170}
7171#endif /* WTERMSIG */
7172
7173#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007174PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007175"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007176Return the signal that stopped the process that provided\n\
7177the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007178
7179static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007180posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007181{
Victor Stinner8c62be82010-05-06 00:08:46 +00007182 WAIT_TYPE status;
7183 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007184
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7186 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007187
Victor Stinner8c62be82010-05-06 00:08:46 +00007188 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007189}
7190#endif /* WSTOPSIG */
7191
7192#endif /* HAVE_SYS_WAIT_H */
7193
7194
Thomas Wouters477c8d52006-05-27 19:21:47 +00007195#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007196#ifdef _SCO_DS
7197/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7198 needed definitions in sys/statvfs.h */
7199#define _SVID3
7200#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007201#include <sys/statvfs.h>
7202
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007203static PyObject*
7204_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7206 if (v == NULL)
7207 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007208
7209#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7211 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7212 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7213 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7214 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7215 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7216 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7217 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7218 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7219 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007220#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007221 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7222 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7223 PyStructSequence_SET_ITEM(v, 2,
7224 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7225 PyStructSequence_SET_ITEM(v, 3,
7226 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7227 PyStructSequence_SET_ITEM(v, 4,
7228 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7229 PyStructSequence_SET_ITEM(v, 5,
7230 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7231 PyStructSequence_SET_ITEM(v, 6,
7232 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7233 PyStructSequence_SET_ITEM(v, 7,
7234 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7235 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7236 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007237#endif
7238
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007240}
7241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007242PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007243"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007244Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007245
7246static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007247posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007248{
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 int fd, res;
7250 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007251
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7253 return NULL;
7254 Py_BEGIN_ALLOW_THREADS
7255 res = fstatvfs(fd, &st);
7256 Py_END_ALLOW_THREADS
7257 if (res != 0)
7258 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007259
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007261}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007262#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007263
7264
Thomas Wouters477c8d52006-05-27 19:21:47 +00007265#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007266#include <sys/statvfs.h>
7267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007268PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007269"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007270Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007271
7272static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007273posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007274{
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 char *path;
7276 int res;
7277 struct statvfs st;
7278 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7279 return NULL;
7280 Py_BEGIN_ALLOW_THREADS
7281 res = statvfs(path, &st);
7282 Py_END_ALLOW_THREADS
7283 if (res != 0)
7284 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007285
Victor Stinner8c62be82010-05-06 00:08:46 +00007286 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007287}
7288#endif /* HAVE_STATVFS */
7289
Fred Drakec9680921999-12-13 16:37:25 +00007290/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7291 * It maps strings representing configuration variable names to
7292 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007293 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007294 * rarely-used constants. There are three separate tables that use
7295 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007296 *
7297 * This code is always included, even if none of the interfaces that
7298 * need it are included. The #if hackery needed to avoid it would be
7299 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007300 */
7301struct constdef {
7302 char *name;
7303 long value;
7304};
7305
Fred Drake12c6e2d1999-12-14 21:25:03 +00007306static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007307conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00007308 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007309{
Christian Heimes217cfd12007-12-02 14:31:20 +00007310 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007311 *valuep = PyLong_AS_LONG(arg);
7312 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007313 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00007314 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00007315 /* look up the value in the table using a binary search */
7316 size_t lo = 0;
7317 size_t mid;
7318 size_t hi = tablesize;
7319 int cmp;
7320 const char *confname;
7321 if (!PyUnicode_Check(arg)) {
7322 PyErr_SetString(PyExc_TypeError,
7323 "configuration names must be strings or integers");
7324 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007325 }
Stefan Krah0e803b32010-11-26 16:16:47 +00007326 confname = _PyUnicode_AsString(arg);
7327 if (confname == NULL)
7328 return 0;
7329 while (lo < hi) {
7330 mid = (lo + hi) / 2;
7331 cmp = strcmp(confname, table[mid].name);
7332 if (cmp < 0)
7333 hi = mid;
7334 else if (cmp > 0)
7335 lo = mid + 1;
7336 else {
7337 *valuep = table[mid].value;
7338 return 1;
7339 }
7340 }
7341 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7342 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007343 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00007344}
7345
7346
7347#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7348static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007349#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007350 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007351#endif
7352#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007353 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007354#endif
Fred Drakec9680921999-12-13 16:37:25 +00007355#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007357#endif
7358#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00007359 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007360#endif
7361#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00007362 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007363#endif
7364#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00007365 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007366#endif
7367#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007369#endif
7370#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007372#endif
7373#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007375#endif
7376#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007377 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007378#endif
7379#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007381#endif
7382#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007384#endif
7385#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007386 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007387#endif
7388#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007389 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007390#endif
7391#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007392 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007393#endif
7394#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007395 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007396#endif
7397#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007398 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007399#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00007400#ifdef _PC_ACL_ENABLED
7401 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
7402#endif
7403#ifdef _PC_MIN_HOLE_SIZE
7404 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
7405#endif
7406#ifdef _PC_ALLOC_SIZE_MIN
7407 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
7408#endif
7409#ifdef _PC_REC_INCR_XFER_SIZE
7410 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
7411#endif
7412#ifdef _PC_REC_MAX_XFER_SIZE
7413 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
7414#endif
7415#ifdef _PC_REC_MIN_XFER_SIZE
7416 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
7417#endif
7418#ifdef _PC_REC_XFER_ALIGN
7419 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
7420#endif
7421#ifdef _PC_SYMLINK_MAX
7422 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
7423#endif
7424#ifdef _PC_XATTR_ENABLED
7425 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
7426#endif
7427#ifdef _PC_XATTR_EXISTS
7428 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
7429#endif
7430#ifdef _PC_TIMESTAMP_RESOLUTION
7431 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
7432#endif
Fred Drakec9680921999-12-13 16:37:25 +00007433};
7434
Fred Drakec9680921999-12-13 16:37:25 +00007435static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007436conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007437{
7438 return conv_confname(arg, valuep, posix_constants_pathconf,
7439 sizeof(posix_constants_pathconf)
7440 / sizeof(struct constdef));
7441}
7442#endif
7443
7444#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007445PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007446"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007447Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007448If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007449
7450static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007451posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007452{
7453 PyObject *result = NULL;
7454 int name, fd;
7455
Fred Drake12c6e2d1999-12-14 21:25:03 +00007456 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7457 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007458 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007459
Stefan Krah0e803b32010-11-26 16:16:47 +00007460 errno = 0;
7461 limit = fpathconf(fd, name);
7462 if (limit == -1 && errno != 0)
7463 posix_error();
7464 else
7465 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007466 }
7467 return result;
7468}
7469#endif
7470
7471
7472#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007473PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007474"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007475Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007476If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007477
7478static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007479posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007480{
7481 PyObject *result = NULL;
7482 int name;
7483 char *path;
7484
7485 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7486 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007487 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007488
Victor Stinner8c62be82010-05-06 00:08:46 +00007489 errno = 0;
7490 limit = pathconf(path, name);
7491 if (limit == -1 && errno != 0) {
7492 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00007493 /* could be a path or name problem */
7494 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007495 else
Stefan Krah99439262010-11-26 12:58:05 +00007496 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00007497 }
7498 else
7499 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007500 }
7501 return result;
7502}
7503#endif
7504
7505#ifdef HAVE_CONFSTR
7506static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007507#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00007508 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007509#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00007510#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007512#endif
7513#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007514 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007515#endif
Fred Draked86ed291999-12-15 15:34:33 +00007516#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007517 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007518#endif
7519#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007520 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007521#endif
7522#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007523 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007524#endif
7525#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007526 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007527#endif
Fred Drakec9680921999-12-13 16:37:25 +00007528#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007529 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007530#endif
7531#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007532 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007533#endif
7534#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007535 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007536#endif
7537#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007539#endif
7540#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007541 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007542#endif
7543#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007545#endif
7546#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007547 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007548#endif
7549#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007551#endif
Fred Draked86ed291999-12-15 15:34:33 +00007552#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00007553 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007554#endif
Fred Drakec9680921999-12-13 16:37:25 +00007555#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00007556 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007557#endif
Fred Draked86ed291999-12-15 15:34:33 +00007558#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007559 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007560#endif
7561#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007562 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007563#endif
7564#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007566#endif
7567#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007568 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007569#endif
Fred Drakec9680921999-12-13 16:37:25 +00007570#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007572#endif
7573#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007574 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007575#endif
7576#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007578#endif
7579#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007580 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007581#endif
7582#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007583 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007584#endif
7585#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007586 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007587#endif
7588#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007589 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007590#endif
7591#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007592 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007593#endif
7594#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007596#endif
7597#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007599#endif
7600#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007602#endif
7603#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007604 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007605#endif
7606#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007608#endif
7609#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007611#endif
7612#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007614#endif
7615#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007617#endif
Fred Draked86ed291999-12-15 15:34:33 +00007618#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007619 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007620#endif
7621#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007623#endif
7624#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007626#endif
7627#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007628 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007629#endif
7630#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007631 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007632#endif
7633#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007635#endif
7636#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007637 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007638#endif
7639#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007641#endif
7642#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007643 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007644#endif
7645#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007646 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007647#endif
7648#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007650#endif
7651#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007652 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007653#endif
7654#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00007655 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007656#endif
Fred Drakec9680921999-12-13 16:37:25 +00007657};
7658
7659static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007660conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007661{
7662 return conv_confname(arg, valuep, posix_constants_confstr,
7663 sizeof(posix_constants_confstr)
7664 / sizeof(struct constdef));
7665}
7666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007667PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007668"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007669Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007670
7671static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007672posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007673{
7674 PyObject *result = NULL;
7675 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00007676 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00007677 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007678
Victor Stinnercb043522010-09-10 23:49:04 +00007679 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
7680 return NULL;
7681
7682 errno = 0;
7683 len = confstr(name, buffer, sizeof(buffer));
7684 if (len == 0) {
7685 if (errno) {
7686 posix_error();
7687 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00007688 }
7689 else {
Victor Stinnercb043522010-09-10 23:49:04 +00007690 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00007691 }
7692 }
Victor Stinnercb043522010-09-10 23:49:04 +00007693
7694 if ((unsigned int)len >= sizeof(buffer)) {
7695 char *buf = PyMem_Malloc(len);
7696 if (buf == NULL)
7697 return PyErr_NoMemory();
7698 confstr(name, buf, len);
7699 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
7700 PyMem_Free(buf);
7701 }
7702 else
7703 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007704 return result;
7705}
7706#endif
7707
7708
7709#ifdef HAVE_SYSCONF
7710static struct constdef posix_constants_sysconf[] = {
7711#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007713#endif
7714#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007716#endif
7717#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007718 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007719#endif
7720#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007721 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007722#endif
7723#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007724 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007725#endif
7726#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007728#endif
7729#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007731#endif
7732#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007734#endif
7735#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007736 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007737#endif
7738#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007739 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007740#endif
Fred Draked86ed291999-12-15 15:34:33 +00007741#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007742 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007743#endif
7744#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00007745 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007746#endif
Fred Drakec9680921999-12-13 16:37:25 +00007747#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007748 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007749#endif
Fred Drakec9680921999-12-13 16:37:25 +00007750#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007752#endif
7753#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007754 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007755#endif
7756#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007758#endif
7759#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007761#endif
7762#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007764#endif
Fred Draked86ed291999-12-15 15:34:33 +00007765#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007767#endif
Fred Drakec9680921999-12-13 16:37:25 +00007768#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007770#endif
7771#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007773#endif
7774#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007775 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007776#endif
7777#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007779#endif
7780#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007782#endif
Fred Draked86ed291999-12-15 15:34:33 +00007783#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007785#endif
Fred Drakec9680921999-12-13 16:37:25 +00007786#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007788#endif
7789#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007791#endif
7792#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007794#endif
7795#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007796 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007797#endif
7798#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007799 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007800#endif
7801#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007803#endif
7804#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007806#endif
7807#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007809#endif
7810#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007811 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007812#endif
7813#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007815#endif
7816#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007818#endif
7819#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007821#endif
7822#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007824#endif
7825#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007826 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007827#endif
7828#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007830#endif
7831#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007833#endif
7834#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007835 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007836#endif
7837#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007839#endif
7840#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007842#endif
7843#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007845#endif
7846#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007848#endif
7849#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007851#endif
7852#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007854#endif
Fred Draked86ed291999-12-15 15:34:33 +00007855#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007857#endif
Fred Drakec9680921999-12-13 16:37:25 +00007858#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007860#endif
7861#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007863#endif
7864#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007866#endif
Fred Draked86ed291999-12-15 15:34:33 +00007867#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007869#endif
Fred Drakec9680921999-12-13 16:37:25 +00007870#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007872#endif
Fred Draked86ed291999-12-15 15:34:33 +00007873#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007875#endif
7876#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007878#endif
Fred Drakec9680921999-12-13 16:37:25 +00007879#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007881#endif
7882#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007884#endif
7885#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007887#endif
7888#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007890#endif
Fred Draked86ed291999-12-15 15:34:33 +00007891#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007893#endif
Fred Drakec9680921999-12-13 16:37:25 +00007894#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007896#endif
7897#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007899#endif
7900#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007902#endif
7903#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007904 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007905#endif
7906#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007907 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007908#endif
7909#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007910 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007911#endif
7912#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007913 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007914#endif
Fred Draked86ed291999-12-15 15:34:33 +00007915#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007917#endif
Fred Drakec9680921999-12-13 16:37:25 +00007918#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007919 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007920#endif
7921#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007923#endif
Fred Draked86ed291999-12-15 15:34:33 +00007924#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007926#endif
Fred Drakec9680921999-12-13 16:37:25 +00007927#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007929#endif
7930#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007932#endif
7933#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007935#endif
7936#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007938#endif
7939#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007940 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007941#endif
7942#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007944#endif
7945#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007946 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007947#endif
7948#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007949 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007950#endif
7951#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007952 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007953#endif
Fred Draked86ed291999-12-15 15:34:33 +00007954#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007955 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007956#endif
7957#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007959#endif
Fred Drakec9680921999-12-13 16:37:25 +00007960#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007962#endif
7963#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007964 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007965#endif
7966#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007967 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007968#endif
7969#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007970 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007971#endif
7972#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007973 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007974#endif
7975#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007976 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007977#endif
7978#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007980#endif
7981#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007983#endif
7984#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007985 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007986#endif
7987#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007988 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007989#endif
7990#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007992#endif
7993#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007995#endif
7996#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007998#endif
7999#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008001#endif
8002#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008003 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
8005#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008006 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008007#endif
8008#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008009 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008010#endif
8011#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008012 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008013#endif
8014#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008016#endif
8017#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008018 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008019#endif
8020#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008022#endif
8023#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008025#endif
8026#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008028#endif
8029#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008031#endif
8032#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008034#endif
8035#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008037#endif
8038#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008040#endif
8041#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
8050#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008052#endif
8053#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008054 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008055#endif
8056#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008058#endif
8059#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008060 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008061#endif
8062#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008063 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008064#endif
Fred Draked86ed291999-12-15 15:34:33 +00008065#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008067#endif
Fred Drakec9680921999-12-13 16:37:25 +00008068#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008070#endif
8071#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008073#endif
8074#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008075 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008076#endif
8077#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008079#endif
8080#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008082#endif
8083#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008085#endif
8086#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
8092#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008093 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008094#endif
8095#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008096 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008097#endif
8098#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008099 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008100#endif
8101#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008103#endif
8104#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008105 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008106#endif
8107#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008108 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008109#endif
8110#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008111 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008112#endif
8113#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008114 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008115#endif
8116#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008117 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
8119#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008121#endif
8122#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
8128#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008130#endif
8131#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
8134#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008135 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008138 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
8146#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008148#endif
8149#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008151#endif
8152#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008153 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008154#endif
8155#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008156 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008157#endif
8158#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008159 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008160#endif
8161#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008162 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008163#endif
8164#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008165 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008166#endif
8167#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008169#endif
8170#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008171 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008172#endif
8173#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008174 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008175#endif
8176#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008178#endif
8179#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008180 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008181#endif
8182#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008183 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008184#endif
8185#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008186 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008187#endif
8188#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008189 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008190#endif
8191#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008192 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008193#endif
8194#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008195 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008196#endif
8197#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008198 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008199#endif
8200#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008201 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008202#endif
8203};
8204
8205static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008206conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008207{
8208 return conv_confname(arg, valuep, posix_constants_sysconf,
8209 sizeof(posix_constants_sysconf)
8210 / sizeof(struct constdef));
8211}
8212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008213PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008214"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008215Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008216
8217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008218posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008219{
8220 PyObject *result = NULL;
8221 int name;
8222
8223 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8224 int value;
8225
8226 errno = 0;
8227 value = sysconf(name);
8228 if (value == -1 && errno != 0)
8229 posix_error();
8230 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008231 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008232 }
8233 return result;
8234}
8235#endif
8236
8237
Fred Drakebec628d1999-12-15 18:31:10 +00008238/* This code is used to ensure that the tables of configuration value names
8239 * are in sorted order as required by conv_confname(), and also to build the
8240 * the exported dictionaries that are used to publish information about the
8241 * names available on the host platform.
8242 *
8243 * Sorting the table at runtime ensures that the table is properly ordered
8244 * when used, even for platforms we're not able to test on. It also makes
8245 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008246 */
Fred Drakebec628d1999-12-15 18:31:10 +00008247
8248static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008249cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008250{
8251 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008253 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008255
8256 return strcmp(c1->name, c2->name);
8257}
8258
8259static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008260setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00008261 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008262{
Fred Drakebec628d1999-12-15 18:31:10 +00008263 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008264 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008265
8266 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8267 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008268 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008270
Barry Warsaw3155db32000-04-13 15:20:40 +00008271 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 PyObject *o = PyLong_FromLong(table[i].value);
8273 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8274 Py_XDECREF(o);
8275 Py_DECREF(d);
8276 return -1;
8277 }
8278 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008279 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008280 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008281}
8282
Fred Drakebec628d1999-12-15 18:31:10 +00008283/* Return -1 on failure, 0 on success. */
8284static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008285setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008286{
8287#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008288 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008289 sizeof(posix_constants_pathconf)
8290 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008291 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008292 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008293#endif
8294#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008295 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008296 sizeof(posix_constants_confstr)
8297 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008298 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008299 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008300#endif
8301#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008302 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008303 sizeof(posix_constants_sysconf)
8304 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008305 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008306 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008307#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008308 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008309}
Fred Draked86ed291999-12-15 15:34:33 +00008310
8311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008312PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008313"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008314Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008315in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008316
8317static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008318posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008319{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008320 abort();
8321 /*NOTREACHED*/
8322 Py_FatalError("abort() called from Python code didn't abort!");
8323 return NULL;
8324}
Fred Drakebec628d1999-12-15 18:31:10 +00008325
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008326#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008327PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008328"startfile(filepath [, operation]) - Start a file with its associated\n\
8329application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008330\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008331When \"operation\" is not specified or \"open\", this acts like\n\
8332double-clicking the file in Explorer, or giving the file name as an\n\
8333argument to the DOS \"start\" command: the file is opened with whatever\n\
8334application (if any) its extension is associated.\n\
8335When another \"operation\" is given, it specifies what should be done with\n\
8336the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008337\n\
8338startfile returns as soon as the associated application is launched.\n\
8339There is no option to wait for the application to close, and no way\n\
8340to retrieve the application's exit status.\n\
8341\n\
8342The filepath is relative to the current directory. If you want to use\n\
8343an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008344the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008345
8346static PyObject *
8347win32_startfile(PyObject *self, PyObject *args)
8348{
Victor Stinner8c62be82010-05-06 00:08:46 +00008349 PyObject *ofilepath;
8350 char *filepath;
8351 char *operation = NULL;
8352 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00008353
Victor Stinner8c62be82010-05-06 00:08:46 +00008354 PyObject *unipath, *woperation = NULL;
8355 if (!PyArg_ParseTuple(args, "U|s:startfile",
8356 &unipath, &operation)) {
8357 PyErr_Clear();
8358 goto normal;
8359 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008360
Victor Stinner8c62be82010-05-06 00:08:46 +00008361 if (operation) {
8362 woperation = PyUnicode_DecodeASCII(operation,
8363 strlen(operation), NULL);
8364 if (!woperation) {
8365 PyErr_Clear();
8366 operation = NULL;
8367 goto normal;
8368 }
8369 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008370
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 Py_BEGIN_ALLOW_THREADS
8372 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8373 PyUnicode_AS_UNICODE(unipath),
8374 NULL, NULL, SW_SHOWNORMAL);
8375 Py_END_ALLOW_THREADS
8376
8377 Py_XDECREF(woperation);
8378 if (rc <= (HINSTANCE)32) {
8379 PyObject *errval = win32_error_unicode("startfile",
8380 PyUnicode_AS_UNICODE(unipath));
8381 return errval;
8382 }
8383 Py_INCREF(Py_None);
8384 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008385
8386normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00008387 if (!PyArg_ParseTuple(args, "O&|s:startfile",
8388 PyUnicode_FSConverter, &ofilepath,
8389 &operation))
8390 return NULL;
8391 filepath = PyBytes_AsString(ofilepath);
8392 Py_BEGIN_ALLOW_THREADS
8393 rc = ShellExecute((HWND)0, operation, filepath,
8394 NULL, NULL, SW_SHOWNORMAL);
8395 Py_END_ALLOW_THREADS
8396 if (rc <= (HINSTANCE)32) {
8397 PyObject *errval = win32_error("startfile", filepath);
8398 Py_DECREF(ofilepath);
8399 return errval;
8400 }
8401 Py_DECREF(ofilepath);
8402 Py_INCREF(Py_None);
8403 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008404}
8405#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008406
Martin v. Löwis438b5342002-12-27 10:16:42 +00008407#ifdef HAVE_GETLOADAVG
8408PyDoc_STRVAR(posix_getloadavg__doc__,
8409"getloadavg() -> (float, float, float)\n\n\
8410Return the number of processes in the system run queue averaged over\n\
8411the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8412was unobtainable");
8413
8414static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008415posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008416{
8417 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008418 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008419 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8420 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008421 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00008422 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008423}
8424#endif
8425
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008426#ifdef MS_WINDOWS
8427
8428PyDoc_STRVAR(win32_urandom__doc__,
8429"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008430Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008431
8432typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8433 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8434 DWORD dwFlags );
8435typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8436 BYTE *pbBuffer );
8437
8438static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00008439/* This handle is never explicitly released. Instead, the operating
8440 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008441static HCRYPTPROV hCryptProv = 0;
8442
Tim Peters4ad82172004-08-30 17:02:04 +00008443static PyObject*
8444win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008445{
Victor Stinner8c62be82010-05-06 00:08:46 +00008446 int howMany;
8447 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008448
Victor Stinner8c62be82010-05-06 00:08:46 +00008449 /* Read arguments */
8450 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8451 return NULL;
8452 if (howMany < 0)
8453 return PyErr_Format(PyExc_ValueError,
8454 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008455
Victor Stinner8c62be82010-05-06 00:08:46 +00008456 if (hCryptProv == 0) {
8457 HINSTANCE hAdvAPI32 = NULL;
8458 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008459
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 /* Obtain handle to the DLL containing CryptoAPI
8461 This should not fail */
8462 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8463 if(hAdvAPI32 == NULL)
8464 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008465
Victor Stinner8c62be82010-05-06 00:08:46 +00008466 /* Obtain pointers to the CryptoAPI functions
8467 This will fail on some early versions of Win95 */
8468 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8469 hAdvAPI32,
8470 "CryptAcquireContextA");
8471 if (pCryptAcquireContext == NULL)
8472 return PyErr_Format(PyExc_NotImplementedError,
8473 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008474
Victor Stinner8c62be82010-05-06 00:08:46 +00008475 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8476 hAdvAPI32, "CryptGenRandom");
8477 if (pCryptGenRandom == NULL)
8478 return PyErr_Format(PyExc_NotImplementedError,
8479 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008480
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 /* Acquire context */
8482 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8483 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8484 return win32_error("CryptAcquireContext", NULL);
8485 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008486
Victor Stinner8c62be82010-05-06 00:08:46 +00008487 /* Allocate bytes */
8488 result = PyBytes_FromStringAndSize(NULL, howMany);
8489 if (result != NULL) {
8490 /* Get random data */
8491 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
8492 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8493 PyBytes_AS_STRING(result))) {
8494 Py_DECREF(result);
8495 return win32_error("CryptGenRandom", NULL);
8496 }
8497 }
8498 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008499}
8500#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008501
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008502PyDoc_STRVAR(device_encoding__doc__,
8503"device_encoding(fd) -> str\n\n\
8504Return a string describing the encoding of the device\n\
8505if the output is a terminal; else return None.");
8506
8507static PyObject *
8508device_encoding(PyObject *self, PyObject *args)
8509{
Victor Stinner8c62be82010-05-06 00:08:46 +00008510 int fd;
8511 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
8512 return NULL;
8513 if (!_PyVerify_fd(fd) || !isatty(fd)) {
8514 Py_INCREF(Py_None);
8515 return Py_None;
8516 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008517#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 if (fd == 0) {
8519 char buf[100];
8520 sprintf(buf, "cp%d", GetConsoleCP());
8521 return PyUnicode_FromString(buf);
8522 }
8523 if (fd == 1 || fd == 2) {
8524 char buf[100];
8525 sprintf(buf, "cp%d", GetConsoleOutputCP());
8526 return PyUnicode_FromString(buf);
8527 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008528#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00008529 {
8530 char *codeset = nl_langinfo(CODESET);
8531 if (codeset != NULL && codeset[0] != 0)
8532 return PyUnicode_FromString(codeset);
8533 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008534#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008535 Py_INCREF(Py_None);
8536 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008537}
8538
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008539#ifdef __VMS
8540/* Use openssl random routine */
8541#include <openssl/rand.h>
8542PyDoc_STRVAR(vms_urandom__doc__,
8543"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008544Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008545
8546static PyObject*
8547vms_urandom(PyObject *self, PyObject *args)
8548{
Victor Stinner8c62be82010-05-06 00:08:46 +00008549 int howMany;
8550 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008551
Victor Stinner8c62be82010-05-06 00:08:46 +00008552 /* Read arguments */
8553 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8554 return NULL;
8555 if (howMany < 0)
8556 return PyErr_Format(PyExc_ValueError,
8557 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008558
Victor Stinner8c62be82010-05-06 00:08:46 +00008559 /* Allocate bytes */
8560 result = PyBytes_FromStringAndSize(NULL, howMany);
8561 if (result != NULL) {
8562 /* Get random data */
8563 if (RAND_pseudo_bytes((unsigned char*)
8564 PyBytes_AS_STRING(result),
8565 howMany) < 0) {
8566 Py_DECREF(result);
8567 return PyErr_Format(PyExc_ValueError,
8568 "RAND_pseudo_bytes");
8569 }
8570 }
8571 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008572}
8573#endif
8574
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008575#ifdef HAVE_SETRESUID
8576PyDoc_STRVAR(posix_setresuid__doc__,
8577"setresuid(ruid, euid, suid)\n\n\
8578Set the current process's real, effective, and saved user ids.");
8579
8580static PyObject*
8581posix_setresuid (PyObject *self, PyObject *args)
8582{
Victor Stinner8c62be82010-05-06 00:08:46 +00008583 /* We assume uid_t is no larger than a long. */
8584 long ruid, euid, suid;
8585 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
8586 return NULL;
8587 if (setresuid(ruid, euid, suid) < 0)
8588 return posix_error();
8589 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008590}
8591#endif
8592
8593#ifdef HAVE_SETRESGID
8594PyDoc_STRVAR(posix_setresgid__doc__,
8595"setresgid(rgid, egid, sgid)\n\n\
8596Set the current process's real, effective, and saved group ids.");
8597
8598static PyObject*
8599posix_setresgid (PyObject *self, PyObject *args)
8600{
Victor Stinner8c62be82010-05-06 00:08:46 +00008601 /* We assume uid_t is no larger than a long. */
8602 long rgid, egid, sgid;
8603 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
8604 return NULL;
8605 if (setresgid(rgid, egid, sgid) < 0)
8606 return posix_error();
8607 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008608}
8609#endif
8610
8611#ifdef HAVE_GETRESUID
8612PyDoc_STRVAR(posix_getresuid__doc__,
8613"getresuid() -> (ruid, euid, suid)\n\n\
8614Get tuple of the current process's real, effective, and saved user ids.");
8615
8616static PyObject*
8617posix_getresuid (PyObject *self, PyObject *noargs)
8618{
Victor Stinner8c62be82010-05-06 00:08:46 +00008619 uid_t ruid, euid, suid;
8620 long l_ruid, l_euid, l_suid;
8621 if (getresuid(&ruid, &euid, &suid) < 0)
8622 return posix_error();
8623 /* Force the values into long's as we don't know the size of uid_t. */
8624 l_ruid = ruid;
8625 l_euid = euid;
8626 l_suid = suid;
8627 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008628}
8629#endif
8630
8631#ifdef HAVE_GETRESGID
8632PyDoc_STRVAR(posix_getresgid__doc__,
8633"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00008634Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008635
8636static PyObject*
8637posix_getresgid (PyObject *self, PyObject *noargs)
8638{
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 uid_t rgid, egid, sgid;
8640 long l_rgid, l_egid, l_sgid;
8641 if (getresgid(&rgid, &egid, &sgid) < 0)
8642 return posix_error();
8643 /* Force the values into long's as we don't know the size of uid_t. */
8644 l_rgid = rgid;
8645 l_egid = egid;
8646 l_sgid = sgid;
8647 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008648}
8649#endif
8650
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008651/* Posix *at family of functions:
8652 faccessat, fchmodat, fchownat, fstatat, futimesat,
8653 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
8654 unlinkat, utimensat, mkfifoat */
8655
8656#ifdef HAVE_FACCESSAT
8657PyDoc_STRVAR(posix_faccessat__doc__,
8658"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
8659Like access() but if path is relative, it is taken as relative to dirfd.\n\
8660flags is optional and can be constructed by ORing together zero or more\n\
8661of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
8662If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8663is interpreted relative to the current working directory.");
8664
8665static PyObject *
8666posix_faccessat(PyObject *self, PyObject *args)
8667{
8668 PyObject *opath;
8669 char *path;
8670 int mode;
8671 int res;
8672 int dirfd, flags = 0;
8673 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
8674 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8675 return NULL;
8676 path = PyBytes_AsString(opath);
8677 Py_BEGIN_ALLOW_THREADS
8678 res = faccessat(dirfd, path, mode, flags);
8679 Py_END_ALLOW_THREADS
8680 Py_DECREF(opath);
8681 return PyBool_FromLong(res == 0);
8682}
8683#endif
8684
8685#ifdef HAVE_FCHMODAT
8686PyDoc_STRVAR(posix_fchmodat__doc__,
8687"fchmodat(dirfd, path, mode, flags=0)\n\n\
8688Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
8689flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8690If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8691is interpreted relative to the current working directory.");
8692
8693static PyObject *
8694posix_fchmodat(PyObject *self, PyObject *args)
8695{
8696 int dirfd, mode, res;
8697 int flags = 0;
8698 PyObject *opath;
8699 char *path;
8700
8701 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
8702 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8703 return NULL;
8704
8705 path = PyBytes_AsString(opath);
8706
8707 Py_BEGIN_ALLOW_THREADS
8708 res = fchmodat(dirfd, path, mode, flags);
8709 Py_END_ALLOW_THREADS
8710 Py_DECREF(opath);
8711 if (res < 0)
8712 return posix_error();
8713 Py_RETURN_NONE;
8714}
8715#endif /* HAVE_FCHMODAT */
8716
8717#ifdef HAVE_FCHOWNAT
8718PyDoc_STRVAR(posix_fchownat__doc__,
8719"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
8720Like chown() but if path is relative, it is taken as relative to dirfd.\n\
8721flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8722If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8723is interpreted relative to the current working directory.");
8724
8725static PyObject *
8726posix_fchownat(PyObject *self, PyObject *args)
8727{
8728 PyObject *opath;
8729 int dirfd, res;
8730 long uid, gid;
8731 int flags = 0;
8732 char *path;
8733
8734 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
8735 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
8736 return NULL;
8737
8738 path = PyBytes_AsString(opath);
8739
8740 Py_BEGIN_ALLOW_THREADS
8741 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
8742 Py_END_ALLOW_THREADS
8743 Py_DECREF(opath);
8744 if (res < 0)
8745 return posix_error();
8746 Py_RETURN_NONE;
8747}
8748#endif /* HAVE_FCHOWNAT */
8749
8750#ifdef HAVE_FSTATAT
8751PyDoc_STRVAR(posix_fstatat__doc__,
8752"fstatat(dirfd, path, flags=0) -> stat result\n\n\
8753Like stat() but if path is relative, it is taken as relative to dirfd.\n\
8754flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8755If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8756is interpreted relative to the current working directory.");
8757
8758static PyObject *
8759posix_fstatat(PyObject *self, PyObject *args)
8760{
8761 PyObject *opath;
8762 char *path;
8763 STRUCT_STAT st;
8764 int dirfd, res, flags = 0;
8765
8766 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
8767 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8768 return NULL;
8769 path = PyBytes_AsString(opath);
8770
8771 Py_BEGIN_ALLOW_THREADS
8772 res = fstatat(dirfd, path, &st, flags);
8773 Py_END_ALLOW_THREADS
8774 Py_DECREF(opath);
8775 if (res != 0)
8776 return posix_error();
8777
8778 return _pystat_fromstructstat(&st);
8779}
8780#endif
8781
8782#ifdef HAVE_FUTIMESAT
8783PyDoc_STRVAR(posix_futimesat__doc__,
8784"futimesat(dirfd, path, (atime, mtime))\n\
8785futimesat(dirfd, path, None)\n\n\
8786Like utime() but if path is relative, it is taken as relative to dirfd.\n\
8787If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8788is interpreted relative to the current working directory.");
8789
8790static PyObject *
8791posix_futimesat(PyObject *self, PyObject *args)
8792{
8793 PyObject *opath;
8794 char *path;
8795 int res, dirfd;
8796 PyObject* arg;
8797
8798 struct timeval buf[2];
8799
8800 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
8801 &dirfd, PyUnicode_FSConverter, &opath, &arg))
8802 return NULL;
8803 path = PyBytes_AsString(opath);
8804 if (arg == Py_None) {
8805 /* optional time values not given */
8806 Py_BEGIN_ALLOW_THREADS
8807 res = futimesat(dirfd, path, NULL);
8808 Py_END_ALLOW_THREADS
8809 }
8810 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
8811 PyErr_SetString(PyExc_TypeError,
8812 "futimesat() arg 3 must be a tuple (atime, mtime)");
8813 Py_DECREF(opath);
8814 return NULL;
8815 }
8816 else {
8817 if (extract_time(PyTuple_GET_ITEM(arg, 0),
8818 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
8819 Py_DECREF(opath);
8820 return NULL;
8821 }
8822 if (extract_time(PyTuple_GET_ITEM(arg, 1),
8823 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
8824 Py_DECREF(opath);
8825 return NULL;
8826 }
8827 Py_BEGIN_ALLOW_THREADS
8828 res = futimesat(dirfd, path, buf);
8829 Py_END_ALLOW_THREADS
8830 }
8831 Py_DECREF(opath);
8832 if (res < 0) {
8833 return posix_error();
8834 }
8835 Py_RETURN_NONE;
8836}
8837#endif
8838
8839#ifdef HAVE_LINKAT
8840PyDoc_STRVAR(posix_linkat__doc__,
8841"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
8842Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
8843and if dstpath is relative, it is taken as relative to dstfd.\n\
8844flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
8845If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
8846srcpath is interpreted relative to the current working directory. This\n\
8847also applies for dstpath.");
8848
8849static PyObject *
8850posix_linkat(PyObject *self, PyObject *args)
8851{
8852 PyObject *osrc, *odst;
8853 char *src, *dst;
8854 int res, srcfd, dstfd;
8855 int flags = 0;
8856
8857 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
8858 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
8859 return NULL;
8860 src = PyBytes_AsString(osrc);
8861 dst = PyBytes_AsString(odst);
8862 Py_BEGIN_ALLOW_THREADS
8863 res = linkat(srcfd, src, dstfd, dst, flags);
8864 Py_END_ALLOW_THREADS
8865 Py_DECREF(osrc);
8866 Py_DECREF(odst);
8867 if (res < 0)
8868 return posix_error();
8869 Py_RETURN_NONE;
8870}
8871#endif /* HAVE_LINKAT */
8872
8873#ifdef HAVE_MKDIRAT
8874PyDoc_STRVAR(posix_mkdirat__doc__,
8875"mkdirat(dirfd, path, mode=0o777)\n\n\
8876Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
8877If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8878is interpreted relative to the current working directory.");
8879
8880static PyObject *
8881posix_mkdirat(PyObject *self, PyObject *args)
8882{
8883 int res, dirfd;
8884 PyObject *opath;
8885 char *path;
8886 int mode = 0777;
8887
8888 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
8889 &dirfd, PyUnicode_FSConverter, &opath, &mode))
8890 return NULL;
8891 path = PyBytes_AsString(opath);
8892 Py_BEGIN_ALLOW_THREADS
8893 res = mkdirat(dirfd, path, mode);
8894 Py_END_ALLOW_THREADS
8895 Py_DECREF(opath);
8896 if (res < 0)
8897 return posix_error();
8898 Py_RETURN_NONE;
8899}
8900#endif
8901
8902#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
8903PyDoc_STRVAR(posix_mknodat__doc__,
8904"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
8905Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
8906If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8907is interpreted relative to the current working directory.");
8908
8909static PyObject *
8910posix_mknodat(PyObject *self, PyObject *args)
8911{
8912 PyObject *opath;
8913 char *filename;
8914 int mode = 0600;
8915 int device = 0;
8916 int res, dirfd;
8917 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
8918 PyUnicode_FSConverter, &opath, &mode, &device))
8919 return NULL;
8920 filename = PyBytes_AS_STRING(opath);
8921 Py_BEGIN_ALLOW_THREADS
8922 res = mknodat(dirfd, filename, mode, device);
8923 Py_END_ALLOW_THREADS
8924 Py_DECREF(opath);
8925 if (res < 0)
8926 return posix_error();
8927 Py_RETURN_NONE;
8928}
8929#endif
8930
8931#ifdef HAVE_OPENAT
8932PyDoc_STRVAR(posix_openat__doc__,
8933"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
8934Like open() but if path is relative, it is taken as relative to dirfd.\n\
8935If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8936is interpreted relative to the current working directory.");
8937
8938static PyObject *
8939posix_openat(PyObject *self, PyObject *args)
8940{
8941 PyObject *ofile;
8942 char *file;
8943 int flag, dirfd, fd;
8944 int mode = 0777;
8945
8946 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
8947 &dirfd, PyUnicode_FSConverter, &ofile,
8948 &flag, &mode))
8949 return NULL;
8950 file = PyBytes_AsString(ofile);
8951 Py_BEGIN_ALLOW_THREADS
8952 fd = openat(dirfd, file, flag, mode);
8953 Py_END_ALLOW_THREADS
8954 Py_DECREF(ofile);
8955 if (fd < 0)
8956 return posix_error();
8957 return PyLong_FromLong((long)fd);
8958}
8959#endif
8960
8961#ifdef HAVE_READLINKAT
8962PyDoc_STRVAR(posix_readlinkat__doc__,
8963"readlinkat(dirfd, path) -> path\n\n\
8964Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
8965If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8966is interpreted relative to the current working directory.");
8967
8968static PyObject *
8969posix_readlinkat(PyObject *self, PyObject *args)
8970{
8971 PyObject *v, *opath;
8972 char buf[MAXPATHLEN];
8973 char *path;
8974 int n, dirfd;
8975 int arg_is_unicode = 0;
8976
8977 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
8978 &dirfd, PyUnicode_FSConverter, &opath))
8979 return NULL;
8980 path = PyBytes_AsString(opath);
8981 v = PySequence_GetItem(args, 1);
8982 if (v == NULL) {
8983 Py_DECREF(opath);
8984 return NULL;
8985 }
8986
8987 if (PyUnicode_Check(v)) {
8988 arg_is_unicode = 1;
8989 }
8990 Py_DECREF(v);
8991
8992 Py_BEGIN_ALLOW_THREADS
8993 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
8994 Py_END_ALLOW_THREADS
8995 Py_DECREF(opath);
8996 if (n < 0)
8997 return posix_error();
8998
8999 if (arg_is_unicode)
9000 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9001 else
9002 return PyBytes_FromStringAndSize(buf, n);
9003}
9004#endif /* HAVE_READLINKAT */
9005
9006#ifdef HAVE_RENAMEAT
9007PyDoc_STRVAR(posix_renameat__doc__,
9008"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9009Like rename() but if oldpath is relative, it is taken as relative to\n\
9010olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9011If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9012oldpath is interpreted relative to the current working directory. This\n\
9013also applies for newpath.");
9014
9015static PyObject *
9016posix_renameat(PyObject *self, PyObject *args)
9017{
9018 int res;
9019 PyObject *opathold, *opathnew;
9020 char *opath, *npath;
9021 int oldfd, newfd;
9022
9023 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9024 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9025 return NULL;
9026 opath = PyBytes_AsString(opathold);
9027 npath = PyBytes_AsString(opathnew);
9028 Py_BEGIN_ALLOW_THREADS
9029 res = renameat(oldfd, opath, newfd, npath);
9030 Py_END_ALLOW_THREADS
9031 Py_DECREF(opathold);
9032 Py_DECREF(opathnew);
9033 if (res < 0)
9034 return posix_error();
9035 Py_RETURN_NONE;
9036}
9037#endif
9038
9039#if HAVE_SYMLINKAT
9040PyDoc_STRVAR(posix_symlinkat__doc__,
9041"symlinkat(src, dstfd, dst)\n\n\
9042Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9043If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9044is interpreted relative to the current working directory.");
9045
9046static PyObject *
9047posix_symlinkat(PyObject *self, PyObject *args)
9048{
9049 int res, dstfd;
9050 PyObject *osrc, *odst;
9051 char *src, *dst;
9052
9053 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9054 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9055 return NULL;
9056 src = PyBytes_AsString(osrc);
9057 dst = PyBytes_AsString(odst);
9058 Py_BEGIN_ALLOW_THREADS
9059 res = symlinkat(src, dstfd, dst);
9060 Py_END_ALLOW_THREADS
9061 Py_DECREF(osrc);
9062 Py_DECREF(odst);
9063 if (res < 0)
9064 return posix_error();
9065 Py_RETURN_NONE;
9066}
9067#endif /* HAVE_SYMLINKAT */
9068
9069#ifdef HAVE_UNLINKAT
9070PyDoc_STRVAR(posix_unlinkat__doc__,
9071"unlinkat(dirfd, path, flags=0)\n\n\
9072Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9073flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9074specified, unlinkat() behaves like rmdir().\n\
9075If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9076is interpreted relative to the current working directory.");
9077
9078static PyObject *
9079posix_unlinkat(PyObject *self, PyObject *args)
9080{
9081 int dirfd, res, flags = 0;
9082 PyObject *opath;
9083 char *path;
9084
9085 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9086 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9087 return NULL;
9088 path = PyBytes_AsString(opath);
9089 Py_BEGIN_ALLOW_THREADS
9090 res = unlinkat(dirfd, path, flags);
9091 Py_END_ALLOW_THREADS
9092 Py_DECREF(opath);
9093 if (res < 0)
9094 return posix_error();
9095 Py_RETURN_NONE;
9096}
9097#endif
9098
9099#ifdef HAVE_UTIMENSAT
9100PyDoc_STRVAR(posix_utimensat__doc__,
9101"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9102 (mtime_sec, mtime_nsec), flags)\n\
9103utimensat(dirfd, path, None, None, flags)\n\n\
9104Updates the timestamps of a file with nanosecond precision. If path is\n\
9105relative, it is taken as relative to dirfd.\n\
9106The second form sets atime and mtime to the current time.\n\
9107flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9108If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9109is interpreted relative to the current working directory.\n\
9110If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9111current time.\n\
9112If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9113
9114static PyObject *
9115posix_utimensat(PyObject *self, PyObject *args)
9116{
9117 PyObject *opath;
9118 char *path;
9119 int res, dirfd, flags = 0;
9120 PyObject *atime, *mtime;
9121
9122 struct timespec buf[2];
9123
9124 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9125 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9126 return NULL;
9127 path = PyBytes_AsString(opath);
9128 if (atime == Py_None && mtime == Py_None) {
9129 /* optional time values not given */
9130 Py_BEGIN_ALLOW_THREADS
9131 res = utimensat(dirfd, path, NULL, flags);
9132 Py_END_ALLOW_THREADS
9133 }
9134 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9135 PyErr_SetString(PyExc_TypeError,
9136 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9137 Py_DECREF(opath);
9138 return NULL;
9139 }
9140 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9141 PyErr_SetString(PyExc_TypeError,
9142 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9143 Py_DECREF(opath);
9144 return NULL;
9145 }
9146 else {
9147 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9148 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9149 Py_DECREF(opath);
9150 return NULL;
9151 }
9152 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9153 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9154 Py_DECREF(opath);
9155 return NULL;
9156 }
9157 Py_BEGIN_ALLOW_THREADS
9158 res = utimensat(dirfd, path, buf, flags);
9159 Py_END_ALLOW_THREADS
9160 }
9161 Py_DECREF(opath);
9162 if (res < 0) {
9163 return posix_error();
9164 }
9165 Py_RETURN_NONE;
9166}
9167#endif
9168
9169#ifdef HAVE_MKFIFOAT
9170PyDoc_STRVAR(posix_mkfifoat__doc__,
9171"mkfifoat(dirfd, path, mode=0o666)\n\n\
9172Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9173If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9174is interpreted relative to the current working directory.");
9175
9176static PyObject *
9177posix_mkfifoat(PyObject *self, PyObject *args)
9178{
9179 PyObject *opath;
9180 char *filename;
9181 int mode = 0666;
9182 int res, dirfd;
9183 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9184 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9185 return NULL;
9186 filename = PyBytes_AS_STRING(opath);
9187 Py_BEGIN_ALLOW_THREADS
9188 res = mkfifoat(dirfd, filename, mode);
9189 Py_END_ALLOW_THREADS
9190 Py_DECREF(opath);
9191 if (res < 0)
9192 return posix_error();
9193 Py_RETURN_NONE;
9194}
9195#endif
9196
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009197static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009198 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009199#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009200 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009201#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009202 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009203#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009204 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009205#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009206 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009207#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009208 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009209#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009210#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009211 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009212#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009213#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009214 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009215#endif /* HAVE_LCHMOD */
9216#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009218#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009219#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009220 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009221#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009222#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009223 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009224#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009225#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009226 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009227#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009228#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009229 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009230#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009231#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009232 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9233 METH_NOARGS, posix_getcwd__doc__},
9234 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9235 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009236#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009237#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009238 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009239#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009240 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009241#ifdef HAVE_FDOPENDIR
9242 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
9243#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009244 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
9245 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009246#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00009247 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009248#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009249#ifdef HAVE_GETPRIORITY
9250 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
9251#endif /* HAVE_GETPRIORITY */
9252#ifdef HAVE_SETPRIORITY
9253 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
9254#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009255#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009256 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009257#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00009258#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009259 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00009260#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009261 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
9262 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
9263 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00009264 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00009265#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00009266 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009267#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00009268#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009269 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00009270 win_symlink__doc__},
9271#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009272#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00009273 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009274#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009275 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009276#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009277 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009278#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00009279 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
9280 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
9281 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009282#ifdef HAVE_FUTIMES
9283 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
9284#endif
9285#ifdef HAVE_LUTIMES
9286 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
9287#endif
9288#ifdef HAVE_FUTIMENS
9289 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
9290#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009291#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00009292 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009293#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00009294 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009295#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00009296 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
9297 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009298#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009299#ifdef HAVE_FEXECVE
9300 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
9301#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00009302#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00009303 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
9304 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009305#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00009306 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
9307 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009308#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00009309#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009310#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00009311 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009312#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009313#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00009314 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009315#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009316#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009318#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00009319#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009320 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00009321#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009322#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009323 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009324#endif /* HAVE_GETEGID */
9325#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009326 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009327#endif /* HAVE_GETEUID */
9328#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009329 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009330#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00009331#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009332 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009333#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009334 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009335#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009336 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009337#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009338#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009339 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009340#endif /* HAVE_GETPPID */
9341#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009342 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009343#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00009344#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009345 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00009346#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00009347#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00009348 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009349#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009350#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00009351 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009352#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00009353#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009354 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00009355#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00009356#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009357 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
9358 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00009359 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00009360#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009361#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009363#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009364#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009365 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009366#endif /* HAVE_SETEUID */
9367#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009368 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009369#endif /* HAVE_SETEGID */
9370#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009371 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009372#endif /* HAVE_SETREUID */
9373#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009374 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009375#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009376#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009378#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009379#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009380 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009381#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00009382#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009383 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00009384#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00009385#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009386 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00009387#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009388#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009389 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009390#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009391#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009392 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009393#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009394#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00009395 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009396#endif /* HAVE_WAIT3 */
9397#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00009398 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009399#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009400#if defined(HAVE_WAITID) && !defined(__APPLE__)
9401 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
9402#endif
Tim Petersab034fa2002-02-01 11:27:43 +00009403#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009405#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009406#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009407 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009408#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009409#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009410 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009411#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009412#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009413 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009414#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009415#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009416 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009417#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009418#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009419 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009420#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00009421 {"open", posix_open, METH_VARARGS, posix_open__doc__},
9422 {"close", posix_close, METH_VARARGS, posix_close__doc__},
9423 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
9424 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
9425 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
9426 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009427#ifdef HAVE_LOCKF
9428 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
9429#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009430 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
9431 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009432#ifdef HAVE_READV
9433 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
9434#endif
9435#ifdef HAVE_PREAD
9436 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
9437#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009438 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009439#ifdef HAVE_WRITEV
9440 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
9441#endif
9442#ifdef HAVE_PWRITE
9443 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
9444#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009445#ifdef HAVE_SENDFILE
9446 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
9447 posix_sendfile__doc__},
9448#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009449 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
9450 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009451#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009452 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009453#endif
9454#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00009455 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009456#endif
Neal Norwitz11690112002-07-30 01:08:28 +00009457#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00009458 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00009459#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009460#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00009461 {"major", posix_major, METH_VARARGS, posix_major__doc__},
9462 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
9463 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009464#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009465#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00009466 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009467#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009468#ifdef HAVE_TRUNCATE
9469 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
9470#endif
9471#ifdef HAVE_POSIX_FALLOCATE
9472 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
9473#endif
9474#ifdef HAVE_POSIX_FADVISE
9475 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
9476#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009477#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009478 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009479#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009480#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009481 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00009482#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009483 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009484#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00009485 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009486#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009487#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009488 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009489#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009490#ifdef HAVE_SYNC
9491 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
9492#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009493#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009494 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009495#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00009496#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009497#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00009498 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00009499#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009500#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009501 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009502#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00009503#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00009504 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009505#endif /* WIFSTOPPED */
9506#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00009507 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009508#endif /* WIFSIGNALED */
9509#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00009510 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009511#endif /* WIFEXITED */
9512#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00009513 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009514#endif /* WEXITSTATUS */
9515#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009516 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009517#endif /* WTERMSIG */
9518#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009519 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009520#endif /* WSTOPSIG */
9521#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00009522#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009523 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009524#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00009525#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009526 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009527#endif
Fred Drakec9680921999-12-13 16:37:25 +00009528#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00009529 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009530#endif
9531#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009533#endif
9534#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009535 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009536#endif
9537#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009538 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009539#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009540 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009541#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009542 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00009543 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00009544 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00009545#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009546#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00009547 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00009548#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009549 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009550 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009551 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009552 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00009553 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009554 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009555#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009556 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009557#endif
9558#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009559 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009560#endif
9561#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009562 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009563#endif
9564#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009565 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009566#endif
9567
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009568/* posix *at family of functions */
9569#ifdef HAVE_FACCESSAT
9570 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
9571#endif
9572#ifdef HAVE_FCHMODAT
9573 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
9574#endif /* HAVE_FCHMODAT */
9575#ifdef HAVE_FCHOWNAT
9576 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
9577#endif /* HAVE_FCHOWNAT */
9578#ifdef HAVE_FSTATAT
9579 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
9580#endif
9581#ifdef HAVE_FUTIMESAT
9582 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
9583#endif
9584#ifdef HAVE_LINKAT
9585 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
9586#endif /* HAVE_LINKAT */
9587#ifdef HAVE_MKDIRAT
9588 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
9589#endif
9590#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9591 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
9592#endif
9593#ifdef HAVE_OPENAT
9594 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
9595#endif
9596#ifdef HAVE_READLINKAT
9597 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
9598#endif /* HAVE_READLINKAT */
9599#ifdef HAVE_RENAMEAT
9600 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
9601#endif
9602#if HAVE_SYMLINKAT
9603 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
9604#endif /* HAVE_SYMLINKAT */
9605#ifdef HAVE_UNLINKAT
9606 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
9607#endif
9608#ifdef HAVE_UTIMENSAT
9609 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
9610#endif
9611#ifdef HAVE_MKFIFOAT
9612 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
9613#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009614 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009615};
9616
9617
Barry Warsaw4a342091996-12-19 23:50:02 +00009618static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009619ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009620{
Victor Stinner8c62be82010-05-06 00:08:46 +00009621 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009622}
9623
Guido van Rossumd48f2521997-12-05 22:19:34 +00009624#if defined(PYOS_OS2)
9625/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009626static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009627{
9628 APIRET rc;
9629 ULONG values[QSV_MAX+1];
9630 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009631 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009632
9633 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009634 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009635 Py_END_ALLOW_THREADS
9636
9637 if (rc != NO_ERROR) {
9638 os2_error(rc);
9639 return -1;
9640 }
9641
Fred Drake4d1e64b2002-04-15 19:40:07 +00009642 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9643 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9644 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9645 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9646 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9647 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9648 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009649
9650 switch (values[QSV_VERSION_MINOR]) {
9651 case 0: ver = "2.00"; break;
9652 case 10: ver = "2.10"; break;
9653 case 11: ver = "2.11"; break;
9654 case 30: ver = "3.00"; break;
9655 case 40: ver = "4.00"; break;
9656 case 50: ver = "5.00"; break;
9657 default:
Tim Peters885d4572001-11-28 20:27:42 +00009658 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00009659 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009660 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009661 ver = &tmp[0];
9662 }
9663
9664 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009665 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009666 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009667
9668 /* Add Indicator of Which Drive was Used to Boot the System */
9669 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9670 tmp[1] = ':';
9671 tmp[2] = '\0';
9672
Fred Drake4d1e64b2002-04-15 19:40:07 +00009673 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009674}
9675#endif
9676
Brian Curtin52173d42010-12-02 18:29:18 +00009677#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009678static int
Brian Curtin52173d42010-12-02 18:29:18 +00009679enable_symlink()
9680{
9681 HANDLE tok;
9682 TOKEN_PRIVILEGES tok_priv;
9683 LUID luid;
9684 int meth_idx = 0;
9685
9686 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009687 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009688
9689 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009690 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009691
9692 tok_priv.PrivilegeCount = 1;
9693 tok_priv.Privileges[0].Luid = luid;
9694 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
9695
9696 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
9697 sizeof(TOKEN_PRIVILEGES),
9698 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009699 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009700
Brian Curtin3b4499c2010-12-28 14:31:47 +00009701 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
9702 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00009703}
9704#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
9705
Barry Warsaw4a342091996-12-19 23:50:02 +00009706static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009707all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009708{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009709#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009710 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009711#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009712#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009713 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009714#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009715#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009716 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009717#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009718#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009719 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009720#endif
Fred Drakec9680921999-12-13 16:37:25 +00009721#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009722 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009723#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009724#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009725 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009726#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009727#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009728 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009729#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009730#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00009731 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009732#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009733#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00009734 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009735#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009736#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009737 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009738#endif
9739#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009740 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009741#endif
9742#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00009743 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009744#endif
9745#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00009746 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009747#endif
9748#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009749 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009750#endif
9751#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00009752 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009753#endif
9754#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009755 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009756#endif
9757#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009758 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009759#endif
9760#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009761 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009762#endif
9763#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009764 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009765#endif
9766#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009767 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009768#endif
9769#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00009770 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009771#endif
9772#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009773 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009774#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009775#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009776 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009777#endif
9778#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00009779 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009780#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009781#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009782 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009783#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009784#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009785 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009786#endif
9787#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009788 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009789#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009790#ifdef PRIO_PROCESS
9791 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
9792#endif
9793#ifdef PRIO_PGRP
9794 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
9795#endif
9796#ifdef PRIO_USER
9797 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
9798#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009799/* posix - constants for *at functions */
9800#ifdef AT_SYMLINK_NOFOLLOW
9801 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
9802#endif
9803#ifdef AT_EACCESS
9804 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
9805#endif
9806#ifdef AT_FDCWD
9807 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
9808#endif
9809#ifdef AT_REMOVEDIR
9810 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
9811#endif
9812#ifdef AT_SYMLINK_FOLLOW
9813 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
9814#endif
9815#ifdef UTIME_NOW
9816 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
9817#endif
9818#ifdef UTIME_OMIT
9819 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
9820#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009821
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009822
Tim Peters5aa91602002-01-30 05:46:57 +00009823/* MS Windows */
9824#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009825 /* Don't inherit in child processes. */
9826 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009827#endif
9828#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00009829 /* Optimize for short life (keep in memory). */
9830 /* MS forgot to define this one with a non-underscore form too. */
9831 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009832#endif
9833#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009834 /* Automatically delete when last handle is closed. */
9835 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009836#endif
9837#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00009838 /* Optimize for random access. */
9839 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009840#endif
9841#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009842 /* Optimize for sequential access. */
9843 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009844#endif
9845
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009846/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009847#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009848 /* Send a SIGIO signal whenever input or output
9849 becomes available on file descriptor */
9850 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009851#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009852#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009853 /* Direct disk access. */
9854 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009855#endif
9856#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00009857 /* Must be a directory. */
9858 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009859#endif
9860#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00009861 /* Do not follow links. */
9862 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009863#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009864#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009865 /* Do not update the access time. */
9866 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009867#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009868
Victor Stinner8c62be82010-05-06 00:08:46 +00009869 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009870#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009871 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009872#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009873#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009874 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009875#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009876#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009877 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009878#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009879#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009880 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009881#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009882#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00009883 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009884#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009885#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00009886 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009887#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009888#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009889 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009890#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009891#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00009892 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009893#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009894#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009895 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009896#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009897#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009898 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009899#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009900#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009901 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009902#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009903#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009904 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009905#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009906#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00009907 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009908#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009909#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00009910 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009911#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009912#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009913 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009914#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009915#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009916 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009917#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009918#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00009919 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009920#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009921
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009922 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009923#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009924 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009925#endif /* ST_RDONLY */
9926#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009927 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009928#endif /* ST_NOSUID */
9929
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009930 /* FreeBSD sendfile() constants */
9931#ifdef SF_NODISKIO
9932 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
9933#endif
9934#ifdef SF_MNOWAIT
9935 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
9936#endif
9937#ifdef SF_SYNC
9938 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
9939#endif
9940
Ross Lagerwall7807c352011-03-17 20:20:30 +02009941 /* constants for posix_fadvise */
9942#ifdef POSIX_FADV_NORMAL
9943 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
9944#endif
9945#ifdef POSIX_FADV_SEQUENTIAL
9946 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
9947#endif
9948#ifdef POSIX_FADV_RANDOM
9949 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
9950#endif
9951#ifdef POSIX_FADV_NOREUSE
9952 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
9953#endif
9954#ifdef POSIX_FADV_WILLNEED
9955 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
9956#endif
9957#ifdef POSIX_FADV_DONTNEED
9958 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
9959#endif
9960
9961 /* constants for waitid */
9962#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
9963 if (ins(d, "P_PID", (long)P_PID)) return -1;
9964 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
9965 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
9966#endif
9967#ifdef WEXITED
9968 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
9969#endif
9970#ifdef WNOWAIT
9971 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
9972#endif
9973#ifdef WSTOPPED
9974 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
9975#endif
9976#ifdef CLD_EXITED
9977 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
9978#endif
9979#ifdef CLD_DUMPED
9980 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
9981#endif
9982#ifdef CLD_TRAPPED
9983 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
9984#endif
9985#ifdef CLD_CONTINUED
9986 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
9987#endif
9988
9989 /* constants for lockf */
9990#ifdef F_LOCK
9991 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
9992#endif
9993#ifdef F_TLOCK
9994 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
9995#endif
9996#ifdef F_ULOCK
9997 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
9998#endif
9999#ifdef F_TEST
10000 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
10001#endif
10002
10003 /* constants for futimens */
10004#ifdef UTIME_NOW
10005 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10006#endif
10007#ifdef UTIME_OMIT
10008 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10009#endif
10010
Guido van Rossum246bc171999-02-01 23:54:31 +000010011#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010012#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010013 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10014 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10015 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10016 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10017 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10018 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10019 if (ins(d, "P_PM", (long)P_PM)) return -1;
10020 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10021 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10022 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10023 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10024 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10025 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10026 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10027 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10028 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10029 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10030 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10031 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10032 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010033#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010034 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10035 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10036 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10037 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10038 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010039#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010040#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010041
Guido van Rossumd48f2521997-12-05 22:19:34 +000010042#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010043 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010044#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010045 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010046}
10047
10048
Tim Peters5aa91602002-01-30 05:46:57 +000010049#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010050#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010051#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010052
10053#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010054#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010055#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010056
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010057#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010058#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010059#define MODNAME "posix"
10060#endif
10061
Martin v. Löwis1a214512008-06-11 05:26:20 +000010062static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010063 PyModuleDef_HEAD_INIT,
10064 MODNAME,
10065 posix__doc__,
10066 -1,
10067 posix_methods,
10068 NULL,
10069 NULL,
10070 NULL,
10071 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010072};
10073
10074
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010075PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010076INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010077{
Victor Stinner8c62be82010-05-06 00:08:46 +000010078 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010079
Brian Curtin52173d42010-12-02 18:29:18 +000010080#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010081 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010082#endif
10083
Victor Stinner8c62be82010-05-06 00:08:46 +000010084 m = PyModule_Create(&posixmodule);
10085 if (m == NULL)
10086 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010087
Victor Stinner8c62be82010-05-06 00:08:46 +000010088 /* Initialize environ dictionary */
10089 v = convertenviron();
10090 Py_XINCREF(v);
10091 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10092 return NULL;
10093 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010094
Victor Stinner8c62be82010-05-06 00:08:46 +000010095 if (all_ins(m))
10096 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010097
Victor Stinner8c62be82010-05-06 00:08:46 +000010098 if (setup_confname_tables(m))
10099 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010100
Victor Stinner8c62be82010-05-06 00:08:46 +000010101 Py_INCREF(PyExc_OSError);
10102 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010103
Guido van Rossumb3d39562000-01-31 18:41:26 +000010104#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010105 if (posix_putenv_garbage == NULL)
10106 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010107#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010108
Victor Stinner8c62be82010-05-06 00:08:46 +000010109 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010110#if defined(HAVE_WAITID) && !defined(__APPLE__)
10111 waitid_result_desc.name = MODNAME ".waitid_result";
10112 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10113#endif
10114
Victor Stinner8c62be82010-05-06 00:08:46 +000010115 stat_result_desc.name = MODNAME ".stat_result";
10116 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10117 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10118 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10119 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10120 structseq_new = StatResultType.tp_new;
10121 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010122
Victor Stinner8c62be82010-05-06 00:08:46 +000010123 statvfs_result_desc.name = MODNAME ".statvfs_result";
10124 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010125#ifdef NEED_TICKS_PER_SECOND
10126# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010127 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010128# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010129 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010130# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010131 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010132# endif
10133#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010134 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010135#if defined(HAVE_WAITID) && !defined(__APPLE__)
10136 Py_INCREF((PyObject*) &WaitidResultType);
10137 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10138#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010139 Py_INCREF((PyObject*) &StatResultType);
10140 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10141 Py_INCREF((PyObject*) &StatVFSResultType);
10142 PyModule_AddObject(m, "statvfs_result",
10143 (PyObject*) &StatVFSResultType);
10144 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010145
10146#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010147 /*
10148 * Step 2 of weak-linking support on Mac OS X.
10149 *
10150 * The code below removes functions that are not available on the
10151 * currently active platform.
10152 *
10153 * This block allow one to use a python binary that was build on
10154 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10155 * OSX 10.4.
10156 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010157#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010158 if (fstatvfs == NULL) {
10159 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10160 return NULL;
10161 }
10162 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010163#endif /* HAVE_FSTATVFS */
10164
10165#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010166 if (statvfs == NULL) {
10167 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10168 return NULL;
10169 }
10170 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010171#endif /* HAVE_STATVFS */
10172
10173# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010174 if (lchown == NULL) {
10175 if (PyObject_DelAttrString(m, "lchown") == -1) {
10176 return NULL;
10177 }
10178 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010179#endif /* HAVE_LCHOWN */
10180
10181
10182#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000010183 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010184
Guido van Rossumb6775db1994-08-01 11:34:53 +000010185}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010186
10187#ifdef __cplusplus
10188}
10189#endif