blob: 7419862879dc2015423d48419ee93236ab86a40b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#endif /* defined(__VMS) */
35
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#ifdef __cplusplus
37extern "C" {
38#endif
39
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000041"This module provides access to operating system functionality that is\n\
42standardized by the C Standard and the POSIX standard (a thinly\n\
43disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000046
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#if defined(PYOS_OS2)
48#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_TYPES_H */
65
66#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000069
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000071#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000072#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000073
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000075#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000077
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#ifdef HAVE_FCNTL_H
79#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000080#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000081
Guido van Rossuma6535fd2001-10-18 19:44:10 +000082#ifdef HAVE_GRP_H
83#include <grp.h>
84#endif
85
Barry Warsaw5676bd12003-01-07 20:57:09 +000086#ifdef HAVE_SYSEXITS_H
87#include <sysexits.h>
88#endif /* HAVE_SYSEXITS_H */
89
Anthony Baxter8a560de2004-10-13 15:30:56 +000090#ifdef HAVE_SYS_LOADAVG_H
91#include <sys/loadavg.h>
92#endif
93
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000094#ifdef HAVE_LANGINFO_H
95#include <langinfo.h>
96#endif
97
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000099/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000100#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#include <process.h>
102#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000103#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000104#define HAVE_GETCWD 1
105#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000106#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#if defined(__OS2__)
108#define HAVE_EXECV 1
109#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000110#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000111#include <process.h>
112#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000113#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_EXECV 1
115#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_OPENDIR 1
117#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000118#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_WAIT 1
120#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000121#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000122#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000123#define HAVE_GETPPID 1
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000124#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000128#define HAVE_SYSTEM 1
129#define HAVE_CWAIT 1
130#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000132#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000133#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
134/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136/* Unix functions that the configure script doesn't check for */
137#define HAVE_EXECV 1
138#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#define HAVE_FORK1 1
141#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_GETCWD 1
143#define HAVE_GETEGID 1
144#define HAVE_GETEUID 1
145#define HAVE_GETGID 1
146#define HAVE_GETPPID 1
147#define HAVE_GETUID 1
148#define HAVE_KILL 1
149#define HAVE_OPENDIR 1
150#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000154#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#endif /* _MSC_VER */
156#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000157#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000159
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000161
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000162#if defined(__sgi)&&_COMPILER_VERSION>=700
163/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 (default) */
165extern char *ctermid_r(char *);
166#endif
167
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000168#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000170extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000172#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000177#endif
178#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int chdir(char *);
180extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000182extern int chdir(const char *);
183extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000185#ifdef __BORLANDC__
186extern int chmod(const char *, int);
187#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000188extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000190/*#ifdef HAVE_FCHMOD
191extern int fchmod(int, mode_t);
192#endif*/
193/*#ifdef HAVE_LCHMOD
194extern int lchmod(const char *, mode_t);
195#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int chown(const char *, uid_t, gid_t);
197extern char *getcwd(char *, int);
198extern char *strerror(int);
199extern int link(const char *, const char *);
200extern int rename(const char *, const char *);
201extern int stat(const char *, struct stat *);
202extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000205#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000207extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000211#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#ifdef HAVE_UTIME_H
214#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000215#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000217#ifdef HAVE_SYS_UTIME_H
218#include <sys/utime.h>
219#define HAVE_UTIME_H /* pretend we do for the rest of this file */
220#endif /* HAVE_SYS_UTIME_H */
221
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#ifdef HAVE_SYS_TIMES_H
223#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000224#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
226#ifdef HAVE_SYS_PARAM_H
227#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000228#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229
230#ifdef HAVE_SYS_UTSNAME_H
231#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000232#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000234#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) strlen((dirent)->d_name)
237#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000238#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#include <direct.h>
240#define NAMLEN(dirent) strlen((dirent)->d_name)
241#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000247#endif
248#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#endif
251#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#endif
254#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259#endif
260#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262#endif
263#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000266#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000267#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000268#endif
269#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000270#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000271#endif
272#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000273#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000274#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000276#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000278#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000279#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000280#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
281#define HAVE_SYMLINK
282#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000283#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284
Guido van Rossumd48f2521997-12-05 22:19:34 +0000285#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000287#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288
Tim Petersbc2e10e2002-03-03 23:17:02 +0000289#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000290#if defined(PATH_MAX) && PATH_MAX > 1024
291#define MAXPATHLEN PATH_MAX
292#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000293#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000294#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000295#endif /* MAXPATHLEN */
296
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000297#ifdef UNION_WAIT
298/* Emulate some macros on systems that have a union instead of macros */
299
300#ifndef WIFEXITED
301#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
302#endif
303
304#ifndef WEXITSTATUS
305#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
306#endif
307
308#ifndef WTERMSIG
309#define WTERMSIG(u_wait) ((u_wait).w_termsig)
310#endif
311
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312#define WAIT_TYPE union wait
313#define WAIT_STATUS_INT(s) (s.w_status)
314
315#else /* !UNION_WAIT */
316#define WAIT_TYPE int
317#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000318#endif /* UNION_WAIT */
319
Greg Wardb48bc172000-03-01 21:51:56 +0000320/* Don't use the "_r" form if we don't need it (also, won't have a
321 prototype for it, at least on Solaris -- maybe others as well?). */
322#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
323#define USE_CTERMID_R
324#endif
325
Fred Drake699f3522000-06-29 21:12:41 +0000326/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000327#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000328#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000329# define STAT win32_stat
330# define FSTAT win32_fstat
331# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000332#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000333# define STAT stat
334# define FSTAT fstat
335# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000336#endif
337
Tim Peters11b23062003-04-23 02:39:17 +0000338#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000339#include <sys/mkdev.h>
340#else
341#if defined(MAJOR_IN_SYSMACROS)
342#include <sys/sysmacros.h>
343#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000344#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
345#include <sys/mkdev.h>
346#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000347#endif
Fred Drake699f3522000-06-29 21:12:41 +0000348
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000349#if defined _MSC_VER && _MSC_VER >= 1400
350/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
351 * valid and throw an assertion if it isn't.
352 * Normally, an invalid fd is likely to be a C program error and therefore
353 * an assertion can be useful, but it does contradict the POSIX standard
354 * which for write(2) states:
355 * "Otherwise, -1 shall be returned and errno set to indicate the error."
356 * "[EBADF] The fildes argument is not a valid file descriptor open for
357 * writing."
358 * Furthermore, python allows the user to enter any old integer
359 * as a fd and should merely raise a python exception on error.
360 * The Microsoft CRT doesn't provide an official way to check for the
361 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000362 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000363 * internal structures involved.
364 * The structures below must be updated for each version of visual studio
365 * according to the file internal.h in the CRT source, until MS comes
366 * up with a less hacky way to do this.
367 * (all of this is to avoid globally modifying the CRT behaviour using
368 * _set_invalid_parameter_handler() and _CrtSetReportMode())
369 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000370/* The actual size of the structure is determined at runtime.
371 * Only the first items must be present.
372 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000373typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000374 intptr_t osfhnd;
375 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000376} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000377
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000378extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000379#define IOINFO_L2E 5
380#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
381#define IOINFO_ARRAYS 64
382#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
383#define FOPEN 0x01
384#define _NO_CONSOLE_FILENO (intptr_t)-2
385
386/* This function emulates what the windows CRT does to validate file handles */
387int
388_PyVerify_fd(int fd)
389{
Victor Stinner8c62be82010-05-06 00:08:46 +0000390 const int i1 = fd >> IOINFO_L2E;
391 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000392
Antoine Pitrou22e41552010-08-15 18:07:50 +0000393 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000394
Victor Stinner8c62be82010-05-06 00:08:46 +0000395 /* Determine the actual size of the ioinfo structure,
396 * as used by the CRT loaded in memory
397 */
398 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
399 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
400 }
401 if (sizeof_ioinfo == 0) {
402 /* This should not happen... */
403 goto fail;
404 }
405
406 /* See that it isn't a special CLEAR fileno */
407 if (fd != _NO_CONSOLE_FILENO) {
408 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
409 * we check pointer validity and other info
410 */
411 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
412 /* finally, check that the file is open */
413 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
414 if (info->osfile & FOPEN) {
415 return 1;
416 }
417 }
418 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000419 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000420 errno = EBADF;
421 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422}
423
424/* the special case of checking dup2. The target fd must be in a sensible range */
425static int
426_PyVerify_fd_dup2(int fd1, int fd2)
427{
Victor Stinner8c62be82010-05-06 00:08:46 +0000428 if (!_PyVerify_fd(fd1))
429 return 0;
430 if (fd2 == _NO_CONSOLE_FILENO)
431 return 0;
432 if ((unsigned)fd2 < _NHANDLE_)
433 return 1;
434 else
435 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000436}
437#else
438/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
439#define _PyVerify_fd_dup2(A, B) (1)
440#endif
441
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000442#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000443/* The following structure was copied from
444 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
445 include doesn't seem to be present in the Windows SDK (at least as included
446 with Visual Studio Express). */
447typedef struct _REPARSE_DATA_BUFFER {
448 ULONG ReparseTag;
449 USHORT ReparseDataLength;
450 USHORT Reserved;
451 union {
452 struct {
453 USHORT SubstituteNameOffset;
454 USHORT SubstituteNameLength;
455 USHORT PrintNameOffset;
456 USHORT PrintNameLength;
457 ULONG Flags;
458 WCHAR PathBuffer[1];
459 } SymbolicLinkReparseBuffer;
460
461 struct {
462 USHORT SubstituteNameOffset;
463 USHORT SubstituteNameLength;
464 USHORT PrintNameOffset;
465 USHORT PrintNameLength;
466 WCHAR PathBuffer[1];
467 } MountPointReparseBuffer;
468
469 struct {
470 UCHAR DataBuffer[1];
471 } GenericReparseBuffer;
472 };
473} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
474
475#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
476 GenericReparseBuffer)
477#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
478
479static int
480_Py_ReadLink(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
481{
482 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
483 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
484 DWORD n_bytes_returned;
485 const wchar_t *ptr;
486 wchar_t *buf;
487 size_t len;
488
489 if (0 == DeviceIoControl(
490 reparse_point_handle,
491 FSCTL_GET_REPARSE_POINT,
492 NULL, 0, /* in buffer */
493 target_buffer, sizeof(target_buffer),
494 &n_bytes_returned,
495 NULL)) /* we're not using OVERLAPPED_IO */
496 return 0;
497
498 if (reparse_tag)
499 *reparse_tag = rdb->ReparseTag;
500
501 if (target_path) {
502 switch (rdb->ReparseTag) {
503 case IO_REPARSE_TAG_SYMLINK:
504 /* XXX: Maybe should use SubstituteName? */
505 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
506 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
507 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
508 break;
509 case IO_REPARSE_TAG_MOUNT_POINT:
510 ptr = rdb->MountPointReparseBuffer.PathBuffer +
511 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
512 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
513 break;
514 default:
515 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
516 return 0;
517 }
518 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
519 if (!buf) {
520 SetLastError(ERROR_OUTOFMEMORY);
521 return 0;
522 }
523 wcsncpy(buf, ptr, len);
524 buf[len] = L'\0';
525 if (wcsncmp(buf, L"\\??\\", 4) == 0)
526 buf[1] = L'\\';
527 *target_path = buf;
528 }
529
530 return 1;
531}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000532#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000533
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000535#ifdef WITH_NEXT_FRAMEWORK
536/* On Darwin/MacOSX a shared library or framework has no access to
537** environ directly, we must obtain it with _NSGetEnviron().
538*/
539#include <crt_externs.h>
540static char **environ;
541#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000543#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544
Barry Warsaw53699e91996-12-10 23:23:01 +0000545static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000546convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547{
Victor Stinner8c62be82010-05-06 00:08:46 +0000548 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000549#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000550 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000551#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000552 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000553#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000554#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000555 APIRET rc;
556 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
557#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000558
Victor Stinner8c62be82010-05-06 00:08:46 +0000559 d = PyDict_New();
560 if (d == NULL)
561 return NULL;
562#ifdef WITH_NEXT_FRAMEWORK
563 if (environ == NULL)
564 environ = *_NSGetEnviron();
565#endif
566#ifdef MS_WINDOWS
567 /* _wenviron must be initialized in this way if the program is started
568 through main() instead of wmain(). */
569 _wgetenv(L"");
570 if (_wenviron == NULL)
571 return d;
572 /* This part ignores errors */
573 for (e = _wenviron; *e != NULL; e++) {
574 PyObject *k;
575 PyObject *v;
576 wchar_t *p = wcschr(*e, L'=');
577 if (p == NULL)
578 continue;
579 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
580 if (k == NULL) {
581 PyErr_Clear();
582 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000583 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000584 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
585 if (v == NULL) {
586 PyErr_Clear();
587 Py_DECREF(k);
588 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000589 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000590 if (PyDict_GetItem(d, k) == NULL) {
591 if (PyDict_SetItem(d, k, v) != 0)
592 PyErr_Clear();
593 }
594 Py_DECREF(k);
595 Py_DECREF(v);
596 }
597#else
598 if (environ == NULL)
599 return d;
600 /* This part ignores errors */
601 for (e = environ; *e != NULL; e++) {
602 PyObject *k;
603 PyObject *v;
604 char *p = strchr(*e, '=');
605 if (p == NULL)
606 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000607 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000608 if (k == NULL) {
609 PyErr_Clear();
610 continue;
611 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000612 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000613 if (v == NULL) {
614 PyErr_Clear();
615 Py_DECREF(k);
616 continue;
617 }
618 if (PyDict_GetItem(d, k) == NULL) {
619 if (PyDict_SetItem(d, k, v) != 0)
620 PyErr_Clear();
621 }
622 Py_DECREF(k);
623 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000624 }
625#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000626#if defined(PYOS_OS2)
627 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
628 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
629 PyObject *v = PyBytes_FromString(buffer);
630 PyDict_SetItemString(d, "BEGINLIBPATH", v);
631 Py_DECREF(v);
632 }
633 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
634 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
635 PyObject *v = PyBytes_FromString(buffer);
636 PyDict_SetItemString(d, "ENDLIBPATH", v);
637 Py_DECREF(v);
638 }
639#endif
640 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641}
642
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643/* Set a POSIX-specific error from errno, and return NULL */
644
Barry Warsawd58d7641998-07-23 16:14:40 +0000645static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000646posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000647{
Victor Stinner8c62be82010-05-06 00:08:46 +0000648 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649}
Barry Warsawd58d7641998-07-23 16:14:40 +0000650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000651posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000652{
Victor Stinner8c62be82010-05-06 00:08:46 +0000653 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000654}
655
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000656
Mark Hammondef8b6542001-05-13 08:04:26 +0000657static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000658posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000659{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000660 PyObject *name_str, *rc;
661 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
662 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000663 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000664 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
665 name_str);
666 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000667 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000668}
669
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000670#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000671static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000672win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000673{
Victor Stinner8c62be82010-05-06 00:08:46 +0000674 /* XXX We should pass the function name along in the future.
675 (winreg.c also wants to pass the function name.)
676 This would however require an additional param to the
677 Windows error object, which is non-trivial.
678 */
679 errno = GetLastError();
680 if (filename)
681 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
682 else
683 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000684}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000685
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686static PyObject *
687win32_error_unicode(char* function, Py_UNICODE* filename)
688{
Victor Stinner8c62be82010-05-06 00:08:46 +0000689 /* XXX - see win32_error for comments on 'function' */
690 errno = GetLastError();
691 if (filename)
692 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
693 else
694 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000695}
696
Thomas Wouters477c8d52006-05-27 19:21:47 +0000697static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000698convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699{
Victor Stinner8c62be82010-05-06 00:08:46 +0000700 if (PyUnicode_CheckExact(*param))
701 Py_INCREF(*param);
702 else if (PyUnicode_Check(*param))
703 /* For a Unicode subtype that's not a Unicode object,
704 return a true Unicode object with the same data. */
705 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
706 PyUnicode_GET_SIZE(*param));
707 else
708 *param = PyUnicode_FromEncodedObject(*param,
709 Py_FileSystemDefaultEncoding,
710 "strict");
711 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000712}
713
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000714#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715
Guido van Rossumd48f2521997-12-05 22:19:34 +0000716#if defined(PYOS_OS2)
717/**********************************************************************
718 * Helper Function to Trim and Format OS/2 Messages
719 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000720static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000721os2_formatmsg(char *msgbuf, int msglen, char *reason)
722{
723 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
724
725 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
726 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
727
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000728 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000729 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
730 }
731
732 /* Add Optional Reason Text */
733 if (reason) {
734 strcat(msgbuf, " : ");
735 strcat(msgbuf, reason);
736 }
737}
738
739/**********************************************************************
740 * Decode an OS/2 Operating System Error Code
741 *
742 * A convenience function to lookup an OS/2 error code and return a
743 * text message we can use to raise a Python exception.
744 *
745 * Notes:
746 * The messages for errors returned from the OS/2 kernel reside in
747 * the file OSO001.MSG in the \OS2 directory hierarchy.
748 *
749 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000750static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000751os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
752{
753 APIRET rc;
754 ULONG msglen;
755
756 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
757 Py_BEGIN_ALLOW_THREADS
758 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
759 errorcode, "oso001.msg", &msglen);
760 Py_END_ALLOW_THREADS
761
762 if (rc == NO_ERROR)
763 os2_formatmsg(msgbuf, msglen, reason);
764 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000765 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000766 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000767
768 return msgbuf;
769}
770
771/* Set an OS/2-specific error and return NULL. OS/2 kernel
772 errors are not in a global variable e.g. 'errno' nor are
773 they congruent with posix error numbers. */
774
Victor Stinner8c62be82010-05-06 00:08:46 +0000775static PyObject *
776os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000777{
778 char text[1024];
779 PyObject *v;
780
781 os2_strerror(text, sizeof(text), code, "");
782
783 v = Py_BuildValue("(is)", code, text);
784 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000785 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000786 Py_DECREF(v);
787 }
788 return NULL; /* Signal to Python that an Exception is Pending */
789}
790
791#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792
793/* POSIX generic methods */
794
Barry Warsaw53699e91996-12-10 23:23:01 +0000795static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000796posix_fildes(PyObject *fdobj, int (*func)(int))
797{
Victor Stinner8c62be82010-05-06 00:08:46 +0000798 int fd;
799 int res;
800 fd = PyObject_AsFileDescriptor(fdobj);
801 if (fd < 0)
802 return NULL;
803 if (!_PyVerify_fd(fd))
804 return posix_error();
805 Py_BEGIN_ALLOW_THREADS
806 res = (*func)(fd);
807 Py_END_ALLOW_THREADS
808 if (res < 0)
809 return posix_error();
810 Py_INCREF(Py_None);
811 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000812}
Guido van Rossum21142a01999-01-08 21:05:37 +0000813
814static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816{
Victor Stinner8c62be82010-05-06 00:08:46 +0000817 PyObject *opath1 = NULL;
818 char *path1;
819 int res;
820 if (!PyArg_ParseTuple(args, format,
821 PyUnicode_FSConverter, &opath1))
822 return NULL;
823 path1 = PyBytes_AsString(opath1);
824 Py_BEGIN_ALLOW_THREADS
825 res = (*func)(path1);
826 Py_END_ALLOW_THREADS
827 if (res < 0)
828 return posix_error_with_allocated_filename(opath1);
829 Py_DECREF(opath1);
830 Py_INCREF(Py_None);
831 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000832}
833
Barry Warsaw53699e91996-12-10 23:23:01 +0000834static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000835posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000836 char *format,
837 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838{
Victor Stinner8c62be82010-05-06 00:08:46 +0000839 PyObject *opath1 = NULL, *opath2 = NULL;
840 char *path1, *path2;
841 int res;
842 if (!PyArg_ParseTuple(args, format,
843 PyUnicode_FSConverter, &opath1,
844 PyUnicode_FSConverter, &opath2)) {
845 return NULL;
846 }
847 path1 = PyBytes_AsString(opath1);
848 path2 = PyBytes_AsString(opath2);
849 Py_BEGIN_ALLOW_THREADS
850 res = (*func)(path1, path2);
851 Py_END_ALLOW_THREADS
852 Py_DECREF(opath1);
853 Py_DECREF(opath2);
854 if (res != 0)
855 /* XXX how to report both path1 and path2??? */
856 return posix_error();
857 Py_INCREF(Py_None);
858 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859}
860
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000861#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000863win32_1str(PyObject* args, char* func,
864 char* format, BOOL (__stdcall *funcA)(LPCSTR),
865 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000866{
Victor Stinner8c62be82010-05-06 00:08:46 +0000867 PyObject *uni;
868 char *ansi;
869 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000870
Victor Stinner8c62be82010-05-06 00:08:46 +0000871 if (!PyArg_ParseTuple(args, wformat, &uni))
872 PyErr_Clear();
873 else {
874 Py_BEGIN_ALLOW_THREADS
875 result = funcW(PyUnicode_AsUnicode(uni));
876 Py_END_ALLOW_THREADS
877 if (!result)
878 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
879 Py_INCREF(Py_None);
880 return Py_None;
881 }
882 if (!PyArg_ParseTuple(args, format, &ansi))
883 return NULL;
884 Py_BEGIN_ALLOW_THREADS
885 result = funcA(ansi);
886 Py_END_ALLOW_THREADS
887 if (!result)
888 return win32_error(func, ansi);
889 Py_INCREF(Py_None);
890 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891
892}
893
894/* This is a reimplementation of the C library's chdir function,
895 but one that produces Win32 errors instead of DOS error codes.
896 chdir is essentially a wrapper around SetCurrentDirectory; however,
897 it also needs to set "magic" environment variables indicating
898 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000899static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900win32_chdir(LPCSTR path)
901{
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 char new_path[MAX_PATH+1];
903 int result;
904 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 if(!SetCurrentDirectoryA(path))
907 return FALSE;
908 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
909 if (!result)
910 return FALSE;
911 /* In the ANSI API, there should not be any paths longer
912 than MAX_PATH. */
913 assert(result <= MAX_PATH+1);
914 if (strncmp(new_path, "\\\\", 2) == 0 ||
915 strncmp(new_path, "//", 2) == 0)
916 /* UNC path, nothing to do. */
917 return TRUE;
918 env[1] = new_path[0];
919 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920}
921
922/* The Unicode version differs from the ANSI version
923 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000924static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000925win32_wchdir(LPCWSTR path)
926{
Victor Stinner8c62be82010-05-06 00:08:46 +0000927 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
928 int result;
929 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930
Victor Stinner8c62be82010-05-06 00:08:46 +0000931 if(!SetCurrentDirectoryW(path))
932 return FALSE;
933 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
934 if (!result)
935 return FALSE;
936 if (result > MAX_PATH+1) {
937 new_path = malloc(result * sizeof(wchar_t));
938 if (!new_path) {
939 SetLastError(ERROR_OUTOFMEMORY);
940 return FALSE;
941 }
942 result = GetCurrentDirectoryW(result, new_path);
943 if (!result) {
944 free(new_path);
945 return FALSE;
946 }
947 }
948 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
949 wcsncmp(new_path, L"//", 2) == 0)
950 /* UNC path, nothing to do. */
951 return TRUE;
952 env[1] = new_path[0];
953 result = SetEnvironmentVariableW(env, new_path);
954 if (new_path != _new_path)
955 free(new_path);
956 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957}
958#endif
959
Martin v. Löwis14694662006-02-03 12:54:16 +0000960#ifdef MS_WINDOWS
961/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
962 - time stamps are restricted to second resolution
963 - file modification times suffer from forth-and-back conversions between
964 UTC and local time
965 Therefore, we implement our own stat, based on the Win32 API directly.
966*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000967#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000968
969struct win32_stat{
970 int st_dev;
971 __int64 st_ino;
972 unsigned short st_mode;
973 int st_nlink;
974 int st_uid;
975 int st_gid;
976 int st_rdev;
977 __int64 st_size;
978 int st_atime;
979 int st_atime_nsec;
980 int st_mtime;
981 int st_mtime_nsec;
982 int st_ctime;
983 int st_ctime_nsec;
984};
985
986static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
987
988static void
989FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
990{
Victor Stinner8c62be82010-05-06 00:08:46 +0000991 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
992 /* Cannot simply cast and dereference in_ptr,
993 since it might not be aligned properly */
994 __int64 in;
995 memcpy(&in, in_ptr, sizeof(in));
996 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
997 /* XXX Win32 supports time stamps past 2038; we currently don't */
998 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000999}
1000
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001static void
1002time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
1003{
Victor Stinner8c62be82010-05-06 00:08:46 +00001004 /* XXX endianness */
1005 __int64 out;
1006 out = time_in + secs_between_epochs;
1007 out = out * 10000000 + nsec_in / 100;
1008 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001009}
1010
Martin v. Löwis14694662006-02-03 12:54:16 +00001011/* Below, we *know* that ugo+r is 0444 */
1012#if _S_IREAD != 0400
1013#error Unsupported C library
1014#endif
1015static int
1016attributes_to_mode(DWORD attr)
1017{
Victor Stinner8c62be82010-05-06 00:08:46 +00001018 int m = 0;
1019 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1020 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1021 else
1022 m |= _S_IFREG;
1023 if (attr & FILE_ATTRIBUTE_READONLY)
1024 m |= 0444;
1025 else
1026 m |= 0666;
1027 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001028}
1029
1030static int
Brian Curtinf5e76d02010-11-24 13:14:05 +00001031attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001032{
Victor Stinner8c62be82010-05-06 00:08:46 +00001033 memset(result, 0, sizeof(*result));
1034 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1035 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1036 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1037 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1038 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001039 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001040 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Martin v. Löwis14694662006-02-03 12:54:16 +00001041
Victor Stinner8c62be82010-05-06 00:08:46 +00001042 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001043}
1044
Guido van Rossumd8faa362007-04-27 19:54:29 +00001045static BOOL
Brian Curtinf5e76d02010-11-24 13:14:05 +00001046attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001047{
Victor Stinner8c62be82010-05-06 00:08:46 +00001048 HANDLE hFindFile;
1049 WIN32_FIND_DATAA FileData;
1050 hFindFile = FindFirstFileA(pszFile, &FileData);
1051 if (hFindFile == INVALID_HANDLE_VALUE)
1052 return FALSE;
1053 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001054 memset(info, 0, sizeof(*info));
1055 info->dwFileAttributes = FileData.dwFileAttributes;
1056 info->ftCreationTime = FileData.ftCreationTime;
1057 info->ftLastAccessTime = FileData.ftLastAccessTime;
1058 info->ftLastWriteTime = FileData.ftLastWriteTime;
1059 info->nFileSizeHigh = FileData.nFileSizeHigh;
1060 info->nFileSizeLow = FileData.nFileSizeLow;
1061/* info->nNumberOfLinks = 1; */
Victor Stinner8c62be82010-05-06 00:08:46 +00001062 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001063}
1064
1065static BOOL
Brian Curtinf5e76d02010-11-24 13:14:05 +00001066attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001067{
Victor Stinner8c62be82010-05-06 00:08:46 +00001068 HANDLE hFindFile;
1069 WIN32_FIND_DATAW FileData;
1070 hFindFile = FindFirstFileW(pszFile, &FileData);
1071 if (hFindFile == INVALID_HANDLE_VALUE)
1072 return FALSE;
1073 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001074 memset(info, 0, sizeof(*info));
1075 info->dwFileAttributes = FileData.dwFileAttributes;
1076 info->ftCreationTime = FileData.ftCreationTime;
1077 info->ftLastAccessTime = FileData.ftLastAccessTime;
1078 info->ftLastWriteTime = FileData.ftLastWriteTime;
1079 info->nFileSizeHigh = FileData.nFileSizeHigh;
1080 info->nFileSizeLow = FileData.nFileSizeLow;
1081/* info->nNumberOfLinks = 1; */
Victor Stinner8c62be82010-05-06 00:08:46 +00001082 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001083}
1084
Brian Curtinf5e76d02010-11-24 13:14:05 +00001085#ifndef SYMLOOP_MAX
1086#define SYMLOOP_MAX ( 88 )
1087#endif
1088
1089static int
1090win32_xstat_for_handle(HANDLE hFile, struct win32_stat *result, BOOL traverse, int depth);
1091
1092static int
1093win32_xstat(const char *path, struct win32_stat *result, BOOL traverse, int depth)
1094{
1095 int code;
1096 HANDLE hFile;
1097 BY_HANDLE_FILE_INFORMATION info;
1098 const char *dot;
1099
1100 hFile = CreateFileA(
1101 path,
1102 0, /* desired access */
1103 0, /* share mode */
1104 NULL, /* security attributes */
1105 OPEN_EXISTING,
1106 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1107 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1108 NULL);
1109
1110 if(hFile == INVALID_HANDLE_VALUE) {
1111 /* Either the target doesn't exist, or we don't have access to
1112 get a handle to it. If the former, we need to return an error.
1113 If the latter, we can use attributes_from_dir. */
1114 if (GetLastError() != ERROR_SHARING_VIOLATION)
1115 goto err;
1116 else {
1117 /* Could not get attributes on open file. Fall back to
1118 reading the directory. */
1119 if (!attributes_from_dir(path, &info))
1120 /* Very strange. This should not fail now */
1121 goto err;
1122 if (traverse && (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
1123 /* Should traverse, but cannot open reparse point handle */
1124 SetLastError(ERROR_SHARING_VIOLATION);
1125 goto err;
1126 }
1127 attribute_data_to_stat(&info, result);
1128 }
1129 }
1130 else {
1131 code = win32_xstat_for_handle(hFile, result, traverse, depth);
1132 CloseHandle(hFile);
1133 if (code != 0)
1134 return code;
1135 }
1136
1137 /* Set S_IEXEC if it is an .exe, .bat, ... */
1138 dot = strrchr(path, '.');
1139 if (dot) {
1140 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1141 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1142 result->st_mode |= 0111;
1143 }
1144 return 0;
1145
1146err:
1147 /* Protocol violation: we explicitly clear errno, instead of
1148 setting it to a POSIX error. Callers should use GetLastError. */
1149 errno = 0;
1150 return -1;
1151}
1152
1153static int
1154win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
1155{
1156 int code;
1157 HANDLE hFile;
1158 BY_HANDLE_FILE_INFORMATION info;
1159 const wchar_t *dot;
1160
1161 hFile = CreateFileW(
1162 path,
1163 0, /* desired access */
1164 0, /* share mode */
1165 NULL, /* security attributes */
1166 OPEN_EXISTING,
1167 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1168 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1169 NULL);
1170
1171 if(hFile == INVALID_HANDLE_VALUE) {
1172 /* Either the target doesn't exist, or we don't have access to
1173 get a handle to it. If the former, we need to return an error.
1174 If the latter, we can use attributes_from_dir. */
1175 if (GetLastError() != ERROR_SHARING_VIOLATION)
1176 goto err;
1177 else {
1178 /* Could not get attributes on open file. Fall back to
1179 reading the directory. */
1180 if (!attributes_from_dir_w(path, &info))
1181 /* Very strange. This should not fail now */
1182 goto err;
1183 if (traverse && (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
1184 /* Should traverse, but cannot open reparse point handle */
1185 SetLastError(ERROR_SHARING_VIOLATION);
1186 goto err;
1187 }
1188 attribute_data_to_stat(&info, result);
1189 }
1190 }
1191 else {
1192 code = win32_xstat_for_handle(hFile, result, traverse, depth);
1193 CloseHandle(hFile);
1194 if (code != 0)
1195 return code;
1196 }
1197
1198 /* Set S_IEXEC if it is an .exe, .bat, ... */
1199 dot = wcsrchr(path, '.');
1200 if (dot) {
1201 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1202 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1203 result->st_mode |= 0111;
1204 }
1205 return 0;
1206
1207err:
1208 /* Protocol violation: we explicitly clear errno, instead of
1209 setting it to a POSIX error. Callers should use GetLastError. */
1210 errno = 0;
1211 return -1;
1212}
1213
1214static int
1215win32_xstat_for_handle(HANDLE hFile, struct win32_stat *result, BOOL traverse, int depth)
1216{
1217 int code;
1218 BOOL reparse_tag;
1219 wchar_t *target_path;
1220 BY_HANDLE_FILE_INFORMATION info;
1221
1222 if (!GetFileInformationByHandle(hFile, &info))
1223 return -1;
1224
1225 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1226 if (traverse) {
1227 if (depth + 1 > SYMLOOP_MAX) {
1228 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1229 return -1;
1230 }
1231 if (!_Py_ReadLink(hFile, NULL, &target_path))
1232 return -1;
1233 code = win32_xstat_w(target_path, result, traverse, depth + 1);
1234 free(target_path);
1235 return code;
1236 } else {
1237 if (!_Py_ReadLink(hFile, &reparse_tag, NULL))
1238 return -1;
1239 attribute_data_to_stat(&info, result);
1240 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1241 /* first clear the S_IFMT bits */
1242 result->st_mode ^= (result->st_mode & 0170000);
1243 /* now set the bits that make this a symlink */
1244 result->st_mode |= 0120000;
1245 }
1246 }
1247 } else {
1248 attribute_data_to_stat(&info, result);
1249 }
1250 return 0;
1251}
1252
Brian Curtind40e6f72010-07-08 21:39:08 +00001253/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1254 win32_stat_w
1255
1256 In Posix, stat automatically traverses symlinks and returns the stat
1257 structure for the target. In Windows, the equivalent GetFileAttributes by
1258 default does not traverse symlinks and instead returns attributes for
1259 the symlink.
1260
1261 Therefore, win32_lstat will get the attributes traditionally, and
1262 win32_stat will first explicitly resolve the symlink target and then will
1263 call win32_lstat on that result.
1264
1265 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1266
1267static int
1268win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001269{
Brian Curtinf5e76d02010-11-24 13:14:05 +00001270 return win32_xstat(path, result, FALSE, 0);
Martin v. Löwis14694662006-02-03 12:54:16 +00001271}
1272
Victor Stinner8c62be82010-05-06 00:08:46 +00001273static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001274win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001275{
Brian Curtinf5e76d02010-11-24 13:14:05 +00001276 return win32_xstat_w(path, result, FALSE, 0);
Brian Curtind40e6f72010-07-08 21:39:08 +00001277}
1278
1279static int
1280win32_stat(const char* path, struct win32_stat *result)
1281{
Brian Curtinf5e76d02010-11-24 13:14:05 +00001282 return win32_xstat(path, result, TRUE, 0);
Brian Curtind40e6f72010-07-08 21:39:08 +00001283}
1284
1285static int
1286win32_stat_w(const wchar_t* path, struct win32_stat *result)
1287{
Brian Curtinf5e76d02010-11-24 13:14:05 +00001288 return win32_xstat_w(path, result, TRUE, 0);
Martin v. Löwis14694662006-02-03 12:54:16 +00001289}
1290
1291static int
1292win32_fstat(int file_number, struct win32_stat *result)
1293{
Victor Stinner8c62be82010-05-06 00:08:46 +00001294 BY_HANDLE_FILE_INFORMATION info;
1295 HANDLE h;
1296 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001297
Victor Stinner8c62be82010-05-06 00:08:46 +00001298 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001299
Victor Stinner8c62be82010-05-06 00:08:46 +00001300 /* Protocol violation: we explicitly clear errno, instead of
1301 setting it to a POSIX error. Callers should use GetLastError. */
1302 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001303
Victor Stinner8c62be82010-05-06 00:08:46 +00001304 if (h == INVALID_HANDLE_VALUE) {
1305 /* This is really a C library error (invalid file handle).
1306 We set the Win32 error to the closes one matching. */
1307 SetLastError(ERROR_INVALID_HANDLE);
1308 return -1;
1309 }
1310 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001311
Victor Stinner8c62be82010-05-06 00:08:46 +00001312 type = GetFileType(h);
1313 if (type == FILE_TYPE_UNKNOWN) {
1314 DWORD error = GetLastError();
1315 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001316 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 }
1318 /* else: valid but unknown file */
1319 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001320
Victor Stinner8c62be82010-05-06 00:08:46 +00001321 if (type != FILE_TYPE_DISK) {
1322 if (type == FILE_TYPE_CHAR)
1323 result->st_mode = _S_IFCHR;
1324 else if (type == FILE_TYPE_PIPE)
1325 result->st_mode = _S_IFIFO;
1326 return 0;
1327 }
1328
1329 if (!GetFileInformationByHandle(h, &info)) {
1330 return -1;
1331 }
1332
Brian Curtinf5e76d02010-11-24 13:14:05 +00001333 attribute_data_to_stat(&info, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001334 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001335 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1336 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001337}
1338
1339#endif /* MS_WINDOWS */
1340
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001341PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001342"stat_result: Result from stat or lstat.\n\n\
1343This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001344 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001345or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1346\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001347Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1348or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001349\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001350See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001351
1352static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001353 {"st_mode", "protection bits"},
1354 {"st_ino", "inode"},
1355 {"st_dev", "device"},
1356 {"st_nlink", "number of hard links"},
1357 {"st_uid", "user ID of owner"},
1358 {"st_gid", "group ID of owner"},
1359 {"st_size", "total size, in bytes"},
1360 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1361 {NULL, "integer time of last access"},
1362 {NULL, "integer time of last modification"},
1363 {NULL, "integer time of last change"},
1364 {"st_atime", "time of last access"},
1365 {"st_mtime", "time of last modification"},
1366 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001367#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001368 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001369#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001370#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001371 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001372#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001373#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001374 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001375#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001376#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001377 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001378#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001379#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001380 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001381#endif
1382#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001383 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001384#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001385 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001386};
1387
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001388#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001389#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001390#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001391#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001392#endif
1393
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001394#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001395#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1396#else
1397#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1398#endif
1399
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001400#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001401#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1402#else
1403#define ST_RDEV_IDX ST_BLOCKS_IDX
1404#endif
1405
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001406#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1407#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1408#else
1409#define ST_FLAGS_IDX ST_RDEV_IDX
1410#endif
1411
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001412#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001413#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001414#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001415#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001416#endif
1417
1418#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1419#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1420#else
1421#define ST_BIRTHTIME_IDX ST_GEN_IDX
1422#endif
1423
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001424static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001425 "stat_result", /* name */
1426 stat_result__doc__, /* doc */
1427 stat_result_fields,
1428 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001429};
1430
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001431PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001432"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1433This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001434 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001435or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001436\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001437See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001438
1439static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001440 {"f_bsize", },
1441 {"f_frsize", },
1442 {"f_blocks", },
1443 {"f_bfree", },
1444 {"f_bavail", },
1445 {"f_files", },
1446 {"f_ffree", },
1447 {"f_favail", },
1448 {"f_flag", },
1449 {"f_namemax",},
1450 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001451};
1452
1453static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001454 "statvfs_result", /* name */
1455 statvfs_result__doc__, /* doc */
1456 statvfs_result_fields,
1457 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001458};
1459
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001460static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001461static PyTypeObject StatResultType;
1462static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001463static newfunc structseq_new;
1464
1465static PyObject *
1466statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1467{
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 PyStructSequence *result;
1469 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001470
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 result = (PyStructSequence*)structseq_new(type, args, kwds);
1472 if (!result)
1473 return NULL;
1474 /* If we have been initialized from a tuple,
1475 st_?time might be set to None. Initialize it
1476 from the int slots. */
1477 for (i = 7; i <= 9; i++) {
1478 if (result->ob_item[i+3] == Py_None) {
1479 Py_DECREF(Py_None);
1480 Py_INCREF(result->ob_item[i]);
1481 result->ob_item[i+3] = result->ob_item[i];
1482 }
1483 }
1484 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001485}
1486
1487
1488
1489/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001490static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001491
1492PyDoc_STRVAR(stat_float_times__doc__,
1493"stat_float_times([newval]) -> oldval\n\n\
1494Determine whether os.[lf]stat represents time stamps as float objects.\n\
1495If newval is True, future calls to stat() return floats, if it is False,\n\
1496future calls return ints. \n\
1497If newval is omitted, return the current setting.\n");
1498
1499static PyObject*
1500stat_float_times(PyObject* self, PyObject *args)
1501{
Victor Stinner8c62be82010-05-06 00:08:46 +00001502 int newval = -1;
1503 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1504 return NULL;
1505 if (newval == -1)
1506 /* Return old value */
1507 return PyBool_FromLong(_stat_float_times);
1508 _stat_float_times = newval;
1509 Py_INCREF(Py_None);
1510 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001511}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001512
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001513static void
1514fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1515{
Victor Stinner8c62be82010-05-06 00:08:46 +00001516 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001517#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001518 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001519#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001520 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001521#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001522 if (!ival)
1523 return;
1524 if (_stat_float_times) {
1525 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1526 } else {
1527 fval = ival;
1528 Py_INCREF(fval);
1529 }
1530 PyStructSequence_SET_ITEM(v, index, ival);
1531 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001532}
1533
Tim Peters5aa91602002-01-30 05:46:57 +00001534/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001535 (used by posix_stat() and posix_fstat()) */
1536static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001537_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001538{
Victor Stinner8c62be82010-05-06 00:08:46 +00001539 unsigned long ansec, mnsec, cnsec;
1540 PyObject *v = PyStructSequence_New(&StatResultType);
1541 if (v == NULL)
1542 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001543
Victor Stinner8c62be82010-05-06 00:08:46 +00001544 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001545#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001546 PyStructSequence_SET_ITEM(v, 1,
1547 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001548#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001549 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001550#endif
1551#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001552 PyStructSequence_SET_ITEM(v, 2,
1553 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001554#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001555 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001556#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001557 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1558 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1559 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001560#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001561 PyStructSequence_SET_ITEM(v, 6,
1562 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001563#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001564 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001565#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001566
Martin v. Löwis14694662006-02-03 12:54:16 +00001567#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001568 ansec = st->st_atim.tv_nsec;
1569 mnsec = st->st_mtim.tv_nsec;
1570 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001571#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001572 ansec = st->st_atimespec.tv_nsec;
1573 mnsec = st->st_mtimespec.tv_nsec;
1574 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001575#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001576 ansec = st->st_atime_nsec;
1577 mnsec = st->st_mtime_nsec;
1578 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001579#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001580 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001581#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001582 fill_time(v, 7, st->st_atime, ansec);
1583 fill_time(v, 8, st->st_mtime, mnsec);
1584 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001585
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001586#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001587 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1588 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001589#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001590#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1592 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001593#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001594#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1596 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001597#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001598#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1600 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001601#endif
1602#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001603 {
1604 PyObject *val;
1605 unsigned long bsec,bnsec;
1606 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001607#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001609#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001610 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001611#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001612 if (_stat_float_times) {
1613 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1614 } else {
1615 val = PyLong_FromLong((long)bsec);
1616 }
1617 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1618 val);
1619 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001620#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001621#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1623 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001624#endif
Fred Drake699f3522000-06-29 21:12:41 +00001625
Victor Stinner8c62be82010-05-06 00:08:46 +00001626 if (PyErr_Occurred()) {
1627 Py_DECREF(v);
1628 return NULL;
1629 }
Fred Drake699f3522000-06-29 21:12:41 +00001630
Victor Stinner8c62be82010-05-06 00:08:46 +00001631 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001632}
1633
Barry Warsaw53699e91996-12-10 23:23:01 +00001634static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001635posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001636 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001637#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001638 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001639#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001640 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001641#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001642 char *wformat,
1643 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001644{
Victor Stinner8c62be82010-05-06 00:08:46 +00001645 STRUCT_STAT st;
1646 PyObject *opath;
1647 char *path;
1648 int res;
1649 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001650
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001651#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001652 PyUnicodeObject *po;
1653 if (PyArg_ParseTuple(args, wformat, &po)) {
1654 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001655
Victor Stinner8c62be82010-05-06 00:08:46 +00001656 Py_BEGIN_ALLOW_THREADS
1657 /* PyUnicode_AS_UNICODE result OK without
1658 thread lock as it is a simple dereference. */
1659 res = wstatfunc(wpath, &st);
1660 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001661
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 if (res != 0)
1663 return win32_error_unicode("stat", wpath);
1664 return _pystat_fromstructstat(&st);
1665 }
1666 /* Drop the argument parsing error as narrow strings
1667 are also valid. */
1668 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001669#endif
1670
Victor Stinner8c62be82010-05-06 00:08:46 +00001671 if (!PyArg_ParseTuple(args, format,
1672 PyUnicode_FSConverter, &opath))
1673 return NULL;
1674 path = PyBytes_AsString(opath);
1675 Py_BEGIN_ALLOW_THREADS
1676 res = (*statfunc)(path, &st);
1677 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001678
Victor Stinner8c62be82010-05-06 00:08:46 +00001679 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001680#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001681 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001682#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001684#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001685 }
1686 else
1687 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001688
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 Py_DECREF(opath);
1690 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001691}
1692
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001693/* POSIX methods */
1694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001695PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001696"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001697Use the real uid/gid to test for access to a path. Note that most\n\
1698operations will use the effective uid/gid, therefore this routine can\n\
1699be used in a suid/sgid environment to test if the invoking user has the\n\
1700specified access to the path. The mode argument can be F_OK to test\n\
1701existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001702
1703static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001704posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001705{
Victor Stinner8c62be82010-05-06 00:08:46 +00001706 PyObject *opath;
1707 char *path;
1708 int mode;
1709
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001710#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 DWORD attr;
1712 PyUnicodeObject *po;
1713 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1714 Py_BEGIN_ALLOW_THREADS
1715 /* PyUnicode_AS_UNICODE OK without thread lock as
1716 it is a simple dereference. */
1717 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1718 Py_END_ALLOW_THREADS
1719 goto finish;
1720 }
1721 /* Drop the argument parsing error as narrow strings
1722 are also valid. */
1723 PyErr_Clear();
1724 if (!PyArg_ParseTuple(args, "O&i:access",
1725 PyUnicode_FSConverter, &opath, &mode))
1726 return NULL;
1727 path = PyBytes_AsString(opath);
1728 Py_BEGIN_ALLOW_THREADS
1729 attr = GetFileAttributesA(path);
1730 Py_END_ALLOW_THREADS
1731 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001732finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001733 if (attr == 0xFFFFFFFF)
1734 /* File does not exist, or cannot read attributes */
1735 return PyBool_FromLong(0);
1736 /* Access is possible if either write access wasn't requested, or
1737 the file isn't read-only, or if it's a directory, as there are
1738 no read-only directories on Windows. */
1739 return PyBool_FromLong(!(mode & 2)
1740 || !(attr & FILE_ATTRIBUTE_READONLY)
1741 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001742#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001743 int res;
1744 if (!PyArg_ParseTuple(args, "O&i:access",
1745 PyUnicode_FSConverter, &opath, &mode))
1746 return NULL;
1747 path = PyBytes_AsString(opath);
1748 Py_BEGIN_ALLOW_THREADS
1749 res = access(path, mode);
1750 Py_END_ALLOW_THREADS
1751 Py_DECREF(opath);
1752 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001753#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001754}
1755
Guido van Rossumd371ff11999-01-25 16:12:23 +00001756#ifndef F_OK
1757#define F_OK 0
1758#endif
1759#ifndef R_OK
1760#define R_OK 4
1761#endif
1762#ifndef W_OK
1763#define W_OK 2
1764#endif
1765#ifndef X_OK
1766#define X_OK 1
1767#endif
1768
1769#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001771"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001773
1774static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001775posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001776{
Victor Stinner8c62be82010-05-06 00:08:46 +00001777 int id;
1778 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001779
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1781 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001782
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001783#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 /* file descriptor 0 only, the default input device (stdin) */
1785 if (id == 0) {
1786 ret = ttyname();
1787 }
1788 else {
1789 ret = NULL;
1790 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001791#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001793#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001794 if (ret == NULL)
1795 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001796 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001797}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001798#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001799
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001800#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001802"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001804
1805static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001806posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001807{
Victor Stinner8c62be82010-05-06 00:08:46 +00001808 char *ret;
1809 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001810
Greg Wardb48bc172000-03-01 21:51:56 +00001811#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001812 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001813#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001814 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001815#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001816 if (ret == NULL)
1817 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001818 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001819}
1820#endif
1821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001822PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001823"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001824Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001825
Barry Warsaw53699e91996-12-10 23:23:01 +00001826static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001827posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001828{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001829#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001830 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001831#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001832 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001833#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001834 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001835#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001836 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001837#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001838}
1839
Fred Drake4d1e64b2002-04-15 19:40:07 +00001840#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001842"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001843Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001844opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001845
1846static PyObject *
1847posix_fchdir(PyObject *self, PyObject *fdobj)
1848{
Victor Stinner8c62be82010-05-06 00:08:46 +00001849 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001850}
1851#endif /* HAVE_FCHDIR */
1852
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001855"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001857
Barry Warsaw53699e91996-12-10 23:23:01 +00001858static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001859posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001860{
Victor Stinner8c62be82010-05-06 00:08:46 +00001861 PyObject *opath = NULL;
1862 char *path = NULL;
1863 int i;
1864 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001865#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001866 DWORD attr;
1867 PyUnicodeObject *po;
1868 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1869 Py_BEGIN_ALLOW_THREADS
1870 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1871 if (attr != 0xFFFFFFFF) {
1872 if (i & _S_IWRITE)
1873 attr &= ~FILE_ATTRIBUTE_READONLY;
1874 else
1875 attr |= FILE_ATTRIBUTE_READONLY;
1876 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1877 }
1878 else
1879 res = 0;
1880 Py_END_ALLOW_THREADS
1881 if (!res)
1882 return win32_error_unicode("chmod",
1883 PyUnicode_AS_UNICODE(po));
1884 Py_INCREF(Py_None);
1885 return Py_None;
1886 }
1887 /* Drop the argument parsing error as narrow strings
1888 are also valid. */
1889 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001890
Victor Stinner8c62be82010-05-06 00:08:46 +00001891 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1892 &opath, &i))
1893 return NULL;
1894 path = PyBytes_AsString(opath);
1895 Py_BEGIN_ALLOW_THREADS
1896 attr = GetFileAttributesA(path);
1897 if (attr != 0xFFFFFFFF) {
1898 if (i & _S_IWRITE)
1899 attr &= ~FILE_ATTRIBUTE_READONLY;
1900 else
1901 attr |= FILE_ATTRIBUTE_READONLY;
1902 res = SetFileAttributesA(path, attr);
1903 }
1904 else
1905 res = 0;
1906 Py_END_ALLOW_THREADS
1907 if (!res) {
1908 win32_error("chmod", path);
1909 Py_DECREF(opath);
1910 return NULL;
1911 }
1912 Py_DECREF(opath);
1913 Py_INCREF(Py_None);
1914 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001915#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001916 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1917 &opath, &i))
1918 return NULL;
1919 path = PyBytes_AsString(opath);
1920 Py_BEGIN_ALLOW_THREADS
1921 res = chmod(path, i);
1922 Py_END_ALLOW_THREADS
1923 if (res < 0)
1924 return posix_error_with_allocated_filename(opath);
1925 Py_DECREF(opath);
1926 Py_INCREF(Py_None);
1927 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001928#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001929}
1930
Christian Heimes4e30a842007-11-30 22:12:06 +00001931#ifdef HAVE_FCHMOD
1932PyDoc_STRVAR(posix_fchmod__doc__,
1933"fchmod(fd, mode)\n\n\
1934Change the access permissions of the file given by file\n\
1935descriptor fd.");
1936
1937static PyObject *
1938posix_fchmod(PyObject *self, PyObject *args)
1939{
Victor Stinner8c62be82010-05-06 00:08:46 +00001940 int fd, mode, res;
1941 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1942 return NULL;
1943 Py_BEGIN_ALLOW_THREADS
1944 res = fchmod(fd, mode);
1945 Py_END_ALLOW_THREADS
1946 if (res < 0)
1947 return posix_error();
1948 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001949}
1950#endif /* HAVE_FCHMOD */
1951
1952#ifdef HAVE_LCHMOD
1953PyDoc_STRVAR(posix_lchmod__doc__,
1954"lchmod(path, mode)\n\n\
1955Change the access permissions of a file. If path is a symlink, this\n\
1956affects the link itself rather than the target.");
1957
1958static PyObject *
1959posix_lchmod(PyObject *self, PyObject *args)
1960{
Victor Stinner8c62be82010-05-06 00:08:46 +00001961 PyObject *opath;
1962 char *path;
1963 int i;
1964 int res;
1965 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1966 &opath, &i))
1967 return NULL;
1968 path = PyBytes_AsString(opath);
1969 Py_BEGIN_ALLOW_THREADS
1970 res = lchmod(path, i);
1971 Py_END_ALLOW_THREADS
1972 if (res < 0)
1973 return posix_error_with_allocated_filename(opath);
1974 Py_DECREF(opath);
1975 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001976}
1977#endif /* HAVE_LCHMOD */
1978
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001979
Thomas Wouterscf297e42007-02-23 15:07:44 +00001980#ifdef HAVE_CHFLAGS
1981PyDoc_STRVAR(posix_chflags__doc__,
1982"chflags(path, flags)\n\n\
1983Set file flags.");
1984
1985static PyObject *
1986posix_chflags(PyObject *self, PyObject *args)
1987{
Victor Stinner8c62be82010-05-06 00:08:46 +00001988 PyObject *opath;
1989 char *path;
1990 unsigned long flags;
1991 int res;
1992 if (!PyArg_ParseTuple(args, "O&k:chflags",
1993 PyUnicode_FSConverter, &opath, &flags))
1994 return NULL;
1995 path = PyBytes_AsString(opath);
1996 Py_BEGIN_ALLOW_THREADS
1997 res = chflags(path, flags);
1998 Py_END_ALLOW_THREADS
1999 if (res < 0)
2000 return posix_error_with_allocated_filename(opath);
2001 Py_DECREF(opath);
2002 Py_INCREF(Py_None);
2003 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002004}
2005#endif /* HAVE_CHFLAGS */
2006
2007#ifdef HAVE_LCHFLAGS
2008PyDoc_STRVAR(posix_lchflags__doc__,
2009"lchflags(path, flags)\n\n\
2010Set file flags.\n\
2011This function will not follow symbolic links.");
2012
2013static PyObject *
2014posix_lchflags(PyObject *self, PyObject *args)
2015{
Victor Stinner8c62be82010-05-06 00:08:46 +00002016 PyObject *opath;
2017 char *path;
2018 unsigned long flags;
2019 int res;
2020 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2021 PyUnicode_FSConverter, &opath, &flags))
2022 return NULL;
2023 path = PyBytes_AsString(opath);
2024 Py_BEGIN_ALLOW_THREADS
2025 res = lchflags(path, flags);
2026 Py_END_ALLOW_THREADS
2027 if (res < 0)
2028 return posix_error_with_allocated_filename(opath);
2029 Py_DECREF(opath);
2030 Py_INCREF(Py_None);
2031 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002032}
2033#endif /* HAVE_LCHFLAGS */
2034
Martin v. Löwis244edc82001-10-04 22:44:26 +00002035#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002036PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002037"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002038Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002039
2040static PyObject *
2041posix_chroot(PyObject *self, PyObject *args)
2042{
Victor Stinner8c62be82010-05-06 00:08:46 +00002043 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002044}
2045#endif
2046
Guido van Rossum21142a01999-01-08 21:05:37 +00002047#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002049"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002050force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002051
2052static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002053posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002054{
Stefan Krah0e803b32010-11-26 16:16:47 +00002055 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002056}
2057#endif /* HAVE_FSYNC */
2058
2059#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002060
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002061#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002062extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2063#endif
2064
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002066"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002067force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002069
2070static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002071posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002072{
Stefan Krah0e803b32010-11-26 16:16:47 +00002073 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002074}
2075#endif /* HAVE_FDATASYNC */
2076
2077
Fredrik Lundh10723342000-07-10 16:38:09 +00002078#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002079PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002080"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002081Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002082
Barry Warsaw53699e91996-12-10 23:23:01 +00002083static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002084posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085{
Victor Stinner8c62be82010-05-06 00:08:46 +00002086 PyObject *opath;
2087 char *path;
2088 long uid, gid;
2089 int res;
2090 if (!PyArg_ParseTuple(args, "O&ll:chown",
2091 PyUnicode_FSConverter, &opath,
2092 &uid, &gid))
2093 return NULL;
2094 path = PyBytes_AsString(opath);
2095 Py_BEGIN_ALLOW_THREADS
2096 res = chown(path, (uid_t) uid, (gid_t) gid);
2097 Py_END_ALLOW_THREADS
2098 if (res < 0)
2099 return posix_error_with_allocated_filename(opath);
2100 Py_DECREF(opath);
2101 Py_INCREF(Py_None);
2102 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002104#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002105
Christian Heimes4e30a842007-11-30 22:12:06 +00002106#ifdef HAVE_FCHOWN
2107PyDoc_STRVAR(posix_fchown__doc__,
2108"fchown(fd, uid, gid)\n\n\
2109Change the owner and group id of the file given by file descriptor\n\
2110fd to the numeric uid and gid.");
2111
2112static PyObject *
2113posix_fchown(PyObject *self, PyObject *args)
2114{
Victor Stinner8c62be82010-05-06 00:08:46 +00002115 int fd;
2116 long uid, gid;
2117 int res;
2118 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2119 return NULL;
2120 Py_BEGIN_ALLOW_THREADS
2121 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2122 Py_END_ALLOW_THREADS
2123 if (res < 0)
2124 return posix_error();
2125 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002126}
2127#endif /* HAVE_FCHOWN */
2128
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002129#ifdef HAVE_LCHOWN
2130PyDoc_STRVAR(posix_lchown__doc__,
2131"lchown(path, uid, gid)\n\n\
2132Change the owner and group id of path to the numeric uid and gid.\n\
2133This function will not follow symbolic links.");
2134
2135static PyObject *
2136posix_lchown(PyObject *self, PyObject *args)
2137{
Victor Stinner8c62be82010-05-06 00:08:46 +00002138 PyObject *opath;
2139 char *path;
2140 long uid, gid;
2141 int res;
2142 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2143 PyUnicode_FSConverter, &opath,
2144 &uid, &gid))
2145 return NULL;
2146 path = PyBytes_AsString(opath);
2147 Py_BEGIN_ALLOW_THREADS
2148 res = lchown(path, (uid_t) uid, (gid_t) gid);
2149 Py_END_ALLOW_THREADS
2150 if (res < 0)
2151 return posix_error_with_allocated_filename(opath);
2152 Py_DECREF(opath);
2153 Py_INCREF(Py_None);
2154 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002155}
2156#endif /* HAVE_LCHOWN */
2157
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002158
Guido van Rossum36bc6801995-06-14 22:54:23 +00002159#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002160static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002161posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002162{
Victor Stinner8c62be82010-05-06 00:08:46 +00002163 char buf[1026];
2164 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002165
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002166#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002167 if (!use_bytes) {
2168 wchar_t wbuf[1026];
2169 wchar_t *wbuf2 = wbuf;
2170 PyObject *resobj;
2171 DWORD len;
2172 Py_BEGIN_ALLOW_THREADS
2173 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2174 /* If the buffer is large enough, len does not include the
2175 terminating \0. If the buffer is too small, len includes
2176 the space needed for the terminator. */
2177 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2178 wbuf2 = malloc(len * sizeof(wchar_t));
2179 if (wbuf2)
2180 len = GetCurrentDirectoryW(len, wbuf2);
2181 }
2182 Py_END_ALLOW_THREADS
2183 if (!wbuf2) {
2184 PyErr_NoMemory();
2185 return NULL;
2186 }
2187 if (!len) {
2188 if (wbuf2 != wbuf) free(wbuf2);
2189 return win32_error("getcwdu", NULL);
2190 }
2191 resobj = PyUnicode_FromWideChar(wbuf2, len);
2192 if (wbuf2 != wbuf) free(wbuf2);
2193 return resobj;
2194 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002195#endif
2196
Victor Stinner8c62be82010-05-06 00:08:46 +00002197 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002198#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002199 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002200#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002201 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002202#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002203 Py_END_ALLOW_THREADS
2204 if (res == NULL)
2205 return posix_error();
2206 if (use_bytes)
2207 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002208 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002209}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002210
2211PyDoc_STRVAR(posix_getcwd__doc__,
2212"getcwd() -> path\n\n\
2213Return a unicode string representing the current working directory.");
2214
2215static PyObject *
2216posix_getcwd_unicode(PyObject *self)
2217{
2218 return posix_getcwd(0);
2219}
2220
2221PyDoc_STRVAR(posix_getcwdb__doc__,
2222"getcwdb() -> path\n\n\
2223Return a bytes string representing the current working directory.");
2224
2225static PyObject *
2226posix_getcwd_bytes(PyObject *self)
2227{
2228 return posix_getcwd(1);
2229}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002230#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002231
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002232
Guido van Rossumb6775db1994-08-01 11:34:53 +00002233#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002234PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002235"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002236Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002237
Barry Warsaw53699e91996-12-10 23:23:01 +00002238static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002239posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002240{
Victor Stinner8c62be82010-05-06 00:08:46 +00002241 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002242}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002243#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002244
Brian Curtin1b9df392010-11-24 20:24:31 +00002245#ifdef MS_WINDOWS
2246PyDoc_STRVAR(win32_link__doc__,
2247"link(src, dst)\n\n\
2248Create a hard link to a file.");
2249
2250static PyObject *
2251win32_link(PyObject *self, PyObject *args)
2252{
2253 PyObject *osrc, *odst;
2254 char *src, *dst;
2255 BOOL rslt;
2256
Brian Curtinfc889c42010-11-28 23:59:46 +00002257 PyUnicodeObject *usrc, *udst;
2258 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2259 Py_BEGIN_ALLOW_THREADS
2260 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2261 PyUnicode_AS_UNICODE(usrc), NULL);
2262 Py_END_ALLOW_THREADS
2263
2264 if (rslt == 0)
2265 return win32_error("link", NULL);
2266
2267 Py_RETURN_NONE;
2268 }
2269
2270 /* Narrow strings also valid. */
2271 PyErr_Clear();
2272
Brian Curtin1b9df392010-11-24 20:24:31 +00002273 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2274 PyUnicode_FSConverter, &odst))
2275 return NULL;
2276
2277 src = PyBytes_AsString(osrc);
2278 dst = PyBytes_AsString(odst);
2279
2280 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002281 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002282 Py_END_ALLOW_THREADS
2283
Stefan Krah30b341f2010-11-27 11:44:18 +00002284 Py_DECREF(osrc);
2285 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002286 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002287 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002288
2289 Py_RETURN_NONE;
2290}
2291#endif /* MS_WINDOWS */
2292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002294PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002295"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002296Return a list containing the names of the entries in the directory.\n\
2297\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002298 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002299\n\
2300The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002301entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002302
Barry Warsaw53699e91996-12-10 23:23:01 +00002303static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002304posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002305{
Victor Stinner8c62be82010-05-06 00:08:46 +00002306 /* XXX Should redo this putting the (now four) versions of opendir
2307 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002308#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002309
Victor Stinner8c62be82010-05-06 00:08:46 +00002310 PyObject *d, *v;
2311 HANDLE hFindFile;
2312 BOOL result;
2313 WIN32_FIND_DATA FileData;
2314 PyObject *opath;
2315 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2316 char *bufptr = namebuf;
2317 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002318
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002319 PyObject *po = NULL;
2320 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002321 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002322 Py_UNICODE *wnamebuf, *po_wchars;
2323
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002324 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002325 po_wchars = L".";
2326 len = 1;
2327 } else {
2328 po_wchars = PyUnicode_AS_UNICODE(po);
2329 len = PyUnicode_GET_SIZE(po);
2330 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002331 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002332 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2333 if (!wnamebuf) {
2334 PyErr_NoMemory();
2335 return NULL;
2336 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002337 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002338 if (len > 0) {
2339 Py_UNICODE wch = wnamebuf[len-1];
2340 if (wch != L'/' && wch != L'\\' && wch != L':')
2341 wnamebuf[len++] = L'\\';
2342 wcscpy(wnamebuf + len, L"*.*");
2343 }
2344 if ((d = PyList_New(0)) == NULL) {
2345 free(wnamebuf);
2346 return NULL;
2347 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002348 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002349 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002350 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002351 if (hFindFile == INVALID_HANDLE_VALUE) {
2352 int error = GetLastError();
2353 if (error == ERROR_FILE_NOT_FOUND) {
2354 free(wnamebuf);
2355 return d;
2356 }
2357 Py_DECREF(d);
2358 win32_error_unicode("FindFirstFileW", wnamebuf);
2359 free(wnamebuf);
2360 return NULL;
2361 }
2362 do {
2363 /* Skip over . and .. */
2364 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2365 wcscmp(wFileData.cFileName, L"..") != 0) {
2366 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2367 if (v == NULL) {
2368 Py_DECREF(d);
2369 d = NULL;
2370 break;
2371 }
2372 if (PyList_Append(d, v) != 0) {
2373 Py_DECREF(v);
2374 Py_DECREF(d);
2375 d = NULL;
2376 break;
2377 }
2378 Py_DECREF(v);
2379 }
2380 Py_BEGIN_ALLOW_THREADS
2381 result = FindNextFileW(hFindFile, &wFileData);
2382 Py_END_ALLOW_THREADS
2383 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2384 it got to the end of the directory. */
2385 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2386 Py_DECREF(d);
2387 win32_error_unicode("FindNextFileW", wnamebuf);
2388 FindClose(hFindFile);
2389 free(wnamebuf);
2390 return NULL;
2391 }
2392 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002393
Victor Stinner8c62be82010-05-06 00:08:46 +00002394 if (FindClose(hFindFile) == FALSE) {
2395 Py_DECREF(d);
2396 win32_error_unicode("FindClose", wnamebuf);
2397 free(wnamebuf);
2398 return NULL;
2399 }
2400 free(wnamebuf);
2401 return d;
2402 }
2403 /* Drop the argument parsing error as narrow strings
2404 are also valid. */
2405 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002406
Victor Stinner8c62be82010-05-06 00:08:46 +00002407 if (!PyArg_ParseTuple(args, "O&:listdir",
2408 PyUnicode_FSConverter, &opath))
2409 return NULL;
2410 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2411 PyErr_SetString(PyExc_ValueError, "path too long");
2412 Py_DECREF(opath);
2413 return NULL;
2414 }
2415 strcpy(namebuf, PyBytes_AsString(opath));
2416 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002417 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002418 if (len > 0) {
2419 char ch = namebuf[len-1];
2420 if (ch != SEP && ch != ALTSEP && ch != ':')
2421 namebuf[len++] = '/';
2422 strcpy(namebuf + len, "*.*");
2423 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002424
Victor Stinner8c62be82010-05-06 00:08:46 +00002425 if ((d = PyList_New(0)) == NULL)
2426 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002427
Antoine Pitroub73caab2010-08-09 23:39:31 +00002428 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002429 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002430 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002431 if (hFindFile == INVALID_HANDLE_VALUE) {
2432 int error = GetLastError();
2433 if (error == ERROR_FILE_NOT_FOUND)
2434 return d;
2435 Py_DECREF(d);
2436 return win32_error("FindFirstFile", namebuf);
2437 }
2438 do {
2439 /* Skip over . and .. */
2440 if (strcmp(FileData.cFileName, ".") != 0 &&
2441 strcmp(FileData.cFileName, "..") != 0) {
2442 v = PyBytes_FromString(FileData.cFileName);
2443 if (v == NULL) {
2444 Py_DECREF(d);
2445 d = NULL;
2446 break;
2447 }
2448 if (PyList_Append(d, v) != 0) {
2449 Py_DECREF(v);
2450 Py_DECREF(d);
2451 d = NULL;
2452 break;
2453 }
2454 Py_DECREF(v);
2455 }
2456 Py_BEGIN_ALLOW_THREADS
2457 result = FindNextFile(hFindFile, &FileData);
2458 Py_END_ALLOW_THREADS
2459 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2460 it got to the end of the directory. */
2461 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2462 Py_DECREF(d);
2463 win32_error("FindNextFile", namebuf);
2464 FindClose(hFindFile);
2465 return NULL;
2466 }
2467 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002468
Victor Stinner8c62be82010-05-06 00:08:46 +00002469 if (FindClose(hFindFile) == FALSE) {
2470 Py_DECREF(d);
2471 return win32_error("FindClose", namebuf);
2472 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002473
Victor Stinner8c62be82010-05-06 00:08:46 +00002474 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002475
Tim Peters0bb44a42000-09-15 07:44:49 +00002476#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002477
2478#ifndef MAX_PATH
2479#define MAX_PATH CCHMAXPATH
2480#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002481 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002482 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002483 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002484 PyObject *d, *v;
2485 char namebuf[MAX_PATH+5];
2486 HDIR hdir = 1;
2487 ULONG srchcnt = 1;
2488 FILEFINDBUF3 ep;
2489 APIRET rc;
2490
Victor Stinner8c62be82010-05-06 00:08:46 +00002491 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002492 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002493 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002494 name = PyBytes_AsString(oname);
2495 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002496 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002497 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002498 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002499 return NULL;
2500 }
2501 strcpy(namebuf, name);
2502 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002503 if (*pt == ALTSEP)
2504 *pt = SEP;
2505 if (namebuf[len-1] != SEP)
2506 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002507 strcpy(namebuf + len, "*.*");
2508
Neal Norwitz6c913782007-10-14 03:23:09 +00002509 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002510 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002511 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002512 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002513
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002514 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2515 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002516 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002517 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2518 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2519 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002520
2521 if (rc != NO_ERROR) {
2522 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002523 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002524 }
2525
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002526 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002527 do {
2528 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002529 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002530 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002531
2532 strcpy(namebuf, ep.achName);
2533
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002534 /* Leave Case of Name Alone -- In Native Form */
2535 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002536
Christian Heimes72b710a2008-05-26 13:28:38 +00002537 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002538 if (v == NULL) {
2539 Py_DECREF(d);
2540 d = NULL;
2541 break;
2542 }
2543 if (PyList_Append(d, v) != 0) {
2544 Py_DECREF(v);
2545 Py_DECREF(d);
2546 d = NULL;
2547 break;
2548 }
2549 Py_DECREF(v);
2550 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2551 }
2552
Victor Stinnerdcb24032010-04-22 12:08:36 +00002553 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002554 return d;
2555#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002556 PyObject *oname;
2557 char *name;
2558 PyObject *d, *v;
2559 DIR *dirp;
2560 struct dirent *ep;
2561 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002562
Victor Stinner8c62be82010-05-06 00:08:46 +00002563 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002564 /* v is never read, so it does not need to be initialized yet. */
2565 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002566 arg_is_unicode = 0;
2567 PyErr_Clear();
2568 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002569 oname = NULL;
2570 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002571 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002572 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002573 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002574 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002575 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002576 Py_BEGIN_ALLOW_THREADS
2577 dirp = opendir(name);
2578 Py_END_ALLOW_THREADS
2579 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002580 return posix_error_with_allocated_filename(oname);
2581 }
2582 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002583 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002584 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002585 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002586 Py_DECREF(oname);
2587 return NULL;
2588 }
2589 for (;;) {
2590 errno = 0;
2591 Py_BEGIN_ALLOW_THREADS
2592 ep = readdir(dirp);
2593 Py_END_ALLOW_THREADS
2594 if (ep == NULL) {
2595 if (errno == 0) {
2596 break;
2597 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002598 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002599 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002600 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002601 Py_DECREF(d);
2602 return posix_error_with_allocated_filename(oname);
2603 }
2604 }
2605 if (ep->d_name[0] == '.' &&
2606 (NAMLEN(ep) == 1 ||
2607 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2608 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002609 if (arg_is_unicode)
2610 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2611 else
2612 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002613 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002614 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002615 break;
2616 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002617 if (PyList_Append(d, v) != 0) {
2618 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002619 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002620 break;
2621 }
2622 Py_DECREF(v);
2623 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002624 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002625 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002626 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002627 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002628
Victor Stinner8c62be82010-05-06 00:08:46 +00002629 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002630
Tim Peters0bb44a42000-09-15 07:44:49 +00002631#endif /* which OS */
2632} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002633
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002634#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002635/* A helper function for abspath on win32 */
2636static PyObject *
2637posix__getfullpathname(PyObject *self, PyObject *args)
2638{
Victor Stinner8c62be82010-05-06 00:08:46 +00002639 PyObject *opath;
2640 char *path;
2641 char outbuf[MAX_PATH*2];
2642 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002643#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 PyUnicodeObject *po;
2645 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2646 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2647 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2648 Py_UNICODE *wtemp;
2649 DWORD result;
2650 PyObject *v;
2651 result = GetFullPathNameW(wpath,
2652 sizeof(woutbuf)/sizeof(woutbuf[0]),
2653 woutbuf, &wtemp);
2654 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2655 woutbufp = malloc(result * sizeof(Py_UNICODE));
2656 if (!woutbufp)
2657 return PyErr_NoMemory();
2658 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2659 }
2660 if (result)
2661 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2662 else
2663 v = win32_error_unicode("GetFullPathNameW", wpath);
2664 if (woutbufp != woutbuf)
2665 free(woutbufp);
2666 return v;
2667 }
2668 /* Drop the argument parsing error as narrow strings
2669 are also valid. */
2670 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002671
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002672#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002673 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2674 PyUnicode_FSConverter, &opath))
2675 return NULL;
2676 path = PyBytes_AsString(opath);
2677 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2678 outbuf, &temp)) {
2679 win32_error("GetFullPathName", path);
2680 Py_DECREF(opath);
2681 return NULL;
2682 }
2683 Py_DECREF(opath);
2684 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2685 return PyUnicode_Decode(outbuf, strlen(outbuf),
2686 Py_FileSystemDefaultEncoding, NULL);
2687 }
2688 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002689} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002690
Brian Curtinf5e76d02010-11-24 13:14:05 +00002691/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2692static int has_GetFinalPathNameByHandle = 0;
2693static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2694 DWORD);
2695static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2696 DWORD);
2697static int
2698check_GetFinalPathNameByHandle()
2699{
2700 HINSTANCE hKernel32;
2701 /* only recheck */
2702 if (!has_GetFinalPathNameByHandle)
2703 {
2704 hKernel32 = GetModuleHandle("KERNEL32");
2705 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2706 "GetFinalPathNameByHandleA");
2707 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2708 "GetFinalPathNameByHandleW");
2709 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2710 Py_GetFinalPathNameByHandleW;
2711 }
2712 return has_GetFinalPathNameByHandle;
2713}
2714
Brian Curtind40e6f72010-07-08 21:39:08 +00002715/* A helper function for samepath on windows */
2716static PyObject *
2717posix__getfinalpathname(PyObject *self, PyObject *args)
2718{
2719 HANDLE hFile;
2720 int buf_size;
2721 wchar_t *target_path;
2722 int result_length;
2723 PyObject *result;
2724 wchar_t *path;
2725
Brian Curtin94622b02010-09-24 00:03:39 +00002726 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002727 return NULL;
2728 }
2729
2730 if(!check_GetFinalPathNameByHandle()) {
2731 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2732 NotImplementedError. */
2733 return PyErr_Format(PyExc_NotImplementedError,
2734 "GetFinalPathNameByHandle not available on this platform");
2735 }
2736
2737 hFile = CreateFileW(
2738 path,
2739 0, /* desired access */
2740 0, /* share mode */
2741 NULL, /* security attributes */
2742 OPEN_EXISTING,
2743 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2744 FILE_FLAG_BACKUP_SEMANTICS,
2745 NULL);
2746
2747 if(hFile == INVALID_HANDLE_VALUE) {
2748 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002749 return PyErr_Format(PyExc_RuntimeError,
2750 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002751 }
2752
2753 /* We have a good handle to the target, use it to determine the
2754 target path name. */
2755 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2756
2757 if(!buf_size)
2758 return win32_error_unicode("GetFinalPathNameByHandle", path);
2759
2760 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2761 if(!target_path)
2762 return PyErr_NoMemory();
2763
2764 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2765 buf_size, VOLUME_NAME_DOS);
2766 if(!result_length)
2767 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2768
2769 if(!CloseHandle(hFile))
2770 return win32_error_unicode("GetFinalPathNameByHandle", path);
2771
2772 target_path[result_length] = 0;
2773 result = PyUnicode_FromUnicode(target_path, result_length);
2774 free(target_path);
2775 return result;
2776
2777} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002778
2779static PyObject *
2780posix__getfileinformation(PyObject *self, PyObject *args)
2781{
2782 HANDLE hFile;
2783 BY_HANDLE_FILE_INFORMATION info;
2784 int fd;
2785
2786 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2787 return NULL;
2788
2789 if (!_PyVerify_fd(fd)) {
2790 PyErr_SetString(PyExc_ValueError, "received invalid file descriptor");
2791 return NULL;
2792 }
2793
2794 hFile = (HANDLE)_get_osfhandle(fd);
2795 if (hFile == INVALID_HANDLE_VALUE)
2796 return win32_error("_getfileinformation", NULL);
2797
2798 if (!GetFileInformationByHandle(hFile, &info))
2799 return win32_error("_getfileinformation", NULL);
2800
2801 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2802 info.nFileIndexHigh,
2803 info.nFileIndexLow);
2804}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002805#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002807PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002808"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002809Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002810
Barry Warsaw53699e91996-12-10 23:23:01 +00002811static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002812posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002813{
Victor Stinner8c62be82010-05-06 00:08:46 +00002814 int res;
2815 PyObject *opath;
2816 char *path;
2817 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002818
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002819#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002820 PyUnicodeObject *po;
2821 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2822 Py_BEGIN_ALLOW_THREADS
2823 /* PyUnicode_AS_UNICODE OK without thread lock as
2824 it is a simple dereference. */
2825 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2826 Py_END_ALLOW_THREADS
2827 if (!res)
2828 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2829 Py_INCREF(Py_None);
2830 return Py_None;
2831 }
2832 /* Drop the argument parsing error as narrow strings
2833 are also valid. */
2834 PyErr_Clear();
2835 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2836 PyUnicode_FSConverter, &opath, &mode))
2837 return NULL;
2838 path = PyBytes_AsString(opath);
2839 Py_BEGIN_ALLOW_THREADS
2840 /* PyUnicode_AS_UNICODE OK without thread lock as
2841 it is a simple dereference. */
2842 res = CreateDirectoryA(path, NULL);
2843 Py_END_ALLOW_THREADS
2844 if (!res) {
2845 win32_error("mkdir", path);
2846 Py_DECREF(opath);
2847 return NULL;
2848 }
2849 Py_DECREF(opath);
2850 Py_INCREF(Py_None);
2851 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002852#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002853
Victor Stinner8c62be82010-05-06 00:08:46 +00002854 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2855 PyUnicode_FSConverter, &opath, &mode))
2856 return NULL;
2857 path = PyBytes_AsString(opath);
2858 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002859#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002860 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002861#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002862 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002863#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002864 Py_END_ALLOW_THREADS
2865 if (res < 0)
2866 return posix_error_with_allocated_filename(opath);
2867 Py_DECREF(opath);
2868 Py_INCREF(Py_None);
2869 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002870#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002871}
2872
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002873
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002874/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2875#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002876#include <sys/resource.h>
2877#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002878
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002879
2880#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002881PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002882"nice(inc) -> new_priority\n\n\
2883Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002884
Barry Warsaw53699e91996-12-10 23:23:01 +00002885static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002886posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002887{
Victor Stinner8c62be82010-05-06 00:08:46 +00002888 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002889
Victor Stinner8c62be82010-05-06 00:08:46 +00002890 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2891 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002892
Victor Stinner8c62be82010-05-06 00:08:46 +00002893 /* There are two flavours of 'nice': one that returns the new
2894 priority (as required by almost all standards out there) and the
2895 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2896 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002897
Victor Stinner8c62be82010-05-06 00:08:46 +00002898 If we are of the nice family that returns the new priority, we
2899 need to clear errno before the call, and check if errno is filled
2900 before calling posix_error() on a returnvalue of -1, because the
2901 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002902
Victor Stinner8c62be82010-05-06 00:08:46 +00002903 errno = 0;
2904 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002905#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002906 if (value == 0)
2907 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002908#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002909 if (value == -1 && errno != 0)
2910 /* either nice() or getpriority() returned an error */
2911 return posix_error();
2912 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002913}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002914#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002915
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002916PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002917"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002918Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002919
Barry Warsaw53699e91996-12-10 23:23:01 +00002920static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002921posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002922{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002923#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002924 PyObject *o1, *o2;
2925 char *p1, *p2;
2926 BOOL result;
2927 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2928 goto error;
2929 if (!convert_to_unicode(&o1))
2930 goto error;
2931 if (!convert_to_unicode(&o2)) {
2932 Py_DECREF(o1);
2933 goto error;
2934 }
2935 Py_BEGIN_ALLOW_THREADS
2936 result = MoveFileW(PyUnicode_AsUnicode(o1),
2937 PyUnicode_AsUnicode(o2));
2938 Py_END_ALLOW_THREADS
2939 Py_DECREF(o1);
2940 Py_DECREF(o2);
2941 if (!result)
2942 return win32_error("rename", NULL);
2943 Py_INCREF(Py_None);
2944 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002945error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002946 PyErr_Clear();
2947 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2948 return NULL;
2949 Py_BEGIN_ALLOW_THREADS
2950 result = MoveFileA(p1, p2);
2951 Py_END_ALLOW_THREADS
2952 if (!result)
2953 return win32_error("rename", NULL);
2954 Py_INCREF(Py_None);
2955 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002956#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002957 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002958#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002959}
2960
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002961
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002962PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002963"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002964Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002965
Barry Warsaw53699e91996-12-10 23:23:01 +00002966static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002967posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002968{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002969#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002970 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002971#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002972 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002973#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002974}
2975
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002977PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002978"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002979Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002980
Barry Warsaw53699e91996-12-10 23:23:01 +00002981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002982posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002983{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002984#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00002985 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002986#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002987 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002988#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002989}
2990
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002991
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002992#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002993PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002994"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002995Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002996
Barry Warsaw53699e91996-12-10 23:23:01 +00002997static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002998posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002999{
Victor Stinner8c62be82010-05-06 00:08:46 +00003000 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003001#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003002 wchar_t *command;
3003 if (!PyArg_ParseTuple(args, "u:system", &command))
3004 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003005
Victor Stinner8c62be82010-05-06 00:08:46 +00003006 Py_BEGIN_ALLOW_THREADS
3007 sts = _wsystem(command);
3008 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003009#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003010 PyObject *command_obj;
3011 char *command;
3012 if (!PyArg_ParseTuple(args, "O&:system",
3013 PyUnicode_FSConverter, &command_obj))
3014 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003015
Victor Stinner8c62be82010-05-06 00:08:46 +00003016 command = PyBytes_AsString(command_obj);
3017 Py_BEGIN_ALLOW_THREADS
3018 sts = system(command);
3019 Py_END_ALLOW_THREADS
3020 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003021#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003022 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003023}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003024#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003025
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003027PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003028"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003029Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003030
Barry Warsaw53699e91996-12-10 23:23:01 +00003031static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003032posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003033{
Victor Stinner8c62be82010-05-06 00:08:46 +00003034 int i;
3035 if (!PyArg_ParseTuple(args, "i:umask", &i))
3036 return NULL;
3037 i = (int)umask(i);
3038 if (i < 0)
3039 return posix_error();
3040 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003041}
3042
Brian Curtind40e6f72010-07-08 21:39:08 +00003043#ifdef MS_WINDOWS
3044
3045/* override the default DeleteFileW behavior so that directory
3046symlinks can be removed with this function, the same as with
3047Unix symlinks */
3048BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3049{
3050 WIN32_FILE_ATTRIBUTE_DATA info;
3051 WIN32_FIND_DATAW find_data;
3052 HANDLE find_data_handle;
3053 int is_directory = 0;
3054 int is_link = 0;
3055
3056 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3057 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3058
3059 /* Get WIN32_FIND_DATA structure for the path to determine if
3060 it is a symlink */
3061 if(is_directory &&
3062 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3063 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3064
3065 if(find_data_handle != INVALID_HANDLE_VALUE) {
3066 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3067 FindClose(find_data_handle);
3068 }
3069 }
3070 }
3071
3072 if (is_directory && is_link)
3073 return RemoveDirectoryW(lpFileName);
3074
3075 return DeleteFileW(lpFileName);
3076}
3077#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003078
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003079PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003080"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003081Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003082
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003083PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003084"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003085Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003086
Barry Warsaw53699e91996-12-10 23:23:01 +00003087static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003088posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003089{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003090#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003091 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3092 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003093#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003094 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003095#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003096}
3097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003098
Guido van Rossumb6775db1994-08-01 11:34:53 +00003099#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003101"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003102Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003103
Barry Warsaw53699e91996-12-10 23:23:01 +00003104static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003105posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003106{
Victor Stinner8c62be82010-05-06 00:08:46 +00003107 struct utsname u;
3108 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003109
Victor Stinner8c62be82010-05-06 00:08:46 +00003110 Py_BEGIN_ALLOW_THREADS
3111 res = uname(&u);
3112 Py_END_ALLOW_THREADS
3113 if (res < 0)
3114 return posix_error();
3115 return Py_BuildValue("(sssss)",
3116 u.sysname,
3117 u.nodename,
3118 u.release,
3119 u.version,
3120 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003121}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003122#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003123
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003124static int
3125extract_time(PyObject *t, long* sec, long* usec)
3126{
Victor Stinner8c62be82010-05-06 00:08:46 +00003127 long intval;
3128 if (PyFloat_Check(t)) {
3129 double tval = PyFloat_AsDouble(t);
3130 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
3131 if (!intobj)
3132 return -1;
3133 intval = PyLong_AsLong(intobj);
3134 Py_DECREF(intobj);
3135 if (intval == -1 && PyErr_Occurred())
3136 return -1;
3137 *sec = intval;
3138 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3139 if (*usec < 0)
3140 /* If rounding gave us a negative number,
3141 truncate. */
3142 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003143 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003144 }
3145 intval = PyLong_AsLong(t);
3146 if (intval == -1 && PyErr_Occurred())
3147 return -1;
3148 *sec = intval;
3149 *usec = 0;
3150 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003151}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003153PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003154"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003155utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003156Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003157second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003158
Barry Warsaw53699e91996-12-10 23:23:01 +00003159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003160posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003161{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003162#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 PyObject *arg;
3164 PyUnicodeObject *obwpath;
3165 wchar_t *wpath = NULL;
3166 PyObject *oapath;
3167 char *apath;
3168 HANDLE hFile;
3169 long atimesec, mtimesec, ausec, musec;
3170 FILETIME atime, mtime;
3171 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003172
Victor Stinner8c62be82010-05-06 00:08:46 +00003173 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3174 wpath = PyUnicode_AS_UNICODE(obwpath);
3175 Py_BEGIN_ALLOW_THREADS
3176 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3177 NULL, OPEN_EXISTING,
3178 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3179 Py_END_ALLOW_THREADS
3180 if (hFile == INVALID_HANDLE_VALUE)
3181 return win32_error_unicode("utime", wpath);
3182 } else
3183 /* Drop the argument parsing error as narrow strings
3184 are also valid. */
3185 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003186
Victor Stinner8c62be82010-05-06 00:08:46 +00003187 if (!wpath) {
3188 if (!PyArg_ParseTuple(args, "O&O:utime",
3189 PyUnicode_FSConverter, &oapath, &arg))
3190 return NULL;
3191 apath = PyBytes_AsString(oapath);
3192 Py_BEGIN_ALLOW_THREADS
3193 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3194 NULL, OPEN_EXISTING,
3195 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3196 Py_END_ALLOW_THREADS
3197 if (hFile == INVALID_HANDLE_VALUE) {
3198 win32_error("utime", apath);
3199 Py_DECREF(oapath);
3200 return NULL;
3201 }
3202 Py_DECREF(oapath);
3203 }
3204
3205 if (arg == Py_None) {
3206 SYSTEMTIME now;
3207 GetSystemTime(&now);
3208 if (!SystemTimeToFileTime(&now, &mtime) ||
3209 !SystemTimeToFileTime(&now, &atime)) {
3210 win32_error("utime", NULL);
3211 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003212 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003213 }
3214 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3215 PyErr_SetString(PyExc_TypeError,
3216 "utime() arg 2 must be a tuple (atime, mtime)");
3217 goto done;
3218 }
3219 else {
3220 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3221 &atimesec, &ausec) == -1)
3222 goto done;
3223 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3224 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3225 &mtimesec, &musec) == -1)
3226 goto done;
3227 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3228 }
3229 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3230 /* Avoid putting the file name into the error here,
3231 as that may confuse the user into believing that
3232 something is wrong with the file, when it also
3233 could be the time stamp that gives a problem. */
3234 win32_error("utime", NULL);
3235 }
3236 Py_INCREF(Py_None);
3237 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003238done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003239 CloseHandle(hFile);
3240 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003241#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003242
Victor Stinner8c62be82010-05-06 00:08:46 +00003243 PyObject *opath;
3244 char *path;
3245 long atime, mtime, ausec, musec;
3246 int res;
3247 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003248
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003249#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003250 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003251#define ATIME buf[0].tv_sec
3252#define MTIME buf[1].tv_sec
3253#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003254/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003255 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003256#define ATIME buf.actime
3257#define MTIME buf.modtime
3258#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003259#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003260 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003261#define ATIME buf[0]
3262#define MTIME buf[1]
3263#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003264#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003265
Mark Hammond817c9292003-12-03 01:22:38 +00003266
Victor Stinner8c62be82010-05-06 00:08:46 +00003267 if (!PyArg_ParseTuple(args, "O&O:utime",
3268 PyUnicode_FSConverter, &opath, &arg))
3269 return NULL;
3270 path = PyBytes_AsString(opath);
3271 if (arg == Py_None) {
3272 /* optional time values not given */
3273 Py_BEGIN_ALLOW_THREADS
3274 res = utime(path, NULL);
3275 Py_END_ALLOW_THREADS
3276 }
3277 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3278 PyErr_SetString(PyExc_TypeError,
3279 "utime() arg 2 must be a tuple (atime, mtime)");
3280 Py_DECREF(opath);
3281 return NULL;
3282 }
3283 else {
3284 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3285 &atime, &ausec) == -1) {
3286 Py_DECREF(opath);
3287 return NULL;
3288 }
3289 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3290 &mtime, &musec) == -1) {
3291 Py_DECREF(opath);
3292 return NULL;
3293 }
3294 ATIME = atime;
3295 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003296#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003297 buf[0].tv_usec = ausec;
3298 buf[1].tv_usec = musec;
3299 Py_BEGIN_ALLOW_THREADS
3300 res = utimes(path, buf);
3301 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003302#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 Py_BEGIN_ALLOW_THREADS
3304 res = utime(path, UTIME_ARG);
3305 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003306#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003307 }
3308 if (res < 0) {
3309 return posix_error_with_allocated_filename(opath);
3310 }
3311 Py_DECREF(opath);
3312 Py_INCREF(Py_None);
3313 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003314#undef UTIME_ARG
3315#undef ATIME
3316#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003317#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003318}
3319
Guido van Rossum85e3b011991-06-03 12:42:10 +00003320
Guido van Rossum3b066191991-06-04 19:40:25 +00003321/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003322
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003323PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003324"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003325Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003326
Barry Warsaw53699e91996-12-10 23:23:01 +00003327static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003328posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003329{
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 int sts;
3331 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3332 return NULL;
3333 _exit(sts);
3334 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003335}
3336
Martin v. Löwis114619e2002-10-07 06:44:21 +00003337#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3338static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003339free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003340{
Victor Stinner8c62be82010-05-06 00:08:46 +00003341 Py_ssize_t i;
3342 for (i = 0; i < count; i++)
3343 PyMem_Free(array[i]);
3344 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003345}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003346
Antoine Pitrou69f71142009-05-24 21:25:49 +00003347static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003348int fsconvert_strdup(PyObject *o, char**out)
3349{
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 PyObject *bytes;
3351 Py_ssize_t size;
3352 if (!PyUnicode_FSConverter(o, &bytes))
3353 return 0;
3354 size = PyBytes_GET_SIZE(bytes);
3355 *out = PyMem_Malloc(size+1);
3356 if (!*out)
3357 return 0;
3358 memcpy(*out, PyBytes_AsString(bytes), size+1);
3359 Py_DECREF(bytes);
3360 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003361}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003362#endif
3363
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003364
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003365#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003366PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003367"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003368Execute an executable path with arguments, replacing current process.\n\
3369\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003370 path: path of executable file\n\
3371 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003372
Barry Warsaw53699e91996-12-10 23:23:01 +00003373static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003374posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003375{
Victor Stinner8c62be82010-05-06 00:08:46 +00003376 PyObject *opath;
3377 char *path;
3378 PyObject *argv;
3379 char **argvlist;
3380 Py_ssize_t i, argc;
3381 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003382
Victor Stinner8c62be82010-05-06 00:08:46 +00003383 /* execv has two arguments: (path, argv), where
3384 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003385
Victor Stinner8c62be82010-05-06 00:08:46 +00003386 if (!PyArg_ParseTuple(args, "O&O:execv",
3387 PyUnicode_FSConverter,
3388 &opath, &argv))
3389 return NULL;
3390 path = PyBytes_AsString(opath);
3391 if (PyList_Check(argv)) {
3392 argc = PyList_Size(argv);
3393 getitem = PyList_GetItem;
3394 }
3395 else if (PyTuple_Check(argv)) {
3396 argc = PyTuple_Size(argv);
3397 getitem = PyTuple_GetItem;
3398 }
3399 else {
3400 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3401 Py_DECREF(opath);
3402 return NULL;
3403 }
3404 if (argc < 1) {
3405 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3406 Py_DECREF(opath);
3407 return NULL;
3408 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003409
Victor Stinner8c62be82010-05-06 00:08:46 +00003410 argvlist = PyMem_NEW(char *, argc+1);
3411 if (argvlist == NULL) {
3412 Py_DECREF(opath);
3413 return PyErr_NoMemory();
3414 }
3415 for (i = 0; i < argc; i++) {
3416 if (!fsconvert_strdup((*getitem)(argv, i),
3417 &argvlist[i])) {
3418 free_string_array(argvlist, i);
3419 PyErr_SetString(PyExc_TypeError,
3420 "execv() arg 2 must contain only strings");
3421 Py_DECREF(opath);
3422 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003423
Victor Stinner8c62be82010-05-06 00:08:46 +00003424 }
3425 }
3426 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003427
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003429
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003431
Victor Stinner8c62be82010-05-06 00:08:46 +00003432 free_string_array(argvlist, argc);
3433 Py_DECREF(opath);
3434 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003435}
3436
Victor Stinner13bb71c2010-04-23 21:41:56 +00003437static char**
3438parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3439{
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 char **envlist;
3441 Py_ssize_t i, pos, envc;
3442 PyObject *keys=NULL, *vals=NULL;
3443 PyObject *key, *val, *key2, *val2;
3444 char *p, *k, *v;
3445 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003446
Victor Stinner8c62be82010-05-06 00:08:46 +00003447 i = PyMapping_Size(env);
3448 if (i < 0)
3449 return NULL;
3450 envlist = PyMem_NEW(char *, i + 1);
3451 if (envlist == NULL) {
3452 PyErr_NoMemory();
3453 return NULL;
3454 }
3455 envc = 0;
3456 keys = PyMapping_Keys(env);
3457 vals = PyMapping_Values(env);
3458 if (!keys || !vals)
3459 goto error;
3460 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3461 PyErr_Format(PyExc_TypeError,
3462 "env.keys() or env.values() is not a list");
3463 goto error;
3464 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003465
Victor Stinner8c62be82010-05-06 00:08:46 +00003466 for (pos = 0; pos < i; pos++) {
3467 key = PyList_GetItem(keys, pos);
3468 val = PyList_GetItem(vals, pos);
3469 if (!key || !val)
3470 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003471
Victor Stinner8c62be82010-05-06 00:08:46 +00003472 if (PyUnicode_FSConverter(key, &key2) == 0)
3473 goto error;
3474 if (PyUnicode_FSConverter(val, &val2) == 0) {
3475 Py_DECREF(key2);
3476 goto error;
3477 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003478
3479#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003480 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3481 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003482#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003483 k = PyBytes_AsString(key2);
3484 v = PyBytes_AsString(val2);
3485 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003486
Victor Stinner8c62be82010-05-06 00:08:46 +00003487 p = PyMem_NEW(char, len);
3488 if (p == NULL) {
3489 PyErr_NoMemory();
3490 Py_DECREF(key2);
3491 Py_DECREF(val2);
3492 goto error;
3493 }
3494 PyOS_snprintf(p, len, "%s=%s", k, v);
3495 envlist[envc++] = p;
3496 Py_DECREF(key2);
3497 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003498#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003500#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003501 }
3502 Py_DECREF(vals);
3503 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003504
Victor Stinner8c62be82010-05-06 00:08:46 +00003505 envlist[envc] = 0;
3506 *envc_ptr = envc;
3507 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003508
3509error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003510 Py_XDECREF(keys);
3511 Py_XDECREF(vals);
3512 while (--envc >= 0)
3513 PyMem_DEL(envlist[envc]);
3514 PyMem_DEL(envlist);
3515 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003516}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003518PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003519"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003520Execute a path with arguments and environment, replacing current process.\n\
3521\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003522 path: path of executable file\n\
3523 args: tuple or list of arguments\n\
3524 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003525
Barry Warsaw53699e91996-12-10 23:23:01 +00003526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003527posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003528{
Victor Stinner8c62be82010-05-06 00:08:46 +00003529 PyObject *opath;
3530 char *path;
3531 PyObject *argv, *env;
3532 char **argvlist;
3533 char **envlist;
3534 Py_ssize_t i, argc, envc;
3535 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3536 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003537
Victor Stinner8c62be82010-05-06 00:08:46 +00003538 /* execve has three arguments: (path, argv, env), where
3539 argv is a list or tuple of strings and env is a dictionary
3540 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003541
Victor Stinner8c62be82010-05-06 00:08:46 +00003542 if (!PyArg_ParseTuple(args, "O&OO:execve",
3543 PyUnicode_FSConverter,
3544 &opath, &argv, &env))
3545 return NULL;
3546 path = PyBytes_AsString(opath);
3547 if (PyList_Check(argv)) {
3548 argc = PyList_Size(argv);
3549 getitem = PyList_GetItem;
3550 }
3551 else if (PyTuple_Check(argv)) {
3552 argc = PyTuple_Size(argv);
3553 getitem = PyTuple_GetItem;
3554 }
3555 else {
3556 PyErr_SetString(PyExc_TypeError,
3557 "execve() arg 2 must be a tuple or list");
3558 goto fail_0;
3559 }
3560 if (!PyMapping_Check(env)) {
3561 PyErr_SetString(PyExc_TypeError,
3562 "execve() arg 3 must be a mapping object");
3563 goto fail_0;
3564 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003565
Victor Stinner8c62be82010-05-06 00:08:46 +00003566 argvlist = PyMem_NEW(char *, argc+1);
3567 if (argvlist == NULL) {
3568 PyErr_NoMemory();
3569 goto fail_0;
3570 }
3571 for (i = 0; i < argc; i++) {
3572 if (!fsconvert_strdup((*getitem)(argv, i),
3573 &argvlist[i]))
3574 {
3575 lastarg = i;
3576 goto fail_1;
3577 }
3578 }
3579 lastarg = argc;
3580 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003581
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 envlist = parse_envlist(env, &envc);
3583 if (envlist == NULL)
3584 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003585
Victor Stinner8c62be82010-05-06 00:08:46 +00003586 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003587
Victor Stinner8c62be82010-05-06 00:08:46 +00003588 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003589
Victor Stinner8c62be82010-05-06 00:08:46 +00003590 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003591
Victor Stinner8c62be82010-05-06 00:08:46 +00003592 while (--envc >= 0)
3593 PyMem_DEL(envlist[envc]);
3594 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003595 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003596 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003597 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003598 Py_DECREF(opath);
3599 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003600}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003601#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003602
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003603
Guido van Rossuma1065681999-01-25 23:20:23 +00003604#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003605PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003606"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003607Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003608\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003609 mode: mode of process creation\n\
3610 path: path of executable file\n\
3611 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003612
3613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003614posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003615{
Victor Stinner8c62be82010-05-06 00:08:46 +00003616 PyObject *opath;
3617 char *path;
3618 PyObject *argv;
3619 char **argvlist;
3620 int mode, i;
3621 Py_ssize_t argc;
3622 Py_intptr_t spawnval;
3623 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003624
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 /* spawnv has three arguments: (mode, path, argv), where
3626 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003627
Victor Stinner8c62be82010-05-06 00:08:46 +00003628 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3629 PyUnicode_FSConverter,
3630 &opath, &argv))
3631 return NULL;
3632 path = PyBytes_AsString(opath);
3633 if (PyList_Check(argv)) {
3634 argc = PyList_Size(argv);
3635 getitem = PyList_GetItem;
3636 }
3637 else if (PyTuple_Check(argv)) {
3638 argc = PyTuple_Size(argv);
3639 getitem = PyTuple_GetItem;
3640 }
3641 else {
3642 PyErr_SetString(PyExc_TypeError,
3643 "spawnv() arg 2 must be a tuple or list");
3644 Py_DECREF(opath);
3645 return NULL;
3646 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003647
Victor Stinner8c62be82010-05-06 00:08:46 +00003648 argvlist = PyMem_NEW(char *, argc+1);
3649 if (argvlist == NULL) {
3650 Py_DECREF(opath);
3651 return PyErr_NoMemory();
3652 }
3653 for (i = 0; i < argc; i++) {
3654 if (!fsconvert_strdup((*getitem)(argv, i),
3655 &argvlist[i])) {
3656 free_string_array(argvlist, i);
3657 PyErr_SetString(
3658 PyExc_TypeError,
3659 "spawnv() arg 2 must contain only strings");
3660 Py_DECREF(opath);
3661 return NULL;
3662 }
3663 }
3664 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003665
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003666#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003667 Py_BEGIN_ALLOW_THREADS
3668 spawnval = spawnv(mode, path, argvlist);
3669 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003670#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003671 if (mode == _OLD_P_OVERLAY)
3672 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003673
Victor Stinner8c62be82010-05-06 00:08:46 +00003674 Py_BEGIN_ALLOW_THREADS
3675 spawnval = _spawnv(mode, path, argvlist);
3676 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003677#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003678
Victor Stinner8c62be82010-05-06 00:08:46 +00003679 free_string_array(argvlist, argc);
3680 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003681
Victor Stinner8c62be82010-05-06 00:08:46 +00003682 if (spawnval == -1)
3683 return posix_error();
3684 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003685#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003686 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003687#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003688 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003689#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003690}
3691
3692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003693PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003694"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003695Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003696\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003697 mode: mode of process creation\n\
3698 path: path of executable file\n\
3699 args: tuple or list of arguments\n\
3700 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003701
3702static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003703posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003704{
Victor Stinner8c62be82010-05-06 00:08:46 +00003705 PyObject *opath;
3706 char *path;
3707 PyObject *argv, *env;
3708 char **argvlist;
3709 char **envlist;
3710 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003711 int mode;
3712 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003713 Py_intptr_t spawnval;
3714 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3715 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003716
Victor Stinner8c62be82010-05-06 00:08:46 +00003717 /* spawnve has four arguments: (mode, path, argv, env), where
3718 argv is a list or tuple of strings and env is a dictionary
3719 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003720
Victor Stinner8c62be82010-05-06 00:08:46 +00003721 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3722 PyUnicode_FSConverter,
3723 &opath, &argv, &env))
3724 return NULL;
3725 path = PyBytes_AsString(opath);
3726 if (PyList_Check(argv)) {
3727 argc = PyList_Size(argv);
3728 getitem = PyList_GetItem;
3729 }
3730 else if (PyTuple_Check(argv)) {
3731 argc = PyTuple_Size(argv);
3732 getitem = PyTuple_GetItem;
3733 }
3734 else {
3735 PyErr_SetString(PyExc_TypeError,
3736 "spawnve() arg 2 must be a tuple or list");
3737 goto fail_0;
3738 }
3739 if (!PyMapping_Check(env)) {
3740 PyErr_SetString(PyExc_TypeError,
3741 "spawnve() arg 3 must be a mapping object");
3742 goto fail_0;
3743 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003744
Victor Stinner8c62be82010-05-06 00:08:46 +00003745 argvlist = PyMem_NEW(char *, argc+1);
3746 if (argvlist == NULL) {
3747 PyErr_NoMemory();
3748 goto fail_0;
3749 }
3750 for (i = 0; i < argc; i++) {
3751 if (!fsconvert_strdup((*getitem)(argv, i),
3752 &argvlist[i]))
3753 {
3754 lastarg = i;
3755 goto fail_1;
3756 }
3757 }
3758 lastarg = argc;
3759 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003760
Victor Stinner8c62be82010-05-06 00:08:46 +00003761 envlist = parse_envlist(env, &envc);
3762 if (envlist == NULL)
3763 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003764
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003765#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003766 Py_BEGIN_ALLOW_THREADS
3767 spawnval = spawnve(mode, path, argvlist, envlist);
3768 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003769#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003770 if (mode == _OLD_P_OVERLAY)
3771 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003772
Victor Stinner8c62be82010-05-06 00:08:46 +00003773 Py_BEGIN_ALLOW_THREADS
3774 spawnval = _spawnve(mode, path, argvlist, envlist);
3775 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003776#endif
Tim Peters25059d32001-12-07 20:35:43 +00003777
Victor Stinner8c62be82010-05-06 00:08:46 +00003778 if (spawnval == -1)
3779 (void) posix_error();
3780 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003781#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003782 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003783#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003784 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003785#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003786
Victor Stinner8c62be82010-05-06 00:08:46 +00003787 while (--envc >= 0)
3788 PyMem_DEL(envlist[envc]);
3789 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003790 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003791 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003792 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003793 Py_DECREF(opath);
3794 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003795}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003796
3797/* OS/2 supports spawnvp & spawnvpe natively */
3798#if defined(PYOS_OS2)
3799PyDoc_STRVAR(posix_spawnvp__doc__,
3800"spawnvp(mode, file, args)\n\n\
3801Execute the program 'file' in a new process, using the environment\n\
3802search path to find the file.\n\
3803\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003804 mode: mode of process creation\n\
3805 file: executable file name\n\
3806 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003807
3808static PyObject *
3809posix_spawnvp(PyObject *self, PyObject *args)
3810{
Victor Stinner8c62be82010-05-06 00:08:46 +00003811 PyObject *opath;
3812 char *path;
3813 PyObject *argv;
3814 char **argvlist;
3815 int mode, i, argc;
3816 Py_intptr_t spawnval;
3817 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003818
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 /* spawnvp has three arguments: (mode, path, argv), where
3820 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003821
Victor Stinner8c62be82010-05-06 00:08:46 +00003822 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3823 PyUnicode_FSConverter,
3824 &opath, &argv))
3825 return NULL;
3826 path = PyBytes_AsString(opath);
3827 if (PyList_Check(argv)) {
3828 argc = PyList_Size(argv);
3829 getitem = PyList_GetItem;
3830 }
3831 else if (PyTuple_Check(argv)) {
3832 argc = PyTuple_Size(argv);
3833 getitem = PyTuple_GetItem;
3834 }
3835 else {
3836 PyErr_SetString(PyExc_TypeError,
3837 "spawnvp() arg 2 must be a tuple or list");
3838 Py_DECREF(opath);
3839 return NULL;
3840 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003841
Victor Stinner8c62be82010-05-06 00:08:46 +00003842 argvlist = PyMem_NEW(char *, argc+1);
3843 if (argvlist == NULL) {
3844 Py_DECREF(opath);
3845 return PyErr_NoMemory();
3846 }
3847 for (i = 0; i < argc; i++) {
3848 if (!fsconvert_strdup((*getitem)(argv, i),
3849 &argvlist[i])) {
3850 free_string_array(argvlist, i);
3851 PyErr_SetString(
3852 PyExc_TypeError,
3853 "spawnvp() arg 2 must contain only strings");
3854 Py_DECREF(opath);
3855 return NULL;
3856 }
3857 }
3858 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003859
Victor Stinner8c62be82010-05-06 00:08:46 +00003860 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003861#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003862 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003863#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003864 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003865#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003866 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003867
Victor Stinner8c62be82010-05-06 00:08:46 +00003868 free_string_array(argvlist, argc);
3869 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003870
Victor Stinner8c62be82010-05-06 00:08:46 +00003871 if (spawnval == -1)
3872 return posix_error();
3873 else
3874 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003875}
3876
3877
3878PyDoc_STRVAR(posix_spawnvpe__doc__,
3879"spawnvpe(mode, file, args, env)\n\n\
3880Execute the program 'file' in a new process, using the environment\n\
3881search path to find the file.\n\
3882\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003883 mode: mode of process creation\n\
3884 file: executable file name\n\
3885 args: tuple or list of arguments\n\
3886 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003887
3888static PyObject *
3889posix_spawnvpe(PyObject *self, PyObject *args)
3890{
Victor Stinner8c62be82010-05-06 00:08:46 +00003891 PyObject *opath
3892 char *path;
3893 PyObject *argv, *env;
3894 char **argvlist;
3895 char **envlist;
3896 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003897 int mode;
3898 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003899 Py_intptr_t spawnval;
3900 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3901 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003902
Victor Stinner8c62be82010-05-06 00:08:46 +00003903 /* spawnvpe has four arguments: (mode, path, argv, env), where
3904 argv is a list or tuple of strings and env is a dictionary
3905 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003906
Victor Stinner8c62be82010-05-06 00:08:46 +00003907 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3908 PyUnicode_FSConverter,
3909 &opath, &argv, &env))
3910 return NULL;
3911 path = PyBytes_AsString(opath);
3912 if (PyList_Check(argv)) {
3913 argc = PyList_Size(argv);
3914 getitem = PyList_GetItem;
3915 }
3916 else if (PyTuple_Check(argv)) {
3917 argc = PyTuple_Size(argv);
3918 getitem = PyTuple_GetItem;
3919 }
3920 else {
3921 PyErr_SetString(PyExc_TypeError,
3922 "spawnvpe() arg 2 must be a tuple or list");
3923 goto fail_0;
3924 }
3925 if (!PyMapping_Check(env)) {
3926 PyErr_SetString(PyExc_TypeError,
3927 "spawnvpe() arg 3 must be a mapping object");
3928 goto fail_0;
3929 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003930
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 argvlist = PyMem_NEW(char *, argc+1);
3932 if (argvlist == NULL) {
3933 PyErr_NoMemory();
3934 goto fail_0;
3935 }
3936 for (i = 0; i < argc; i++) {
3937 if (!fsconvert_strdup((*getitem)(argv, i),
3938 &argvlist[i]))
3939 {
3940 lastarg = i;
3941 goto fail_1;
3942 }
3943 }
3944 lastarg = argc;
3945 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003946
Victor Stinner8c62be82010-05-06 00:08:46 +00003947 envlist = parse_envlist(env, &envc);
3948 if (envlist == NULL)
3949 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003950
Victor Stinner8c62be82010-05-06 00:08:46 +00003951 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003952#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003953 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003954#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003955 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003956#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003957 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003958
Victor Stinner8c62be82010-05-06 00:08:46 +00003959 if (spawnval == -1)
3960 (void) posix_error();
3961 else
3962 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003963
Victor Stinner8c62be82010-05-06 00:08:46 +00003964 while (--envc >= 0)
3965 PyMem_DEL(envlist[envc]);
3966 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003967 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003968 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003969 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003970 Py_DECREF(opath);
3971 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003972}
3973#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003974#endif /* HAVE_SPAWNV */
3975
3976
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003977#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003978PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003979"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003980Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3981\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003982Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003983
3984static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003985posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003986{
Victor Stinner8c62be82010-05-06 00:08:46 +00003987 pid_t pid;
3988 int result = 0;
3989 _PyImport_AcquireLock();
3990 pid = fork1();
3991 if (pid == 0) {
3992 /* child: this clobbers and resets the import lock. */
3993 PyOS_AfterFork();
3994 } else {
3995 /* parent: release the import lock. */
3996 result = _PyImport_ReleaseLock();
3997 }
3998 if (pid == -1)
3999 return posix_error();
4000 if (result < 0) {
4001 /* Don't clobber the OSError if the fork failed. */
4002 PyErr_SetString(PyExc_RuntimeError,
4003 "not holding the import lock");
4004 return NULL;
4005 }
4006 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004007}
4008#endif
4009
4010
Guido van Rossumad0ee831995-03-01 10:34:45 +00004011#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004012PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004013"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004014Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004016
Barry Warsaw53699e91996-12-10 23:23:01 +00004017static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004018posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004019{
Victor Stinner8c62be82010-05-06 00:08:46 +00004020 pid_t pid;
4021 int result = 0;
4022 _PyImport_AcquireLock();
4023 pid = fork();
4024 if (pid == 0) {
4025 /* child: this clobbers and resets the import lock. */
4026 PyOS_AfterFork();
4027 } else {
4028 /* parent: release the import lock. */
4029 result = _PyImport_ReleaseLock();
4030 }
4031 if (pid == -1)
4032 return posix_error();
4033 if (result < 0) {
4034 /* Don't clobber the OSError if the fork failed. */
4035 PyErr_SetString(PyExc_RuntimeError,
4036 "not holding the import lock");
4037 return NULL;
4038 }
4039 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004040}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004041#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004042
Neal Norwitzb59798b2003-03-21 01:43:31 +00004043/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004044/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4045#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004046#define DEV_PTY_FILE "/dev/ptc"
4047#define HAVE_DEV_PTMX
4048#else
4049#define DEV_PTY_FILE "/dev/ptmx"
4050#endif
4051
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004052#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004053#ifdef HAVE_PTY_H
4054#include <pty.h>
4055#else
4056#ifdef HAVE_LIBUTIL_H
4057#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004058#else
4059#ifdef HAVE_UTIL_H
4060#include <util.h>
4061#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004062#endif /* HAVE_LIBUTIL_H */
4063#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004064#ifdef HAVE_STROPTS_H
4065#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004066#endif
4067#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004068
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004069#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004070PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004071"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004072Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004073
4074static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004075posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004076{
Victor Stinner8c62be82010-05-06 00:08:46 +00004077 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004078#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004079 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004080#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004081#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004082 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004083#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004084 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004085#endif
4086#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004087
Thomas Wouters70c21a12000-07-14 14:28:33 +00004088#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004089 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4090 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004091#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004092 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4093 if (slave_name == NULL)
4094 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004095
Victor Stinner8c62be82010-05-06 00:08:46 +00004096 slave_fd = open(slave_name, O_RDWR);
4097 if (slave_fd < 0)
4098 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004099#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004100 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4101 if (master_fd < 0)
4102 return posix_error();
4103 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4104 /* change permission of slave */
4105 if (grantpt(master_fd) < 0) {
4106 PyOS_setsig(SIGCHLD, sig_saved);
4107 return posix_error();
4108 }
4109 /* unlock slave */
4110 if (unlockpt(master_fd) < 0) {
4111 PyOS_setsig(SIGCHLD, sig_saved);
4112 return posix_error();
4113 }
4114 PyOS_setsig(SIGCHLD, sig_saved);
4115 slave_name = ptsname(master_fd); /* get name of slave */
4116 if (slave_name == NULL)
4117 return posix_error();
4118 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4119 if (slave_fd < 0)
4120 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004121#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004122 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4123 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004124#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004125 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004126#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004127#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004128#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004129
Victor Stinner8c62be82010-05-06 00:08:46 +00004130 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004131
Fred Drake8cef4cf2000-06-28 16:40:38 +00004132}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004133#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004134
4135#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004136PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004137"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004138Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4139Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004140To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004141
4142static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004143posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004144{
Victor Stinner8c62be82010-05-06 00:08:46 +00004145 int master_fd = -1, result = 0;
4146 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004147
Victor Stinner8c62be82010-05-06 00:08:46 +00004148 _PyImport_AcquireLock();
4149 pid = forkpty(&master_fd, NULL, NULL, NULL);
4150 if (pid == 0) {
4151 /* child: this clobbers and resets the import lock. */
4152 PyOS_AfterFork();
4153 } else {
4154 /* parent: release the import lock. */
4155 result = _PyImport_ReleaseLock();
4156 }
4157 if (pid == -1)
4158 return posix_error();
4159 if (result < 0) {
4160 /* Don't clobber the OSError if the fork failed. */
4161 PyErr_SetString(PyExc_RuntimeError,
4162 "not holding the import lock");
4163 return NULL;
4164 }
4165 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004166}
4167#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004168
Guido van Rossumad0ee831995-03-01 10:34:45 +00004169#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004170PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004171"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004172Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004173
Barry Warsaw53699e91996-12-10 23:23:01 +00004174static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004175posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004176{
Victor Stinner8c62be82010-05-06 00:08:46 +00004177 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004178}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004179#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004180
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004181
Guido van Rossumad0ee831995-03-01 10:34:45 +00004182#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004183PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004184"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004185Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004186
Barry Warsaw53699e91996-12-10 23:23:01 +00004187static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004188posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004189{
Victor Stinner8c62be82010-05-06 00:08:46 +00004190 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004191}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004192#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004193
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004194
Guido van Rossumad0ee831995-03-01 10:34:45 +00004195#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004196PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004197"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004198Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004199
Barry Warsaw53699e91996-12-10 23:23:01 +00004200static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004201posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004202{
Victor Stinner8c62be82010-05-06 00:08:46 +00004203 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004204}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004205#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004206
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004208PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004209"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004210Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004211
Barry Warsaw53699e91996-12-10 23:23:01 +00004212static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004213posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004214{
Victor Stinner8c62be82010-05-06 00:08:46 +00004215 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004216}
4217
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004218
Fred Drakec9680921999-12-13 16:37:25 +00004219#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004220PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004221"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004222Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004223
4224static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004225posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004226{
4227 PyObject *result = NULL;
4228
Fred Drakec9680921999-12-13 16:37:25 +00004229#ifdef NGROUPS_MAX
4230#define MAX_GROUPS NGROUPS_MAX
4231#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004232 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004233#define MAX_GROUPS 64
4234#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004235 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004236
4237 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4238 * This is a helper variable to store the intermediate result when
4239 * that happens.
4240 *
4241 * To keep the code readable the OSX behaviour is unconditional,
4242 * according to the POSIX spec this should be safe on all unix-y
4243 * systems.
4244 */
4245 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004246 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004247
Victor Stinner8c62be82010-05-06 00:08:46 +00004248 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004249 if (n < 0) {
4250 if (errno == EINVAL) {
4251 n = getgroups(0, NULL);
4252 if (n == -1) {
4253 return posix_error();
4254 }
4255 if (n == 0) {
4256 /* Avoid malloc(0) */
4257 alt_grouplist = grouplist;
4258 } else {
4259 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4260 if (alt_grouplist == NULL) {
4261 errno = EINVAL;
4262 return posix_error();
4263 }
4264 n = getgroups(n, alt_grouplist);
4265 if (n == -1) {
4266 PyMem_Free(alt_grouplist);
4267 return posix_error();
4268 }
4269 }
4270 } else {
4271 return posix_error();
4272 }
4273 }
4274 result = PyList_New(n);
4275 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004276 int i;
4277 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004278 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004279 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004280 Py_DECREF(result);
4281 result = NULL;
4282 break;
Fred Drakec9680921999-12-13 16:37:25 +00004283 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004284 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004285 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004286 }
4287
4288 if (alt_grouplist != grouplist) {
4289 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004290 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004291
Fred Drakec9680921999-12-13 16:37:25 +00004292 return result;
4293}
4294#endif
4295
Antoine Pitroub7572f02009-12-02 20:46:48 +00004296#ifdef HAVE_INITGROUPS
4297PyDoc_STRVAR(posix_initgroups__doc__,
4298"initgroups(username, gid) -> None\n\n\
4299Call the system initgroups() to initialize the group access list with all of\n\
4300the groups of which the specified username is a member, plus the specified\n\
4301group id.");
4302
4303static PyObject *
4304posix_initgroups(PyObject *self, PyObject *args)
4305{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004306 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004307 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004308 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004309 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004310
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004311 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4312 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004314 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004315
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004316 res = initgroups(username, (gid_t) gid);
4317 Py_DECREF(oname);
4318 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004319 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004320
Victor Stinner8c62be82010-05-06 00:08:46 +00004321 Py_INCREF(Py_None);
4322 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004323}
4324#endif
4325
Martin v. Löwis606edc12002-06-13 21:09:11 +00004326#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004327PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004328"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004329Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004330
4331static PyObject *
4332posix_getpgid(PyObject *self, PyObject *args)
4333{
Victor Stinner8c62be82010-05-06 00:08:46 +00004334 pid_t pid, pgid;
4335 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4336 return NULL;
4337 pgid = getpgid(pid);
4338 if (pgid < 0)
4339 return posix_error();
4340 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004341}
4342#endif /* HAVE_GETPGID */
4343
4344
Guido van Rossumb6775db1994-08-01 11:34:53 +00004345#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004346PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004347"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004348Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004349
Barry Warsaw53699e91996-12-10 23:23:01 +00004350static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004351posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004352{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004353#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004354 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004355#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004356 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004357#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004358}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004359#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004360
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004361
Guido van Rossumb6775db1994-08-01 11:34:53 +00004362#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004363PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004364"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004365Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004366
Barry Warsaw53699e91996-12-10 23:23:01 +00004367static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004368posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004369{
Guido van Rossum64933891994-10-20 21:56:42 +00004370#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004372#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004374#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 return posix_error();
4376 Py_INCREF(Py_None);
4377 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004378}
4379
Guido van Rossumb6775db1994-08-01 11:34:53 +00004380#endif /* HAVE_SETPGRP */
4381
Guido van Rossumad0ee831995-03-01 10:34:45 +00004382#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004383
4384#ifdef MS_WINDOWS
4385#include <tlhelp32.h>
4386
4387static PyObject*
4388win32_getppid()
4389{
4390 HANDLE snapshot;
4391 pid_t mypid;
4392 PyObject* result = NULL;
4393 BOOL have_record;
4394 PROCESSENTRY32 pe;
4395
4396 mypid = getpid(); /* This function never fails */
4397
4398 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4399 if (snapshot == INVALID_HANDLE_VALUE)
4400 return PyErr_SetFromWindowsErr(GetLastError());
4401
4402 pe.dwSize = sizeof(pe);
4403 have_record = Process32First(snapshot, &pe);
4404 while (have_record) {
4405 if (mypid == (pid_t)pe.th32ProcessID) {
4406 /* We could cache the ulong value in a static variable. */
4407 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4408 break;
4409 }
4410
4411 have_record = Process32Next(snapshot, &pe);
4412 }
4413
4414 /* If our loop exits and our pid was not found (result will be NULL)
4415 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4416 * error anyway, so let's raise it. */
4417 if (!result)
4418 result = PyErr_SetFromWindowsErr(GetLastError());
4419
4420 CloseHandle(snapshot);
4421
4422 return result;
4423}
4424#endif /*MS_WINDOWS*/
4425
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004426PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004427"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004428Return the parent's process id. If the parent process has already exited,\n\
4429Windows machines will still return its id; others systems will return the id\n\
4430of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004431
Barry Warsaw53699e91996-12-10 23:23:01 +00004432static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004433posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004434{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004435#ifdef MS_WINDOWS
4436 return win32_getppid();
4437#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004438 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004439#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004440}
4441#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004442
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004443
Fred Drake12c6e2d1999-12-14 21:25:03 +00004444#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004445PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004446"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004447Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004448
4449static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004450posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004451{
Victor Stinner8c62be82010-05-06 00:08:46 +00004452 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004453#ifdef MS_WINDOWS
4454 wchar_t user_name[UNLEN + 1];
4455 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4456
4457 if (GetUserNameW(user_name, &num_chars)) {
4458 /* num_chars is the number of unicode chars plus null terminator */
4459 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4460 }
4461 else
4462 result = PyErr_SetFromWindowsErr(GetLastError());
4463#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 char *name;
4465 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004466
Victor Stinner8c62be82010-05-06 00:08:46 +00004467 errno = 0;
4468 name = getlogin();
4469 if (name == NULL) {
4470 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004471 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004472 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004473 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004474 }
4475 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004476 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004477 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004478#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004479 return result;
4480}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004481#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004482
Guido van Rossumad0ee831995-03-01 10:34:45 +00004483#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004484PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004485"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004486Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004487
Barry Warsaw53699e91996-12-10 23:23:01 +00004488static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004489posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004490{
Victor Stinner8c62be82010-05-06 00:08:46 +00004491 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004492}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004493#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004494
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004495
Guido van Rossumad0ee831995-03-01 10:34:45 +00004496#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004497PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004498"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004499Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004500
Barry Warsaw53699e91996-12-10 23:23:01 +00004501static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004502posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004503{
Victor Stinner8c62be82010-05-06 00:08:46 +00004504 pid_t pid;
4505 int sig;
4506 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4507 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004508#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004509 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4510 APIRET rc;
4511 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004512 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004513
4514 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4515 APIRET rc;
4516 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004517 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004518
4519 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004520 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004521#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004522 if (kill(pid, sig) == -1)
4523 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004524#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004525 Py_INCREF(Py_None);
4526 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004527}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004528#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004529
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004530#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004531PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004532"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004533Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004534
4535static PyObject *
4536posix_killpg(PyObject *self, PyObject *args)
4537{
Victor Stinner8c62be82010-05-06 00:08:46 +00004538 int sig;
4539 pid_t pgid;
4540 /* XXX some man pages make the `pgid` parameter an int, others
4541 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4542 take the same type. Moreover, pid_t is always at least as wide as
4543 int (else compilation of this module fails), which is safe. */
4544 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4545 return NULL;
4546 if (killpg(pgid, sig) == -1)
4547 return posix_error();
4548 Py_INCREF(Py_None);
4549 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004550}
4551#endif
4552
Brian Curtineb24d742010-04-12 17:16:38 +00004553#ifdef MS_WINDOWS
4554PyDoc_STRVAR(win32_kill__doc__,
4555"kill(pid, sig)\n\n\
4556Kill a process with a signal.");
4557
4558static PyObject *
4559win32_kill(PyObject *self, PyObject *args)
4560{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004561 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004562 DWORD pid, sig, err;
4563 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004564
Victor Stinner8c62be82010-05-06 00:08:46 +00004565 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4566 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004567
Victor Stinner8c62be82010-05-06 00:08:46 +00004568 /* Console processes which share a common console can be sent CTRL+C or
4569 CTRL+BREAK events, provided they handle said events. */
4570 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4571 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4572 err = GetLastError();
4573 PyErr_SetFromWindowsErr(err);
4574 }
4575 else
4576 Py_RETURN_NONE;
4577 }
Brian Curtineb24d742010-04-12 17:16:38 +00004578
Victor Stinner8c62be82010-05-06 00:08:46 +00004579 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4580 attempt to open and terminate the process. */
4581 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4582 if (handle == NULL) {
4583 err = GetLastError();
4584 return PyErr_SetFromWindowsErr(err);
4585 }
Brian Curtineb24d742010-04-12 17:16:38 +00004586
Victor Stinner8c62be82010-05-06 00:08:46 +00004587 if (TerminateProcess(handle, sig) == 0) {
4588 err = GetLastError();
4589 result = PyErr_SetFromWindowsErr(err);
4590 } else {
4591 Py_INCREF(Py_None);
4592 result = Py_None;
4593 }
Brian Curtineb24d742010-04-12 17:16:38 +00004594
Victor Stinner8c62be82010-05-06 00:08:46 +00004595 CloseHandle(handle);
4596 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004597}
4598#endif /* MS_WINDOWS */
4599
Guido van Rossumc0125471996-06-28 18:55:32 +00004600#ifdef HAVE_PLOCK
4601
4602#ifdef HAVE_SYS_LOCK_H
4603#include <sys/lock.h>
4604#endif
4605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004606PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004607"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004608Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004609
Barry Warsaw53699e91996-12-10 23:23:01 +00004610static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004611posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004612{
Victor Stinner8c62be82010-05-06 00:08:46 +00004613 int op;
4614 if (!PyArg_ParseTuple(args, "i:plock", &op))
4615 return NULL;
4616 if (plock(op) == -1)
4617 return posix_error();
4618 Py_INCREF(Py_None);
4619 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004620}
4621#endif
4622
Guido van Rossumb6775db1994-08-01 11:34:53 +00004623#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004624PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004625"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004626Set the current process's user id.");
4627
Barry Warsaw53699e91996-12-10 23:23:01 +00004628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004629posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004630{
Victor Stinner8c62be82010-05-06 00:08:46 +00004631 long uid_arg;
4632 uid_t uid;
4633 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4634 return NULL;
4635 uid = uid_arg;
4636 if (uid != uid_arg) {
4637 PyErr_SetString(PyExc_OverflowError, "user id too big");
4638 return NULL;
4639 }
4640 if (setuid(uid) < 0)
4641 return posix_error();
4642 Py_INCREF(Py_None);
4643 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004644}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004645#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004646
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004647
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004648#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004649PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004650"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004651Set the current process's effective user id.");
4652
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004653static PyObject *
4654posix_seteuid (PyObject *self, PyObject *args)
4655{
Victor Stinner8c62be82010-05-06 00:08:46 +00004656 long euid_arg;
4657 uid_t euid;
4658 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4659 return NULL;
4660 euid = euid_arg;
4661 if (euid != euid_arg) {
4662 PyErr_SetString(PyExc_OverflowError, "user id too big");
4663 return NULL;
4664 }
4665 if (seteuid(euid) < 0) {
4666 return posix_error();
4667 } else {
4668 Py_INCREF(Py_None);
4669 return Py_None;
4670 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004671}
4672#endif /* HAVE_SETEUID */
4673
4674#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004675PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004676"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004677Set the current process's effective group id.");
4678
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004679static PyObject *
4680posix_setegid (PyObject *self, PyObject *args)
4681{
Victor Stinner8c62be82010-05-06 00:08:46 +00004682 long egid_arg;
4683 gid_t egid;
4684 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4685 return NULL;
4686 egid = egid_arg;
4687 if (egid != egid_arg) {
4688 PyErr_SetString(PyExc_OverflowError, "group id too big");
4689 return NULL;
4690 }
4691 if (setegid(egid) < 0) {
4692 return posix_error();
4693 } else {
4694 Py_INCREF(Py_None);
4695 return Py_None;
4696 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004697}
4698#endif /* HAVE_SETEGID */
4699
4700#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004701PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004702"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004703Set the current process's real and effective user ids.");
4704
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004705static PyObject *
4706posix_setreuid (PyObject *self, PyObject *args)
4707{
Victor Stinner8c62be82010-05-06 00:08:46 +00004708 long ruid_arg, euid_arg;
4709 uid_t ruid, euid;
4710 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4711 return NULL;
4712 if (ruid_arg == -1)
4713 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4714 else
4715 ruid = ruid_arg; /* otherwise, assign from our long */
4716 if (euid_arg == -1)
4717 euid = (uid_t)-1;
4718 else
4719 euid = euid_arg;
4720 if ((euid_arg != -1 && euid != euid_arg) ||
4721 (ruid_arg != -1 && ruid != ruid_arg)) {
4722 PyErr_SetString(PyExc_OverflowError, "user id too big");
4723 return NULL;
4724 }
4725 if (setreuid(ruid, euid) < 0) {
4726 return posix_error();
4727 } else {
4728 Py_INCREF(Py_None);
4729 return Py_None;
4730 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004731}
4732#endif /* HAVE_SETREUID */
4733
4734#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004735PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004736"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004737Set the current process's real and effective group ids.");
4738
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004739static PyObject *
4740posix_setregid (PyObject *self, PyObject *args)
4741{
Victor Stinner8c62be82010-05-06 00:08:46 +00004742 long rgid_arg, egid_arg;
4743 gid_t rgid, egid;
4744 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4745 return NULL;
4746 if (rgid_arg == -1)
4747 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4748 else
4749 rgid = rgid_arg; /* otherwise, assign from our long */
4750 if (egid_arg == -1)
4751 egid = (gid_t)-1;
4752 else
4753 egid = egid_arg;
4754 if ((egid_arg != -1 && egid != egid_arg) ||
4755 (rgid_arg != -1 && rgid != rgid_arg)) {
4756 PyErr_SetString(PyExc_OverflowError, "group id too big");
4757 return NULL;
4758 }
4759 if (setregid(rgid, egid) < 0) {
4760 return posix_error();
4761 } else {
4762 Py_INCREF(Py_None);
4763 return Py_None;
4764 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004765}
4766#endif /* HAVE_SETREGID */
4767
Guido van Rossumb6775db1994-08-01 11:34:53 +00004768#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004769PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004770"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004771Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004772
Barry Warsaw53699e91996-12-10 23:23:01 +00004773static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004774posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004775{
Victor Stinner8c62be82010-05-06 00:08:46 +00004776 long gid_arg;
4777 gid_t gid;
4778 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4779 return NULL;
4780 gid = gid_arg;
4781 if (gid != gid_arg) {
4782 PyErr_SetString(PyExc_OverflowError, "group id too big");
4783 return NULL;
4784 }
4785 if (setgid(gid) < 0)
4786 return posix_error();
4787 Py_INCREF(Py_None);
4788 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004789}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004790#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004791
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004792#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004793PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004794"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004795Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004796
4797static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004798posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004799{
Victor Stinner8c62be82010-05-06 00:08:46 +00004800 int i, len;
4801 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004802
Victor Stinner8c62be82010-05-06 00:08:46 +00004803 if (!PySequence_Check(groups)) {
4804 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4805 return NULL;
4806 }
4807 len = PySequence_Size(groups);
4808 if (len > MAX_GROUPS) {
4809 PyErr_SetString(PyExc_ValueError, "too many groups");
4810 return NULL;
4811 }
4812 for(i = 0; i < len; i++) {
4813 PyObject *elem;
4814 elem = PySequence_GetItem(groups, i);
4815 if (!elem)
4816 return NULL;
4817 if (!PyLong_Check(elem)) {
4818 PyErr_SetString(PyExc_TypeError,
4819 "groups must be integers");
4820 Py_DECREF(elem);
4821 return NULL;
4822 } else {
4823 unsigned long x = PyLong_AsUnsignedLong(elem);
4824 if (PyErr_Occurred()) {
4825 PyErr_SetString(PyExc_TypeError,
4826 "group id too big");
4827 Py_DECREF(elem);
4828 return NULL;
4829 }
4830 grouplist[i] = x;
4831 /* read back the value to see if it fitted in gid_t */
4832 if (grouplist[i] != x) {
4833 PyErr_SetString(PyExc_TypeError,
4834 "group id too big");
4835 Py_DECREF(elem);
4836 return NULL;
4837 }
4838 }
4839 Py_DECREF(elem);
4840 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004841
Victor Stinner8c62be82010-05-06 00:08:46 +00004842 if (setgroups(len, grouplist) < 0)
4843 return posix_error();
4844 Py_INCREF(Py_None);
4845 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004846}
4847#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004848
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004849#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4850static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004851wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004852{
Victor Stinner8c62be82010-05-06 00:08:46 +00004853 PyObject *result;
4854 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004855
Victor Stinner8c62be82010-05-06 00:08:46 +00004856 if (pid == -1)
4857 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004858
Victor Stinner8c62be82010-05-06 00:08:46 +00004859 if (struct_rusage == NULL) {
4860 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4861 if (m == NULL)
4862 return NULL;
4863 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4864 Py_DECREF(m);
4865 if (struct_rusage == NULL)
4866 return NULL;
4867 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004868
Victor Stinner8c62be82010-05-06 00:08:46 +00004869 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4870 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4871 if (!result)
4872 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004873
4874#ifndef doubletime
4875#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4876#endif
4877
Victor Stinner8c62be82010-05-06 00:08:46 +00004878 PyStructSequence_SET_ITEM(result, 0,
4879 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4880 PyStructSequence_SET_ITEM(result, 1,
4881 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004882#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004883 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4884 SET_INT(result, 2, ru->ru_maxrss);
4885 SET_INT(result, 3, ru->ru_ixrss);
4886 SET_INT(result, 4, ru->ru_idrss);
4887 SET_INT(result, 5, ru->ru_isrss);
4888 SET_INT(result, 6, ru->ru_minflt);
4889 SET_INT(result, 7, ru->ru_majflt);
4890 SET_INT(result, 8, ru->ru_nswap);
4891 SET_INT(result, 9, ru->ru_inblock);
4892 SET_INT(result, 10, ru->ru_oublock);
4893 SET_INT(result, 11, ru->ru_msgsnd);
4894 SET_INT(result, 12, ru->ru_msgrcv);
4895 SET_INT(result, 13, ru->ru_nsignals);
4896 SET_INT(result, 14, ru->ru_nvcsw);
4897 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004898#undef SET_INT
4899
Victor Stinner8c62be82010-05-06 00:08:46 +00004900 if (PyErr_Occurred()) {
4901 Py_DECREF(result);
4902 return NULL;
4903 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004904
Victor Stinner8c62be82010-05-06 00:08:46 +00004905 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004906}
4907#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4908
4909#ifdef HAVE_WAIT3
4910PyDoc_STRVAR(posix_wait3__doc__,
4911"wait3(options) -> (pid, status, rusage)\n\n\
4912Wait for completion of a child process.");
4913
4914static PyObject *
4915posix_wait3(PyObject *self, PyObject *args)
4916{
Victor Stinner8c62be82010-05-06 00:08:46 +00004917 pid_t pid;
4918 int options;
4919 struct rusage ru;
4920 WAIT_TYPE status;
4921 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004922
Victor Stinner8c62be82010-05-06 00:08:46 +00004923 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4924 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004925
Victor Stinner8c62be82010-05-06 00:08:46 +00004926 Py_BEGIN_ALLOW_THREADS
4927 pid = wait3(&status, options, &ru);
4928 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004929
Victor Stinner8c62be82010-05-06 00:08:46 +00004930 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004931}
4932#endif /* HAVE_WAIT3 */
4933
4934#ifdef HAVE_WAIT4
4935PyDoc_STRVAR(posix_wait4__doc__,
4936"wait4(pid, options) -> (pid, status, rusage)\n\n\
4937Wait for completion of a given child process.");
4938
4939static PyObject *
4940posix_wait4(PyObject *self, PyObject *args)
4941{
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 pid_t pid;
4943 int options;
4944 struct rusage ru;
4945 WAIT_TYPE status;
4946 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004947
Victor Stinner8c62be82010-05-06 00:08:46 +00004948 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4949 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004950
Victor Stinner8c62be82010-05-06 00:08:46 +00004951 Py_BEGIN_ALLOW_THREADS
4952 pid = wait4(pid, &status, options, &ru);
4953 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004954
Victor Stinner8c62be82010-05-06 00:08:46 +00004955 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004956}
4957#endif /* HAVE_WAIT4 */
4958
Guido van Rossumb6775db1994-08-01 11:34:53 +00004959#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004960PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004961"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004962Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004963
Barry Warsaw53699e91996-12-10 23:23:01 +00004964static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004965posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004966{
Victor Stinner8c62be82010-05-06 00:08:46 +00004967 pid_t pid;
4968 int options;
4969 WAIT_TYPE status;
4970 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004971
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4973 return NULL;
4974 Py_BEGIN_ALLOW_THREADS
4975 pid = waitpid(pid, &status, options);
4976 Py_END_ALLOW_THREADS
4977 if (pid == -1)
4978 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004979
Victor Stinner8c62be82010-05-06 00:08:46 +00004980 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004981}
4982
Tim Petersab034fa2002-02-01 11:27:43 +00004983#elif defined(HAVE_CWAIT)
4984
4985/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004986PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004987"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004988"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004989
4990static PyObject *
4991posix_waitpid(PyObject *self, PyObject *args)
4992{
Victor Stinner8c62be82010-05-06 00:08:46 +00004993 Py_intptr_t pid;
4994 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004995
Victor Stinner8c62be82010-05-06 00:08:46 +00004996 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4997 return NULL;
4998 Py_BEGIN_ALLOW_THREADS
4999 pid = _cwait(&status, pid, options);
5000 Py_END_ALLOW_THREADS
5001 if (pid == -1)
5002 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005003
Victor Stinner8c62be82010-05-06 00:08:46 +00005004 /* shift the status left a byte so this is more like the POSIX waitpid */
5005 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005006}
5007#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005008
Guido van Rossumad0ee831995-03-01 10:34:45 +00005009#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005010PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005011"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005012Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005013
Barry Warsaw53699e91996-12-10 23:23:01 +00005014static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005015posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005016{
Victor Stinner8c62be82010-05-06 00:08:46 +00005017 pid_t pid;
5018 WAIT_TYPE status;
5019 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005020
Victor Stinner8c62be82010-05-06 00:08:46 +00005021 Py_BEGIN_ALLOW_THREADS
5022 pid = wait(&status);
5023 Py_END_ALLOW_THREADS
5024 if (pid == -1)
5025 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005026
Victor Stinner8c62be82010-05-06 00:08:46 +00005027 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005028}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005029#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005032PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005033"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005034Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005035
Barry Warsaw53699e91996-12-10 23:23:01 +00005036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005037posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005038{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005039#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005040 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005041#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005042#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005043 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5044 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005045#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005046 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005047#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005048#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005049}
5050
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005051
Guido van Rossumb6775db1994-08-01 11:34:53 +00005052#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005053PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005054"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005055Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005056
Barry Warsaw53699e91996-12-10 23:23:01 +00005057static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005058posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005059{
Victor Stinner8c62be82010-05-06 00:08:46 +00005060 PyObject* v;
5061 char buf[MAXPATHLEN];
5062 PyObject *opath;
5063 char *path;
5064 int n;
5065 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005066
Victor Stinner8c62be82010-05-06 00:08:46 +00005067 if (!PyArg_ParseTuple(args, "O&:readlink",
5068 PyUnicode_FSConverter, &opath))
5069 return NULL;
5070 path = PyBytes_AsString(opath);
5071 v = PySequence_GetItem(args, 0);
5072 if (v == NULL) {
5073 Py_DECREF(opath);
5074 return NULL;
5075 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005076
Victor Stinner8c62be82010-05-06 00:08:46 +00005077 if (PyUnicode_Check(v)) {
5078 arg_is_unicode = 1;
5079 }
5080 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005081
Victor Stinner8c62be82010-05-06 00:08:46 +00005082 Py_BEGIN_ALLOW_THREADS
5083 n = readlink(path, buf, (int) sizeof buf);
5084 Py_END_ALLOW_THREADS
5085 if (n < 0)
5086 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005087
Victor Stinner8c62be82010-05-06 00:08:46 +00005088 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005089 if (arg_is_unicode)
5090 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5091 else
5092 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005093}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005094#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005095
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005096
Brian Curtin52173d42010-12-02 18:29:18 +00005097#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005098PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005099"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005100Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005101
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005103posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005104{
Victor Stinner8c62be82010-05-06 00:08:46 +00005105 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005106}
5107#endif /* HAVE_SYMLINK */
5108
Brian Curtind40e6f72010-07-08 21:39:08 +00005109#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5110
5111PyDoc_STRVAR(win_readlink__doc__,
5112"readlink(path) -> path\n\n\
5113Return a string representing the path to which the symbolic link points.");
5114
Brian Curtind40e6f72010-07-08 21:39:08 +00005115/* Windows readlink implementation */
5116static PyObject *
5117win_readlink(PyObject *self, PyObject *args)
5118{
5119 wchar_t *path;
5120 DWORD n_bytes_returned;
5121 DWORD io_result;
5122 PyObject *result;
5123 HANDLE reparse_point_handle;
5124
5125 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5126 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5127 wchar_t *print_name;
5128
5129 if (!PyArg_ParseTuple(args,
5130 "u:readlink",
5131 &path))
5132 return NULL;
5133
5134 /* First get a handle to the reparse point */
5135 Py_BEGIN_ALLOW_THREADS
5136 reparse_point_handle = CreateFileW(
5137 path,
5138 0,
5139 0,
5140 0,
5141 OPEN_EXISTING,
5142 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5143 0);
5144 Py_END_ALLOW_THREADS
5145
5146 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5147 {
5148 return win32_error_unicode("readlink", path);
5149 }
5150
5151 Py_BEGIN_ALLOW_THREADS
5152 /* New call DeviceIoControl to read the reparse point */
5153 io_result = DeviceIoControl(
5154 reparse_point_handle,
5155 FSCTL_GET_REPARSE_POINT,
5156 0, 0, /* in buffer */
5157 target_buffer, sizeof(target_buffer),
5158 &n_bytes_returned,
5159 0 /* we're not using OVERLAPPED_IO */
5160 );
5161 CloseHandle(reparse_point_handle);
5162 Py_END_ALLOW_THREADS
5163
5164 if (io_result==0)
5165 {
5166 return win32_error_unicode("readlink", path);
5167 }
5168
5169 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5170 {
5171 PyErr_SetString(PyExc_ValueError,
5172 "not a symbolic link");
5173 return NULL;
5174 }
Brian Curtin74e45612010-07-09 15:58:59 +00005175 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5176 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5177
5178 result = PyUnicode_FromWideChar(print_name,
5179 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005180 return result;
5181}
5182
5183#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5184
Brian Curtin52173d42010-12-02 18:29:18 +00005185#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005186
5187/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5188static int has_CreateSymbolicLinkW = 0;
5189static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5190static int
5191check_CreateSymbolicLinkW()
5192{
5193 HINSTANCE hKernel32;
5194 /* only recheck */
5195 if (has_CreateSymbolicLinkW)
5196 return has_CreateSymbolicLinkW;
5197 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005198 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5199 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005200 if (Py_CreateSymbolicLinkW)
5201 has_CreateSymbolicLinkW = 1;
5202 return has_CreateSymbolicLinkW;
5203}
5204
5205PyDoc_STRVAR(win_symlink__doc__,
5206"symlink(src, dst, target_is_directory=False)\n\n\
5207Create a symbolic link pointing to src named dst.\n\
5208target_is_directory is required if the target is to be interpreted as\n\
5209a directory.\n\
5210This function requires Windows 6.0 or greater, and raises a\n\
5211NotImplementedError otherwise.");
5212
5213static PyObject *
5214win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5215{
5216 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5217 PyObject *src, *dest;
5218 int target_is_directory = 0;
5219 DWORD res;
5220 WIN32_FILE_ATTRIBUTE_DATA src_info;
5221
5222 if (!check_CreateSymbolicLinkW())
5223 {
5224 /* raise NotImplementedError */
5225 return PyErr_Format(PyExc_NotImplementedError,
5226 "CreateSymbolicLinkW not found");
5227 }
5228 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5229 kwlist, &src, &dest, &target_is_directory))
5230 return NULL;
5231 if (!convert_to_unicode(&src)) { return NULL; }
5232 if (!convert_to_unicode(&dest)) {
5233 Py_DECREF(src);
5234 return NULL;
5235 }
5236
5237 /* if src is a directory, ensure target_is_directory==1 */
5238 if(
5239 GetFileAttributesExW(
5240 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5241 ))
5242 {
5243 target_is_directory = target_is_directory ||
5244 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5245 }
5246
5247 Py_BEGIN_ALLOW_THREADS
5248 res = Py_CreateSymbolicLinkW(
5249 PyUnicode_AsUnicode(dest),
5250 PyUnicode_AsUnicode(src),
5251 target_is_directory);
5252 Py_END_ALLOW_THREADS
5253 Py_DECREF(src);
5254 Py_DECREF(dest);
5255 if (!res)
5256 {
5257 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5258 }
5259
5260 Py_INCREF(Py_None);
5261 return Py_None;
5262}
Brian Curtin52173d42010-12-02 18:29:18 +00005263#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005264
5265#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005266#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5267static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005268system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005269{
5270 ULONG value = 0;
5271
5272 Py_BEGIN_ALLOW_THREADS
5273 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5274 Py_END_ALLOW_THREADS
5275
5276 return value;
5277}
5278
5279static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005280posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005281{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005282 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005283 return Py_BuildValue("ddddd",
5284 (double)0 /* t.tms_utime / HZ */,
5285 (double)0 /* t.tms_stime / HZ */,
5286 (double)0 /* t.tms_cutime / HZ */,
5287 (double)0 /* t.tms_cstime / HZ */,
5288 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005289}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005290#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005291#define NEED_TICKS_PER_SECOND
5292static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005293static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005294posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005295{
Victor Stinner8c62be82010-05-06 00:08:46 +00005296 struct tms t;
5297 clock_t c;
5298 errno = 0;
5299 c = times(&t);
5300 if (c == (clock_t) -1)
5301 return posix_error();
5302 return Py_BuildValue("ddddd",
5303 (double)t.tms_utime / ticks_per_second,
5304 (double)t.tms_stime / ticks_per_second,
5305 (double)t.tms_cutime / ticks_per_second,
5306 (double)t.tms_cstime / ticks_per_second,
5307 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005308}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005309#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005310#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005311
5312
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005313#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005314#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005315static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005316posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005317{
Victor Stinner8c62be82010-05-06 00:08:46 +00005318 FILETIME create, exit, kernel, user;
5319 HANDLE hProc;
5320 hProc = GetCurrentProcess();
5321 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5322 /* The fields of a FILETIME structure are the hi and lo part
5323 of a 64-bit value expressed in 100 nanosecond units.
5324 1e7 is one second in such units; 1e-7 the inverse.
5325 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5326 */
5327 return Py_BuildValue(
5328 "ddddd",
5329 (double)(user.dwHighDateTime*429.4967296 +
5330 user.dwLowDateTime*1e-7),
5331 (double)(kernel.dwHighDateTime*429.4967296 +
5332 kernel.dwLowDateTime*1e-7),
5333 (double)0,
5334 (double)0,
5335 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005336}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005337#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005338
5339#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005340PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005341"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005342Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005343#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005344
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005345
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005346#ifdef HAVE_GETSID
5347PyDoc_STRVAR(posix_getsid__doc__,
5348"getsid(pid) -> sid\n\n\
5349Call the system call getsid().");
5350
5351static PyObject *
5352posix_getsid(PyObject *self, PyObject *args)
5353{
Victor Stinner8c62be82010-05-06 00:08:46 +00005354 pid_t pid;
5355 int sid;
5356 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5357 return NULL;
5358 sid = getsid(pid);
5359 if (sid < 0)
5360 return posix_error();
5361 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005362}
5363#endif /* HAVE_GETSID */
5364
5365
Guido van Rossumb6775db1994-08-01 11:34:53 +00005366#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005367PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005368"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005369Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005370
Barry Warsaw53699e91996-12-10 23:23:01 +00005371static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005372posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005373{
Victor Stinner8c62be82010-05-06 00:08:46 +00005374 if (setsid() < 0)
5375 return posix_error();
5376 Py_INCREF(Py_None);
5377 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005378}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005379#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005380
Guido van Rossumb6775db1994-08-01 11:34:53 +00005381#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005382PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005383"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005384Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005385
Barry Warsaw53699e91996-12-10 23:23:01 +00005386static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005387posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005388{
Victor Stinner8c62be82010-05-06 00:08:46 +00005389 pid_t pid;
5390 int pgrp;
5391 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5392 return NULL;
5393 if (setpgid(pid, pgrp) < 0)
5394 return posix_error();
5395 Py_INCREF(Py_None);
5396 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005397}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005398#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005399
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005400
Guido van Rossumb6775db1994-08-01 11:34:53 +00005401#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005402PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005403"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005404Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005405
Barry Warsaw53699e91996-12-10 23:23:01 +00005406static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005407posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005408{
Victor Stinner8c62be82010-05-06 00:08:46 +00005409 int fd;
5410 pid_t pgid;
5411 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5412 return NULL;
5413 pgid = tcgetpgrp(fd);
5414 if (pgid < 0)
5415 return posix_error();
5416 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005417}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005418#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005419
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005420
Guido van Rossumb6775db1994-08-01 11:34:53 +00005421#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005422PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005423"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005424Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005425
Barry Warsaw53699e91996-12-10 23:23:01 +00005426static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005427posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005428{
Victor Stinner8c62be82010-05-06 00:08:46 +00005429 int fd;
5430 pid_t pgid;
5431 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5432 return NULL;
5433 if (tcsetpgrp(fd, pgid) < 0)
5434 return posix_error();
5435 Py_INCREF(Py_None);
5436 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005437}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005438#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005439
Guido van Rossum687dd131993-05-17 08:34:16 +00005440/* Functions acting on file descriptors */
5441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005442PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005443"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005444Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005445
Barry Warsaw53699e91996-12-10 23:23:01 +00005446static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005447posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005448{
Victor Stinner8c62be82010-05-06 00:08:46 +00005449 PyObject *ofile;
5450 char *file;
5451 int flag;
5452 int mode = 0777;
5453 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005454
5455#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005456 PyUnicodeObject *po;
5457 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5458 Py_BEGIN_ALLOW_THREADS
5459 /* PyUnicode_AS_UNICODE OK without thread
5460 lock as it is a simple dereference. */
5461 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5462 Py_END_ALLOW_THREADS
5463 if (fd < 0)
5464 return posix_error();
5465 return PyLong_FromLong((long)fd);
5466 }
5467 /* Drop the argument parsing error as narrow strings
5468 are also valid. */
5469 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005470#endif
5471
Victor Stinner8c62be82010-05-06 00:08:46 +00005472 if (!PyArg_ParseTuple(args, "O&i|i",
5473 PyUnicode_FSConverter, &ofile,
5474 &flag, &mode))
5475 return NULL;
5476 file = PyBytes_AsString(ofile);
5477 Py_BEGIN_ALLOW_THREADS
5478 fd = open(file, flag, mode);
5479 Py_END_ALLOW_THREADS
5480 if (fd < 0)
5481 return posix_error_with_allocated_filename(ofile);
5482 Py_DECREF(ofile);
5483 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005484}
5485
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005487PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005488"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005489Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005490
Barry Warsaw53699e91996-12-10 23:23:01 +00005491static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005492posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005493{
Victor Stinner8c62be82010-05-06 00:08:46 +00005494 int fd, res;
5495 if (!PyArg_ParseTuple(args, "i:close", &fd))
5496 return NULL;
5497 if (!_PyVerify_fd(fd))
5498 return posix_error();
5499 Py_BEGIN_ALLOW_THREADS
5500 res = close(fd);
5501 Py_END_ALLOW_THREADS
5502 if (res < 0)
5503 return posix_error();
5504 Py_INCREF(Py_None);
5505 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005506}
5507
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005508
Victor Stinner8c62be82010-05-06 00:08:46 +00005509PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005510"closerange(fd_low, fd_high)\n\n\
5511Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5512
5513static PyObject *
5514posix_closerange(PyObject *self, PyObject *args)
5515{
Victor Stinner8c62be82010-05-06 00:08:46 +00005516 int fd_from, fd_to, i;
5517 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5518 return NULL;
5519 Py_BEGIN_ALLOW_THREADS
5520 for (i = fd_from; i < fd_to; i++)
5521 if (_PyVerify_fd(i))
5522 close(i);
5523 Py_END_ALLOW_THREADS
5524 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005525}
5526
5527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005528PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005529"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005530Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005531
Barry Warsaw53699e91996-12-10 23:23:01 +00005532static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005533posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005534{
Victor Stinner8c62be82010-05-06 00:08:46 +00005535 int fd;
5536 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5537 return NULL;
5538 if (!_PyVerify_fd(fd))
5539 return posix_error();
5540 Py_BEGIN_ALLOW_THREADS
5541 fd = dup(fd);
5542 Py_END_ALLOW_THREADS
5543 if (fd < 0)
5544 return posix_error();
5545 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005546}
5547
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005549PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005550"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005551Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005552
Barry Warsaw53699e91996-12-10 23:23:01 +00005553static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005554posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005555{
Victor Stinner8c62be82010-05-06 00:08:46 +00005556 int fd, fd2, res;
5557 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5558 return NULL;
5559 if (!_PyVerify_fd_dup2(fd, fd2))
5560 return posix_error();
5561 Py_BEGIN_ALLOW_THREADS
5562 res = dup2(fd, fd2);
5563 Py_END_ALLOW_THREADS
5564 if (res < 0)
5565 return posix_error();
5566 Py_INCREF(Py_None);
5567 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005568}
5569
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005571PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005572"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005573Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005574
Barry Warsaw53699e91996-12-10 23:23:01 +00005575static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005576posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005577{
Victor Stinner8c62be82010-05-06 00:08:46 +00005578 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005579#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005580 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005581#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005582 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005583#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005584 PyObject *posobj;
5585 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5586 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005587#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005588 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5589 switch (how) {
5590 case 0: how = SEEK_SET; break;
5591 case 1: how = SEEK_CUR; break;
5592 case 2: how = SEEK_END; break;
5593 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005594#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005595
5596#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005597 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005598#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005599 pos = PyLong_Check(posobj) ?
5600 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005601#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005602 if (PyErr_Occurred())
5603 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005604
Victor Stinner8c62be82010-05-06 00:08:46 +00005605 if (!_PyVerify_fd(fd))
5606 return posix_error();
5607 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005608#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005609 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005610#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005611 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005612#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005613 Py_END_ALLOW_THREADS
5614 if (res < 0)
5615 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005616
5617#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005618 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005619#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005620 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005621#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005622}
5623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005625PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005626"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005627Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005628
Barry Warsaw53699e91996-12-10 23:23:01 +00005629static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005630posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005631{
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 int fd, size;
5633 Py_ssize_t n;
5634 PyObject *buffer;
5635 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5636 return NULL;
5637 if (size < 0) {
5638 errno = EINVAL;
5639 return posix_error();
5640 }
5641 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5642 if (buffer == NULL)
5643 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005644 if (!_PyVerify_fd(fd)) {
5645 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005646 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005647 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005648 Py_BEGIN_ALLOW_THREADS
5649 n = read(fd, PyBytes_AS_STRING(buffer), size);
5650 Py_END_ALLOW_THREADS
5651 if (n < 0) {
5652 Py_DECREF(buffer);
5653 return posix_error();
5654 }
5655 if (n != size)
5656 _PyBytes_Resize(&buffer, n);
5657 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005658}
5659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005661PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005662"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005663Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005664
Barry Warsaw53699e91996-12-10 23:23:01 +00005665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005666posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005667{
Victor Stinner8c62be82010-05-06 00:08:46 +00005668 Py_buffer pbuf;
5669 int fd;
5670 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005671
Victor Stinner8c62be82010-05-06 00:08:46 +00005672 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5673 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005674 if (!_PyVerify_fd(fd)) {
5675 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005676 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005677 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 Py_BEGIN_ALLOW_THREADS
5679 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5680 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005681 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005682 if (size < 0)
5683 return posix_error();
5684 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005685}
5686
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005687
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005688PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005689"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005690Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005691
Barry Warsaw53699e91996-12-10 23:23:01 +00005692static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005693posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005694{
Victor Stinner8c62be82010-05-06 00:08:46 +00005695 int fd;
5696 STRUCT_STAT st;
5697 int res;
5698 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5699 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005700#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005701 /* on OpenVMS we must ensure that all bytes are written to the file */
5702 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005703#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005704 if (!_PyVerify_fd(fd))
5705 return posix_error();
5706 Py_BEGIN_ALLOW_THREADS
5707 res = FSTAT(fd, &st);
5708 Py_END_ALLOW_THREADS
5709 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005710#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005711 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005712#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005713 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005714#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005715 }
Tim Peters5aa91602002-01-30 05:46:57 +00005716
Victor Stinner8c62be82010-05-06 00:08:46 +00005717 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005718}
5719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005720PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005721"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005722Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005723connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005724
5725static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005726posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005727{
Victor Stinner8c62be82010-05-06 00:08:46 +00005728 int fd;
5729 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5730 return NULL;
5731 if (!_PyVerify_fd(fd))
5732 return PyBool_FromLong(0);
5733 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005734}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005735
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005736#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005737PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005738"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005739Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005740
Barry Warsaw53699e91996-12-10 23:23:01 +00005741static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005742posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005743{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005744#if defined(PYOS_OS2)
5745 HFILE read, write;
5746 APIRET rc;
5747
Victor Stinner8c62be82010-05-06 00:08:46 +00005748 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005749 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005750 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005751 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005752 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005753
5754 return Py_BuildValue("(ii)", read, write);
5755#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005756#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005757 int fds[2];
5758 int res;
5759 Py_BEGIN_ALLOW_THREADS
5760 res = pipe(fds);
5761 Py_END_ALLOW_THREADS
5762 if (res != 0)
5763 return posix_error();
5764 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005765#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005766 HANDLE read, write;
5767 int read_fd, write_fd;
5768 BOOL ok;
5769 Py_BEGIN_ALLOW_THREADS
5770 ok = CreatePipe(&read, &write, NULL, 0);
5771 Py_END_ALLOW_THREADS
5772 if (!ok)
5773 return win32_error("CreatePipe", NULL);
5774 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5775 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5776 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005777#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005778#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005779}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005780#endif /* HAVE_PIPE */
5781
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005782
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005783#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005784PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005785"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005786Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005787
Barry Warsaw53699e91996-12-10 23:23:01 +00005788static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005789posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005790{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005791 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005792 char *filename;
5793 int mode = 0666;
5794 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005795 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5796 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005797 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005798 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005799 Py_BEGIN_ALLOW_THREADS
5800 res = mkfifo(filename, mode);
5801 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005802 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005803 if (res < 0)
5804 return posix_error();
5805 Py_INCREF(Py_None);
5806 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005807}
5808#endif
5809
5810
Neal Norwitz11690112002-07-30 01:08:28 +00005811#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005812PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005813"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005814Create a filesystem node (file, device special file or named pipe)\n\
5815named filename. mode specifies both the permissions to use and the\n\
5816type of node to be created, being combined (bitwise OR) with one of\n\
5817S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005818device defines the newly created device special file (probably using\n\
5819os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005820
5821
5822static PyObject *
5823posix_mknod(PyObject *self, PyObject *args)
5824{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005825 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005826 char *filename;
5827 int mode = 0600;
5828 int device = 0;
5829 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005830 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5831 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005832 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005833 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005834 Py_BEGIN_ALLOW_THREADS
5835 res = mknod(filename, mode, device);
5836 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005837 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005838 if (res < 0)
5839 return posix_error();
5840 Py_INCREF(Py_None);
5841 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005842}
5843#endif
5844
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005845#ifdef HAVE_DEVICE_MACROS
5846PyDoc_STRVAR(posix_major__doc__,
5847"major(device) -> major number\n\
5848Extracts a device major number from a raw device number.");
5849
5850static PyObject *
5851posix_major(PyObject *self, PyObject *args)
5852{
Victor Stinner8c62be82010-05-06 00:08:46 +00005853 int device;
5854 if (!PyArg_ParseTuple(args, "i:major", &device))
5855 return NULL;
5856 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005857}
5858
5859PyDoc_STRVAR(posix_minor__doc__,
5860"minor(device) -> minor number\n\
5861Extracts a device minor number from a raw device number.");
5862
5863static PyObject *
5864posix_minor(PyObject *self, PyObject *args)
5865{
Victor Stinner8c62be82010-05-06 00:08:46 +00005866 int device;
5867 if (!PyArg_ParseTuple(args, "i:minor", &device))
5868 return NULL;
5869 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005870}
5871
5872PyDoc_STRVAR(posix_makedev__doc__,
5873"makedev(major, minor) -> device number\n\
5874Composes a raw device number from the major and minor device numbers.");
5875
5876static PyObject *
5877posix_makedev(PyObject *self, PyObject *args)
5878{
Victor Stinner8c62be82010-05-06 00:08:46 +00005879 int major, minor;
5880 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5881 return NULL;
5882 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005883}
5884#endif /* device macros */
5885
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005886
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005887#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005888PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005889"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005890Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005891
Barry Warsaw53699e91996-12-10 23:23:01 +00005892static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005893posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005894{
Victor Stinner8c62be82010-05-06 00:08:46 +00005895 int fd;
5896 off_t length;
5897 int res;
5898 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005899
Victor Stinner8c62be82010-05-06 00:08:46 +00005900 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5901 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005902
5903#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005904 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005905#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005906 length = PyLong_Check(lenobj) ?
5907 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005908#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005909 if (PyErr_Occurred())
5910 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005911
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 Py_BEGIN_ALLOW_THREADS
5913 res = ftruncate(fd, length);
5914 Py_END_ALLOW_THREADS
5915 if (res < 0)
5916 return posix_error();
5917 Py_INCREF(Py_None);
5918 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005919}
5920#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005921
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005922#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005923PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005924"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005925Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005926
Fred Drake762e2061999-08-26 17:23:54 +00005927/* Save putenv() parameters as values here, so we can collect them when they
5928 * get re-set with another call for the same key. */
5929static PyObject *posix_putenv_garbage;
5930
Tim Peters5aa91602002-01-30 05:46:57 +00005931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005932posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005933{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005934#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005935 wchar_t *s1, *s2;
5936 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005937#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 PyObject *os1, *os2;
5939 char *s1, *s2;
5940 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005941#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005942 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005944
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005945#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 if (!PyArg_ParseTuple(args,
5947 "uu:putenv",
5948 &s1, &s2))
5949 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005950#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005951 if (!PyArg_ParseTuple(args,
5952 "O&O&:putenv",
5953 PyUnicode_FSConverter, &os1,
5954 PyUnicode_FSConverter, &os2))
5955 return NULL;
5956 s1 = PyBytes_AsString(os1);
5957 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005958#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005959
5960#if defined(PYOS_OS2)
5961 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5962 APIRET rc;
5963
Guido van Rossumd48f2521997-12-05 22:19:34 +00005964 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005965 if (rc != NO_ERROR) {
5966 os2_error(rc);
5967 goto error;
5968 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005969
5970 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5971 APIRET rc;
5972
Guido van Rossumd48f2521997-12-05 22:19:34 +00005973 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005974 if (rc != NO_ERROR) {
5975 os2_error(rc);
5976 goto error;
5977 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005978 } else {
5979#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005980 /* XXX This can leak memory -- not easy to fix :-( */
5981 /* len includes space for a trailing \0; the size arg to
5982 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005983#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005984 len = wcslen(s1) + wcslen(s2) + 2;
5985 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005986#else
Victor Stinner84ae1182010-05-06 22:05:07 +00005987 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005989#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005990 if (newstr == NULL) {
5991 PyErr_NoMemory();
5992 goto error;
5993 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005994#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005995 newenv = PyUnicode_AsUnicode(newstr);
5996 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5997 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005998 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005999 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006000 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006001#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006002 newenv = PyBytes_AS_STRING(newstr);
6003 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6004 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006006 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006007 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006008#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006009
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 /* Install the first arg and newstr in posix_putenv_garbage;
6011 * this will cause previous value to be collected. This has to
6012 * happen after the real putenv() call because the old value
6013 * was still accessible until then. */
6014 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006015#ifdef MS_WINDOWS
6016 PyTuple_GET_ITEM(args, 0),
6017#else
6018 os1,
6019#endif
6020 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 /* really not much we can do; just leak */
6022 PyErr_Clear();
6023 }
6024 else {
6025 Py_DECREF(newstr);
6026 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006027
6028#if defined(PYOS_OS2)
6029 }
6030#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006031
Martin v. Löwis011e8422009-05-05 04:43:17 +00006032#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006033 Py_DECREF(os1);
6034 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006035#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006036 Py_RETURN_NONE;
6037
6038error:
6039#ifndef MS_WINDOWS
6040 Py_DECREF(os1);
6041 Py_DECREF(os2);
6042#endif
6043 Py_XDECREF(newstr);
6044 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006045}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006046#endif /* putenv */
6047
Guido van Rossumc524d952001-10-19 01:31:59 +00006048#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006049PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006050"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006051Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006052
6053static PyObject *
6054posix_unsetenv(PyObject *self, PyObject *args)
6055{
Victor Stinner84ae1182010-05-06 22:05:07 +00006056#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006058
Victor Stinner8c62be82010-05-06 00:08:46 +00006059 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6060 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006061#else
6062 PyObject *os1;
6063 char *s1;
6064
6065 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6066 PyUnicode_FSConverter, &os1))
6067 return NULL;
6068 s1 = PyBytes_AsString(os1);
6069#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006070
Victor Stinner8c62be82010-05-06 00:08:46 +00006071 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006072
Victor Stinner8c62be82010-05-06 00:08:46 +00006073 /* Remove the key from posix_putenv_garbage;
6074 * this will cause it to be collected. This has to
6075 * happen after the real unsetenv() call because the
6076 * old value was still accessible until then.
6077 */
6078 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006079#ifdef MS_WINDOWS
6080 PyTuple_GET_ITEM(args, 0)
6081#else
6082 os1
6083#endif
6084 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006085 /* really not much we can do; just leak */
6086 PyErr_Clear();
6087 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006088
Victor Stinner84ae1182010-05-06 22:05:07 +00006089#ifndef MS_WINDOWS
6090 Py_DECREF(os1);
6091#endif
6092 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006093}
6094#endif /* unsetenv */
6095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006096PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006097"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006098Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006099
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006101posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006102{
Victor Stinner8c62be82010-05-06 00:08:46 +00006103 int code;
6104 char *message;
6105 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6106 return NULL;
6107 message = strerror(code);
6108 if (message == NULL) {
6109 PyErr_SetString(PyExc_ValueError,
6110 "strerror() argument out of range");
6111 return NULL;
6112 }
6113 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006114}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006115
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006116
Guido van Rossumc9641791998-08-04 15:26:23 +00006117#ifdef HAVE_SYS_WAIT_H
6118
Fred Drake106c1a02002-04-23 15:58:02 +00006119#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006120PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006121"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006122Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006123
6124static PyObject *
6125posix_WCOREDUMP(PyObject *self, PyObject *args)
6126{
Victor Stinner8c62be82010-05-06 00:08:46 +00006127 WAIT_TYPE status;
6128 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006129
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6131 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006132
Victor Stinner8c62be82010-05-06 00:08:46 +00006133 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006134}
6135#endif /* WCOREDUMP */
6136
6137#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006138PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006139"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006140Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006141job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006142
6143static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006144posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006145{
Victor Stinner8c62be82010-05-06 00:08:46 +00006146 WAIT_TYPE status;
6147 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006148
Victor Stinner8c62be82010-05-06 00:08:46 +00006149 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6150 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006151
Victor Stinner8c62be82010-05-06 00:08:46 +00006152 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006153}
6154#endif /* WIFCONTINUED */
6155
Guido van Rossumc9641791998-08-04 15:26:23 +00006156#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006157PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006158"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006159Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006160
6161static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006162posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006163{
Victor Stinner8c62be82010-05-06 00:08:46 +00006164 WAIT_TYPE status;
6165 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006166
Victor Stinner8c62be82010-05-06 00:08:46 +00006167 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6168 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006169
Victor Stinner8c62be82010-05-06 00:08:46 +00006170 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006171}
6172#endif /* WIFSTOPPED */
6173
6174#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006175PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006176"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006177Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006178
6179static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006180posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006181{
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 WAIT_TYPE status;
6183 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006184
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6186 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006187
Victor Stinner8c62be82010-05-06 00:08:46 +00006188 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006189}
6190#endif /* WIFSIGNALED */
6191
6192#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006193PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006194"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006195Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006196system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006197
6198static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006199posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006200{
Victor Stinner8c62be82010-05-06 00:08:46 +00006201 WAIT_TYPE status;
6202 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006203
Victor Stinner8c62be82010-05-06 00:08:46 +00006204 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6205 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006206
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006208}
6209#endif /* WIFEXITED */
6210
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006211#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006212PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006213"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006214Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006215
6216static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006217posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006218{
Victor Stinner8c62be82010-05-06 00:08:46 +00006219 WAIT_TYPE status;
6220 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006221
Victor Stinner8c62be82010-05-06 00:08:46 +00006222 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6223 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006224
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006226}
6227#endif /* WEXITSTATUS */
6228
6229#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006230PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006231"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006232Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006233value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006234
6235static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006236posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006237{
Victor Stinner8c62be82010-05-06 00:08:46 +00006238 WAIT_TYPE status;
6239 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006240
Victor Stinner8c62be82010-05-06 00:08:46 +00006241 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6242 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006243
Victor Stinner8c62be82010-05-06 00:08:46 +00006244 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006245}
6246#endif /* WTERMSIG */
6247
6248#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006249PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006250"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006251Return the signal that stopped the process that provided\n\
6252the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006253
6254static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006255posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006256{
Victor Stinner8c62be82010-05-06 00:08:46 +00006257 WAIT_TYPE status;
6258 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006259
Victor Stinner8c62be82010-05-06 00:08:46 +00006260 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6261 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006262
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006264}
6265#endif /* WSTOPSIG */
6266
6267#endif /* HAVE_SYS_WAIT_H */
6268
6269
Thomas Wouters477c8d52006-05-27 19:21:47 +00006270#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006271#ifdef _SCO_DS
6272/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6273 needed definitions in sys/statvfs.h */
6274#define _SVID3
6275#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006276#include <sys/statvfs.h>
6277
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006278static PyObject*
6279_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6281 if (v == NULL)
6282 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006283
6284#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006285 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6286 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6287 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6288 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6289 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6290 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6291 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6292 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6293 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6294 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006295#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006296 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6297 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6298 PyStructSequence_SET_ITEM(v, 2,
6299 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6300 PyStructSequence_SET_ITEM(v, 3,
6301 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6302 PyStructSequence_SET_ITEM(v, 4,
6303 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6304 PyStructSequence_SET_ITEM(v, 5,
6305 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6306 PyStructSequence_SET_ITEM(v, 6,
6307 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6308 PyStructSequence_SET_ITEM(v, 7,
6309 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6310 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6311 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006312#endif
6313
Victor Stinner8c62be82010-05-06 00:08:46 +00006314 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006315}
6316
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006317PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006318"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006319Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006320
6321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006322posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006323{
Victor Stinner8c62be82010-05-06 00:08:46 +00006324 int fd, res;
6325 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006326
Victor Stinner8c62be82010-05-06 00:08:46 +00006327 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6328 return NULL;
6329 Py_BEGIN_ALLOW_THREADS
6330 res = fstatvfs(fd, &st);
6331 Py_END_ALLOW_THREADS
6332 if (res != 0)
6333 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006334
Victor Stinner8c62be82010-05-06 00:08:46 +00006335 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006336}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006337#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006338
6339
Thomas Wouters477c8d52006-05-27 19:21:47 +00006340#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006341#include <sys/statvfs.h>
6342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006343PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006344"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006345Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006346
6347static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006348posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006349{
Victor Stinner8c62be82010-05-06 00:08:46 +00006350 char *path;
6351 int res;
6352 struct statvfs st;
6353 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6354 return NULL;
6355 Py_BEGIN_ALLOW_THREADS
6356 res = statvfs(path, &st);
6357 Py_END_ALLOW_THREADS
6358 if (res != 0)
6359 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006360
Victor Stinner8c62be82010-05-06 00:08:46 +00006361 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006362}
6363#endif /* HAVE_STATVFS */
6364
Fred Drakec9680921999-12-13 16:37:25 +00006365/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6366 * It maps strings representing configuration variable names to
6367 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006368 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006369 * rarely-used constants. There are three separate tables that use
6370 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006371 *
6372 * This code is always included, even if none of the interfaces that
6373 * need it are included. The #if hackery needed to avoid it would be
6374 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006375 */
6376struct constdef {
6377 char *name;
6378 long value;
6379};
6380
Fred Drake12c6e2d1999-12-14 21:25:03 +00006381static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006382conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006383 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006384{
Christian Heimes217cfd12007-12-02 14:31:20 +00006385 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006386 *valuep = PyLong_AS_LONG(arg);
6387 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006388 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006389 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006390 /* look up the value in the table using a binary search */
6391 size_t lo = 0;
6392 size_t mid;
6393 size_t hi = tablesize;
6394 int cmp;
6395 const char *confname;
6396 if (!PyUnicode_Check(arg)) {
6397 PyErr_SetString(PyExc_TypeError,
6398 "configuration names must be strings or integers");
6399 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006400 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006401 confname = _PyUnicode_AsString(arg);
6402 if (confname == NULL)
6403 return 0;
6404 while (lo < hi) {
6405 mid = (lo + hi) / 2;
6406 cmp = strcmp(confname, table[mid].name);
6407 if (cmp < 0)
6408 hi = mid;
6409 else if (cmp > 0)
6410 lo = mid + 1;
6411 else {
6412 *valuep = table[mid].value;
6413 return 1;
6414 }
6415 }
6416 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6417 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006418 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006419}
6420
6421
6422#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6423static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006424#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006425 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006426#endif
6427#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006428 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006429#endif
Fred Drakec9680921999-12-13 16:37:25 +00006430#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006431 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006432#endif
6433#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006434 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006435#endif
6436#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006437 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006438#endif
6439#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006440 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006441#endif
6442#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006443 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006444#endif
6445#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006446 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006447#endif
6448#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006449 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006450#endif
6451#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006452 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006453#endif
6454#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006455 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006456#endif
6457#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006458 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006459#endif
6460#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006461 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006462#endif
6463#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006464 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006465#endif
6466#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006468#endif
6469#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006471#endif
6472#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006473 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006474#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006475#ifdef _PC_ACL_ENABLED
6476 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6477#endif
6478#ifdef _PC_MIN_HOLE_SIZE
6479 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6480#endif
6481#ifdef _PC_ALLOC_SIZE_MIN
6482 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6483#endif
6484#ifdef _PC_REC_INCR_XFER_SIZE
6485 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6486#endif
6487#ifdef _PC_REC_MAX_XFER_SIZE
6488 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6489#endif
6490#ifdef _PC_REC_MIN_XFER_SIZE
6491 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6492#endif
6493#ifdef _PC_REC_XFER_ALIGN
6494 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6495#endif
6496#ifdef _PC_SYMLINK_MAX
6497 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6498#endif
6499#ifdef _PC_XATTR_ENABLED
6500 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6501#endif
6502#ifdef _PC_XATTR_EXISTS
6503 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6504#endif
6505#ifdef _PC_TIMESTAMP_RESOLUTION
6506 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6507#endif
Fred Drakec9680921999-12-13 16:37:25 +00006508};
6509
Fred Drakec9680921999-12-13 16:37:25 +00006510static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006511conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006512{
6513 return conv_confname(arg, valuep, posix_constants_pathconf,
6514 sizeof(posix_constants_pathconf)
6515 / sizeof(struct constdef));
6516}
6517#endif
6518
6519#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006520PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006521"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006522Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006523If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006524
6525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006526posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006527{
6528 PyObject *result = NULL;
6529 int name, fd;
6530
Fred Drake12c6e2d1999-12-14 21:25:03 +00006531 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6532 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006533 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006534
Stefan Krah0e803b32010-11-26 16:16:47 +00006535 errno = 0;
6536 limit = fpathconf(fd, name);
6537 if (limit == -1 && errno != 0)
6538 posix_error();
6539 else
6540 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006541 }
6542 return result;
6543}
6544#endif
6545
6546
6547#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006548PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006549"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006550Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006551If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006552
6553static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006554posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006555{
6556 PyObject *result = NULL;
6557 int name;
6558 char *path;
6559
6560 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6561 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006562 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006563
Victor Stinner8c62be82010-05-06 00:08:46 +00006564 errno = 0;
6565 limit = pathconf(path, name);
6566 if (limit == -1 && errno != 0) {
6567 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006568 /* could be a path or name problem */
6569 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006570 else
Stefan Krah99439262010-11-26 12:58:05 +00006571 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006572 }
6573 else
6574 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006575 }
6576 return result;
6577}
6578#endif
6579
6580#ifdef HAVE_CONFSTR
6581static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006582#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006583 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006584#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006585#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006586 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006587#endif
6588#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006589 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006590#endif
Fred Draked86ed291999-12-15 15:34:33 +00006591#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006593#endif
6594#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006595 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006596#endif
6597#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006599#endif
6600#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006601 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006602#endif
Fred Drakec9680921999-12-13 16:37:25 +00006603#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006604 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006605#endif
6606#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006607 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006608#endif
6609#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006611#endif
6612#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006614#endif
6615#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006617#endif
6618#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006620#endif
6621#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006622 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006623#endif
6624#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006625 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006626#endif
Fred Draked86ed291999-12-15 15:34:33 +00006627#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006628 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006629#endif
Fred Drakec9680921999-12-13 16:37:25 +00006630#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006632#endif
Fred Draked86ed291999-12-15 15:34:33 +00006633#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006635#endif
6636#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006637 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006638#endif
6639#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006641#endif
6642#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006644#endif
Fred Drakec9680921999-12-13 16:37:25 +00006645#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006647#endif
6648#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006650#endif
6651#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006653#endif
6654#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006656#endif
6657#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006659#endif
6660#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006662#endif
6663#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006664 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006665#endif
6666#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006668#endif
6669#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006671#endif
6672#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006674#endif
6675#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006676 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006677#endif
6678#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006680#endif
6681#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006683#endif
6684#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006686#endif
6687#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006689#endif
6690#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006692#endif
Fred Draked86ed291999-12-15 15:34:33 +00006693#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006695#endif
6696#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006698#endif
6699#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006701#endif
6702#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006704#endif
6705#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006707#endif
6708#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006710#endif
6711#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006713#endif
6714#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006716#endif
6717#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006719#endif
6720#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006722#endif
6723#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006724 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006725#endif
6726#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006728#endif
6729#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006731#endif
Fred Drakec9680921999-12-13 16:37:25 +00006732};
6733
6734static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006735conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006736{
6737 return conv_confname(arg, valuep, posix_constants_confstr,
6738 sizeof(posix_constants_confstr)
6739 / sizeof(struct constdef));
6740}
6741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006742PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006743"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006744Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006745
6746static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006747posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006748{
6749 PyObject *result = NULL;
6750 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006751 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006752 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006753
Victor Stinnercb043522010-09-10 23:49:04 +00006754 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6755 return NULL;
6756
6757 errno = 0;
6758 len = confstr(name, buffer, sizeof(buffer));
6759 if (len == 0) {
6760 if (errno) {
6761 posix_error();
6762 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006763 }
6764 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006765 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006766 }
6767 }
Victor Stinnercb043522010-09-10 23:49:04 +00006768
6769 if ((unsigned int)len >= sizeof(buffer)) {
6770 char *buf = PyMem_Malloc(len);
6771 if (buf == NULL)
6772 return PyErr_NoMemory();
6773 confstr(name, buf, len);
6774 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6775 PyMem_Free(buf);
6776 }
6777 else
6778 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006779 return result;
6780}
6781#endif
6782
6783
6784#ifdef HAVE_SYSCONF
6785static struct constdef posix_constants_sysconf[] = {
6786#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006788#endif
6789#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006791#endif
6792#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006793 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006794#endif
6795#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006797#endif
6798#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006799 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006800#endif
6801#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006803#endif
6804#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006806#endif
6807#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006808 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006809#endif
6810#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006812#endif
6813#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006815#endif
Fred Draked86ed291999-12-15 15:34:33 +00006816#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006817 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006818#endif
6819#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006821#endif
Fred Drakec9680921999-12-13 16:37:25 +00006822#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006824#endif
Fred Drakec9680921999-12-13 16:37:25 +00006825#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006827#endif
6828#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006830#endif
6831#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006832 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006833#endif
6834#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006836#endif
6837#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006838 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006839#endif
Fred Draked86ed291999-12-15 15:34:33 +00006840#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006841 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006842#endif
Fred Drakec9680921999-12-13 16:37:25 +00006843#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006845#endif
6846#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006848#endif
6849#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006851#endif
6852#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006854#endif
6855#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006857#endif
Fred Draked86ed291999-12-15 15:34:33 +00006858#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006860#endif
Fred Drakec9680921999-12-13 16:37:25 +00006861#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006863#endif
6864#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006866#endif
6867#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006869#endif
6870#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006872#endif
6873#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006875#endif
6876#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006878#endif
6879#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006881#endif
6882#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006884#endif
6885#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006887#endif
6888#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006889 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006890#endif
6891#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006893#endif
6894#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006896#endif
6897#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006899#endif
6900#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006902#endif
6903#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006905#endif
6906#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006908#endif
6909#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006911#endif
6912#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006914#endif
6915#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006917#endif
6918#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006920#endif
6921#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006923#endif
6924#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006926#endif
6927#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006929#endif
Fred Draked86ed291999-12-15 15:34:33 +00006930#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006932#endif
Fred Drakec9680921999-12-13 16:37:25 +00006933#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006934 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006935#endif
6936#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006938#endif
6939#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006941#endif
Fred Draked86ed291999-12-15 15:34:33 +00006942#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006944#endif
Fred Drakec9680921999-12-13 16:37:25 +00006945#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006947#endif
Fred Draked86ed291999-12-15 15:34:33 +00006948#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006950#endif
6951#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006953#endif
Fred Drakec9680921999-12-13 16:37:25 +00006954#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006956#endif
6957#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006959#endif
6960#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006961 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006962#endif
6963#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006965#endif
Fred Draked86ed291999-12-15 15:34:33 +00006966#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006968#endif
Fred Drakec9680921999-12-13 16:37:25 +00006969#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006971#endif
6972#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006974#endif
6975#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006977#endif
6978#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006980#endif
6981#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006983#endif
6984#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006986#endif
6987#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006989#endif
Fred Draked86ed291999-12-15 15:34:33 +00006990#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006991 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006992#endif
Fred Drakec9680921999-12-13 16:37:25 +00006993#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006994 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006995#endif
6996#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006998#endif
Fred Draked86ed291999-12-15 15:34:33 +00006999#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007001#endif
Fred Drakec9680921999-12-13 16:37:25 +00007002#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007004#endif
7005#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007007#endif
7008#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007010#endif
7011#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
7014#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007016#endif
7017#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
7020#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007022#endif
7023#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007025#endif
7026#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007028#endif
Fred Draked86ed291999-12-15 15:34:33 +00007029#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007031#endif
7032#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007034#endif
Fred Drakec9680921999-12-13 16:37:25 +00007035#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007037#endif
7038#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007040#endif
7041#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007043#endif
7044#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007046#endif
7047#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007049#endif
7050#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007052#endif
7053#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007055#endif
7056#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007058#endif
7059#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007061#endif
7062#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
7065#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007067#endif
7068#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007070#endif
7071#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007073#endif
7074#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007076#endif
7077#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007079#endif
7080#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007082#endif
7083#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007085#endif
7086#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007088#endif
7089#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007091#endif
7092#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007094#endif
7095#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007097#endif
7098#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007100#endif
7101#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007103#endif
7104#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007106#endif
7107#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007109#endif
7110#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007112#endif
7113#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007115#endif
7116#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007118#endif
7119#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007120 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007121#endif
7122#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007124#endif
7125#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007127#endif
7128#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007130#endif
7131#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007133#endif
7134#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007136#endif
7137#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007139#endif
Fred Draked86ed291999-12-15 15:34:33 +00007140#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007142#endif
Fred Drakec9680921999-12-13 16:37:25 +00007143#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007145#endif
7146#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007148#endif
7149#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007151#endif
7152#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
7155#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007157#endif
7158#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007160#endif
7161#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
7164#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007166#endif
7167#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007169#endif
7170#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007172#endif
7173#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007175#endif
7176#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007178#endif
7179#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007181#endif
7182#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007184#endif
7185#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007187#endif
7188#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007190#endif
7191#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007193#endif
7194#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007196#endif
7197#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007199#endif
7200#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007202#endif
7203#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007205#endif
7206#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007208#endif
7209#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007211#endif
7212#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007214#endif
7215#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007217#endif
7218#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007220#endif
7221#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007223#endif
7224#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007226#endif
7227#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007229#endif
7230#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
7233#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007235#endif
7236#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007238#endif
7239#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007241#endif
7242#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007244#endif
7245#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007247#endif
7248#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007250#endif
7251#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007253#endif
7254#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007256#endif
7257#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007259#endif
7260#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007262#endif
7263#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007265#endif
7266#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007268#endif
7269#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007271#endif
7272#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007274#endif
7275#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007277#endif
7278};
7279
7280static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007281conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007282{
7283 return conv_confname(arg, valuep, posix_constants_sysconf,
7284 sizeof(posix_constants_sysconf)
7285 / sizeof(struct constdef));
7286}
7287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007288PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007289"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007290Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007291
7292static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007293posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007294{
7295 PyObject *result = NULL;
7296 int name;
7297
7298 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7299 int value;
7300
7301 errno = 0;
7302 value = sysconf(name);
7303 if (value == -1 && errno != 0)
7304 posix_error();
7305 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007306 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007307 }
7308 return result;
7309}
7310#endif
7311
7312
Fred Drakebec628d1999-12-15 18:31:10 +00007313/* This code is used to ensure that the tables of configuration value names
7314 * are in sorted order as required by conv_confname(), and also to build the
7315 * the exported dictionaries that are used to publish information about the
7316 * names available on the host platform.
7317 *
7318 * Sorting the table at runtime ensures that the table is properly ordered
7319 * when used, even for platforms we're not able to test on. It also makes
7320 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007321 */
Fred Drakebec628d1999-12-15 18:31:10 +00007322
7323static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007324cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007325{
7326 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007328 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007329 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007330
7331 return strcmp(c1->name, c2->name);
7332}
7333
7334static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007335setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007337{
Fred Drakebec628d1999-12-15 18:31:10 +00007338 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007339 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007340
7341 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7342 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007343 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007345
Barry Warsaw3155db32000-04-13 15:20:40 +00007346 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 PyObject *o = PyLong_FromLong(table[i].value);
7348 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7349 Py_XDECREF(o);
7350 Py_DECREF(d);
7351 return -1;
7352 }
7353 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007354 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007355 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007356}
7357
Fred Drakebec628d1999-12-15 18:31:10 +00007358/* Return -1 on failure, 0 on success. */
7359static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007360setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007361{
7362#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007363 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007364 sizeof(posix_constants_pathconf)
7365 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007366 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007367 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007368#endif
7369#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007370 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007371 sizeof(posix_constants_confstr)
7372 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007373 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007374 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007375#endif
7376#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007377 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007378 sizeof(posix_constants_sysconf)
7379 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007380 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007381 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007382#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007383 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007384}
Fred Draked86ed291999-12-15 15:34:33 +00007385
7386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007387PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007388"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007389Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007390in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007391
7392static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007393posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007394{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007395 abort();
7396 /*NOTREACHED*/
7397 Py_FatalError("abort() called from Python code didn't abort!");
7398 return NULL;
7399}
Fred Drakebec628d1999-12-15 18:31:10 +00007400
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007401#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007402PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007403"startfile(filepath [, operation]) - Start a file with its associated\n\
7404application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007405\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007406When \"operation\" is not specified or \"open\", this acts like\n\
7407double-clicking the file in Explorer, or giving the file name as an\n\
7408argument to the DOS \"start\" command: the file is opened with whatever\n\
7409application (if any) its extension is associated.\n\
7410When another \"operation\" is given, it specifies what should be done with\n\
7411the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007412\n\
7413startfile returns as soon as the associated application is launched.\n\
7414There is no option to wait for the application to close, and no way\n\
7415to retrieve the application's exit status.\n\
7416\n\
7417The filepath is relative to the current directory. If you want to use\n\
7418an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007419the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007420
7421static PyObject *
7422win32_startfile(PyObject *self, PyObject *args)
7423{
Victor Stinner8c62be82010-05-06 00:08:46 +00007424 PyObject *ofilepath;
7425 char *filepath;
7426 char *operation = NULL;
7427 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007428
Victor Stinner8c62be82010-05-06 00:08:46 +00007429 PyObject *unipath, *woperation = NULL;
7430 if (!PyArg_ParseTuple(args, "U|s:startfile",
7431 &unipath, &operation)) {
7432 PyErr_Clear();
7433 goto normal;
7434 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007435
Victor Stinner8c62be82010-05-06 00:08:46 +00007436 if (operation) {
7437 woperation = PyUnicode_DecodeASCII(operation,
7438 strlen(operation), NULL);
7439 if (!woperation) {
7440 PyErr_Clear();
7441 operation = NULL;
7442 goto normal;
7443 }
7444 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007445
Victor Stinner8c62be82010-05-06 00:08:46 +00007446 Py_BEGIN_ALLOW_THREADS
7447 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7448 PyUnicode_AS_UNICODE(unipath),
7449 NULL, NULL, SW_SHOWNORMAL);
7450 Py_END_ALLOW_THREADS
7451
7452 Py_XDECREF(woperation);
7453 if (rc <= (HINSTANCE)32) {
7454 PyObject *errval = win32_error_unicode("startfile",
7455 PyUnicode_AS_UNICODE(unipath));
7456 return errval;
7457 }
7458 Py_INCREF(Py_None);
7459 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007460
7461normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007462 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7463 PyUnicode_FSConverter, &ofilepath,
7464 &operation))
7465 return NULL;
7466 filepath = PyBytes_AsString(ofilepath);
7467 Py_BEGIN_ALLOW_THREADS
7468 rc = ShellExecute((HWND)0, operation, filepath,
7469 NULL, NULL, SW_SHOWNORMAL);
7470 Py_END_ALLOW_THREADS
7471 if (rc <= (HINSTANCE)32) {
7472 PyObject *errval = win32_error("startfile", filepath);
7473 Py_DECREF(ofilepath);
7474 return errval;
7475 }
7476 Py_DECREF(ofilepath);
7477 Py_INCREF(Py_None);
7478 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007479}
7480#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007481
Martin v. Löwis438b5342002-12-27 10:16:42 +00007482#ifdef HAVE_GETLOADAVG
7483PyDoc_STRVAR(posix_getloadavg__doc__,
7484"getloadavg() -> (float, float, float)\n\n\
7485Return the number of processes in the system run queue averaged over\n\
7486the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7487was unobtainable");
7488
7489static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007490posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007491{
7492 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007493 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007494 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7495 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007496 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007497 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007498}
7499#endif
7500
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007501#ifdef MS_WINDOWS
7502
7503PyDoc_STRVAR(win32_urandom__doc__,
7504"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007505Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007506
7507typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7508 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7509 DWORD dwFlags );
7510typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7511 BYTE *pbBuffer );
7512
7513static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007514/* This handle is never explicitly released. Instead, the operating
7515 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007516static HCRYPTPROV hCryptProv = 0;
7517
Tim Peters4ad82172004-08-30 17:02:04 +00007518static PyObject*
7519win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007520{
Victor Stinner8c62be82010-05-06 00:08:46 +00007521 int howMany;
7522 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007523
Victor Stinner8c62be82010-05-06 00:08:46 +00007524 /* Read arguments */
7525 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7526 return NULL;
7527 if (howMany < 0)
7528 return PyErr_Format(PyExc_ValueError,
7529 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007530
Victor Stinner8c62be82010-05-06 00:08:46 +00007531 if (hCryptProv == 0) {
7532 HINSTANCE hAdvAPI32 = NULL;
7533 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007534
Victor Stinner8c62be82010-05-06 00:08:46 +00007535 /* Obtain handle to the DLL containing CryptoAPI
7536 This should not fail */
7537 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7538 if(hAdvAPI32 == NULL)
7539 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007540
Victor Stinner8c62be82010-05-06 00:08:46 +00007541 /* Obtain pointers to the CryptoAPI functions
7542 This will fail on some early versions of Win95 */
7543 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7544 hAdvAPI32,
7545 "CryptAcquireContextA");
7546 if (pCryptAcquireContext == NULL)
7547 return PyErr_Format(PyExc_NotImplementedError,
7548 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007549
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7551 hAdvAPI32, "CryptGenRandom");
7552 if (pCryptGenRandom == NULL)
7553 return PyErr_Format(PyExc_NotImplementedError,
7554 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007555
Victor Stinner8c62be82010-05-06 00:08:46 +00007556 /* Acquire context */
7557 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7558 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7559 return win32_error("CryptAcquireContext", NULL);
7560 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007561
Victor Stinner8c62be82010-05-06 00:08:46 +00007562 /* Allocate bytes */
7563 result = PyBytes_FromStringAndSize(NULL, howMany);
7564 if (result != NULL) {
7565 /* Get random data */
7566 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7567 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7568 PyBytes_AS_STRING(result))) {
7569 Py_DECREF(result);
7570 return win32_error("CryptGenRandom", NULL);
7571 }
7572 }
7573 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007574}
7575#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007576
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007577PyDoc_STRVAR(device_encoding__doc__,
7578"device_encoding(fd) -> str\n\n\
7579Return a string describing the encoding of the device\n\
7580if the output is a terminal; else return None.");
7581
7582static PyObject *
7583device_encoding(PyObject *self, PyObject *args)
7584{
Victor Stinner8c62be82010-05-06 00:08:46 +00007585 int fd;
7586 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7587 return NULL;
7588 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7589 Py_INCREF(Py_None);
7590 return Py_None;
7591 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007592#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 if (fd == 0) {
7594 char buf[100];
7595 sprintf(buf, "cp%d", GetConsoleCP());
7596 return PyUnicode_FromString(buf);
7597 }
7598 if (fd == 1 || fd == 2) {
7599 char buf[100];
7600 sprintf(buf, "cp%d", GetConsoleOutputCP());
7601 return PyUnicode_FromString(buf);
7602 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007603#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007604 {
7605 char *codeset = nl_langinfo(CODESET);
7606 if (codeset != NULL && codeset[0] != 0)
7607 return PyUnicode_FromString(codeset);
7608 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007609#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 Py_INCREF(Py_None);
7611 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007612}
7613
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007614#ifdef __VMS
7615/* Use openssl random routine */
7616#include <openssl/rand.h>
7617PyDoc_STRVAR(vms_urandom__doc__,
7618"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007619Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007620
7621static PyObject*
7622vms_urandom(PyObject *self, PyObject *args)
7623{
Victor Stinner8c62be82010-05-06 00:08:46 +00007624 int howMany;
7625 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007626
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 /* Read arguments */
7628 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7629 return NULL;
7630 if (howMany < 0)
7631 return PyErr_Format(PyExc_ValueError,
7632 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007633
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 /* Allocate bytes */
7635 result = PyBytes_FromStringAndSize(NULL, howMany);
7636 if (result != NULL) {
7637 /* Get random data */
7638 if (RAND_pseudo_bytes((unsigned char*)
7639 PyBytes_AS_STRING(result),
7640 howMany) < 0) {
7641 Py_DECREF(result);
7642 return PyErr_Format(PyExc_ValueError,
7643 "RAND_pseudo_bytes");
7644 }
7645 }
7646 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007647}
7648#endif
7649
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007650#ifdef HAVE_SETRESUID
7651PyDoc_STRVAR(posix_setresuid__doc__,
7652"setresuid(ruid, euid, suid)\n\n\
7653Set the current process's real, effective, and saved user ids.");
7654
7655static PyObject*
7656posix_setresuid (PyObject *self, PyObject *args)
7657{
Victor Stinner8c62be82010-05-06 00:08:46 +00007658 /* We assume uid_t is no larger than a long. */
7659 long ruid, euid, suid;
7660 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7661 return NULL;
7662 if (setresuid(ruid, euid, suid) < 0)
7663 return posix_error();
7664 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007665}
7666#endif
7667
7668#ifdef HAVE_SETRESGID
7669PyDoc_STRVAR(posix_setresgid__doc__,
7670"setresgid(rgid, egid, sgid)\n\n\
7671Set the current process's real, effective, and saved group ids.");
7672
7673static PyObject*
7674posix_setresgid (PyObject *self, PyObject *args)
7675{
Victor Stinner8c62be82010-05-06 00:08:46 +00007676 /* We assume uid_t is no larger than a long. */
7677 long rgid, egid, sgid;
7678 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7679 return NULL;
7680 if (setresgid(rgid, egid, sgid) < 0)
7681 return posix_error();
7682 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007683}
7684#endif
7685
7686#ifdef HAVE_GETRESUID
7687PyDoc_STRVAR(posix_getresuid__doc__,
7688"getresuid() -> (ruid, euid, suid)\n\n\
7689Get tuple of the current process's real, effective, and saved user ids.");
7690
7691static PyObject*
7692posix_getresuid (PyObject *self, PyObject *noargs)
7693{
Victor Stinner8c62be82010-05-06 00:08:46 +00007694 uid_t ruid, euid, suid;
7695 long l_ruid, l_euid, l_suid;
7696 if (getresuid(&ruid, &euid, &suid) < 0)
7697 return posix_error();
7698 /* Force the values into long's as we don't know the size of uid_t. */
7699 l_ruid = ruid;
7700 l_euid = euid;
7701 l_suid = suid;
7702 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007703}
7704#endif
7705
7706#ifdef HAVE_GETRESGID
7707PyDoc_STRVAR(posix_getresgid__doc__,
7708"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007709Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007710
7711static PyObject*
7712posix_getresgid (PyObject *self, PyObject *noargs)
7713{
Victor Stinner8c62be82010-05-06 00:08:46 +00007714 uid_t rgid, egid, sgid;
7715 long l_rgid, l_egid, l_sgid;
7716 if (getresgid(&rgid, &egid, &sgid) < 0)
7717 return posix_error();
7718 /* Force the values into long's as we don't know the size of uid_t. */
7719 l_rgid = rgid;
7720 l_egid = egid;
7721 l_sgid = sgid;
7722 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007723}
7724#endif
7725
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007726static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007728#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007730#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007732#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007734#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007735 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007736#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007737 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007738#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007739#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007740 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007741#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007742#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007743 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007744#endif /* HAVE_LCHMOD */
7745#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007747#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007748#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007750#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007751#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007752 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007753#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007754#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007755 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007756#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007757#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007758 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007759#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007760#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007761 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7762 METH_NOARGS, posix_getcwd__doc__},
7763 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7764 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007765#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007766#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007768#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7770 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7771 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007772#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007774#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007775#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007777#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007778#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007779 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007780#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007781 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7782 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7783 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007785#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007786 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007787#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007788#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
7789 {"_symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
7790 win_symlink__doc__},
7791#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007792#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007794#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007795 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007796#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007798#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007799 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7800 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7801 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007802#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007803 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007804#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007806#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007807 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7808 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007809#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007810#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007811 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7812 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007813#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7815 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007816#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007817#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007818#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007819 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007820#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007821#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007823#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007824#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007825 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007826#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007827#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007828 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007829#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007830#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007832#endif /* HAVE_GETEGID */
7833#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007835#endif /* HAVE_GETEUID */
7836#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007837 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007838#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007839#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007840 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007841#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007843#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007845#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007846#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007848#endif /* HAVE_GETPPID */
7849#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007851#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007852#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007854#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007855#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007857#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007858#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007860#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007861#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007863#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007864#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7866 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007867 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007868#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007869#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007871#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007872#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007874#endif /* HAVE_SETEUID */
7875#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007877#endif /* HAVE_SETEGID */
7878#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007880#endif /* HAVE_SETREUID */
7881#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007882 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007883#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007884#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007885 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007886#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007887#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007888 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007889#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007890#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007891 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007892#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007893#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007894 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007895#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007896#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007898#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007899#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007900 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007901#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007902#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007903 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007904#endif /* HAVE_WAIT3 */
7905#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007907#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007908#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007910#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007911#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007913#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007914#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007916#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007917#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007919#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007920#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007922#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007923#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007925#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007926 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7927 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7928 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7929 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7930 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7931 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7932 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7933 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7934 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7935 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7936 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007937#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007938 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007939#endif
7940#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007942#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007943#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007944 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007945#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007946#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007947 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7948 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7949 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007950#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007951#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007952 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007953#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007954#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007955 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007956#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007957#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007959#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007961#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007962 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007963#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007964#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007966#endif
7967#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007968 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007969#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007970#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007971#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007973#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007974#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007976#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007977#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007979#endif /* WIFSTOPPED */
7980#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007982#endif /* WIFSIGNALED */
7983#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007985#endif /* WIFEXITED */
7986#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007988#endif /* WEXITSTATUS */
7989#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007991#endif /* WTERMSIG */
7992#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007993 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007994#endif /* WSTOPSIG */
7995#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007996#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007998#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007999#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008001#endif
Fred Drakec9680921999-12-13 16:37:25 +00008002#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008003 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
8005#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008006 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008007#endif
8008#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008009 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008010#endif
8011#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008012 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008013#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008015#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008016 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008017 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008018 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008019#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008020#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008022#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008023 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008025 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008026 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008028 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008029#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008031#endif
8032#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008034#endif
8035#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008037#endif
8038#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008040#endif
8041
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008043};
8044
8045
Barry Warsaw4a342091996-12-19 23:50:02 +00008046static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008047ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008048{
Victor Stinner8c62be82010-05-06 00:08:46 +00008049 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008050}
8051
Guido van Rossumd48f2521997-12-05 22:19:34 +00008052#if defined(PYOS_OS2)
8053/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008054static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008055{
8056 APIRET rc;
8057 ULONG values[QSV_MAX+1];
8058 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008059 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008060
8061 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008062 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008063 Py_END_ALLOW_THREADS
8064
8065 if (rc != NO_ERROR) {
8066 os2_error(rc);
8067 return -1;
8068 }
8069
Fred Drake4d1e64b2002-04-15 19:40:07 +00008070 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8071 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8072 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8073 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8074 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8075 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8076 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008077
8078 switch (values[QSV_VERSION_MINOR]) {
8079 case 0: ver = "2.00"; break;
8080 case 10: ver = "2.10"; break;
8081 case 11: ver = "2.11"; break;
8082 case 30: ver = "3.00"; break;
8083 case 40: ver = "4.00"; break;
8084 case 50: ver = "5.00"; break;
8085 default:
Tim Peters885d4572001-11-28 20:27:42 +00008086 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008088 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008089 ver = &tmp[0];
8090 }
8091
8092 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008093 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008094 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008095
8096 /* Add Indicator of Which Drive was Used to Boot the System */
8097 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8098 tmp[1] = ':';
8099 tmp[2] = '\0';
8100
Fred Drake4d1e64b2002-04-15 19:40:07 +00008101 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008102}
8103#endif
8104
Brian Curtin52173d42010-12-02 18:29:18 +00008105#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
8106void
8107enable_symlink()
8108{
8109 HANDLE tok;
8110 TOKEN_PRIVILEGES tok_priv;
8111 LUID luid;
8112 int meth_idx = 0;
8113
8114 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
8115 return;
8116
8117 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
8118 return;
8119
8120 tok_priv.PrivilegeCount = 1;
8121 tok_priv.Privileges[0].Luid = luid;
8122 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8123
8124 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8125 sizeof(TOKEN_PRIVILEGES),
8126 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
8127 return;
8128
8129 if(GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
8130 /* We couldn't acquire the necessary privilege, so leave the
8131 method hidden for this user. */
8132 return;
8133 } else {
8134 /* We've successfully acquired the symlink privilege so rename
8135 the method to it's proper "os.symlink" name. */
8136 while(posix_methods[meth_idx].ml_meth != (PyCFunction)win_symlink)
8137 meth_idx++;
8138 posix_methods[meth_idx].ml_name = "symlink";
8139
8140 return;
8141 }
8142}
8143#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8144
Barry Warsaw4a342091996-12-19 23:50:02 +00008145static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008146all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008147{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008148#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008149 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008150#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008151#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008152 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008153#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008154#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008155 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008156#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008157#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008158 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008159#endif
Fred Drakec9680921999-12-13 16:37:25 +00008160#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008161 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008162#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008163#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008164 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008165#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008166#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008167 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008168#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008169#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008170 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008171#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008172#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008173 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008174#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008175#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008176 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008177#endif
8178#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008179 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008180#endif
8181#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008182 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008183#endif
8184#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008185 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008186#endif
8187#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008188 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008189#endif
8190#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008191 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008192#endif
8193#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008194 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008195#endif
8196#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008197 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008198#endif
8199#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008200 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008201#endif
8202#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008203 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008204#endif
8205#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008206 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008207#endif
8208#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008209 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008210#endif
8211#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008212 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008213#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008214#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008215 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008216#endif
8217#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008218 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008219#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008220#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008221 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008222#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008223#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008224 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008225#endif
8226#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008227 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008228#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008229
Tim Peters5aa91602002-01-30 05:46:57 +00008230/* MS Windows */
8231#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 /* Don't inherit in child processes. */
8233 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008234#endif
8235#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008236 /* Optimize for short life (keep in memory). */
8237 /* MS forgot to define this one with a non-underscore form too. */
8238 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008239#endif
8240#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 /* Automatically delete when last handle is closed. */
8242 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008243#endif
8244#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008245 /* Optimize for random access. */
8246 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008247#endif
8248#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 /* Optimize for sequential access. */
8250 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008251#endif
8252
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008253/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008254#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008255 /* Send a SIGIO signal whenever input or output
8256 becomes available on file descriptor */
8257 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008258#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008259#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008260 /* Direct disk access. */
8261 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008262#endif
8263#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 /* Must be a directory. */
8265 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008266#endif
8267#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 /* Do not follow links. */
8269 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008270#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008271#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 /* Do not update the access time. */
8273 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008274#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008275
Victor Stinner8c62be82010-05-06 00:08:46 +00008276 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008277#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008278 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008279#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008280#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008281 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008282#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008283#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008284 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008285#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008286#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008288#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008289#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008291#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008292#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008294#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008295#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008297#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008298#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008299 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008300#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008301#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008302 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008303#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008304#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008306#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008307#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008308 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008309#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008310#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008312#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008313#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008315#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008316#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008317 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008318#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008319#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008320 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008321#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008322#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008323 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008324#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008325#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008327#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008328
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008329 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008330#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008331 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008332#endif /* ST_RDONLY */
8333#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008334 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008335#endif /* ST_NOSUID */
8336
Guido van Rossum246bc171999-02-01 23:54:31 +00008337#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008338#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8340 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8341 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8342 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8343 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8344 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8345 if (ins(d, "P_PM", (long)P_PM)) return -1;
8346 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8347 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8348 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8349 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8350 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8351 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8352 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8353 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8354 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8355 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8356 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8357 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8358 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008359#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008360 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8361 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8362 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8363 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8364 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008365#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008366#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008367
Guido van Rossumd48f2521997-12-05 22:19:34 +00008368#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008369 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008370#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008372}
8373
8374
Tim Peters5aa91602002-01-30 05:46:57 +00008375#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008376#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008377#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008378
8379#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008380#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008381#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008382
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008383#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008384#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008385#define MODNAME "posix"
8386#endif
8387
Martin v. Löwis1a214512008-06-11 05:26:20 +00008388static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008389 PyModuleDef_HEAD_INIT,
8390 MODNAME,
8391 posix__doc__,
8392 -1,
8393 posix_methods,
8394 NULL,
8395 NULL,
8396 NULL,
8397 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008398};
8399
8400
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008401PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008402INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008403{
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008405
Brian Curtin52173d42010-12-02 18:29:18 +00008406#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
8407 enable_symlink();
8408#endif
8409
Victor Stinner8c62be82010-05-06 00:08:46 +00008410 m = PyModule_Create(&posixmodule);
8411 if (m == NULL)
8412 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008413
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 /* Initialize environ dictionary */
8415 v = convertenviron();
8416 Py_XINCREF(v);
8417 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8418 return NULL;
8419 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008420
Victor Stinner8c62be82010-05-06 00:08:46 +00008421 if (all_ins(m))
8422 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008423
Victor Stinner8c62be82010-05-06 00:08:46 +00008424 if (setup_confname_tables(m))
8425 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008426
Victor Stinner8c62be82010-05-06 00:08:46 +00008427 Py_INCREF(PyExc_OSError);
8428 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008429
Guido van Rossumb3d39562000-01-31 18:41:26 +00008430#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008431 if (posix_putenv_garbage == NULL)
8432 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008433#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008434
Victor Stinner8c62be82010-05-06 00:08:46 +00008435 if (!initialized) {
8436 stat_result_desc.name = MODNAME ".stat_result";
8437 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8438 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8439 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8440 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8441 structseq_new = StatResultType.tp_new;
8442 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008443
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 statvfs_result_desc.name = MODNAME ".statvfs_result";
8445 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008446#ifdef NEED_TICKS_PER_SECOND
8447# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008448 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008449# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008450 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008451# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008453# endif
8454#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008455 }
8456 Py_INCREF((PyObject*) &StatResultType);
8457 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8458 Py_INCREF((PyObject*) &StatVFSResultType);
8459 PyModule_AddObject(m, "statvfs_result",
8460 (PyObject*) &StatVFSResultType);
8461 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008462
8463#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008464 /*
8465 * Step 2 of weak-linking support on Mac OS X.
8466 *
8467 * The code below removes functions that are not available on the
8468 * currently active platform.
8469 *
8470 * This block allow one to use a python binary that was build on
8471 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8472 * OSX 10.4.
8473 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008474#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008475 if (fstatvfs == NULL) {
8476 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8477 return NULL;
8478 }
8479 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008480#endif /* HAVE_FSTATVFS */
8481
8482#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008483 if (statvfs == NULL) {
8484 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8485 return NULL;
8486 }
8487 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008488#endif /* HAVE_STATVFS */
8489
8490# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008491 if (lchown == NULL) {
8492 if (PyObject_DelAttrString(m, "lchown") == -1) {
8493 return NULL;
8494 }
8495 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008496#endif /* HAVE_LCHOWN */
8497
8498
8499#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008501
Guido van Rossumb6775db1994-08-01 11:34:53 +00008502}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008503
8504#ifdef __cplusplus
8505}
8506#endif